Resize LVM virtio disk

VM's on my Proxmox machine make use of LVM for easy partitioning in case something goes wrong. The 1TB partition was almost full so I had to resize the disk. This can be done live on a linux machine with Linux kernel > 3.6.

Using the qm command we can resize the virtio disk on proxmox:

qm resize 100 virtio0 +1024G

This expanded the virtual disk with 1TB of diskspace. I am using thin provisioning so this does not instantly take away the 1TB of diskspace.

Then on the proxmox VM I checked the storage LVM partition:

df -h
Filesystem                     Size  Used Avail Use% Mounted on
udev                            16G     0   16G   0% /dev
tmpfs                          3.2G  9.1M  3.2G   1% /run
/dev/mapper/plex--vg-root      976G  858G   69G  93% /

As seen above there was only 69G of space left.

Using fdisk we can see the disk size itself has increased:

fdisk -l
Disk /dev/vda: 2 TiB, 2147483648000 bytes, 4194304000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xcdd89a40

Device     Boot   Start        End    Sectors   Size Id Type
/dev/vda1  *       2048     999423     997376   487M 83 Linux
/dev/vda2       1001470 2097149951 2096148482     1T  5 Extended
/dev/vda5       1001472 2097149951 2096148480     1T 8e Linux LVM

/dev/vda2 is the complete LVM storage partition, with /dev/vda5 being the single partition. I then expanded the /dev/vda2 disk to the complete size with:

parted /dev/vda
resizepart 2 100%
/dev/vda1  *       2048     999423     997376   487M 83 Linux
/dev/vda2       1001470 4194303999 4193302530     2T  5 Extended
/dev/vda5       1001472 2097149951 2096148480     1T 8e Linux LVM

The /dev/vda2 is now 2T. Using fdisk again we can now create a LVM partition on the remaining 1T of storage.
Select the correct disk (/dev/vda), and then using n for a new partition. Naming it /dev/vda6 and then just enter on the first and last sector. Since we want the complete disk.

Then we edit the partition using "t". And flagging it 8e for Linux LVM. And finally writing changes using "w".

fdisk /dev/vda

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (2097152000-4194303999, default 2097152000):
Last sector, +sectors or +size{K,M,G,T,P} (2097152000-4194303999, default 4194303999):

Created a new partition 6 of type 'Linux' and of size 1000 GiB.

Command (m for help): t
Partition number (1,2,5,6, default 6): 6
Partition type (type L to list all types): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

We can then see the new partition using fdisk:

fdisk -l /dev/vda
Disk /dev/vda: 2 TiB, 2147483648000 bytes, 4194304000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xcdd89a40

Device     Boot      Start        End    Sectors   Size Id Type
/dev/vda1  *          2048     999423     997376   487M 83 Linux
/dev/vda2          1001470 4194303999 4193302530     2T  5 Extended
/dev/vda5          1001472 2097149951 2096148480     1T 8e Linux LVM
/dev/vda6       2097152000 4194303999 2097152000     1T 8e Linux LVM

The kernel now does not yet know the changes that have been made on the LVM volume. To update this we use:

partprobe -s

Then we create the physical volume on the vda6:

pvcreate /dev/vda6
  Physical volume "/dev/vda6" successfully created

And we add the volume to the volume group:

vgextend plex-vg /dev/vda6
  Volume group "plex-vg" successfully extended

If we scan the physical volumes on the machine we see the new PV.

pvscan
  PV /dev/vda5   VG plex-vg         lvm2 [1000.00 GiB / 0    free]
  PV /dev/vda6   VG plex-vg         lvm2 [1000.00 GiB / 1000.00 GiB free]
  Total: 2 [2 TiB] / in use: 2 [2 TiB] / in no VG: 0 [0   ]

Now we can extend the LV aswell:

 lvextend /dev/plex-vg/root /dev/vda6
  Size of logical volume plex-vg/root changed from 1000 GiB (253829 extents) to 2 TiB (509828 extents).
  Logical volume root successfully resized.

And one final last command to resize the fs completely:

resize2fs /dev/plex-vg/root
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/plex-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 62, new_desc_blocks = 125
The filesystem on /dev/plex-vg/root is now 522063872 (4k) blocks long.

And we are done. The LVM volume has been increased by 1TB as seen by df:

df -h
Filesystem                     Size  Used Avail Use% Mounted on
udev                            16G     0   16G   0% /dev
tmpfs                          3.2G  9.2M  3.2G   1% /run
/dev/mapper/plex--vg-root      2.0T  870G 1002G  47% /