Add:genswap.sh

Former-commit-id: d090e14af6b5d2b571f0deae23532b4f02c04773
This commit is contained in:
mollusk 2018-06-08 09:15:31 -07:00
parent 7782ae69d2
commit 7a5a5aa2d3

76
genswap.sh Executable file
View File

@ -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] <path>\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