Skip to content

Commit a82bf4f

Browse files
committed
Added swap settings
1 parent 02e48fe commit a82bf4f

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

setup.sh

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ function includeDependencies() {
1212
source "${current_dir}/setupLibrary.sh"
1313
}
1414

15+
function setupSwap() {
16+
createSwap
17+
mountSwap
18+
tweakSwapSettings "10" "50"
19+
saveSwapSettings
20+
}
21+
1522
current_dir=$(getCurrentDir)
1623
includeDependencies
1724

@@ -32,5 +39,6 @@ read -rp $'Paste in the public SSH key for the new user:\n' sshKey
3239
addSSHKey "${username}" "${sshKey}"
3340
changeSSHConfig
3441
setupUfw
42+
setupSwap
3543

3644
sudo service ssh restart

setupLibrary.sh

+18
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ function createSwap() {
5555
sudo swapon /swapfile
5656
}
5757

58+
function mountSwap() {
59+
sudo cp /etc/fstab /etc/fstab.bak
60+
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
61+
}
62+
63+
function tweakSwapSettings() {
64+
local swappiness=${1}
65+
local vfs_cache_pressure=${2}
66+
67+
sudo sysctl vm.swappiness=${swappiness}
68+
sudo sysctl vm.vfs_cache_pressure=${vfs_cache_pressure}
69+
}
70+
71+
function saveSwapSettings() {
72+
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
73+
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
74+
}
75+
5876
function getPhysicalMemory() {
5977
local phymem=$(free -g|awk '/^Mem:/{print $2}')
6078
if [[ ${phymem} == '0' ]]; then

tests/tests.sh

+12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ function testSwap() {
6363
assertContains "/swapfile" "$(sudo swapon --show)"
6464
}
6565

66+
function testSwapSettings() {
67+
local swappiness=$(cat /proc/sys/vm/swappiness)
68+
local cache_pressure=$(cat /proc/sys/vm/vfs_cache_pressure)
69+
70+
tweakSwapSettings 10 50
71+
72+
assertEquals "$(cat /proc/sys/vm/swappiness)" "10"
73+
assertEquals "$(cat /proc/sys/vm/vfs_cache_pressure)" "50"
74+
75+
tweakSwapSettings "${swappiness}" "${cache_pressure}"
76+
}
77+
6678
function testTeardown () {
6779
echo "Test Teardown"
6880

0 commit comments

Comments
 (0)