Extend a partition of a VM

Extend a partition of a VM

Since the update of august 2017 it is possible to choose a custom size for the root disk or resize an existing disk, but before that extra space can be used it has to be manually assigned in the operating system. The Ubuntu 14.04 and 16.04 templates have the root partition mounted over LVM, using a logical partition of DOS type. Even though this guide is written for those templates it is advisable no to skip the Check the configuration chapter.

Check the configuration

Before continuing you should confirm that the system you are working on has this configuration, otherwise you risk destroying the system.

First check if you're using LVM:

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

df -h shows that / is mounted in /dev/mapper/Ubuntu–vg-root and that means that LVM is in use.

Now check the root's logical volume name.

# lvs
  LV    VG        Attr      LSize   Pool Origin Data%  Move Log Copy%  Convert
  root  Ubuntu-vg -wi-ao---   6.03g                                           
  swap1 Ubuntu-vg -wi-ao--- 748.00m

Also check the associate volume group name and its physical volume.

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

We can see that root is in a logical volume called root inside a volume group called Ubuntu-vg that uses /dev/sda5 as a physical volume.

Take into account that /dev/sda5 is a logical partition that Ten en cuenta además que en el sistema de particionado utilizado, /dev/sda5 es una partición lógica que forma parte de la partición extendida /dev/sda2.


There are 5 steps necessary:

  1. Extend the extended partition /dev/sda2 to fill all the free space of the disk.
  2. Extend the logical partition /dev/sda5 to fill the free space in the extended partition.
  3. Extend the physical volume of volume group Ubuntu-vg.
  4. Extend the logical volume root to fill the free space of the volume group.
  5. Extend the filesystem to fill the free space of the logical volume.

Run parted. With the command print free we see that the disk has a total of 21.5GB and free space of 14GB at the end of the disk.

# parted /dev/vda
(parted) print free
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
        32.3kB  1049kB  1016kB            Free Space
 1      1049kB  256MB   255MB   primary   ext2         boot
        256MB   257MB   1048kB            Free Space
 2      257MB   7515MB  7258MB  extended
 5      257MB   7515MB  7258MB  logical                lvm
        7515MB  21.5GB  14.0GB            Free Space

First the extended and logical partitions have to be extended (in that order) so they fill the disk and reach the end at 21.5GB (steps 1 and 2).

(parted) resizepart 2 21.5GB                                              
(parted) resizepart 5 21.5GB
(parted) quit

Once done and having exited parted, we extend the physical volume in the volume group ( step 3).

# pvresize /dev/vda5
  Physical volume "/dev/vda5" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

Now we extend the logical volume (step 4).

# lvextend -l+100%FREE /dev/mapper/Ubuntu--vg-root 
  Extending logical volume root to 19.03 GiB
  Logical volume root successfully resized

Finally, the filesystem is extended (step 5). Por último, extendemos el sistema de archivos (paso 5).

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

Once this is done all the free space should be assigned to the system's root.