How to remove existing traditional swap partitions
Swap space is a common and important aspect of computing today regardless of operating system. Linux uses swap space to substitute for RAM when it becomes too full to effectively support additional programs or data. It is a way to temporarily enable the system to keep running albeit at the cost of reduced performance. However the use of zram for swap space has provided an interesting, if counter-intuitive, method for providing a reasonable amount of swap space while significantly improving swap performance.
In a previous article I wrote about using zram for Linux swap space and showed how to configure and activate it. That article covers zram sizing as well as the advantages of using zram for swap instead of traditional hard disk drives (HDD) or solid state devices (SSD). In another article, How I troubleshoot swappiness and startup time on Linux, I wrote about tuning zram swap space using the vm.swappiness kernel parameter.
What I haven’t previously discussed is how to disable and remove those traditional swap files and partitions.
Removing Traditional Swap
This process is not as straightforward as it should be. It is not hard but it took me some research to figure it out because there is a lot of old and incorrect information out there on the Internet. This procedure works for me on Fedora 36 and later.
First turn off swap for the existing swap partitions and files. This can be done using swapoff /dev/nameofswapdevice but it might be easiest to just turn off all swap with the swapoff -a command. This command also turns off any existing Zram swap.
# swapoff -a
Remove the entries for all traditional swap partitions or files in the /etc/fstab file. I originally commented these out in case of unexpected problems so that it would be easy to reactivate them. I deleted those entries after a few days of running with only zram swap. Zram swap does not require an entry in the /etc/fstab file.
You’d think – at least I did at first – that this would be all that is needed and you could remove the swap partitions or the logical volumes designated as swap. But–no. I did remove the logical volume I had designated as swap space and rebooted to test. The reboot failed and hung very early in the boot process.
Fortunately I have configured my kernel so that it displays boot and startup messages rather than the graphical boot designed to hide the “scary stuff” from users. As a result I was able to see the error message indicating that the kernel couldn’t find the swap volume. To recover from this I booted from a Live Fedora USB drive and created a new swap volume. It is not necessary to do anything else. Then I rebooted and removed the swap entries in kernel line of /etc/defaults/grub. The grub boot defaults are set in the /etc/default/grub file. Changes are made to this file and then the grub.cfg file is regenerated.
The default /etc/default/grub configuration file is simple and we only need concern ourselves with one line.
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="resume=/dev/mapper/vg01-swap rd.lvm.lv=vg01/root rd.lvm.lv=vg01/swap rd.lvm.lv=vg01/usr rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true
Change the GRUB_CMDLINE_LINUX
line to the following.
GRUB_CMDLINE_LINUX="rd.lvm.lv=vg01/root rd.lvm.lv=vg01/usr"
Removing “rhgb quiet
” kernel options causes all of the kernel boot messages and systemd startup messages to be displayed. This can make it easier to quickly locate problems during the boot and startup phases. Removing “resume=/dev/mapper/vg01-swap” and “rd.lvm.lv=vg01/swap” prevents the kernel from looking for the swap volume.
To make these changes take effect it is necessary to rebuild /boot/grub2/grub.cfg. Make a backup of the current grub.cfg file and then run the following command.
# grub2-mkconfig > /boot/grub2/grub.cfg
After this I deleted the swap partition, ran swapon -a and verified with swapon –show and lsblk. Rebooting the system gave me a final check to ensure that the system did boot properly and that the only swap is the Zram swap.
Did you notice how easy it is to make changes to the kernel configuration?
Turn off swap for partition
remove entry for swap in /etc/fstab
remove swap entries in kernel line of /etc/defaults/grub
regen grub
Optional – disable swap service
remove swap volume or partition
test by rebooting