Automatically Create/Remove and Mount swap file in Linux using Shell script

Few days ago we have covered an article about swap file creation in Linux using three ways which is the common method but it requires manual human effort.

Today I have found a small shell script (Two shell script, one for swap file creation and another one for removing swap file) which was written by Gary Stafford that help us to create/remove & mount swap file automatically in Linux.

By default the script create and mount 512MB swapfile. If you want more swap space and different file name you have to modify the script accordingly. It’s not a big deal to modify the script since it’s very handy and small script,  I have colored the line where you want to modify the script.

Suggested Read : 3 Easy Ways To Create Or Extend Swap Space In Linux

How to check current swap size

Lets first check the size of existing swap space partition using free & swapon command.

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           2.0G        1.3G        139M         45M        483M        426M
Swap:          2.0G        655M        1.4G

$ swapon --show
NAME      TYPE      SIZE   USED PRIO
/dev/sda5 partition   2G 655.2M   -1

The above output clearly shows 2GB is my current swap space.

Create Swap File

Create create_swap.sh file and add below script to automate the swap space creation and mounting.

$ nano create_swap.sh
#!/bin/sh

# size of swapfile in megabytes
swapsize=1024

# 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

echo '--------------------------------------------'
echo 'Check whether the swap space created or not?'
echo '--------------------------------------------'
swapon --show

Add execute permission to the file.

$ sudo +x create_swap.sh

Run the file to create and mount swap file.

$ sudo ./create_swap.sh

swapfile not found. Adding swapfile.
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=d9004261-396a-4321-a45f-9923e3e1328c
--------------------------------------------
Check whether the swap space created or not?
--------------------------------------------
NAME      TYPE       SIZE   USED PRIO
/dev/sda5 partition    2G 954.1M   -1
/swapfile file      1024M     0B   -2

Yes I can see the new 1024M swapfile. Reboot the system to use the new swap file.

Remove Swap File

If the swap file is no longer required, then create remove_swap.sh file and add below script to remove swap file and its mount point from /etc/fstab.

$ nano remove_swap.sh
#!/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

echo '--------------------------------------------'
echo 'Check whether the swap space removed or not?'
echo '--------------------------------------------'
swapon --show

Add execute permission to the file.

$ sudo +x remove_swap.sh

Run the file to remve and unmount swap file.

$ sudo ./remove_swap.sh

swapfile found. Removing swapfile.
swapoff: /dev/sda5: swapoff failed: Cannot allocate memory
--------------------------------------------
Check whether the swap space removed or not?
--------------------------------------------
NAME      TYPE      SIZE   USED PRIO
/dev/sda5 partition   2G 951.8M   -1

3 Comments on “Automatically Create/Remove and Mount swap file in Linux using Shell script”

  1. hi! really nice! two things that I see:
    1 – do you forgote the chmod here??
    $ sudo +x create_swap.sh

    2- as you say for XFS need to use dd to create the file, but do not work in btrfs.

    Thanks for all!!! Cheers!!

  2. on Red Hat 7, if you use fallocate command and then use mkswap, the swapon command will fail.
    but the old dd command to generate a file is successful. dd if=/dev/zero of=/swapfile count=1000 bs=1024k

Leave a Reply to nerdtron Cancel reply

Your email address will not be published. Required fields are marked *