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

By default, the installation process for Fedora 20 creates an LVM partition on your disk within which it creates both a root volume and a large swap volume. If you have plenty of memory, you might wish to delete that swap volume and reclaim its space for your root volume. Here's how to do so safely. This process might work for other Fedora versions but I have only tested on Fedora 20. I picked up these tips in different pages on the web, but they were not all collected together in the one place, which is why I've created this step-by-step page.

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 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" i.e. 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 something like this:

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 >/boot/grub2/grub.cfg

Georg Sauthoff writes to suggest the alternative:

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

He also warns 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

Finally, he advises that if you are not on Fedora, but instead RedHat or CentOS, then that command might need to be replaced with one of the following:

grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg

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

dracut --force --regenerate-all

Georg also writes that this step was not required on CentOS 7. I have re-confirmed that it is definitely required on Fedora 20. If you are uncertain, it is safe to run it regardless.

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.