From 7a5a5aa2d322e4f4a334875fec53e6cdb6aedc5f Mon Sep 17 00:00:00 2001 From: mollusk Date: Fri, 8 Jun 2018 09:15:31 -0700 Subject: [PATCH] Add:genswap.sh Former-commit-id: d090e14af6b5d2b571f0deae23532b4f02c04773 --- genswap.sh | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 genswap.sh diff --git a/genswap.sh b/genswap.sh new file mode 100755 index 0000000..727296e --- /dev/null +++ b/genswap.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +SWAPPATH=~/ +SWAPNAME="swapfile" +TOTALRAM=$(free -m | gawk '/Mem:/{print $2}') +VERSION="0.1.0" +BUILDDATE="2018-06-05" + + +function autoSwap(){ + echo -e "\nTotal Physical RAM is: ${TOTALRAM} MB\n" + echo -e "\nCreating a ${TOTALRAM} MB file in:${SWAPPATH}..\n" + echo -e "Enter to Accept | Ctrl+c to cancel\n" + read + + echo -e "\nCreating file in ${SWAPPATH}...\n" + sudo fallocate -l ${TOTALRAM}M ${SWAPPATH}${SWAPNAME} + ls -lh ${SWAPPATH}${SWAPNAME} + + sleep 1 + + echo -e "\nSetting permission of ${SWAPPATH}${SWAPNAME} to 600...\n" + sudo chmod 600 ${SWAPPATH}${SWAPNAME} + ls -lh ${SWAPPATH}${SWAPNAME} + + sleep 1 + + echo -e -n "\nDo you want to mark ${SWAPPATH}${SWAPNAME} as swap?(Y/n): " + read markswap + + if [ "${markswap}" = "n" ];then + exit 0 + fi + + echo -e "\nMarking ${SWAPPATH}${SWAPNAME} as swap...\n\n" + sudo mkswap ${SWAPPATH}${SWAPNAME} + + echo -e -n "\nDo you want to add swapfile to fstab?(Y/n): " + read addfstab + + if [ "${addfstab}" = "n" ];then + exit 0 + fi + + echo -e "\nCreating backup of /etc/fstab ...\n" + sudo cp /etc/fstab /etc/fstab.bak + + echo -e "Adding ${SWAPPATH}${SWAPNAME} to /etc/fstab ..\n" + echo "${SWAPPATH}${SWAPNAME} none swap sw 0 0" | sudo tee -a /etc/fstab +} + + +function help(){ + echo -e "\nUsage: $0 [-a] [--auto] [-m] [--manual] \n\n" + echo -e "-a | --auto Automatically create and size swapfile based on physical memory\n" + echo -e "-m | --manual Manually create and size swapfile based on user input\n" +} + +function version(){ + echo -e "Version ${VERSION}, built: ${BUILDDATE}\n" +} + +case "${1}" in + + -a|--auto) + autoSwap + ;; + + -v|version|--version) + version + ;; + + *) + help + ;; +esac \ No newline at end of file