Extend a LVM partition after increasing its virtual disk on Virtualbox

No Linux machine at work? the easy way could be to simply install Virtualbox in one of the PC, create a VDI and install Ubuntu 14.04 in it. But the day will come when you need more space! Here is how to resize it:

Resize Virtualbox VDI

Open Virtualbox, make sure to shutdown your virtual machine. Then open a terminal (here windows):

cd “C:\Program Files\Oracle\VirtualBox”
VBoxManage list vms
VBoxManage clonehd uuid 0000000000000000 backup.vdi
VBoxManage modifyhd uuid 0000000000000000 --resize 204800

This is the Virtualbox official manual of available commands. And this is a website to convert Gb into Mb (–resize takes Mb as input, 200Gb = 204800Mb).

Boot Gparted to resize the partition

After a default install of Ubuntu Server on one physical disk, you will have a SWAP partition, and then an extended partition in which you’ll have your LVM partition (So 3 partitions). Download Gparted .iso, then in your Virtualbox VM settings add a optical drive that point to Gparted.iso, start the VM, press F12 and choose to boot on CD-ROM. Then in Gparted resize first the extended partition to take all the available space, and then same for the LVM partition. Confirm changes, and reboot the VM on its hard drive.

Resize the LVM stack

Let’s resize the PV (Physical Volume) so it takes all the

user@ubuntuServer: sudo su
root@ubuntuServer# pvs
  PV         VG              Fmt  Attr PSize   PFree
  /dev/sda5  ubuntuServer-vg lvm2 a--  199.76g 98.16g
root@ubuntuServer# pvresize /dev/sda5
  Physical volume "/dev/sda5" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

Now let’s extend the LV (Logical Volume) to the full size of the PV. First display its name:

root@ubuntuServer:# lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntuServer-vg/root
  LV Name                root
  VG Name                ubuntuServer-vg
  LV UUID                HLv3Z1-50dU-h5Cv-w3mD-3025-4CS1-0WBxd4
  LV Write Access        read/write
  LV Creation host, time ubuntuServer, 2014-08-13 13:08:17 +0800
  LV Status              available
  # open                 1
  LV Size                100.59 GiB
  Current LE             25752
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

  --- Logical volume ---
  LV Path                /dev/ubuntuServer-vg/swap_1
  LV Name                swap_1
  VG Name                ubuntuServer-vg
  LV UUID                sAT582-dGyq-UeNy-9hgL-p6bY-MkTy-L1EMmN
  LV Write Access        read/write
  LV Creation host, time ubuntuServer, 2014-08-13 13:08:17 +0800
  LV Status              available
  # open                 2
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1

And then extend it to full size available (100%):

root@ubuntuServer:/home/cartolux# lvextend -l+100%FREE /dev/ubuntuServer-vg/root     
  Extending logical volume root to 198.76 GiB
  Logical volume root successfully resized

Now let’s check filesystem of partition:

root@ubuntuServer:# df -Th
Filesystem                        Type      Size  Used Avail Use% Mounted on
/dev/mapper/ubuntuServer--vg-root ext4      99G   12G  83G   12% /
none                              tmpfs     4.0K     0  4.0K   0% /sys/fs/cgroup
udev                              devtmpfs  487M  4.0K  487M   1% /dev
tmpfs                             tmpfs     100M  432K   99M   1% /run
none                              tmpfs     5.0M     0  5.0M   0% /run/lock
none                              tmpfs     497M     0  497M   0% /run/shm
none                              tmpfs     100M     0  100M   0% /run/user
/dev/sda1                         ext2      236M  125M   99M  56% /boot

It’s ext4, and as you can see, the filesystem size is still 99G (and not 200G as wanted). So the last step is to extend the filesystem on the whole LV:

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

That’s it! Now you can run df -Th again and see that the available space has increased. Congrats, job done!