Wednesday 18 December 2019

How to resize VirtualBox snapshots hard disk (no cloning required!)

I have a Win 10 VM with couple of snapshots which I used for some software testing. I wanted to reuse them for some development but dev software & frameworks I needed to install are disk space hungry and I had to increase disk size. My initial research was telling me I'd need to discard snapshots as I'd be able only to increase disk size of the original (base/root) image only:

#9103 (modifyvm --resize doesn't work with snapshots) – Oracle VM VirtualBox
#13046 (modifyhd --resize should not be permitted when snapshots exist) – Oracle VM VirtualBox
#10818 (modifyhd --resize should warn users about existing snapshots) – Oracle VM VirtualBox
virtualbox - How do I increase the hard disk size of the virtual machine? - Ask Ubuntu
How To Enlarge a Virtual Machine’s Disk in VirtualBox or VMware


Nevertheless, I wanted to give it a try. My idea was to resize the base and all snapshot images and then fiddle with either Windows Disk Management tool from within guest OS or with GParted tool from a live CD. Before such experiments it is always good to keep original copies of all VM files.

It is worth noting that my host OS is Windows 10 Pro (with installed Feature update to Windows 10, version 1909) and Virtual Box is 6.0:



Let me walk you now through the steps I took.

I first resized the base image to desired size (80GB):


C:\Program Files\Oracle\VirtualBox> VBoxManage modifymedium disk "C:\VirtualBox VMs\Win10 pro\Win10 Pro.vdi" --resize 81920
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%


Apart from base vdi I did this for all snapshots. Here is the example command for one of them:

C:\Program Files\Oracle\VirtualBox>VBoxManage modifymedium disk "C:\VirtualBox VMs\Win10 pro\Snapshots\{f51b69a4-8da7-42fb-b0d8-a6f9217bf726}.vdi" --resize 81920
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%


At this point when I start the last snapshot Win OS would see that there is extra unallocated space but Disk Management tool won't allow me to expand the existing ntfs partition.





I recalled using once GParted to resize base image of Ubuntu (I think I followed these instructions: How to resize a virtualbox VDI disk under windows | derekmolloy.ie14.04 - Guest system does not see the updated disk size in VirtualBox - Ask Ubuntuvirtualbox.org • View topic - Increasing the size of a Windows Guest drive (VBoxManage)) so I downloaded GParted ISO (gparted-live-1.0.0-5-amd64.iso) to my host OS.

I booted the last snapshot from GParted live ISO. Steps to do this are:


  • Select the snapshot >> Settings >> Storage >> Storage Devices, select CD (Optical Drive) >> click on the CD icon and select Choose Virtual Optical Disk file and select GParted iso file.
  • Select the snapshot >> Settings >> System >> Boot order. Here make sure that Optical goes before Hard Disk.


This is what appears upon starting the snapshot now:


I sticked with default settings and then chose UK English:




In gparted - Use the unallocated space to expand Windows (Ntfs Partition) - Ask Ubuntu I found the solution I could apply to my problem:

To expand the NTFS partition, the free space must follow it. Right now it is in the middle of your extended partition. To fix this, you will need to move the ext4 partition to the right, then resize the start of the extended partition to the right, then you can expand the NTFS partition.

So I had to swap places of small-ish recovery partition and unallocated space in order to make unallocated portion to be located exactly after the partition I wanted to expand:


Moving small partition (sda3) to the end:





We can see that small ntfs partition is now located after the unallocated segment:


I then went on expanding my target partition (sda2) onto entire unallocated segment:


I could not figure out quickly how to get rid of 1MB of unallocated space gap so I just left it there:




Moving sda3 to the end went well:


...but expanding sda2 didn't:



I checked the log to see what caused this failure:



This error didn't discourage me as log message was providing the idea what to do next: to run chkdsk /f on Windows. I then quit GParted and shut down snapshot.

I set snapshot to boot from Hard Disk again (into Win guest), set Windows to perform chkdsk on reboot, rebooted it and could see this:



I then repeated booting snapshot from GParted live disk and all steps were now successful :)





I then set up booting snapshot back from Hard Disk and in Win host OS I could see that Disk Management tool is reporting a new size of the C: partition. Yay!

No comments: