2

I have an existing Ubuntu VirtualBox VM (Windows 10 host). It's currently using a 50GB fixed size disk. Judging by what I see when I look at its filesystem, its currently using ~29.7GB of that space (so, yes, 20.3GB free).

I would like to make the disk dynamic, and would also like to set the upper bound of this disk to 35GB in order to save space on my laptop's SSD. I'm aware of vboxmanage's clonemedium command, but have a couple of questions:

  1. What will happen to my install on the clone's side? In other words, is it smart enough to see that there's 20.3GB of 'space' in the original disk and shrink the clone down to size (29.7GB)? Do I run the risk of data being lost, despite the 'free space' of the original?

  2. Is there a way to specify the upper bound of the dynamic disk through the command line, or, say, an XML file? The documentation doesn't mention anything about it. I certainly don't want the disk to grow indefinitely.

1 Answers1

1

To convert a virtual disk from fixed to dynamic

VBoxManage.exe clonemedium disk "\path\to\source.vdi" "\path\to\destination.vdi" –variant Standard

Then in VirtualBox for the VM, replace source.vdi by destination.vdi.

Compact the disk

  • Boot the Ubuntu VM and install zerofree:

    sudo apt install zerofree
    
  • Boot into recovery mode by restarting the VM and repeatedly pressing Esc while booting

  • In the Grub menu, select "Advanced options for Ubuntu" and press Enter
  • Select the "(recovery mode)" option for the most recent kernel
  • Select root to boot to a root shell prompt
  • When prompted for "Press Enter for maintenance", press Enter
  • To find which virtual disk to zero, run the command df
  • Zero the free space by the command zerofree -v /dev/sda1 (replace sda1 by disk)
  • Shut down the VM by the command halt
  • Compact the disk using the command

    VBoxManage.exe modifymedium disk "\path\to\disk.vdi" --compact
    

    How much space you save depends on how much empty space there was to recover. This will reduce the physical size of the image without affecting its logical size.

  • To reduce the disk logical size as seen by the guest OS:

    VBoxManage modifymedium disk "\path\to\disk.vdi" --resize <megabytes>
    

Useful references:

harrymc
  • 498,455