Changing virtual disks

In VirtualBox you can choose either a dynamic or fixed size virtual disk. Dynamic disks are faster to create and more significantly can grow to larger sizes. Fixed size disks are often faster to use, but can’t grow larger once they fill up. I recently had a requirement to change a dynamic virtual disk to a larger, fixed disk to allow me to manage the processing I was undertaking in a more robust manner. This is how I did that.

Note: these instructions relate to VirtualBox running on an Ubuntu 18.04 host

A: Locate the disk to convert

At a Bash prompt, use the VBoxManage list command to find information about the virtual drives on the host.

VBoxManage list hdds 

#creates output to help find Location and UUID for next steps:
#UUID: 64d1442b-0d35-4e61-b3e8-341f32c6119a
#Parent UUID: base
#State: created
#Type: normal (base)
#Location: /home/myuser/VirtualBox VMs/disk.vdi
#Storage format: VDI
#Capacity: 20000 MBytes
#Encryption: disabled

B: Convert the disk

The first step is to clone the drive to a new disk that is fixed in size, using the –variant flag.

VBoxManage clonemedium disk "/path/to/disk.vdi" "/path/to/disk_F.vdi" --variant Fixed

Next we need to resize the new virtual disk to the size we want. Make sure that there is enough physical disk space on the host to do this. The following command changes the size of the virtual hard drive to 30000 MB.

VBoxManage modifyhd "/path/to/disk_F.vdi" −−resize 30000

C: Remove the old disk

To remove the existing virtual disk from VirtualBox you must right-click on the virtual machine that uses the virtual disk. Select Settings | Storage and then right-click on the original VDI (the one that you want to remove). Select Remove Attachment. Now, your VM does not have the virtual disk attached, and if this holds the operating system the machine will not be able to run.

D: Tidy up behind you

Once you are happy that the new disk has been created then run the following command to remove the original disk from VirtualBox’s registry and delete it.

VBoxManage closemedium 5ed5527f-8c63-4750-825d-c2a7b1e8b0ef --delete 

mv "/path/to/disk_F.vdi" "/path/to/disk.vdi"

The mv command in Bash can be used to rename the disk file back to that of the original.

E: Attach the new disk

In VirtualBox, right-click the virtual machine you want to attach the disk to – this should be the same one that you removed the disk from in step C! Select Settings | Storage and right-click the SATA controller, selecting Add Hard Disk | Choose Existing Disk and then find the file you just created and renamed.

If everything went smoothly, the VM should now be bootable, and have viability of your transformed disk.