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

vdivirtualboxwindows 7

I'm trying to figure out how to change the size of a VDI file. I'm new to VirtualBox, and I got a lot to learn. But I think I got the hang of the basics. I have already installed Windows 7 as my first guest. The host is also Windows 7. I wasn't sure how much disk space to allocate, so I went for a VDI size of 40 GB and fixed size for increased performance. But I have changed my mind now, and I want to add another 10 GB. I've been clicking around the program and reading the Help menu contents, but it's not helping. It explains disk controllers, SAS, SATA, SCSI, yada, yada, but not what I'm looking for.

I searched the world wide web for answers, the VirtualBox forum says you're supposed to use Gparted if you want the easiest method (some mod posted a sticky). Some users discuss using dd command. But that's all Linux stuff. Also, there are plenty of questions here at SU that deal with increasing and decreasing VDI file sizes, but they are all about using Linux as host and Windows as guest.

I found this nice blog post on how to do it with the modifyhd command of VBoxManage. This guy is also doing it on Linux as host OS. He even wanted the same size (50 GB) for his VDI file as I did for mine, what a coincidence. So I thought I would give it a try anyway, either make it or break it. What can possibly go wrong?… eh? Well this is the ugly surprise I got.

C:\Program Files\Oracle\VirtualBox>VBoxManage.exe modifyhd "%userprofile%\virtua
lbox vms\sg2_win7_x64_lab\sg2_win7_x64_lab.vdi" --resize 51200
0%...
Progress state: VBOX_E_NOT_SUPPORTED
VBoxManage.exe: error: Resize hard disk operation for this format is not impleme
nted yet!

C:\Program Files\Oracle\VirtualBox>

a

Say what now?!… can't resize virtual hard disks of the VDI format? You don't say? Not supporting your own format? Now how about that…

Is there really no simple way to change the size of a VDI file, preferably with a few clicks inside VirtualBox? Anything in progress?

Best Answer

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