How to extend an Ubuntu Linux Logical Volume Manager disk

So, every now and then we can ran into issues where the virtual harddisk of a VM is becoming full.

Obviously these days every disk should ideally be used in a 1:1 mapping between disk and mountpoint, except for the boot image which will have the boot partition and possible a swap partition.

Having a single mounting point per full disk, will ease system administrations.

When one has expanded the virtual disk in their virtualization platform, one needs to expand the disk inside the virtual machine. This consists basically out of 4 steps.

  1. Scan for updated virtual harddisk
  2. Grow partition on virtual harddisk
  3. Resize Physical Volume in LVM
  4. Resize/extend Logical Volume in LVM

These steps are not without risks! Each of those steps, if performed incorrectly, one risks data loss. Making a backup or a snapshot of the virtual machine is highly recommended and should never be ignored before making changes with risk of data loss.

Scanning for updated virtual harddisks

echo 1 | sudo tee /sys/class/block/sda/device/rescan

Please take care of updating the bold printed part sda in the command above to reflect the actual harddisk that has been resized in the virtualization platorm. One can use the lsblk command to check for the drive indicator

for disk in {a..z}; do 
	filetoscan="/sys/class/block/sd"$disk"/device/rescan"
	if [ -f $filetoscan ]; then
		echo 1 | sudo tee $filetoscan 
	fi
done

When not sure which disks are expanded, or expanding multiple disks, the above small script would loop through all disks in the sd* chain simulating any SATA disks.

Grow partition on virtual disk

After scanning one really will need to have the drive indicator to grow the partition; Use the command below to grow the specific partition

sudo growpart /dev/sda 3

Below one can see a simulated output of this process

localuser@server:~$ lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0   60G  0 disk
├─sda1                      8:1    0    1M  0 part
├─sda2                      8:2    0    1G  0 part /boot
└─sda3                      8:3    0   29G  0 part
  └─ubuntu--vg-ubuntu--lv 252:0    0   29G  0 lvm  /
sdb                         8:16   0   30G  0 disk
└─sdb1                      8:17   0   30G  0 part /home
sdc                         8:32   0  450G  0 disk
└─sdc1                      8:33   0  450G  0 part /home/supersizeduser
sdd                         8:48   0  100G  0 disk
sr0                        11:0    1 1024M  0 rom
localuser@server:~$ 
localuser@server:~$ 
localuser@server:~$ sudo growpart /dev/sda 3
localuser@server:~$ 
localuser@server:~$ lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0   60G  0 disk
├─sda1                      8:1    0    1M  0 part
├─sda2                      8:2    0    1G  0 part /boot
└─sda3                      8:3    0   59G  0 part
  └─ubuntu--vg-ubuntu--lv 252:0    0   29G  0 lvm  /
sdb                         8:16   0   30G  0 disk
└─sdb1                      8:17   0   30G  0 part /home
sdc                         8:32   0  450G  0 disk
└─sdc1                      8:33   0  450G  0 part /home/supersizeduser
sdd                         8:48   0  100G  0 disk
sr0                        11:0    1 1024M  0 rom
localuser@server:~$ 

Resize Physical Volume in LVM

After having resized the virtual disk it is important to resize the Physical Volume group in LVM. One can use the command pvresize and refer to the same disk and partition as has been done before.

pvresize /dev/sda3

Resize/extend Logical Volume in LVM

Last step is to resize the actual logical volume that is created in/with the phyisical volume.

For that we can use the following command to resize the standard first logical volume in an Ubuntu installation

lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv

Summary of commands

So summarized the commands would become (assuming you are acting as root and we are resizing the basic first partition and root mounting point):

lsblk
echo 1 | sudo tee /sys/class/block/sda/device/rescan
growpart /dev/sda 3
pvresize /dev/sda3
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.