genswap.sh: require root to run, remove sudo from file

Former-commit-id: 271db86f3101556275bc21997f0a6ac331235662
Former-commit-id: 7f2857e332d23067a27059f5423564fc99089937
This commit is contained in:
mollusk 2018-12-19 01:00:01 -07:00
parent fa1496dccd
commit 752adcd0dc

View File

@ -29,6 +29,11 @@ if [ ! -f /usr/bin/gawk ];then
exit 0 exit 0
fi fi
function autoSwap(){ function autoSwap(){
if [[ $EUID -ne 0 ]]; then
printf "${LRED}This script must be run as root${NC}\n" 1>&2
exit 1
fi
echo -e "\n${LGREEN}----------------------------------------------------------------${NC}" echo -e "\n${LGREEN}----------------------------------------------------------------${NC}"
echo -e "\n${LCYAN}Total Physical RAM is: ${TOTALRAM} MB\n${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 "${LPURPLE}Creating a ${TOTALRAM} MB file in:${SWAPPATH}..\n${NC}"
@ -37,13 +42,13 @@ function autoSwap(){
read read
echo -e "${LCYAN}\nCreating file in ${SWAPPATH}...\n${NC}" echo -e "${LCYAN}\nCreating file in ${SWAPPATH}...\n${NC}"
sudo fallocate -l ${TOTALRAM}M ${SWAPPATH}${SWAPNAME} fallocate -l ${TOTALRAM}M ${SWAPPATH}${SWAPNAME}
ls -lh ${SWAPPATH}${SWAPNAME} ls -lh ${SWAPPATH}${SWAPNAME}
sleep 1 sleep 1
echo -e "\n${LCYAN}Setting permission of ${SWAPPATH}${SWAPNAME} to 600...\n${NC}" echo -e "\n${LCYAN}Setting permission of ${SWAPPATH}${SWAPNAME} to 600...\n${NC}"
sudo chmod 600 ${SWAPPATH}${SWAPNAME} chmod 600 ${SWAPPATH}${SWAPNAME}
ls -lh ${SWAPPATH}${SWAPNAME} ls -lh ${SWAPPATH}${SWAPNAME}
sleep 1 sleep 1
@ -56,10 +61,10 @@ function autoSwap(){
fi fi
echo -e "\n${LCYAN}Marking ${SWAPPATH}${SWAPNAME} as swap...\n\n${NC}" echo -e "\n${LCYAN}Marking ${SWAPPATH}${SWAPNAME} as swap...\n\n${NC}"
sudo mkswap ${SWAPPATH}${SWAPNAME} mkswap ${SWAPPATH}${SWAPNAME}
echo -e "${LCYAN}Enabling Swap file for use...${NC}\n" echo -e "${LCYAN}Enabling Swap file for use...${NC}\n"
sudo swapon ${SWAPPATH}${SWAPNAME} swapon ${SWAPPATH}${SWAPNAME}
echo -e -n "\n${YELLOW}Do you want to add swapfile to fstab?(Y/n): ${NC}" echo -e -n "\n${YELLOW}Do you want to add swapfile to fstab?(Y/n): ${NC}"
read addfstab read addfstab
@ -69,7 +74,7 @@ function autoSwap(){
fi fi
echo -e "\n${LCYAN}Creating backup of /etc/fstab ...\n${NC}" echo -e "\n${LCYAN}Creating backup of /etc/fstab ...\n${NC}"
sudo cp -v /etc/fstab /etc/fstab.bak cp -v /etc/fstab /etc/fstab.bak
echo -e "${LCYAN}Adding ${SWAPPATH}${SWAPNAME} to /etc/fstab ..\n${NC}" 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
@ -98,7 +103,10 @@ function autoSwap(){
} }
function setSwappinessLvl(){ function setSwappinessLvl(){
if [[ $EUID -ne 0 ]]; then
printf "${LRED}This script must be run as root${NC}\n" 1>&2
exit 1
fi
echo -e -n "\n${YELLOW}Enter a number for swappiness (0-100)[default=10]: ${NC}" echo -e -n "\n${YELLOW}Enter a number for swappiness (0-100)[default=10]: ${NC}"
read setSwappiness read setSwappiness