Resizing a Raw Virtual Machine LVM Partition Using fdisk

As the amount of data we store continues to grow and grow, there is bound to become a time that you will need to resize your partitions to add more disk space. On our of my MySQL replication virtual servers, I created it initially with a 200GB disk. After moving all the data over, was wishing I would have had a bit more space. Good news is this can easily be done. I should note that I am using raw image files, not lvms, so there is a bit more work required.

Before we go any further, it’s important to note that we will be modifying the partition table in this document. Modifying the partition table could result in complete data loss if not done correctly. Please make sure you have a full backup of your system before proceeding. If you’re not sure what you’re doing, it’s best to practice this on a test system first. I will not be held responsible for your failure to back up your system and/or data loss that may occur.

That being said… let’s get to the fun stuff. We will be adding an additional 50GB of space to this virtual machine.  We will begin on the host server by adding the additional space to the image file. Make sure the virtual machine is shut down first.

You can now start the guest virtual machine and log in. You’ll notice both df and pvdisplay still report the old partition sizes.

We will be using fdisk to resize the partition. Open the disk and make a note of the current partition layout. We will need this information later. The disk size here should reflect the amount of space we added above.

The next two steps are where you need to exercise extreme caution. Before continuing, please reread the disclaimer regarding backups above. In order to resize the partition, we need to delete and recreate it. When you recreate the partition, you must create it with the same exact starting unit and you must create it with an ending point the same or greater unit. Delete the partition and print the partition table to verify it’s gone.

Recreate the partition using the same starting unit. In our example, the starting unit is 1026048. For the ending unit, we want the maximum amount of free disk space, which is automatically populated by default, so we left the prompt blank. Print the partition table again to verify.

Now you’ll notice the new partition type is “83 Linux” where it was “8e Linux LVM” before. Use the type command to change the type of the new partition back to what it was to begin with. Again, print the partition table to verify. It never hurts to verify each step to ensure the commands you’re entering are being executed correctly.

Compare the new partition table to the old one. The start unit should be exactly the same after as before. The end unit should be greater than or equal to what it was before. The Id/System partition types should match exactly. If everything looks okay, write the changes and reboot the system.

We’re not done, but getting close. Once the guest reboots, log back in. We resized the partition, but still need to resize the LVM physical volume. This is done with the pvresize command. The additional disk space should now be reflected in pvdisplay.

Assign all free space to the lv of your choice, in our case, this is lvroot

Lastly… resize the filesystem to make the additional space available. We are using ext4, so this is done with resize2fs. If you’re using xfs, this will be done with xfs_growfs.

If all goes well, you will now see the additional space in df and are done. It wouldn’t hurt to do a filesystem check with fsck just to make sure everything is in order.

 

Leave a Reply