jitty-scripts/genswap.sh
mollusk 371a93d751 genswap.sh: swap enabled after marked, reboot prompt to verify fstab
Former-commit-id: 4afaec57fbe8c47000fb69cd10be3c87b74ea19a
2018-09-19 03:14:11 -07:00

159 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
SWAPPATH=/
SWAPNAME="swapfile"
TOTALRAM=$(free -m | gawk '/Mem:/{print $2}')
SWAPPINESS="10"
SYSCTLCONF="/etc/sysctl.conf"
VERSION="0.1.0"
BUILDDATE="2018-06-05"
# Colors
RED='\033[0;31m'
LRED="\033[1;31m"
BLUE="\033[0;34m"
LBLUE="\033[1;34m"
GREEN="\033[0;32m"
LGREEN="\033[1;32m"
YELLOW="\033[1;33m"
CYAN="\033[0;36m"
LCYAN="\033[1;36m"
PURPLE="\033[0;35m"
LPURPLE="\033[1;35m"
BWHITE="\e[1m"
NC='\033[0m' # No Color
if [ ! -f /usr/bin/gawk ];then
echo -e "\nPlease install 'gawk' with your package manager\n"
exit 0
fi
function autoSwap(){
echo -e "\n${LGREEN}----------------------------------------------------------------${NC}"
echo -e "\n${LCYAN}Total Physical RAM is: ${TOTALRAM} MB\n${NC}"
echo -e "${LPURPLE}Creating a ${TOTALRAM} MB file in:${SWAPPATH}..\n${NC}"
echo -e "${YELLOW}Enter to Accept | Ctrl+c to cancel\n${NC}"
echo -e "${LGREEN}------------------------------------------------------------------${NC}"
read
echo -e "${LCYAN}\nCreating file in ${SWAPPATH}...\n${NC}"
sudo fallocate -l ${TOTALRAM}M ${SWAPPATH}${SWAPNAME}
ls -lh ${SWAPPATH}${SWAPNAME}
sleep 1
echo -e "\n${LCYAN}Setting permission of ${SWAPPATH}${SWAPNAME} to 600...\n${NC}"
sudo chmod 600 ${SWAPPATH}${SWAPNAME}
ls -lh ${SWAPPATH}${SWAPNAME}
sleep 1
echo -e -n "\n${YELLOW}Do you want to mark ${SWAPPATH}${SWAPNAME} as swap?(Y/n): ${NC}"
read markswap
if [ "${markswap}" = "n" ] || [ "${markswap}" = "N" ];then
exit 0
fi
echo -e "\n${LCYAN}Marking ${SWAPPATH}${SWAPNAME} as swap...\n\n${NC}"
sudo mkswap ${SWAPPATH}${SWAPNAME}
echo -e "${LCYAN}Enabling Swap file for use...${NC}\n"
sudo swapon ${SWAPPATH}${SWAPNAME}
echo -e -n "\n${YELLOW}Do you want to add swapfile to fstab?(Y/n): ${NC}"
read addfstab
if [ "${addfstab}" = "n" ];then
exit 0
fi
echo -e "\n${LCYAN}Creating backup of /etc/fstab ...\n${NC}"
sudo cp -v /etc/fstab /etc/fstab.bak
echo -e "${LCYAN}Adding ${SWAPPATH}${SWAPNAME} to /etc/fstab ..\n${NC}"
echo "${SWAPPATH}${SWAPNAME} none swap sw 0 0" | sudo tee -a /etc/fstab
CHECKFSTAB=$(cat /etc/fstab | grep "swap" | gawk '/swap/{print $2}')
if [ $CHECKFSTAB != "swap" ];then
echo -e "${LRED}Swap line does not exist in /etc/fstab, please manually check this.${NC}\n"
exit 1
else
echo -e "${LGREEN}Swap successfully added to /etc/fstab! Reboot to verify success...${NC}\n"
echo -e "${YELLOW}Do you want to reboot your system now?(n/Y): ${NC}"
read rebootCheck
if [ "${rebootCheck}" = "y" ] || [ "${rebootCheck}" = "Y" ];then
echo -e "${LRED}Rebooting system in 3 seconds...${NC}\n"
sleep 3
sudo reboot
else
exit 0
fi
fi
}
function swappiness(){
echo -e -n "\n${YELLOW}Enter a number for swappiness (0-100)[default=10]: ${NC}"
read setSwappiness
if [ -z "${setSwappiness}" ];then
echo -e "\n${LCYAN}Setting default (10)\n${NC}"
echo "vm.swappiness=${SWAPPINESS}" | sudo tee -a /etc/sysctl.conf
elif [[ ! "${setSwappiness}" =~ ^-?[0-9]+$ ]];then
echo -e "\n${LRED}${setSwappiness} is not a number\n${NC}"
exit 0
elif [ "${setSwappiness}" -lt "0" ];then
echo -e "\n${LRED}Please input a number greater than 0 or less than 100\n${NC}"
exit 0
elif [ "${setSwappiness}" -gt "100" ];then
echo -e -n "\n${LRED}Please input a number greater than 0 or less than 100\n${NC}"
exit 0
else
echo "vm.swappiness=${setSwappiness}" | sudo tee -a ${SYSCTLCONF}
SWAPPINESSSET=$(cat ${SYSCTLCONF} | grep vm.swappiness=${setSwappiness})
if [ ${SWAPPINESSSET} = "vm.swappiness=${setSwappiness}" ] ;then
echo -e "${LGREEN}Swappiness value set successfully!\n${NC}"
else
echo -e "${LRED}Swappiness not set sucessfully...\n${NC}"
fi
fi
}
function help(){
echo -e "\nUsage: $0 [-a] [--auto] [-s] [--swappiness]\n\n"
echo -e "-a | --auto Automatically create and size swapfile based on physical memory\n"
echo -e "-s | --swappiness Set the swappiness value to minimize swap usage\n\n"
}
function version(){
echo -e "Version ${VERSION}, Build Date: ${BUILDDATE}\n"
}
case "${1}" in
-a|--auto)
autoSwap
;;
-v|version|--version)
version
;;
-s|--swappiness)
swappiness
;;
*)
help
;;
esac