Website/create_swap.sh

231 lines
5.9 KiB
Bash

#! /bin/bash
#check permissions
if [[ $EUID -ne 0 ]]; then
echo ""
echo "This script must be run as root! Login as root, sudo or su."
echo ""
exit 1;
fi
# #load code functions
# source create_swap_salt.sh
# #setup permissions for functions
# chmod 500 create_swap_salt.sh
# echo ""
# echo "--------------------------------------------------------------------------"
# echo "setupSwap - creates swap space on your server based on AWS guidelines"
# echo "--------------------------------------------------------------------------"
# echo ""
# echo "This will remove an existing swap file and then create a new one. "
# echo "Please read the disclaimer and review the code before proceeding."
# echo ""
# echo -n " Do you want to proceed? (y/n): "; read proceed
# if [ "$proceed" == "y" ]; then
# echo ""
# swapSetup
# else
# echo "You chose to exit. Bye!"
# fi
# echo ""
# echo "--------------------------------------------------------------------------"
# echo ""
# exit 0
removeSwap() {
echo "Will remove swap and backup fstab."
echo ""
#get the date time to help the scripts
backupTime=$(date +%y-%m-%d--%H-%M-%S)
#get the swapfile name
swapSpace=$(swapon -s | tail -1 | awk '{print $1}' | cut -d '/' -f 2)
#debug: echo $swapSpace
#turn off swapping
swapoff /"$swapSpace"
#make backup of fstab
cp /etc/fstab /etc/fstab."$backupTime"
#remove swap space entry from fstab
sed -i "/swap/d" /etc/fstab
#remove swapfile
rm -f "/$swapSpace"
echo ""
echo "--> Done"
echo ""
}
###############
### Spinner ###
###############
function spinner() {
{
spinner="/|\\-/|\\-"
while :
do
for i in $(seq 0 7)
do
echo -n "${spinner:$i:1}"
echo -en "\010"
sleep 1
done
done
}
}
########################################
### Add Swappiness setting to sysctl ###
########################################
if ! grep swappiness /etc/sysctl.conf; then
echo 80 > /proc/sys/vm/swappiness
echo 'vm.swappiness = 80' >> /etc/sysctl.conf
fi
################################################################################################
### This function identifies available ram, calculates the swap file size and configures it ####
################################################################################################
function createSwap() {
{
echo "Will create a swap and setup fstab."
echo ""
### Get available physical ram
availMemMb=$(grep MemTotal /proc/meminfo | awk '{print $2}')
### Debug: echo $availMemMb
### Convert from kb to mb to gb
gb=$(awk "BEGIN {print $availMemMb/1024/1024}")
### to debug: echo $gb
### Round the number to nearest gb
gb=$(echo "$gb" | awk '{print ($0-int($0)<0.499)?int($0):int($0)+1}')
### to debug: echo $gb
echo "-> Available Physical RAM: $gb Gb"
echo ""
if [ "$gb" -eq 0 ]; then
echo "Something went wrong! Memory cannot be 0!"
exit 1;
fi
if [ "$gb" -le 2 ]; then
echo " Memory is less than or equal to 2 Gb"
swapSizeGb=$(( gb * 2 ))
echo " -> Set swap size to $swapSizeGb Gb"
fi
if [ "$gb" -gt 2 ] && [ "$gb" -lt 32 ]; then
echo " Memory is more than 2 Gb and less than to 32 Gb."
swapSizeGb=$(( gb + 4 - 2 ))
echo " -> Set swap size to $swapSizeGb Gb."
fi
if [ "$gb" -gt 32 ]; then
echo " Memory is more than or equal to 32 Gb."
swapSizeGb=$gb
echo " -> Set swap size to $swapSizeGb Gb."
fi
echo ""
echo "Creating the swap file! This may take a few minutes."
echo ""
############################
### Create the swap file ###
############################
### start the spinner:
spinner &
### Note its PID:
SPIN_PID=$!
### Kill the spinner on any signal, including our own exit.
trap "kill -9 $SPIN_PID" $(seq 0 15)
### Convert gb to mb to avoid error:
mb=$(( gb * 1024 ))
### Create a swap file on root system and set file size to mb variable
echo "-> Create swap file."
echo ""
dd if=/dev/zero of=/swapfile bs=1M count=$mb
### Set the rw permissions
echo "-> Set swap file permissions."
echo ""
chmod 600 /swapfile
### Mkswap file
echo "-> Creating swap."
echo ""
mkswap /swapfile
### Enable the swap file for use
echo "-> Turn on swap."
echo ""
swapon /swapfile
echo ""
### Check and update (if needed) the fstab
if grep -q "swap" /etc/fstab; then
echo "-> The fstab contains a swap entry."
#do nothing
else
echo "-> The fstab does not contain a swap entry. Adding an entry."
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
fi
echo ""
echo "--> Done"
echo ""
exit 1
}
}
function swapSetup() {
{
#check if swap is on
isSwapOn=$(swapon -s | tail -1)
if [[ "$isSwapOn" == "" ]]; then
echo "No swap has been configured! Will create."
echo ""
createSwap
else
echo "Swap has been configured. Will remove and then re-create the swap."
echo ""
removeSwap
createSwap
fi
echo 'Setup swap complete! Check output to confirm everything is good.'
}
}
swapSetup
##### Notes #####
#Root
# lvresize /swap -L +2G (to increase swap by 2G)
# lvreduce /swap -L -1G (to reduce the swap by 1Gb)
#Drive
# lvresize /dev/swap -L +2G (to increase swap by 2G)
# lvreduce /dev/swap -L -1G (to reduce the swap by 1Gb)