Added function to add swap file

This commit is contained in:
Jason Hee committed 2016-11-28 02:09:14 +08:00
1 parent 9ef673594e
commit 02e48fe201
2 files changed
+42 -3

No files matched your search

+23
View File
@@ -40,3 +40,26 @@ function setupUfw() {
sudo ufw allow OpenSSH sudo ufw allow OpenSSH
yes y | sudo ufw enable yes y | sudo ufw enable
} }
function createSwap() {
local swapmem=$(($(getPhysicalMemory) * 2))
# Anything over 4GB in swap is probably unnecessary as a RAM fallback
if [[ ${swapmem} > 4 ]]; then
phymem=4
fi
sudo fallocate -l ${swapmem}G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
}
function getPhysicalMemory() {
local phymem=$(free -g|awk '/^Mem:/{print $2}')
if [[ ${phymem} == '0' ]]; then
echo 1
else
echo ${phymem}
fi
}
+19 -3
View File
@@ -56,15 +56,21 @@ function testUfw() {
assertContains "OpenSSH" "${ufw_status}" assertContains "OpenSSH" "${ufw_status}"
} }
function testSwap() {
createSwap
assertContains "/swapfile" "$(ls -lh /swapfile)"
assertContains "/swapfile" "$(sudo swapon --show)"
}
function testTeardown () { function testTeardown () {
echo "Test Teardown" echo "Test Teardown"
deleteTestUser deleteTestUser
revertSudoers revertSudoers
revertSSHConfig revertSSHConfig
revertUfw
sudo ufw delete allow OpenSSH deleteSwap
sudo ufw disable
} }
### Helper Functions ### ### Helper Functions ###
@@ -89,4 +95,14 @@ function revertSSHConfig() {
sudo rm -rf /etc/ssh/sshd_config.old sudo rm -rf /etc/ssh/sshd_config.old
} }
function revertUfw() {
sudo ufw delete allow OpenSSH
sudo ufw disable
}
function deleteSwap() {
sudo swapoff /swapfile
sudo rm /swapfile
}
runUnitTests runUnitTests