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

Reference
modifyhd
is now known asmodifymedium
but remains backward compatible – see virtualbox.org/manual/ch08.html#vboxmanage-modifyvdi