From d06e0d3abefa188affccb2876e9026719c02112c Mon Sep 17 00:00:00 2001 From: Pieds-Nus Date: Mon, 19 Sep 2022 21:56:30 +0000 Subject: [PATCH] swapctl.sh : added support to update fstab --- swapctl.sh | 68 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/swapctl.sh b/swapctl.sh index d82da4b..79eed19 100644 --- a/swapctl.sh +++ b/swapctl.sh @@ -1,5 +1,6 @@ #!/bin/bash +sep="\n\t#############\n" usage="$0 -d DIR -n NAME -s SIZE -p PRIO add|remove Add or remove swapfiles to micro-manage disk usage. -d DIR : set DIRectory to place swapfile in. Default: /mnt @@ -8,13 +9,17 @@ Add or remove swapfiles to micro-manage disk usage. -p PRIO : Priority to use with swapon. Range from 0 to 1000. Default: 500 -h : Display this help and exit." -test $# -eq 0 && ( echo -e "$usage" ; echo "You must provide either 'add' or 'remove' !" ; exit 1 ) +if test $# -eq 0 ; then + echo -e "$usage" + echo "You must provide either 'add' or 'remove' !" + exit 1 +fi SWAP_FOLDER="/mnt" SWAP_FILENAME="swapfile" -SWAP_SIZE="12" #in MB -SWAP_PRIO_MAX=500 #range 0..1000 -SWAP_PRIO_INC=10 +SWAP_SIZE="1024" #in MB +SWAP_PRIO_MAX=750 #range 0..1000 +SWAP_PRIO_INC=50 while getopts hp:s:n:d: arg ; do case $arg in @@ -33,15 +38,16 @@ if test "$1" = "add" ; then N=$(ls $SWAP_FOLDER/$SWAP_FILENAME* | wc -l) swapfile=$SWAP_FOLDER/$SWAP_FILENAME$N + prio=$((($SWAP_PRIO_MAX - $N * $SWAP_PRIO_INC))) - echo "Creating swapfile: $swapfile" + echo -e "$sep Creating swapfile: $swapfile$sep" sudo dd if=/dev/urandom of=$swapfile bs=1M count=$SWAP_SIZE status=progress sudo chmod 0600 $swapfile sudo mkswap $swapfile - sudo swapon -p $((($SWAP_PRIO_MAX - $N * $SWAP_PRIO_INC))) $swapfile + sudo swapon -p $prio $swapfile - echo -e "\n\n\n\n" + echo -e "\n\n" sudo swapon elif test "$1" = "remove" ; then @@ -49,11 +55,55 @@ elif test "$1" = "remove" ; then N=$(ls $SWAP_FOLDER/$SWAP_FILENAME* | wc -l) swapfile=$SWAP_FOLDER/$SWAP_FILENAME$((($N - 1))) - echo "Removing swapfile: $swapfile" + echo -e "$sep Removing swapfile: $swapfile$sep" sudo swapoff $swapfile && sudo rm $swapfile - echo -e "\n\n\n\n" + echo -e "\n\n" sudo swapon fi + +echo -e "$sep Editing /etc/fstab accordingly$sep" + +tmpfile=/tmp/swapctl.fstab + +if test $1 = "add" ; then + cat /etc/fstab > $tmpfile + echo -e "$swapfile\t\t\t\t\t\t\t\t\tnone\t\t\tswap\tsw,pri=$prio\t\t\t0\t\t0" >> $tmpfile +else + cat /etc/fstab | grep -v $swapfile > $tmpfile + +fi + +echo -e "# The lines below this one will be the new fstab file : \n\n $(cat $tmpfile)" | more + +while true ; do + + echo -e "$sep What to do with this fstab ?" + echo -e "[ \t\e[4ma\\e[0mccept changes | \e[4me\e[0mdit file | dis\e[4mc\e[0mard changes | show \e[4md\e[0miff ]" + read prompt + + case $prompt in + a|accept) + sudo mv /etc/fstab.2 /etc/fstab.3 + sudo mv /etc/fstab.1 /etc/fstab.2 + sudo mv /etc/fstab /etc/fstab.1 + sudo mv $tmpfile /etc/fstab + echo -e "$sep Changes accepted. New contents of /etc/fstab :$sep" + cat /etc/fstab | more + break + ;; + e|edit) + ${EDITOR:-nano} $tmpfile + ;; + c|discard) + echo "$sep Changes discarded. /etc/fstab remains unchanged.$sep" + break + ;; + d|diff) + echo -e "\n\n\tdiff\t\t/etc/fstab\t\t\t\t\t\t/tmp/swapctl.fstab\n" + diff --tabsize=4 -E -Z -b -t -y /etc/fstab /tmp/swapctl.fstab | more + ;; + esac +done