Extend VM's LVM with a new disk

>>Back to main page

Extend VM's LVM with a new disk

Virtual machines created from templates usually have their file systems mounted over LVM. This allows to add more disks and to extend directly the / filesystem. You can check it with:

# df -h /
S.ficheros                    Size Used  Avail Use% Mounted in
/dev/mapper/Ubuntu14--vg-root   8,5G   6,3G  1,8G  79% /

You'll see / in /dev/mapper. In this case /dev/mapper/Ubuntu14--vg-root is a LVM logical volume named root in the volume group Ubuntu14-vg.

To see which physical devices belong to the volume group use:

# pvs
  PV         VG          Fmt  Attr PSize PFree
  /dev/sda5  Ubuntu14-vg lvm2 a--  9,76g    0 

In this case the volume group is formed by just one partition of disk /dev/sda. A volume group can be composed of partitions and/or full disks.

We will add a new disk and use it wholly without partitioning to extend the logical volume /dev/mapper/Ubuntu14–vg-root.

After adding a data disk in the Cloudstack interface, system should have a new unpartitioned disk available:

# cat /proc/partitions 
major minor  #blocks  name

 253        0    5242880 vda
  11        0    1048575 sr0
   8        0   10485760 sda
   8        1     248832 sda1
   8        2          1 sda2
   8        5   10233856 sda5
 252        0    9183232 dm-0
 252        1    1048576 dm-1

Depending on the system, it may be necessary to reboot the VM for it to discover the new disk.

Discard dm-0 and dm-1.sda is the system disk and is already partitioned. sr0is the virtual CD-ROM. So the just added disk is vda (note that it also matches in size).

Once identified the new disk, the first step is to create a physical lvm volume:

Is very important to identify correctly each disk because the name of the added disk can vary depending on the operating system installed. If you follow this instructions blindly it is possible to make irreversible damage to the VM. The following command is potentially damaging, so take care..
# pvcreate /dev/vda
  Physical volume "/dev/vda" successfully created

Then the just created physical volume is added to the volume group Ubuntu14-vg:

# vgextend Ubuntu14-vg /dev/vda
  Volume group "Ubuntu14-vg" successfully extended

All the free space in the volume group is added to the logical volume:

# lvextend -l +100%FREE /dev/mapper/Ubuntu14--vg-root 
  Extending logical volume root to 13,75 GiB
  Logical volume root successfully resized

Lastly, the filesystem in the logical volume must be resized. In our case, it can be done online because ext4 is used:

# resize2fs /dev/mapper/Ubuntu14--vg-root
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/mapper/Ubuntu14--vg-root is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/Ubuntu14--vg-root is now 3605504 blocks long.

Now, the system will have the added space in /:

# df -h /
S.ficheros                    Tamaño Usados  Disp Uso% Montado en
/dev/mapper/Ubuntu14--vg-root    14G   6,3G  6,5G  50% /

This operation can be repeated with different volumes to enlarge even more the partition space.