Create Linux Swap Script
This is a simple script I use to setup the swap on servers that are running weblogic or an Oracle DB, but I also use it to configure swap on the other cloud providers, like Digital Ocean, Linode, Vultr and Contabo to name a few...
You can download this script here or at the bottom of the page
Systems Variables Defined
##########################
#### System Variables ####
##########################
swpsize=4 # set the size of swapfile needed in gigabytes
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
swpsize=4 # set the size of swapfile needed in gigabytes
You can change swpsize=4 to swpsize=$1 if you want to add the size on the command line other wise you just need to set the size of the swap you are wanting or that is required for the application setup, I use the manual setting since I combine my scripts to do everything at one time
Checking for a swap file and if there is one checking if it is the required size and reporting the finding to the screen
########################################################################
#### 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 =================================================================================='
If an existing swapfile is found, turn it off if it's not big enough...
#######################################################
#### Turn off existing swap if needing replacement ####
#######################################################
if echo "$dir" | grep -i swap; then
swapoff /"${swpname}"
rm -f /"$swpname"
fi
Create the size swapfile needed
############################################
#### 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
I'm pointing this out just in case you get an error trying to activate the swap, some linux systems don't like to have fallocate create their swaps due, to the holes and will only work using dd as mentioned in the swapon manpage and I quote
From swapon(8) manpage: Files with holes The swap file implementation in the kernel expects to be able to write to the file directly, without the assistance of the filesystem. This is a problem on files with holes or on copy-on-write files on filesystems like Btrfs. Commands like cp(1) or truncat…
#################################################
#### Fallocate does not work on some systems ####
#################################################
# dd if=/dev/zero of=/.SwapFile count=${swpsize} bs=1MiB
I moved to fallocate due to the speed as some of the longer scripts would take way too long to update or modify the systems. The error you will see is swapon: /.SwapFile: swapon failed: Invalid argument, to fix this comment out # fallocate -l ${swpsize}g /.SwapFile
line and uncomment the line containing dd if=/dev/zero of=/.SwapFile count=${swpsize} bs=1MiB
re-run the script and all should work or if you like using dd more you can change over to that.
Configure and make the new swapfile active
################################################
#### Configure and enable the new swap file ####
################################################
chmod 600 /.SwapFile
mkswap /.SwapFile
swapon /.SwapFile
Do a check to make sure the swap is active and reporting it to the screen
###########################################
#### 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
checking fstab for the new swapfile and adding it to fstab if it's not there.
######################################################################
#### 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
You can download this script here
Comments