From 02e48fe201c19e63d8000c672cc8bafd87bb2f8f Mon Sep 17 00:00:00 2001 From: Jason Hee Date: Mon, 28 Nov 2016 02:09:14 +0800 Subject: [PATCH] Added function to add swap file --- setupLibrary.sh | 23 +++++++++++++++++++++++ tests/tests.sh | 22 +++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/setupLibrary.sh b/setupLibrary.sh index 710afb6..5aadd1e 100644 --- a/setupLibrary.sh +++ b/setupLibrary.sh @@ -39,4 +39,27 @@ function changeSSHConfig() { function setupUfw() { sudo ufw allow OpenSSH 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 } \ No newline at end of file diff --git a/tests/tests.sh b/tests/tests.sh index 21e9b56..98fe51b 100644 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -56,15 +56,21 @@ function testUfw() { assertContains "OpenSSH" "${ufw_status}" } +function testSwap() { + createSwap + + assertContains "/swapfile" "$(ls -lh /swapfile)" + assertContains "/swapfile" "$(sudo swapon --show)" +} + function testTeardown () { echo "Test Teardown" deleteTestUser revertSudoers revertSSHConfig - - sudo ufw delete allow OpenSSH - sudo ufw disable + revertUfw + deleteSwap } ### Helper Functions ### @@ -89,4 +95,14 @@ function revertSSHConfig() { 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 \ No newline at end of file