How to resize partition of a virtualbox guest ubuntu server
Like a lot of people, I use to click “next” without thinking… I installed an ubuntu server on a VirtualBox VM, and the assistant proposes 8 Gb for the size of the drive, which is a bit short. Let’s see how to resize it.
1st - Resize the drive
On the host, you can do it with VBoxManage : VBoxManage modifyhd /path/to/vdi --resize 81920
81920 is the size of the new disk in Mb. Beware of the snapshots! If you have snapshots, then the actual drive of the VM is in the snapshot folder of the VM folder, otherwise it is inside the VM folder directly.
2nd (optional) - You miss the snapshot point
In my case, i forgot that I did snapshot, so I was resizing the original vdi and not the last “snapshotted” version. Then I did a mess with my VDIs : I tried to delete the snapshot, It didn’t work, and I had a VM in an original state not corresponding to the snapshot version of the VDI.
At the end I had the original vdi and the last snapshotted version. Thank to this post, I manage to create a new VDI with all the snapshots included:
VBoxManage clonehd original.vdi Machine-full.vdi VBoxManage clonehd original-snapshooted.vdi Machine-full.vdi --existing
If you have more than one snapshot, you can add as many snapshots as you want, but you have to do it sequentially and in the same order as the snapshots has been taken.
3rd - Increase the size of the physical volume, the volume group, the logical volume and the root volume
Following this post, I increased the size of the server partition. The idea is to create a new physical volume, then you extend your volume group to this new volume, then the logical volume and at the end the root volume. In the ubuntu server VM terminal (or a remote SSH session) : sudo parted /dev/sda print
(to know the size of the resized disk and the position of the last partition) sudo parted /dev/sda "mkpart primary 8589MB 85.9G"
(8589MB being the end of the last partition and 85.9G being the size of the disk)
sudo pvcreate /dev/sda3
with pvdisplay, you check if the physical volume has been created correctly.
Then with pvs
, you get the name of the volume group (in my case “ubuntu-dev-vg”)
You add the new volume to the group : sudo vgextend ubuntu-dev-vg /dev/sda3
(You can check if it’s correct with pvs and/or pvdisplay)
Now you have to extent your logical volume sudo lvextend -l +100%FREE /dev/ubuntu-dev-vg/root
And resize your root volume sudo resize2fs /dev/ubuntu-dev-vg/root
That’s it ! You just have to reboot the server, or check the file system with e2fsck and done !