From 8e31bed47dccc744e399a4e67873603c50b0175e Mon Sep 17 00:00:00 2001 From: Michael Kroes Date: Mon, 14 Mar 2016 11:18:12 +0100 Subject: [PATCH 01/10] Add a swap file to the system if system memory is less than 2GB and only if no swap file exists --- setup/system.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/setup/system.sh b/setup/system.sh index 1aeec458..a7b5cee4 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -4,6 +4,52 @@ source setup/functions.sh # load our functions # Basic System Configuration # ------------------------- +# ### Add swap space to the system + +# If the physical memory of the system is below 2GB it is wise to create a +# swap file. This will make the system more resiliant to memory spikes and +# prevent for instance spam filtering from crashing + +# We will create a 1G file, this should be a good balance between disk usage +# and buffers for the system + +# See https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04 +# for reference + +SWAP_MOUNTED=$(grep "swap" /proc/mounts) +TOTAL_PHYSICAL_MEM=$(head -n 1 /proc/meminfo | awk '{print $2}') +if [ $TOTAL_PHYSICAL_MEM -lt 19000000 ]; then +if [ -z "$SWAP_MOUNTED" ]; then +if [ ! -e /swapfile ]; then + echo "Adding swap to the system..." + + # Allocate and active the swap file + fallocate -l 1G /swapfile + chmod 600 /swapfile + hide_output mkswap /swapfile + swapon /swapfile + + # Make sure swap is activated on boot + echo "/swapfile none swap sw 0 0" >> /etc/fstab + + # Make sure the system only swaps as a last resort + SWAPPINESS=$(grep vm.swappiness /etc/sysctl.conf) + if [ -z "$SWAPPINESS" ]; then + hide_output sysctl vm.swappiness=10 + echo "vm.swappiness=10" >> /etc/sysctl.conf + fi + + # Make sure the systeem keeps the file system inodes in + # memory as long as possible + PRESSURE=$(grep vm.vfs_cache_pressure /etc/sysctl.conf) + if [ -z "$PRESSURE" ]; then + hide_output sysctl vm.vfs_cache_pressure=50 + echo "vm.vfs_cache_pressure=50" >> /etc/sysctl.conf + fi +fi +fi +fi + # ### Add Mail-in-a-Box's PPA. # We've built several .deb packages on our own that we want to include. From 326cb9679993159fdd9a1d6a3d1ec44f87450478 Mon Sep 17 00:00:00 2001 From: Michael Kroes Date: Mon, 14 Mar 2016 11:26:30 +0100 Subject: [PATCH 02/10] One zero to many in the memory requirements for swap space --- setup/system.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/system.sh b/setup/system.sh index a7b5cee4..87b0342f 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -18,7 +18,7 @@ source setup/functions.sh # load our functions SWAP_MOUNTED=$(grep "swap" /proc/mounts) TOTAL_PHYSICAL_MEM=$(head -n 1 /proc/meminfo | awk '{print $2}') -if [ $TOTAL_PHYSICAL_MEM -lt 19000000 ]; then +if [ $TOTAL_PHYSICAL_MEM -lt 1900000 ]; then if [ -z "$SWAP_MOUNTED" ]; then if [ ! -e /swapfile ]; then echo "Adding swap to the system..." From e9677ae68dfe0d089b3b82307e2ed4bac09a0169 Mon Sep 17 00:00:00 2001 From: Michael Kroes Date: Mon, 14 Mar 2016 11:40:54 +0100 Subject: [PATCH 03/10] Use tools/editconf.py to manipulate sysctl.conf --- setup/system.sh | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/setup/system.sh b/setup/system.sh index 87b0342f..b6942496 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -33,19 +33,13 @@ if [ ! -e /swapfile ]; then echo "/swapfile none swap sw 0 0" >> /etc/fstab # Make sure the system only swaps as a last resort - SWAPPINESS=$(grep vm.swappiness /etc/sysctl.conf) - if [ -z "$SWAPPINESS" ]; then - hide_output sysctl vm.swappiness=10 - echo "vm.swappiness=10" >> /etc/sysctl.conf - fi + hide_output sysctl vm.swappiness=10 + tools/editconf.py /etc/sysctl.conf vm.swappiness=10 # Make sure the systeem keeps the file system inodes in # memory as long as possible - PRESSURE=$(grep vm.vfs_cache_pressure /etc/sysctl.conf) - if [ -z "$PRESSURE" ]; then - hide_output sysctl vm.vfs_cache_pressure=50 - echo "vm.vfs_cache_pressure=50" >> /etc/sysctl.conf - fi + hide_output sysctl vm.vfs_cache_pressure=50 + tools/editconf.py /etc/sysctl.conf vm.vfs_cache_pressure=50 fi fi fi From f8dd18fb08877d532387b82e03135ce1c496a031 Mon Sep 17 00:00:00 2001 From: Michael Kroes Date: Mon, 14 Mar 2016 11:44:32 +0100 Subject: [PATCH 04/10] Fix spacing --- setup/system.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/system.sh b/setup/system.sh index b6942496..09c2f6d2 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -38,7 +38,7 @@ if [ ! -e /swapfile ]; then # Make sure the systeem keeps the file system inodes in # memory as long as possible - hide_output sysctl vm.vfs_cache_pressure=50 + hide_output sysctl vm.vfs_cache_pressure=50 tools/editconf.py /etc/sysctl.conf vm.vfs_cache_pressure=50 fi fi From a71679c66d751421a12e6e184a94857cc4949ed7 Mon Sep 17 00:00:00 2001 From: Michael Kroes Date: Mon, 14 Mar 2016 15:43:50 +0100 Subject: [PATCH 05/10] Fix typo --- setup/system.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/system.sh b/setup/system.sh index 09c2f6d2..ab9658ee 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -23,7 +23,7 @@ if [ -z "$SWAP_MOUNTED" ]; then if [ ! -e /swapfile ]; then echo "Adding swap to the system..." - # Allocate and active the swap file + # Allocate and activate the swap file fallocate -l 1G /swapfile chmod 600 /swapfile hide_output mkswap /swapfile From 41c6c9c72e861376bdde58503239ab352a3fe9b6 Mon Sep 17 00:00:00 2001 From: Michael Kroes Date: Tue, 15 Mar 2016 06:48:21 +0100 Subject: [PATCH 06/10] Remove kernel tweaks while allocating swap and only allocate swap if we have more than 5GB of diskspace available --- setup/system.sh | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/setup/system.sh b/setup/system.sh index ab9658ee..05b0e2cb 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -11,16 +11,19 @@ source setup/functions.sh # load our functions # prevent for instance spam filtering from crashing # We will create a 1G file, this should be a good balance between disk usage -# and buffers for the system +# and buffers for the system. We will only allocate this file if there is more +# than 5GB of disk space available # See https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04 # for reference SWAP_MOUNTED=$(grep "swap" /proc/mounts) TOTAL_PHYSICAL_MEM=$(head -n 1 /proc/meminfo | awk '{print $2}') +AVAILABLE_DISK_SPACE=$(df / --output=avail | tail -n 1) if [ $TOTAL_PHYSICAL_MEM -lt 1900000 ]; then if [ -z "$SWAP_MOUNTED" ]; then if [ ! -e /swapfile ]; then +if [ $AVAILABLE_DISK_SPACE -gt 5242880 ]; then echo "Adding swap to the system..." # Allocate and activate the swap file @@ -31,15 +34,7 @@ if [ ! -e /swapfile ]; then # Make sure swap is activated on boot echo "/swapfile none swap sw 0 0" >> /etc/fstab - - # Make sure the system only swaps as a last resort - hide_output sysctl vm.swappiness=10 - tools/editconf.py /etc/sysctl.conf vm.swappiness=10 - - # Make sure the systeem keeps the file system inodes in - # memory as long as possible - hide_output sysctl vm.vfs_cache_pressure=50 - tools/editconf.py /etc/sysctl.conf vm.vfs_cache_pressure=50 +fi fi fi fi From 1be652b72fc6aee73a682dcc561072e308835498 Mon Sep 17 00:00:00 2001 From: Michael Kroes Date: Wed, 16 Mar 2016 07:16:25 +0100 Subject: [PATCH 07/10] Add extra check when adding swap to see if swap would be allocated on next boot. Only activate swap on boot if initial activation succeeded. Refactor if statement for readability. Add comments on all checks performed. --- setup/system.sh | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/setup/system.sh b/setup/system.sh index 05b0e2cb..40d498c2 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -14,16 +14,27 @@ source setup/functions.sh # load our functions # and buffers for the system. We will only allocate this file if there is more # than 5GB of disk space available +# The following checks are performed: +# - Check if swap is currently mountend by looking at /proc/mounts +# - Check if the user intents to activate swap on next boot by checking fstab entries. +# - Check if a swapfile already exists +# - Check the memory requirements +# - Check available diskspace + # See https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04 # for reference SWAP_MOUNTED=$(grep "swap" /proc/mounts) +SWAP_IN_FSTAB=$(grep "swap" /etc/fstab) TOTAL_PHYSICAL_MEM=$(head -n 1 /proc/meminfo | awk '{print $2}') AVAILABLE_DISK_SPACE=$(df / --output=avail | tail -n 1) -if [ $TOTAL_PHYSICAL_MEM -lt 1900000 ]; then -if [ -z "$SWAP_MOUNTED" ]; then -if [ ! -e /swapfile ]; then -if [ $AVAILABLE_DISK_SPACE -gt 5242880 ]; then +if + [ -z "$SWAP_MOUNTED" ] && + [ -z "$SWAP_IN_FSTAB" ] && + [ ! -e /swapfile ] && + [ $TOTAL_PHYSICAL_MEM -lt 1900000 ] && + [ $AVAILABLE_DISK_SPACE -gt 5242880 ] +then echo "Adding swap to the system..." # Allocate and activate the swap file @@ -32,11 +43,11 @@ if [ $AVAILABLE_DISK_SPACE -gt 5242880 ]; then hide_output mkswap /swapfile swapon /swapfile - # Make sure swap is activated on boot - echo "/swapfile none swap sw 0 0" >> /etc/fstab -fi -fi -fi + # Check if swap is mounted then activate on boot + ACTIVATED_SWAP=$(swapon -s | grep "\/swapfile") + if [ -n "$ACTIVATED_SWAP" ]; then + echo "/swapfile none swap sw 0 0" >> /etc/fstab + fi fi # ### Add Mail-in-a-Box's PPA. From dfe1374901fb3abbbd19a82b00780c388ee25be1 Mon Sep 17 00:00:00 2001 From: Michael Kroes Date: Wed, 16 Mar 2016 09:06:17 +0100 Subject: [PATCH 08/10] Use dd to create swapfile. Check if root fs is btrfs. Display message if swap wasn't allocated --- setup/system.sh | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/setup/system.sh b/setup/system.sh index 40d498c2..7904b1f6 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -18,6 +18,8 @@ source setup/functions.sh # load our functions # - Check if swap is currently mountend by looking at /proc/mounts # - Check if the user intents to activate swap on next boot by checking fstab entries. # - Check if a swapfile already exists +# - Check if the root file system is not btrfs, might be an incompatible version with +# swapfiles. User should hanle it them selves. # - Check the memory requirements # - Check available diskspace @@ -26,27 +28,32 @@ source setup/functions.sh # load our functions SWAP_MOUNTED=$(grep "swap" /proc/mounts) SWAP_IN_FSTAB=$(grep "swap" /etc/fstab) +ROOT_IS_BTRFS=$(grep "\/ .*btrfs" /proc/mounts) TOTAL_PHYSICAL_MEM=$(head -n 1 /proc/meminfo | awk '{print $2}') AVAILABLE_DISK_SPACE=$(df / --output=avail | tail -n 1) if [ -z "$SWAP_MOUNTED" ] && [ -z "$SWAP_IN_FSTAB" ] && [ ! -e /swapfile ] && + [ -z "$ROOT_IS_BTRFS" ] && [ $TOTAL_PHYSICAL_MEM -lt 1900000 ] && [ $AVAILABLE_DISK_SPACE -gt 5242880 ] then echo "Adding swap to the system..." # Allocate and activate the swap file - fallocate -l 1G /swapfile - chmod 600 /swapfile - hide_output mkswap /swapfile - swapon /swapfile + dd if=/dev/zero of=/swapfile bs=1024 count=$[1024*1024] status=none + if [ -e /swapfile ]; then + chmod 600 /swapfile + hide_output mkswap /swapfile + swapon /swapfile + fi # Check if swap is mounted then activate on boot - ACTIVATED_SWAP=$(swapon -s | grep "\/swapfile") - if [ -n "$ACTIVATED_SWAP" ]; then + if [ -n "$(swapon -s | grep "\/swapfile")" ]; then echo "/swapfile none swap sw 0 0" >> /etc/fstab + else + echo "ERROR: Swap allocation failed" fi fi From 85e6118ae30787b9f70e811ab42cd56ab3588697 Mon Sep 17 00:00:00 2001 From: Michael Kroes Date: Wed, 16 Mar 2016 12:54:19 +0100 Subject: [PATCH 09/10] Add extra comment about blocksizes. Inline if, to check if swap was mounted --- setup/system.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/setup/system.sh b/setup/system.sh index 7904b1f6..93086322 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -41,7 +41,8 @@ if then echo "Adding swap to the system..." - # Allocate and activate the swap file + # Allocate and activate the swap file. Allocate in 1KB chuncks + # doing it in one go, could fail on low memory systems dd if=/dev/zero of=/swapfile bs=1024 count=$[1024*1024] status=none if [ -e /swapfile ]; then chmod 600 /swapfile @@ -50,7 +51,7 @@ then fi # Check if swap is mounted then activate on boot - if [ -n "$(swapon -s | grep "\/swapfile")" ]; then + if swapon -s | grep -q "\/swapfile"; then echo "/swapfile none swap sw 0 0" >> /etc/fstab else echo "ERROR: Swap allocation failed" From b7bc9e11f3549a97402f2757a7dc8313bb07a2b1 Mon Sep 17 00:00:00 2001 From: Michael Kroes Date: Tue, 22 Mar 2016 16:40:56 +0100 Subject: [PATCH 10/10] Check that swap is mounted using /proc/swaps --- setup/system.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup/system.sh b/setup/system.sh index 93086322..39a1656d 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -15,7 +15,7 @@ source setup/functions.sh # load our functions # than 5GB of disk space available # The following checks are performed: -# - Check if swap is currently mountend by looking at /proc/mounts +# - Check if swap is currently mountend by looking at /proc/swaps # - Check if the user intents to activate swap on next boot by checking fstab entries. # - Check if a swapfile already exists # - Check if the root file system is not btrfs, might be an incompatible version with @@ -26,7 +26,7 @@ source setup/functions.sh # load our functions # See https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04 # for reference -SWAP_MOUNTED=$(grep "swap" /proc/mounts) +SWAP_MOUNTED=$(cat /proc/swaps | tail -n+2) SWAP_IN_FSTAB=$(grep "swap" /etc/fstab) ROOT_IS_BTRFS=$(grep "\/ .*btrfs" /proc/mounts) TOTAL_PHYSICAL_MEM=$(head -n 1 /proc/meminfo | awk '{print $2}')