Expand LVM Logical Volume on Virtual Machine

I had a very specific problem. I have a LVM running on a single physical volume on a Virtual Machine. The size of the logical volume is ~150GB and I wish to expand it to ~250GB.

I was able to expand the physical volume from my Virtual Machine manager, but the change in physical size is not reflected on my LVM logical volume.

I didn't find any answers directly, and through some experimentation found a solution. I thought I'll share it in my answer below.


An example LVM diagram:

enter image description here

The idea of LVM is it abstracts the logical volume away from the physical volume, such that multiple physical volumes will appear to the user a single continuous logical volume.

The root file system is in /dev/file-server-vg/root and the swap is in /dev/file-server-vg/swap. The idea is we can actually have multiple real hard disks in the PV layer, and they can be all added into a Volume Group, which will allow them to all appear as a single continuous file system to the system at /dev/file-server-vg/root.

In this problem I have been using /dev/file-server-vg/root as a file server and it is running out of space. Luckily this system is on a virtual machine, and I was able to expand the physical volume /dev/sda using the virtual machine manager. However, the increase in size doesn’t get automatically passed to the /dev/file-server-vg/root file system. We need to do a few simple command to get this to work.

Before we start, use pvdisplay to see which physical volumes are present. This will list all the physical volumes and the volume group that it belongs to. In this case we only have one physical volume that is /dev/sda3.

  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               file-server-vg
  PV Size               155.26 GiB / not usable 0   
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              65346
  Free PE               0
  Allocated PE          65346
  PV UUID               0EXhc4-EoHZ-b79R-ncSe-SWXI-ztPe-Fsc7zk

The current partition table can be listed by using sudo parted and then print.

Number  Start   End    Size   File system  Name  Flags
 1      1049kB  538MB  537MB  fat32              boot
 2      538MB   794MB  256MB  ext2
 3      794MB   167GB  166GB                     lvm

In this example we want to expand the capacity from 155.56GiB to 255.56GiB. Using the virtual machine manager we can usually extend the disk space, and in this case we will expand it by 100GiB.

After expansion is successful, the resulting partition table (use print free) will look like the following. Please note the “Free space” shows 108GB (~100GiB).

Number  Start   End    Size   File system  Name  Flags
 1      1049kB  538MB  537MB  fat32              boot
 2      538MB   794MB  256MB  ext2
 3      794MB   275GB  166GB                     lvm
      167GB   275GB  108GB  Free Space

There are two way to expand the logical volume: Create a new partition and add this to the Volume Group, or Increase the same partition.

In my opinion, where possible, the second method is neater. Of course if /dev/sda3 is a physical hard disk, and you bought another hard disk to expand the space, then it will be logical to use the first method.

In parted type resize 3 794MB 275GB will resize partition 3 to take up all the free space, well almost:

Number  Start   End     Size    File system  Name  Flags
        17.4kB  1049kB  1031kB  Free Space
 1      1049kB  538MB   537MB   fat32              boot
 2      538MB   794MB   256MB   ext2
 3      794MB   275GB   274GB                      lvm
        275GB   275GB   1032kB  Free Space

This is good enough, just 1MB not being used.

Next we will reboot the machine. This is needed to refresh the partition table.

What you will see now is that the physical volume has increased in size with pvdisplay

--- Physical volume ---
  PV Name               /dev/sda3
  VG Name               file-server-vg
  PV Size               255.26 GiB / not usable 0   
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              65346
  Free PE               0
  Allocated PE          65346
  PV UUID               0EXhc4-EoHZ-b79R-ncSe-SWXI-ztPe-Fsc7zk

This increase in size will be automatically passed to the volume group, using vgdisplay you get the following. Please note vgextend doesn’t actually do what you might think it does. It doesn’t take up the free space, instead it is used for adding new physical volumes to a particular volume group.

--- Volume group ---
  VG Name               file-server-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               255.26 GiB
  PE Size               4.00 MiB
  Total PE              65346
  Alloc PE / Size       65346 / 255.26 GiB
  Free  PE / Size       0 / 0   
  VG UUID               NwwIgT-W0SH-aeTe-d078-hcxx-XZ6W-QfnSA0

The logical volume doesn’t increase in size automatically. This is actually makes sense because the computer has no idea which logical volume you want to increase in size, or perhaps you want to create a new logical volume with the extra space in the volume group. So with lvdisplay you will see the following table, showing the /dev/file-server-vg/root only has 153.26GiB, but don’t worry we are very close.

--- Logical volume ---
  LV Path                /dev/file-server-vg/root
  LV Name                root
  VG Name                file-server-vg
  LV UUID                l6qvYZ-AmYw-tdgi-2jUu-XNB2-gecQ-6fDPt8
  LV Write Access        read/write
  LV Creation host, time file-server, 2014-07-10 14:54:58 +1000
  LV Status              available
  # open                 1
  LV Size                153.26 GiB
  Current LE             64834
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

  --- Logical volume ---
  LV Path                /dev/file-server-vg/swap_1
  LV Name                swap_1
  VG Name                file-server-vg
  LV UUID                OohMkN-rGLa-fM4M-6he2-MbM9-BBc3-Ck2Hs5
  LV Write Access        read/write
  LV Creation host, time file-server, 2014-07-10 14:54:58 +1000
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1

The way to increase the logical volume is to use lvextend -L +100GB /dev/file-server-vg/root

After that expand the file system with resize2fs /dev/file-server-vg/root

And you are done. You can do lvdisplay to check that the /dev/file-server-vg/root volume has extended to 253.26GiB

  --- Logical volume ---
  LV Path                /dev/file-server-vg/root
  LV Name                root
  VG Name                file-server-vg
  LV UUID                l6qvYZ-AmYw-tdgi-2jUu-XNB2-gecQ-6fDPt8
  LV Write Access        read/write
  LV Creation host, time file-server, 2014-07-10 14:54:58 +1000
  LV Status              available
  # open                 1
  LV Size                253.26 GiB
  Current LE             64834
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

  --- Logical volume ---
  LV Path                /dev/file-server-vg/swap_1
  LV Name                swap_1
  VG Name                file-server-vg
  LV UUID                OohMkN-rGLa-fM4M-6he2-MbM9-BBc3-Ck2Hs5
  LV Write Access        read/write
  LV Creation host, time file-server, 2014-07-10 14:54:58 +1000
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1