Disk (extend) Resizing LVM on Linux

Resizing the Hard Drive

Firstly, expand the HDD in the hypervisor.

After resizing the hard drive, you may want to rescan the disk for Linux to recognise the new size. The following command will rescan all the disks.

# Assuming VirtIO SCSI
$ for x in /sys/class/scsi_disk/*; do echo '1' > $x/device/rescan; done

If the disk is still the same size in fdisk, then you’ll need to reboot the server.

Resizing the Partition

Depending on the partition layout, you will need to either resize 1 or 2 partitions, assuming that the partitions are at the end of the disk.

  • In layout 1, you’ll have just a single partition that is the LVM partition.
  • In layout 2, you’ll have an extended partition that contains the LVM partition.

The process to resize partitions are the same, except that in layout 2, you’ll need to resize 2 partitions, with the extended one being resized first so that it’s able to contain the LVM partition.

Use parted to resize the partitions. Start parted on /dev/sda

$ parted /dev/sda

Normally we use Bytes as a unit, and then use the print free command to see where the partition needs to end to fill up the Free Space.

(parted) unit b
(parted) print free
Number Start End Size Type File system Flags
32256B 1048575B 1016320B Free Space
1 1048576B 255852543B 254803968B primary ext2 boot
255852544B 256900095B 1047552B Free Space
2 256900096B 53687091200B 53430191104B primary lvm
53687091201B 75161927679B 5369757696B Free Space

To resize it, a simple resizepart command is used.

(parted) resizepart 2 75161927679B

In the case of an extended partition containing the LVM partition, the command above just needs to be run against the 2 partition numbers, with the extended partition being extended first, and the LVM partition being extended second with the ending byte being one less than the extended partition.

In this example, partition 2 is the extended partition, and partition 5 is the LVM partition.

(parted) resizepart 2 75161927679B
(parted) resizepart 5 75161927678B

Resizing the Physical Volume

After resizing the partition, you’ll need to resize the physical volume. This is as simple as running the following:

$ pvresize /dev/sda2

Replace /dev/sda2 with the partition that has just been extended.

Resizing the Logical Volume

Next, the logical volume will need to be resized with lvresize [lvpath].

You can choose to resize to fill up the empty space, or you can select a specific size. In this example, I’ve resized the logical volume to use all available space.

Replace /dev/VolGroup/lv_root with the path of the logical volume that needs resizing.

$ lvresize -l +100%FREE /dev/VolGroup/lv_root

You should see the following output once the logical volume is resized.

Size of logical volume VolGroup/lv_root changed from 48.54 GiB (12426 extents) to 68.54 GiB (17546 extents).
Logical volume lv_root successfully resized

Resizing the Filesystem

The last thing to do is to resize the filesystem. This can be done live on ext4 with the resize2fs tool.

Replace /dev/mapper/VolGroup-lv_root with the path to the filesystem that needs resizing.

If you’re unsure of the filesystem, lvscan will display the path to the filesystem.

$ resize2fs /dev/mapper/VolGroup-lv_root

Once the resize is complete, you should be able to run a df and see the new filesystem size. In my case, the filesystem was grown from 50G to almost 70G

$ df -h

Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
68G 44G 20G 69% /