Extending a linux LVM partition after expanding its virtual hard drive using parted

This assumes you already have expanded the hard drive within VMware or VirtualBox, or whatever your virtualization solution you use. You will also need to make sure parted is installed on the system you are working on. This guide is using GNU parted 3.1

Disclaimer: Parted is a partition modification tool make sure you have a good backup/snapshot of your machine before proceeding as you may lose data when using the parted tool. I am not responsible for your lost data. Again, make sure you have a VALIDATED backup or snapshot of the machine you are working on.

Steps on Linux

  1. Get linux to recognize the new hard drive size
    To check what size hard drive linux sees run: fdisk-l
    You should see something like the following:

  2. Extend the partition table.
    Run: parted
    Depending on the number of physical drive you have you may have to select the disk you want to modify. I only have one disk and did not have to do this. but you may need to run the command: select And enter your drive device that you got from the information in step 1.

    I always run print as a sanity check and to make sure everything is showing up as expected.

    You will then run the command: resizepart X Where X is your partition number that you are expanding. You will then be asked how big you want the partition to be. I set this to the size of the hard drive as it appears in linux. In my case it reads 322GB even though it is 300GiB. I set slightly over to make sure I captured all of the disk and it seemed to shrink it down the appropriate amount. So I would recommend this to other users, but I have not tested it myself.

    I reran the print command to make sure it took my settings and then quit. Ignoring the message about needing to update FSTAB as I didn’t create any new partitions.

  3. Extend the Physical Volume
    This is easy enough just run pvresize /dev/X
    Where “/dev/X” is your device. No other parameters are needed it will extend the physical volume to the maximum usable size. I verified this by running pvs before and after the pvresize command.

  4. Extend the Logical Volume
    Now there are a few ways you can go at this point if you want to extend multiple Logical Volumes you would use a command like this: lvresize -L +500M /dev/VG/LV
    Where “+500M” is the size you want to increase the logical volume by and “/dev/VG/LV” is the path to the logical volume you want to extend.

    In my case however, I only wanted to extend one logical volume and I wanted it to use the rest of the physical volume possible, so I used the command: lvresize /dev/centos/root /dev/sda2
    This tells lvresize to use all available space on the physical volume for my logical volume /dev/centos/root.

  5. Extend the Partition

    This part depends on your filesystem type, you can figure this out with the command df -Th If you are using ext2/ext3/ext4 you want to to use the resize2fs tool. See the man page for more details on how to use this tool. But the jest of it is: resize2fs /dev/centos/root
    I however am using xfs as it is the default for CentOS 7. I ran the command:
    xfs_growfs /dev/mapper/centos-root
    This finishes nearly instantly and rerunning: df -Th will show you if the new size has taken or not.