Extending an LVM partition on a Ubuntu VM

Logical volumes allow for flexibility of resizing system partitions on the fly without any downtime. This is a pretty nice feature to have in Guest OS since one does not have to worry too much about allocating the right amount of disk upfront. In a previous life, I have spent many a waking hour reinstalling and reconfiguring partitions offline using a gparted live CD. This is not only a time consuming and tedious process but also requires the host to be taken offline. In this post, I am going to do a quick walkthrough of how to extend a LVM partition on a Ubuntu VM by adding a virtual disk to a running VM.

The first step is to add the disk to the VM; I use the C# client to manage my ESX VMs:

VirtualDisk

The disk will not be visible to the Guest OS directly; you may need to trigger a rescan of the devices. This is easy to do on Linux but it should be possible on any other Guest OS as well:


echo "0 0 0" > /sys/class/scsi_host/host0/scan
fdisk -l

Note that when I tried this on Ubuntu Server 16.04 LTS, it required a reboot to detect the disk. Once the disk is visible under /dev, we can open the disk using cfdisk and create a partition on it. Lets also format it using ext4:


mkfs.ext4 /dev/sdb1

where sdb1 is the new partition created on /dev/sdb. The next step is to create the physical volume for the partition and we can do this using the pvcreate command:


root@mediaserver:~# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created

To extend a logical volume, we would first need to find out the name of the volume using the “vgdisplay” command. In the case of my setup, this is the output for “vgdisplay”:


root@mediaserver:~# vgdisplay
--- Volume group ---
VG Name mediaserver-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 5
VG Access read/write
...
...

Lets extend the mediaserver-vg volume using vgextend:


root@mediaserver:~# vgextend mediaserver-vg /dev/sdb1
Volume group "mediaserver-vg" successfully extended

Lets do a pvscan to check if everything looks good:


root@mediaserver:~# pvscan
PV /dev/sda5 VG mediaserver-vg lvm2 [30.05 GiB / 324.00 MiB free]
PV /dev/sdb1 VG mediaserver-vg lvm2 [30.00 GiB / 30.00 GiB free]
Total: 2 [60.05 GiB] / in use: 2 [60.05 GiB] / in no VG: 0 [0 ]

root@mediaserver:~# vgdisplay
--- Volume group ---
VG Name mediaserver-vg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 60.05 GiB
...
...

Now that we have added a physical volume to the VG, we need to extend the logical volume partition which is running out of space. Information about the LVs can be displayed using the lvdisplay command:


root@mediaserver:~# lvdisplay
--- Logical volume ---
LV Path /dev/mediaserver-vg/root
LV Name root
VG Name mediaserver-vg
LV UUID NDkzNB-kyPq-4qZV-bpTr-rY8z-zqXr-jwpZeH
LV Write Access read/write
LV Creation host, time ,
LV Status available
# open 1
LV Size 28.74 GiB

Note that the LV size is only 28.74 GB and has not been extended by the extra 30GB that we added. We would need to use the “lvextend” command to do this:


root@mediaserver:~# lvextend /dev/mediaserver-vg/root /dev/sdb1
Extending logical volume root to 58.73 GiB
Logical volume root successfully resized

root@mediaserver:~# lvdisplay
--- Logical volume ---
LV Path /dev/mediaserver-vg/root
LV Name root
VG Name mediaserver-vg
LV UUID NDkzNB-kyPq-4qZV-bpTr-rY8z-zqXr-jwpZeH
LV Write Access read/write
LV Creation host, time ,
LV Status available
# open 1
LV Size 58.73 GiB

So, we have extended our root partition volume to 58.73GB from 28.74GB (+30GB). The last step is to resize the filesystem to occupy the newly allocated space using resize2fs:


root@mediaserver:~# resize2fs /dev/mediaserver-vg/root
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/mediaserver-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 4
The filesystem on /dev/mediaserver-vg/root is now 15396864 blocks long.

root@mediaserver:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/mediaserver--vg-root 58G 12G 44G 22% /

There, we have extended the root partition of a running VM without any downtime 🙂

Leave a Reply

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