Sometimes I create a VirtualBox disk for usage in a VM with a certain size. I think it will be enough for what I want to use the VM for. After using the VM for a while, it happens (often) that I need more space than I initially assigned. For example if want to install additional software.
In this blog I describe the steps I’ve done to increase the size a VirtualBox Ubuntu guest disk without loosing data. I’ve done a pretty default Ubuntu 16.04 installation as guest in VirtualBox. If your partition layout differs, the steps to take are similar but might differ slightly. Do think before you execute these (or similar) steps since if you do not do them correctly, you might screw up your partitions and potentially lose data.
If you have created a nice VM and want to distribute it, it helps if the VM export is not too large. This makes uploading and copying the VM much easier. At the end of this blog post I give some tips on how to make an export of a VM small.
The below described method has caused problems for some people (see comments) so make sure you have a backup of your VM before starting.
Step by step
Step 1: Make sure you have a VDI disk image
If you imported an OVA file, you will end up with a VMDK disk image. First detach the image from the VM;
Clone the VMDK to a VDI file. This operation also has the benefit of creating a backup; the original VMDK file is still there. If you have already done this step before, you might have a registered VDI file with the same name inside your VirtualBox Media Manager. If that is the case, the clone operation will fail. First remove the VDI file from the VirtualBox Media Manager before proceeding. Below is the clone command for a VMDK file to a VDI file.
“c:\Program Files\Oracle\VirtualBox\VBoxManage.exe” clonemedium –format VDI kafkaworkshoposajava-disk1.vmdk kafkaworkshoposajava-disk1.vdi
Clone medium created in format ‘VDI’. UUID: cabc9b44-386d-41af-af70-6b8e73df5596
Step 2: Resize the VDI disk image
The below command resizes the VDI disk image to 20000Mb.
D:\VM\KafkaWorkshop>”c:\Program Files\Oracle\VirtualBox\VBoxManage.exe” modifymedium kafkaworkshoposajava-disk1.vdi –resize 20000
Step 3: Attach the new VDI disk and the Ubuntu boot ISO image
Step 4: Boot the VM
Boot the VM and press F12 in the boot screen. Boot from CDROM
Click Try Ubuntu
Step 5: Configure the disks with GParted
Deactivate the disk
Resize the extended partition to fill all available space.
Resize the partition itself to fill all available space.
Finally check the disk.
Click the green V icon: Apply All Operations
Step 6: Make the assigned space available
sudo lvresize -l +100%FREE /dev/mapper/ubuntu–vg-root
Start the machine and check the result
If you want to distribute an export of your VM and want to have it as small as possible, the following might help:
sudo telinit 1
mount -o remount,ro /dev/sda1
zerofree -v /dev/sda1
Update /etc/fstab and set the options of /dev/mapper/ubuntu–vg-root to ro but remember the previous options! In my case before the change it was errors=remount-ro.
As root: telinit 1
mount -o remount,rw /dev/mapper/ubuntu-vg-root
Update fstab with vi and restore the previously set options. Restart. Now the free space has been zeroed out. On your host OS (Windows in my case) do the following:
“c:\Program Files\Oracle\VirtualBox\VBoxManage.exe” modifymedium –compact kafkaworkshoposajava-disk1.vdi
This makes sure if you export the VDI, the exported disks will be as small as they can get with their contents. You can make the export even smaller by using 7-zip to compress the result. If you want good compression, set the dictionary size and block size in 7-zip to large values.