From a4fb1014e39353b51dd26f67bc5f61ee84809f77 Mon Sep 17 00:00:00 2001 From: Brian Zalewski Date: Sun, 25 Dec 2022 08:41:35 +0000 Subject: [PATCH] Update file run_onchange_before_configure-swap.tmpl --- .../run_onchange_before_configure-swap.tmpl | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/home/.chezmoiscripts/linux/run_onchange_before_configure-swap.tmpl b/home/.chezmoiscripts/linux/run_onchange_before_configure-swap.tmpl index 552b4fb4..ce163357 100644 --- a/home/.chezmoiscripts/linux/run_onchange_before_configure-swap.tmpl +++ b/home/.chezmoiscripts/linux/run_onchange_before_configure-swap.tmpl @@ -4,26 +4,28 @@ {{ includeTemplate "universal/profile" }} {{ includeTemplate "universal/logg" }} -MEMORY_IN_KB="$(grep MemTotal /proc/meminfo | sed 's/.* \(.*\) kB/\1/')" -MEMORY_IN_GB="$((MEMORY_IN_KB / 1024 / 1024))" +if [ ! -f /swapfile ]; then + MEMORY_IN_KB="$(grep MemTotal /proc/meminfo | sed 's/.* \(.*\) kB/\1/')" + MEMORY_IN_GB="$((MEMORY_IN_KB / 1024 / 1024))" -if [ "$MEMORY_IN_GB" -gt 64 ]; then - SWAP_SPACE="$((MEMORY_IN_GB / 10))" -elif [ "$MEMORY_IN_GB" -gt 32 ]; then - SWAP_SPACE="$((MEMORY_IN_GB / 8))" -elif [ "$MEMORY_IN_GB" -gt 8 ]; then - SWAP_SPACE="$((MEMORY_IN_GB / 4))" -else - SWAP_SPACE="$MEMORY_IN_GB" -fi + if [ "$MEMORY_IN_GB" -gt 64 ]; then + SWAP_SPACE="$((MEMORY_IN_GB / 10))" + elif [ "$MEMORY_IN_GB" -gt 32 ]; then + SWAP_SPACE="$((MEMORY_IN_GB / 8))" + elif [ "$MEMORY_IN_GB" -gt 8 ]; then + SWAP_SPACE="$((MEMORY_IN_GB / 4))" + else + SWAP_SPACE="$MEMORY_IN_GB" + fi -logg info "Creating a $SWAP_SPACE GB /swapfile" -sudo fallocate -l "$SWAP_SPACE" /swapfile -sudo chmod 600 /swapfile -sudo mkswap /swapfile -sudo swapon /swapfile -if cat /etc/fstab | grep "/swapfile"; then - sudo sed -i '/\/swapfile/\/swapfile none swap sw 0 0/' /etc/fstab -else - echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab + logg info "Creating a $SWAP_SPACE GB /swapfile" + sudo fallocate -l "$SWAP_SPACE" /swapfile + sudo chmod 600 /swapfile + sudo mkswap /swapfile + sudo swapon /swapfile + if cat /etc/fstab | grep "/swapfile"; then + sudo sed -i '/\/swapfile/\/swapfile none swap sw 0 0/' /etc/fstab + else + echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab + fi fi