How to safely delete the LVM swap volume and extend the root volume on Fedora

This how-to for the Fedora operating system (a GNU/Linux distribution) describes how to free up space on your hard disk that Fedora claims by default during installation.

By default, Fedora's installation process creates an LVM partition on your disk within which it creates both a root volume (for normal file storage) and a large swap volume (to supplement physical memory, or RAM). If you have plenty of physical memory (RAM), you might wish to delete that swap volume and reclaim its space for your filesystem on the root volume. Here's how to do so safely.

This sequence of steps might work for other versions of Fedora but I have tested it only on versions 20 and 29. I have though also tested it and found it to work on CentOS 7. It might work on Red Hat too. Note though that if you try it on CentOS or Red Hat, you will need in various places which I've highlighted in tan to substitute "fedora" with "centos" or "redhat", respectively.

Step 1. Open up a terminal. Acquire root privileges in the terminal shell by typing this command:

su

Step 2. Edit /etc/fstab and delete the line that has swap mounted automatically at boot. Type:

vi /etc/fstab

This will open up /etc/fstab in the vi editor. Use the arrow keys to move the cursor to the line with information about swap that looks something like this:

/dev/mapper/fedora-swap swap                    swap    defaults        0 0

When the cursor is on that line, make sure caps lock is off and press the "d" key twice in succession. This will delete the line. Then hold down shift and press the "z" key twice in succession. This will save the file and exit vi.

Step 3. Turn off the already-mounted swap for this session so that the next step doesn't result in an error. Type:

swapoff -a

Step 4. Delete the swap volume permanently. Type:

lvremove /dev/mapper/fedora-swap

Step 5. Expand the root volume into the space made available by deleting the swap volume. This command performs two tasks. Firstly, the -l+100%FREE parameter instructs lvextend to expand the volume to fill all available space. Secondly, the -r parameter instructs lvextend to resize the filesystem within the volume to match the new size of the volume. Note that it is safe to do this "live": that is, while the volume is mounted. Type:

lvextend -r -l+100%FREE /dev/mapper/fedora-root

Step 6. Remove references to the swap volume from the default GRUB configuration file. Type:

vi /etc/default/grub

This will open up /etc/default/grub in the vi editor. Use the arrow keys to move the cursor to the very first character in the text rd.lvm.lv=fedora/swap, which occurs in a line starting with "GRUB_CMDLINE_LINUX=". The line will look something like this - I've emboldened the relevant text in it:

GRUB_CMDLINE_LINUX="rd.lvm.lv=fedora/swap vconsole.font=latarcyrheb-sun16 rd.lvm.lv=fedora/root $([ -x /usr/sbin/rhcrashkernel-param ] && /usr/sbin/rhcrashkernel-param || :) rhgb quiet"

When the cursor is on the first character, the "r", then, first making sure caps lock is still off, type "d9w". This instructs vi to delete 9 words, which will delete the entire phrase up to and including the space character after "swap" (each of the two dots, the single equals sign and the single forward slash count as a single word). Next, hold down the shift key and press the "z" key twice in succession. Again, this will save the file and exit vi.

Step 7. Rebuild the working GRUB configuration file, which will incorporate the changes you made to the default GRUB configuration file in the previous step. Type:

grub2-mkconfig -o /boot/grub2/grub.cfg

Georg Sauthoff writes in to warn that if you use UEFI-Boot, then you will need to write to the file at a different location, as follows:

grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg

Step 8. Rebuild the initramfs images, incorporating the changes you made in the previous two steps.

Georg also writes that this step was not required on CentOS 7. I have confirmed this. I have re-confirmed that this step definitely is required on Fedora 20 though.

Warning: Ethan Brown writes in to caution that this step caused him a subsequent boot failure on CentOS 7 which he suspects had something to do with his having FIPS enabled, since the error message he got was:

dracut: FATAL: FIPS integrity test failed
dracut: Refusing to continue
system halted.

I have been unable to replicate the problem on a minimal fresh CentOS 7 installation with FIPS enabled (regardless of whether I enabled it at system installation or post-installation), but since this step seems to be unnecessary on CentOS 7 anyway, you might choose to skip it for safety on that platform, especially if you have FIPS enabled. Otherwise:

Type:

dracut --force --regenerate-all

Step 9 (Not required, but recommended, to make sure nothing's gone wrong). Reboot. If all has gone well, the boot process will be as normal, and your root partition will have grown by the size of your deleted swap partition (in fact, this will already have been the case prior to rebooting).

That's it! If you have any feedback, please feel free to contact me.

Problems you will have avoided

The procedure above avoids boot problems. If you stop at step 5 and then reboot, your system will hang at boot for a long time, and then eventually drop into an emergency dracut shell, displaying something very similar to the following messages:

[  OK  ] Found device /dev/mapper/fedora-root.
[  317.141592] dracut-initqueue[231]: Warning: Could not boot.
[  317.158352] dracut-initqueue[231]: Warning: /dev/fedora/swap does not exist
         Starting Dracut Emergency Shell...
Warning: /dev/fedora/swap does not exist

Generating "/run/initramfs/rdsosreport.txt"


Entering emergency mode. Exit the shell to continue.
Type "journalctl" to view system logs.
You might want to save "/run/initramfs/rdsosreport.txt" to a USB stick or /boot
after mounting them and attach it to a bug report.


dracut:/#

In this case, all that is required is to type "exit", and then to follow steps 1, 6, 7 and 8 to resolve the problem.