diff --git a/setup.sh b/setup.sh index 7e668c2..3799898 100644 --- a/setup.sh +++ b/setup.sh @@ -12,6 +12,13 @@ function includeDependencies() { source "${current_dir}/setupLibrary.sh" } +function setupSwap() { + createSwap + mountSwap + tweakSwapSettings "10" "50" + saveSwapSettings +} + current_dir=$(getCurrentDir) includeDependencies @@ -32,5 +39,6 @@ read -rp $'Paste in the public SSH key for the new user:\n' sshKey addSSHKey "${username}" "${sshKey}" changeSSHConfig setupUfw +setupSwap sudo service ssh restart \ No newline at end of file diff --git a/setupLibrary.sh b/setupLibrary.sh index 5aadd1e..4d7b861 100644 --- a/setupLibrary.sh +++ b/setupLibrary.sh @@ -55,6 +55,24 @@ function createSwap() { sudo swapon /swapfile } +function mountSwap() { + sudo cp /etc/fstab /etc/fstab.bak + echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab +} + +function tweakSwapSettings() { + local swappiness=${1} + local vfs_cache_pressure=${2} + + sudo sysctl vm.swappiness=${swappiness} + sudo sysctl vm.vfs_cache_pressure=${vfs_cache_pressure} +} + +function saveSwapSettings() { + echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf + echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf +} + function getPhysicalMemory() { local phymem=$(free -g|awk '/^Mem:/{print $2}') if [[ ${phymem} == '0' ]]; then diff --git a/tests/tests.sh b/tests/tests.sh index 98fe51b..f2f5b81 100644 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -63,6 +63,18 @@ function testSwap() { assertContains "/swapfile" "$(sudo swapon --show)" } +function testSwapSettings() { + local swappiness=$(cat /proc/sys/vm/swappiness) + local cache_pressure=$(cat /proc/sys/vm/vfs_cache_pressure) + + tweakSwapSettings 10 50 + + assertEquals "$(cat /proc/sys/vm/swappiness)" "10" + assertEquals "$(cat /proc/sys/vm/vfs_cache_pressure)" "50" + + tweakSwapSettings "${swappiness}" "${cache_pressure}" +} + function testTeardown () { echo "Test Teardown"