Shell Script to Automate Creation of Swap File on Linux

Use shell scripts to create and remove Linux swap files on the fly.

Introduction

Recently, while scripting the installation of Oracle’s WebLogic Server, I ran into an issue with a lack of a swap space. I was automating the installation of WebLogic in Silent Mode on a Vagrant VM. The VM was built from an Ubuntu Cloud Image of Ubuntu ServerUbuntu Cloud Images are pre-installed disk images that have been customized by Ubuntu engineering to run on cloud-platforms such as Amazon EC2, Openstack, Windows, LXC, and Vagrant. The Ubuntu image did not have the minimum 512 MB of swap space required by the WebLogic installer.

Swap Space Error During Install

Swap Space Error During WebLogic Install

Swap

According to Gary Sims on Linux.com, “Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Scripts

To create the required swap space, I could create either a swap partition or a swap file. I chose to create a swap file, using a shell script. Actually, there are two scripts. The first script creates creates a 512 MB swap file as a pre-step in the automated installation of WebLogic. Once the WebLogic installation is complete, the second, optional script, may be ran to remove the swap file. ArchWiki (wiki.archlinux.org)  has an excellent post on swap space I referenced to build my first script.

Use a ‘sudo ./create_swap.sh’ command to create the swap file and display the results in the terminal.

#!/bin/sh
# size of swapfile in megabytes
swapsize=512
# does the swap file already exist?
grep -q "swapfile" /etc/fstab
# if not then create it
if [ $? -ne 0 ]; then
echo 'swapfile not found. Adding swapfile.'
fallocate -l ${swapsize}M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap defaults 0 0' >> /etc/fstab
else
echo 'swapfile found. No changes made.'
fi
# output results to terminal
cat /proc/swaps
cat /proc/meminfo | grep Swap
view raw create_swap.sh hosted with ❤ by GitHub
Creating the Swap File

Creating the Swap File

Installing without Swap Space Error

Installing WebLogic with a Swap File

If the swap file is no longer required, the second script will remove it. Use a ‘sudo ./remove_swap.sh’ command to remove the swap file and display the results in the terminal. LinuxQuestions.org has a good Forum post on removing swap files I referenced to build my second script.

#!/bin/sh
# does the swap file exist?
grep -q "swapfile" /etc/fstab
# if it does then remove it
if [ $? -eq 0 ]; then
echo 'swapfile found. Removing swapfile.'
sed -i '/swapfile/d' /etc/fstab
echo "3" > /proc/sys/vm/drop_caches
swapoff -a
rm -f /swapfile
else
echo 'No swapfile found. No changes made.'
fi
# output results to terminal
cat /proc/swaps
cat /proc/meminfo | grep Swap
view raw remove_swap.sh hosted with ❤ by GitHub
Removing Swap File

Removing the Swap File

, , , , ,