jitty-scripts/genswap.sh

75 lines
1.7 KiB
Bash
Raw Normal View History

#!/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]\n\n"
echo -e "-a | --auto Automatically create and size swapfile based on physical memory\n"
}
function version(){
echo -e "Version ${VERSION}, built: ${BUILDDATE}\n"
}
case "${1}" in
-a|--auto)
autoSwap
;;
-v|version|--version)
version
;;
*)
help
;;
esac