102 lines
5.6 KiB
Bash
102 lines
5.6 KiB
Bash
#!/bin/bash
|
|
|
|
####################################################################
|
|
#### Make a Swap script - Just set the size and the script ####
|
|
#### does the rest. ####
|
|
#### ####
|
|
#### Author: Phil Connor ####
|
|
#### Contact: contact@mylinux.work ####
|
|
#### Version 2.25 ####
|
|
#### ####
|
|
#### To use this script chmod it to 755 ####
|
|
#### or simply type bash mk_swap.sh ####
|
|
####################################################################
|
|
|
|
########################
|
|
#### User Variables ####
|
|
########################
|
|
swpsize=4 # set the size of swapfile needed in gigabytes
|
|
|
|
##########################
|
|
#### System Variables ####
|
|
##########################
|
|
swpneed=$((swpsize * 1024)) # total the swap needs to be in mb's
|
|
dir=$(ls -la --block-size=M /) # / part dir file list
|
|
swpexist=$(echo "$dir" | grep -i swap | awk '{ print $5 }' | tr -d 'M"') # does the swap file already exist?
|
|
swpname=$(echo "$dir" | grep -i swap | awk '{ print $9 }') # If it already exists what is the name of the swap file
|
|
swppres=$(free -m | sed -n '3 p' | awk '{ print $2 }') # If it exists and is present what size is it
|
|
|
|
########################################################################
|
|
#### Check If the swap file already exist and if it's large enough? ####
|
|
########################################################################
|
|
if (( swpneed < swpexist )) || (( swpneed < swppres )); then
|
|
echo -e '\e[01;37m ======================================================================='
|
|
echo -e '\e[01;32m ====================================================================='
|
|
echo -e '\e[01;32m ==== \e[01;37m A Large Enough Swapfile was Found! No Changes Needed... \e[01;32m ===='
|
|
echo -e '\e[01;32m ====================================================================='
|
|
echo -e '\e[01;37m ======================================================================='
|
|
elif (( swpneed > swpexist )) || (( swpneed > swppres )); then
|
|
echo -e '\e[01;37m =================================================================================='
|
|
echo -e '\e[01;31m ================================================================================'
|
|
echo -e '\e[01;31m ==== \e[01;37m A Large Enough Swapfile was not found! Creating Larger SwapFile... \e[01;31m ===='
|
|
echo -e '\e[01;31m ================================================================================'
|
|
echo -e '\e[01;37m =================================================================================='
|
|
|
|
#######################################################
|
|
#### Turn off existing swap if needing replacement ####
|
|
#######################################################
|
|
if echo "$dir" | grep -i swap; then
|
|
swapoff /"${swpname}"
|
|
rm -f /"$swpname"
|
|
fi
|
|
|
|
############################################
|
|
#### Create the requested size swapfile ####
|
|
############################################
|
|
fallocate -l ${swpsize}g /.SwapFile
|
|
|
|
#################################################
|
|
#### Fallocate does not work on some systems ####
|
|
#################################################
|
|
# dd if=/dev/zero of=/.SwapFile count=${swpsize} bs=1MiB
|
|
|
|
################################################
|
|
#### Configure and enable the new swap file ####
|
|
################################################
|
|
chmod 600 /.SwapFile
|
|
mkswap /.SwapFile
|
|
swapon /.SwapFile
|
|
|
|
###########################################
|
|
#### Check to make sure swap is active ####
|
|
###########################################
|
|
echo -e '\e[01;37m ================================================================================='
|
|
echo -e '\e[01;32m ==============================================================================='
|
|
echo -e '\e[01;32m ==== \e[01;37m Checking whether the swap space was mounted and is active or not! \e[01;32m ===='
|
|
echo -e '\e[01;32m ==============================================================================='
|
|
echo -e '\e[01;37m ================================================================================='
|
|
R=$(swapon -s)
|
|
if [ -n "$R" ]; then
|
|
echo -e '\e[01;32m ============'
|
|
echo -e '\e[01;32m ============'
|
|
echo -e '\e[01;32m =============================================================================='
|
|
echo -e "\e[01;37m$R"
|
|
echo -e '\e[01;32m =============================================================================='
|
|
echo -e '\e[01;37m ================================================================================'
|
|
else
|
|
echo -e '\e[01;31m ============'
|
|
echo -e '\e[01;31m ============'
|
|
echo -e '\e[01;31m ============================================================================'
|
|
echo -e "\e[01;37m ==== Something Went Wrong no Swap was Loaded ===="
|
|
echo -e '\e[01;31m ============================================================================'
|
|
echo -e '\e[01;37m =============================================================================='
|
|
fi
|
|
|
|
######################################################################
|
|
#### Check to see if the created swap is listed in the fstab file ####
|
|
######################################################################
|
|
if ! grep -q "SwapFile" /etc/fstab; then
|
|
echo "/.SwapFile swap swap defaults 0 0" >> /etc/fstab
|
|
fi
|
|
fi
|
|
|