genswap.sh:add swappiness support

Former-commit-id: 7c1073978edf4b8f538bdacfec7b12cb9943faf7
This commit is contained in:
mollusk 2018-06-11 12:37:15 -07:00
parent 6b6c066d7a
commit 48dc7b4107

View File

@ -3,59 +3,110 @@
SWAPPATH=/ SWAPPATH=/
SWAPNAME="swapfile" SWAPNAME="swapfile"
TOTALRAM=$(free -m | gawk '/Mem:/{print $2}') TOTALRAM=$(free -m | gawk '/Mem:/{print $2}')
SWAPPINESS="10"
SYSCTLCONF="/etc/sysctl.conf"
VERSION="0.1.0" VERSION="0.1.0"
BUILDDATE="2018-06-05" 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 if [ ! -f /usr/bin/gawk ];then
echo -e "\nPlease install 'gawk' with your package manager\n" echo -e "\nPlease install 'gawk' with your package manager\n"
exit 0 exit 0
fi fi
function autoSwap(){ function autoSwap(){
echo -e "\nTotal Physical RAM is: ${TOTALRAM} MB\n" echo -e "\n${LGREEN}----------------------------------------------------------------${NC}"
echo -e "\nCreating a ${TOTALRAM} MB file in:${SWAPPATH}..\n" echo -e "\n${LCYAN}Total Physical RAM is: ${TOTALRAM} MB\n${NC}"
echo -e "Enter to Accept | Ctrl+c to cancel\n" 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 read
echo -e "\nCreating file in ${SWAPPATH}...\n" echo -e "${LCYAN}\nCreating file in ${SWAPPATH}...\n${NC}"
sudo fallocate -l ${TOTALRAM}M ${SWAPPATH}${SWAPNAME} sudo fallocate -l ${TOTALRAM}M ${SWAPPATH}${SWAPNAME}
ls -lh ${SWAPPATH}${SWAPNAME} ls -lh ${SWAPPATH}${SWAPNAME}
sleep 1 sleep 1
echo -e "\nSetting permission of ${SWAPPATH}${SWAPNAME} to 600...\n" echo -e "\n${LCYAN}Setting permission of ${SWAPPATH}${SWAPNAME} to 600...\n${NC}"
sudo chmod 600 ${SWAPPATH}${SWAPNAME} sudo chmod 600 ${SWAPPATH}${SWAPNAME}
ls -lh ${SWAPPATH}${SWAPNAME} ls -lh ${SWAPPATH}${SWAPNAME}
sleep 1 sleep 1
echo -e -n "\nDo you want to mark ${SWAPPATH}${SWAPNAME} as swap?(Y/n): " echo -e -n "\n${YELLOW}Do you want to mark ${SWAPPATH}${SWAPNAME} as swap?(Y/n): ${NC}"
read markswap read markswap
if [ "${markswap}" = "n" ];then if [ "${markswap}" = "n" ];then
exit 0 exit 0
fi fi
echo -e "\nMarking ${SWAPPATH}${SWAPNAME} as swap...\n\n" echo -e "\n${LCYAN}Marking ${SWAPPATH}${SWAPNAME} as swap...\n\n${NC}"
sudo mkswap ${SWAPPATH}${SWAPNAME} sudo mkswap ${SWAPPATH}${SWAPNAME}
echo -e -n "\nDo you want to add swapfile to fstab?(Y/n): " echo -e -n "\n${YELLOW}Do you want to add swapfile to fstab?(Y/n): ${NC}"
read addfstab read addfstab
if [ "${addfstab}" = "n" ];then if [ "${addfstab}" = "n" ];then
exit 0 exit 0
fi fi
echo -e "\nCreating backup of /etc/fstab ...\n" echo -e "\n${LCYAN}Creating backup of /etc/fstab ...\n${NC}"
sudo cp /etc/fstab /etc/fstab.bak sudo cp /etc/fstab /etc/fstab.bak
echo -e "Adding ${SWAPPATH}${SWAPNAME} to /etc/fstab ..\n" echo -e "${LCYAN}Adding ${SWAPPATH}${SWAPNAME} to /etc/fstab ..\n${NC}"
echo "${SWAPPATH}${SWAPNAME} none swap sw 0 0" | sudo tee -a /etc/fstab echo "${SWAPPATH}${SWAPNAME} none swap sw 0 0" | sudo tee -a /etc/fstab
} }
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(){ function help(){
echo -e "\nUsage: $0 [-a] [--auto]\n\n" 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 "-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(){ function version(){
@ -72,6 +123,10 @@ case "${1}" in
version version
;; ;;
-s|--swappiness)
swappiness
;;
*) *)
help help
;; ;;