How to change fixed size VDI with modifyhd command in Windows?

Summary:

  • VBoxManage.exe modifyhd --resize can increase (but not decrease) the logical size of disk image (the size seen by the guest OS).
  • VBoxManage.exe modifyhd inputfile.vdi --compact can decrease the physical disk image size (the size of the image file on disk as seen by the host OS). This however does not change the logical disk size. Note that this only works if free space has been zero'ed in the guest OS first.
  • modifyhd only works only with the dynamic format variant ("dynamically allocated image").
  • modifyhd only works with VDI and VHD image formats.
  • If you are using snapshots you will have to clone the latest snapshot VDI from "Snapshots" subfolder.

For details see section 8.23. VBoxManage modifyhd in the manual.


The reason why decreasing the logical disk size of a VDI has not been implemented yet is to prevent data corruption which could occur and you would have unbootable VMs as a result of it. Therefore, when the --resize option is used, the new disk size must be greater than the current size. It is not allowed to be less than or equal to the current size. In other words a 40 GB disk can only be resized to 41 GB or greater than that, it cannot be resized to 40 GB (that's just dumb) or 39 GB or less than that.

Old versions of VirtualBox created VDI files as "fixed" format varient. The new default setting is now to create "dynamic" format variants when new VDI (virtual disk image) files are created. (I'm guessing this is the case since the introduction of dynamic format variant, whichever version that was introduced in.)

So what the error message "operation for this format is not implemented yet" means is that your VDI file uses the "fixed" format variant. To get around this problem you will have to clone your VDI file using the clonehd command. To view what format variant your VDI file is in at the moment, you can use the showhdinfo command.

Working Example

Show VDI information about the current disk.

Input command: vboxmanage.exe showhdinfo path\inputfile.vdi

C:\Program Files\Oracle\VirtualBox>VBoxManage.exe showhdinfo "%userprofile%\virt
ualbox vms\sg2_win7_x64_lab\sg2_win7_x64_lab.vdi"
UUID:           132e9af1-0428-49f4-bc45-4d84680e17f5
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       C:\Users\Name\VirtualBox VMs\sg2_win7_x64_lab\sg2_win7_x64_lab.
vdi
Storage format: VDI
Format variant: fixed default
Capacity:       40960 MBytes
Size on disk:   40962 MBytes
In use by VMs:  sg2_win7_x64_lab (UUID: dcd106b3-7ed6-4f19-ad94-820ab4dc10d3)

C:\Program Files\Oracle\VirtualBox>

See where it says "fixed default"? That's why it spits out the error above when you try to resize it.

Clone the old VDI file to a new VDI file.

Input command: vboxmanage.exe clonehd path\inputfile.vdi path\outputfile.vdi

C:\Program Files\Oracle\VirtualBox>VBoxManage.exe clonehd "%userprofile%\virtual
box vms\sg2_win7_x64_lab\sg2_win7_x64_lab.vdi" "%userprofile%\virtualbox vms\sg2
_win7_x64_lab\sg2_win7_x64_lab_clone.vdi"
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: 34dafa68-3093-4946-926a-8237ea263
e5c

C:\Program Files\Oracle\VirtualBox>

Voilà! The old file has now been cloned to a new file. Since the default setting in new versions of VirtualBox is to use "dynamic" format variant, you don't have to define that explicitly. Although, if you are working with an older version of VirtualBox, then you might want to set the option to "dynamic" (if at all available) to ensure better compatibility and/or flexibility with future versions of VirtualBox.

Show VDI information about the cloned disk.

Input command: vboxmanage.exe showhdinfo path\inputfile_clone.vdi

C:\Program Files\Oracle\VirtualBox>VBoxManage.exe showhdinfo "%userprofile%\virt
ualbox vms\sg2_win7_x64_lab\sg2_win7_x64_lab_clone.vdi"
UUID:           34dafa68-3093-4946-926a-8237ea263e5c
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       C:\Users\Name\virtualbox vms\sg2_win7_x64_lab\sg2_win7_x64_lab_
clone.vdi
Storage format: VDI
Format variant: dynamic default
Capacity:       40960 MBytes
Size on disk:   7806 MBytes

C:\Program Files\Oracle\VirtualBox>

See how it now says "dynamic default"? How nice!

(On the side note! The reason it says 7806 MB as physical size here is because I had used the --compact option on the original VDI file in an attempt to enforce conversion to dynamic format variant. Just something I read on the VirtualBox forum, it supposedly would have worked with older versions, so I thought I might as well give that a try. It didn't work.)

Resizing the cloned disk.

Input command: vboxmanage.exe modifyhd path\inputfile.vdi --resize 51200

C:\Program Files\Oracle\VirtualBox>VBoxManage.exe modifyhd "%userprofile%\virtua
lbox vms\sg2_win7_x64_lab\sg2_win7_x64_lab_clone.vdi" --resize 51200
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

C:\Program Files\Oracle\VirtualBox>

Success! No format bullshitting no more. Just remember to resize the new, cloned disk, and not the original one. Keep in mind that the size is expressed in MB. I believe you can use e.g. 51200 as well as 50G (or possibly "GB"). There is also the option --resizebyte which can be used to express the size in bytes.

Show VDI information about the resized clone disk.

Input command: vboxmanage.exe showhdinfo path\inputfile_clone.vdi

C:\Program Files\Oracle\VirtualBox>VBoxManage.exe showhdinfo "%userprofile%\virt
ualbox vms\sg2_win7_x64_lab\sg2_win7_x64_lab_clone.vdi"
UUID:           34dafa68-3093-4946-926a-8237ea263e5c
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       C:\Users\Name\virtualbox vms\sg2_win7_x64_lab\sg2_win7_x64_lab_
clone.vdi
Storage format: VDI
Format variant: dynamic default
Capacity:       51200 MBytes
Size on disk:   7806 MBytes

C:\Program Files\Oracle\VirtualBox>

That's it! From here what you have to do is add this cloned drive as your new VDI for the VM. You do that inside VirtualBox. I won't go into that. If you have been able to read and understand this so far then you probably don't need no further guiding. You can either delete the old "fixed" file, or leave it as a backup. Make sure you test the new VDI file before you delete the original source file.

Note! You will not immediately see the new size of the cloned disk. You have to boot the VM with it, and then you have to use your partition management tool to expand your partition to fill the virtual disk (or create more partitions). For Windows, just run diskmgmt.msc and you'll be able to expand the partition in there.

Screenshots

a b c d e f g

Reference

  • forums.virtualbox.org - VBoxManage modifyhd --resize == VBOX_E_NOT_SUPPORTED
  • forums.virtualbox.org - 8.23. VBoxManage modifyhd
  • www.virtualbox.org - 8.23. VBoxManage modifyhd
  • SU - Resize partition of Windows 7 running on VirtualBox with dynamically allocated storage

Here's a fairly simple process that worked for me to resize a VirtualBox (v. 4.3.16) fixed size disk to a 60GB dynamic disk on my Mac (OS X 10.9.4) with Linux (Ubuntu 14.04) running as the guest OS:

In Terminal on the Mac, CD into the directory with the VDI file and type the following:

VBoxManage showhdinfo mydisk.vdi  
VBoxManage clonehd mydisk.vdi mydiskClone.vdi  
VBoxManage modifyhd mydiskClone.vdi --resize 61440  
VBoxManage showhdinfo mydiskClone.vdi

In VirtualBox application storage section, add mydiskClone.vdi as a second HD, then start the VM normally.

In the shell on the guest OS, type:

sudo fdisk /dev/sdb
- delete all partitions
- create new partition on full disk

Reboot the guest. When it comes back up, go to the shell again and type:

sudo resize2fs /dev/sdb1

Shutdown the guest OS.

In VirtualBox application storage section again, set mydiskClone.vdi to be the boot disk. Then start guest OS and verify new disk size using properties of "Computer" in the file browser.


OK, so I made it happen without cloning.

I had a 100GB VDI file (fixed format) with Ubuntu on it. Working fine. I wanted to free up some space in the host by shrinking the VM.

Steps:

  1. Cleanup

Deleted unwanted files. 45GB free. I only need 30GB to be freed.

  1. Fix the guest OS

I only have one partition, so cannot do anything from inside guest Ubuntu. Mounted Ubuntu Live in the VM and booted from it.

Used gparted to shrink the partition to 70GB. Created a new partition in the unused space in ext4.

  1. Wipe the free space

The free space in a the new partition (30GB).

Followed this guide : Shrink virtual hard drive on Ubuntu as Guest and Host OS using zerofree

My new partition was /dev/sda3.

$ sudo apt-get install zerofree
$ sudo init 1

...

# mkdir /tmp3
# mount -o ro -t ext4 /dev/sda3 /tmp3
# zerofree -v /dev/sda3
  1. Compact the VDI

    VBoxManage modifyhd "ubuntu.vdi" compact

It took a while, but it worked!