Skip to content

Commit 02e48fe

Browse files
committed
Added function to add swap file
1 parent 9ef6735 commit 02e48fe

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

setupLibrary.sh

+23
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,27 @@ function changeSSHConfig() {
3939
function setupUfw() {
4040
sudo ufw allow OpenSSH
4141
yes y | sudo ufw enable
42+
}
43+
44+
function createSwap() {
45+
local swapmem=$(($(getPhysicalMemory) * 2))
46+
47+
# Anything over 4GB in swap is probably unnecessary as a RAM fallback
48+
if [[ ${swapmem} > 4 ]]; then
49+
phymem=4
50+
fi
51+
52+
sudo fallocate -l ${swapmem}G /swapfile
53+
sudo chmod 600 /swapfile
54+
sudo mkswap /swapfile
55+
sudo swapon /swapfile
56+
}
57+
58+
function getPhysicalMemory() {
59+
local phymem=$(free -g|awk '/^Mem:/{print $2}')
60+
if [[ ${phymem} == '0' ]]; then
61+
echo 1
62+
else
63+
echo ${phymem}
64+
fi
4265
}

tests/tests.sh

+19-3
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,21 @@ function testUfw() {
5656
assertContains "OpenSSH" "${ufw_status}"
5757
}
5858

59+
function testSwap() {
60+
createSwap
61+
62+
assertContains "/swapfile" "$(ls -lh /swapfile)"
63+
assertContains "/swapfile" "$(sudo swapon --show)"
64+
}
65+
5966
function testTeardown () {
6067
echo "Test Teardown"
6168

6269
deleteTestUser
6370
revertSudoers
6471
revertSSHConfig
65-
66-
sudo ufw delete allow OpenSSH
67-
sudo ufw disable
72+
revertUfw
73+
deleteSwap
6874
}
6975

7076
### Helper Functions ###
@@ -89,4 +95,14 @@ function revertSSHConfig() {
8995
sudo rm -rf /etc/ssh/sshd_config.old
9096
}
9197

98+
function revertUfw() {
99+
sudo ufw delete allow OpenSSH
100+
sudo ufw disable
101+
}
102+
103+
function deleteSwap() {
104+
sudo swapoff /swapfile
105+
sudo rm /swapfile
106+
}
107+
92108
runUnitTests

0 commit comments

Comments
 (0)