Prior to version 4.0, expanding the size of a virtual disk was not a task that was directly supported by VirtualBox. After some trial and error, I've cobbled together a solution documented below which consolidates and amends information found elsewhere online.
For VirtualBox versions before 4.0, the procedure, in broad strokes, involves the following steps.
- Create a new virtual disk.
- Clone the existing virtual disk into the new virtual disk.
- Replace the old virtual disk on your virtual machine with the new one.
- Expand the partition in the new virtual disk clone.
As of VirtualBox 4.0 (released December 22, 2010), the process has gotten a lot simpler. You may replace the first three steps with a single VBoxManage command. [Thanks to Przemysław for pointing this out.] You will still need to perform the final step of expanding the partition in your newly resized virtual disk image.
Prerequisites
Before you start this procedure you'll need to do the following.
- Make sure you have the VBoxManage command-line tool installed on your host system.
- Download the ISO for a GParted Live CD or else a Linux Live CD using a Linux distribution that includes the GParted partition editor utility.
- IMPORTANT: If the virtual disk you want to resize is attached to a virtual machine with snapshots, you will need to delete these snapshots so that all disk state information is merged into the base virtual disk VDI file.
0. Resize the virtual disk (VirtualBox version 4.0+ only)
If you are using VirtualBox version 4.0 or later, you can resize the logical capacity of a virtual disk using the VBoxManage modifyhd --resize command.
On the host system, run the following command:
cd /path/to/vbox/disks
VBoxManage modifyhd OldDisk.vdi –-resize 30000
where OldDisk.vdi is the filename of the virtual disk VDI file you want enlarge and 30000 is the new maximum size (in megabytes) of the virtual disk.
If your host OS is Windows, then the commands you need to enter at the Command Prompt will look more like the following.
cd "C:\Documents and Settings\myusername\.VirtualBox\HardDisks"
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd OldDisk.vdi --resize 30000
Once you've resized your existing virtual disk, you should skip to Step 4: Expand the partition on the larger disk.
1. Create a new virtual disk
You can create a new virtual disk of the desired storage size in two ways: using the VirtualBox GUI or using the VirtualBox command-line utility VBoxManage.
1a. Using the VirtualBox GUI
On the host system, launch VirtualBox, and navigate to File → Virtual Media Manager… → Hard Disks. Click Add to add a new virtual hard disk, and step through the wizard, making sure to create a new maximum disk size that accommodates your needs.
1b. Using the VirtualBox command-line utility
On the host system, run the following command:
cd /path/to/vbox/disks
VBoxManage createhd –-filename NewDisk.vdi --size 30000 --remember
where NewDisk.vdi is the filename of the new virtual disk VDI file and 30000 is the maximum size (in megabytes) of the virtual disk.
Note that on Windows you may need to designate the full path to the VBoxManage command. For example, on a Windows host system at the Command Prompt, the above commands will instead look something like the following.
cd "C:\Documents and Settings\myusername\.VirtualBox\HardDisks"
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" createhd –-filename NewDisk.vdi --size 30000 --remember
2. Clone the existing virtual disk into the new virtual disk
Now clone the old, small virtual disk into the new, large virtual disk.
cd /path/to/vbox/disks
VBoxManage clonehd OldDisk.vdi NewDisk.vdi --existing
If your host OS is Windows, then the commands you need to enter at the Command Prompt will look more like the following.
cd "C:\Documents and Settings\myusername\.VirtualBox\HardDisks"
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" clonehd OldDisk.vdi NewDisk.vdi --existing
3. Replace the old virtual disk on your virtual machine with the new one
3a. Using the VirtualBox GUI
On the host system, launch VirtualBox, and select the virtual machine whose hard disk you are expanding, and navigate to Settings → Storage. Change the SATA Controller so that it uses the new virtual disk file instead of the old one.
3b. Using the VirtualBox command-line utility
VBoxManage modifyvm "My Virtual Machine" --hda none
VBoxManage modifyvm "My Virtual Machine" --hda NewDisk.vdi
Or for Windows host systems:
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyvm "My Virtual Machine" --hda none
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyvm "My Virtual Machine" --hda NewDisk.vdi
where My Virtual Machine is the name of the virtual machine whose disk you are expanding.
(At this point you may want to boot your virtual machine to test whether it works. If you do, remember to shut it down before continuing.)
4. Expand the partition in the larger virtual disk
Now that you have an enlarged version of your virtual disk, you will want to expand the new disk's partitions to take advantage of the increased disk size. To do so we'll use the GParted partition editor.
If you have created a GParted Live CD or Linux Live CD, place it in the CD/DVD drive on your host machine, and change the settings of your virtual machine to use the host drive as its CD/DVD drive. If you have the ISO for the Live CD, simply configure your virtual machine so that it uses the Live CD ISO file for its CD/DVD drive. Navigate to Settings → Storage to configure the CD/DVD settings for your virtual machine.
Make sure that the virtual machine is configured to include CD/DVD-ROM in the boot order. To check the boot order, navigate to Settings → System → Motherboard.
Start the virtual machine. The virtual machine should boot from the Live CD.
If you are using the GParted Live CD, then you should see the GParted utility. If you are using a Linux Live CD, you may need to click on a menu item to indicate you want to try the Linux distribution without installing it.
Once the Gnome or KDE desktop environment is finished loading, open the Terminal application, and then enter sudo gparted to launch the GParted utility.
Now that you have launched GParted, in the upper right of the GParted window, make sure the correct disk has been selected — e.g. /dev/sda (30.00GB). Select the partition you want to expand — e.g. /dev/sda1 — and click the Resize/Move button.
In the pop-up window, expand the partition to use the maximum size. You can do so by dragging the right edge of the partition block all the way to the left. Click the Resize/Move button.
Note: If there is a swap partition, you may need to take a few extra steps to ensure that the unallocated space is immediately after the partition you want to grow. See the addendum below for more information on how to move the unallocated space.
Click the Apply button to apply the partition changes.
Quit GParted, and shutdown the virtual machine. Eject the CD from the host machine's CD/DVD drive or modify the virtual machine settings to unmount the ISO from the virtual machine CD/DVD drive.
Start the virtual machine again, and at the command line, type df -h to validate that the partition has been resized properly.
References
- Expanding VirtualBox dynamic hard disks. 2009 Dec 21. Retrieved 2011 Jan 30.
- How to enlarge a VirtualBox disk. 2007 Nov 25. Retrieved 2011 Jan 30.
- Cloning and Copying VirtualBox virtual machines. 2008 Nov 3. Retrieved 2011 Jan 30.
ADDENDUM: Special Notes Regarding Swap Partitions
If virtual disk you are editing via GParted has a swap partition, you may find that the newly available free space on the disk is separated from the partition you want to expand by the swap partition. This presents a problem because in order to expand a partition, the free, unallocated disk space must be located immediately after the partition.
One way to deal with the problem is to move the swap partition so that the unallocated space is before the swap partition rather than after it. Another is to delete the swap partition altogether.
Moving the swap partition
Right-click the swap partition, and select the Swapoff item in the context menu to disable the swap partition. Doing so will unlock the swap partition so that you may modify it.
Select the extended partition that contains the swap partition, and click on the Resize/Move button.
In the pop-up window, take note of the partition size (e.g. 486 MB).
Drag the right edge of the partition all the way to the right, and then drag the left edge of the partition toward the right until the new partition size is back to what it was — in this example, 486 MB. Click the Resize/Move button.
Click the Apply button. Re-enable the swap partition by right-clicking on it and selecting Swapon.
You should now be able to expand the main partition into the unallocated space.
Deleting the swap partition
Right-click the swap partition, and select the Swapoff item in the context menu to disable the swap partition. Doing so will unlock the swap partition so that you may modify it.
Select the extended partition that contains the swap partition, and then click the Delete button.
You should now be able to expand the main partition into the unallocated space.
Actually your statement about VB is no longer true (to some extent). Since VB 4 you can resize dynamic VDI files via VBoxManage using modifyhd command and --resize option. If you want to resize fixed VDI, then you can use my little tool called vidma - Virtual Disks Manipulator. See:
ReplyDeletehttps://github.com/przemoc/vidma
http://tinyurl.com/vbox-vidma
Oh, nice. That's an extremely helpful piece of information. I'll update the above post accordingly.
ReplyDeleteI add only that vidma since version 0.0.3 supports resizing dynamic VDI files (fixed ones were supported from the beginning).
ReplyDeleteit's a good idea to read and perform the prerequisite detail about snapshots, or gparted will report the disk size as if it wasn't grown by modifyhd.
ReplyDeleteI was unable to delete snapshots after modifyhd had been performed, but restored to my pre-modifyhd backup and then it worked.
You don't need to bother with a Live CD. Open the VM. Go to Control Panel, Administrative Tools, Computer Management, Disk Management, right click on the "Unallocated" space, select "Extend Volume", click "Next", click "Finish", and DONE. No reboot necessary. This should save you 5 minutes or so. Thank you for the great post!
ReplyDeleteSorry, that is of course if you have a Windows guest VM. Otherwise, follow step 4.
ReplyDeleteThank you very much, It has been very helpful.
ReplyDeleteI had the same issue with snapshots as @JohnTeldiot. What I did instead was restore the snapshot and delete it afterwards.
Thank you! Super helpful!
ReplyDeleteThanks @keoko for the snapshot fix, got flummoxed then saved.
Step 4 was what I wanted to know because after resizing, the drive did not appear to have been resized and was still showing as full. For some reason the Linux CD did not boot inside the VM but as Windows was installed I used its own disk management tool and got the job done. You just need to expand the existing partition to include the unallocated space, which is what gets created from the earlier resizing.
ReplyDeleteHi, is it definitely OK to delete snapshots? I know it is merging them to the base vdi but I have installed some software on the VM and would hate to lose it when deleting snapshots. And is it definitely not possible to do the modifyhd command when you have snapshots on the VM already?
ReplyDeleteThanks in advance!
the file-path to the .VDI files for Vista is C:\Users\User\VirtualBox VMs\'guest name'\
ReplyDeleteand the default install path for VBoxManage is
C:\Program Files\Oracle\VirtualBox
(so no change on the 2nd path)
I'm going through this process but I'm stuck using gparted. I'm trying to resize an hfs+ partition and apparently gparted can't increase those? is that true?
ReplyDeleteI'm trying to follow theses steps to resize the virtual HD of an OS X install in VirtualBox. It works, except that when I'm in Gparted, the OSX partition (hfs+) does not seem supported by Gparted, and it won't allow me to increase its size. Instead it tells me that it is unable to read the contents of the file system (the hfs+ partition) and that it's missing the hfsprogs software package. Which I don't understand because I have the latest release and it SHOULD include hfsprogs. Can you help? Thanks.
ReplyDeleteThanks. The swap partition stuff was my missing link.
ReplyDeleteThat's the last time I skimp on VM disk space.
That article really helped - thanks a lot!
ReplyDeleteGreat Article. Thanks
ReplyDeleteExcellent post... Thanks a lot....your post consolidated everypiece in a single place.
ReplyDeleteGreat post -- exactly what I needed to resize the disk space of my Ubuntu VM. Thanks lots!
ReplyDeleteA great big THANK YOU Anonymous for pointing out that the Linux or other boot CD's were not needed in Windows environment as the disk can easily be "extended" from disk manager console within.
ReplyDeleteThanks and this was a fantastic find.
S. Murad
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd OldDisk.vdi --resize 30000
ReplyDeleteA Great find of the day!
Thank you Eugene for taking the time to put all these instructions out there.
It saved me a days worth of work.
S Murad
Hey, thanks! It was much less work than what I expected when I first skimmed through this post.
ReplyDeleteBTW, I still have an .img disk, and VBoxManage didn't want to resize it.
To fix this, just go to VB, and clone your machine. Clone's drive will be a .vdi, that you would be able to resize.
brilliant, thanks a tonne !
ReplyDeleteThank you so much! 30 seconds to expand a Win2008 disk on a Ubuntu host.
ReplyDeleteVery helpful. I resize my Linux Mint guest's disk from 7G to 20G, and move the swap to the last. Works fine.
ReplyDeleteThanks a lot.
My Virtual Box version bigger than 4.0 but --resize didn't work for me but the other method worked like a charm. Thanks.
ReplyDelete--resize worked fine for me. I had to go through a few "more hoops" than you to move my swap partition (I presume because mine was on an extended partition), see http://superuser.com/questions/305462/how-to-increase-a-virtualbox-disk-size/400520#400520
ReplyDeleteAlso note that you can "move" a partition by clicking in the middle of it and dragging it, which is simpler than dragging both arrows one after another.
This comment has been removed by a blog administrator.
ReplyDeleteThanks! It worked!
ReplyDeleteThis has been very helpful! However, let me point out that I had a lot of trouble getting this done using Virtual Box 4.0. However, after upgrading to 4.1, I could first clone, then resize and finally expand (with gparted) an originally fixed-size, bootable WinXP drive.
ReplyDeleteGreat Post. I was able to resize my Ubuntu VM with these steps.
ReplyDeleteQuick Tip for ADDENDUM:
I had an extended partition containing a swap partition between my root partition and unallocated space. So i tried to follow ADDENDUM steps but i was not able to move the extended partition in one step. So, i extended the "extended partition" with unallocated space, then moved the swap partition to the end of this new partition and finally shrink the extended partition to its original size, leaving unallocated space close to my root partition.
thanks a lot! i like people who post stuff which works out of the box
ReplyDeleteI was having trouble with the swap in a VM with Lubuntu running as a guest on a Windows 7 host.
ReplyDelete1) Did the "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd OldDisk.vdi --resize 15000
2) Followed instructions here on how to use gparted when the swap partition won't let you resize or move it. http://blog.mwpreston.net/2012/06/22/expanding-a-linux-disk-with-gparted-and-getting-swap-out-of-the-way/
Done.
sorry that was blah.vhd not vdi but it worked just the same.
ReplyDeleteThis is the best article on how to do this by far. I tried both methods. First I expanded an existing ubuntu dynamic disk. Then I created a new fixed disk of the same new size and finally cloned the dynamic disk to the fixed disk. As expected, the fixed disk runs noticeably faster. Thanks much for the simple instructions.
ReplyDeleteOn Windows 8 you need to run VirtualBox as Administrator in order to allow the Create Server COM operation to work otherwise you will get CO_CREATE_SERVER ERROR
ReplyDeleteMy 2 cents....
You have to REMOVE the Virtual Disk Images from those that VirtualBox knows about before using VBoxManage modifyhd or clonehd, or you get an error like this:
ReplyDelete# VBoxManage modifyhd ./Windows\ XP.vdi --resize $((10*1024))
VBoxManage: error: Cannot register the hard disk '/home/meermanr/VirtualBox VMs/Windows XP/./Windows XP.vdi' {fb2be3ee-9d27-4412-aa94-125d4db6ec2d} because a hard disk '/home/meermanr/VirtualBox VMs/Windows XP/Windows XP.vdi' with UUID {fb2be3ee-9d27-4412-aa94-125d4db6ec2d} already exists
VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component VirtualBox, interface IVirtualBox, callee nsISupports
Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, AccessMode_ReadWrite, fForceNewUuidOnOpen, pMedium.asOutParam())" at line 210 of file VBoxManageDisk.cpp
To remove a disk, open VirtualBox (v4.1.12), File > Virtual Media Manager, then select the disk image you want to modify and hit remove. When prompted select REMOVE followed by KEEP, since you do want to preserve the file data.
Thank you! Great post, easy to follow and super helpful.
ReplyDeleteThanks a lot for this!
ReplyDeleteWorked fine as described. Thank you!
ReplyDeleteThanks, it worked as described. The only subtlety: when choosing the Live CD you have to click the button "Leave empty" in the confirmation dialog box. Then D: will appear on the right side.
ReplyDeleteI'm having the exact same problem as Oliver, "I'm trying to follow theses steps to resize the virtual HD of an OS X install in VirtualBox. It works, except that when I'm in Gparted, the OSX partition (hfs+) does not seem supported by Gparted, and it won't allow me to increase its size. Instead it tells me that it is unable to read the contents of the file system (the hfs+ partition) and that it's missing the hfsprogs software package. Which I don't understand because I have the latest release and it SHOULD include hfsprogs. Can you help? Thanks."
ReplyDeleteHelp would really be appreciated.
i am getting this error "Syntax VBoxManage modifyhd "C:\Users\USER\VirtualBox VMs\gautamxp\64g1b.vdi" -resize 65536error: --resize: RTGetOpt: Command line option has argument with bad form
ReplyDeleteat."
input is
VBoxManage modifyhd "C:\Users\USER\VirtualBox VMs\gautamxp\64g1b.vdi" -resize 65536
Anonymous: You need to use --resize, not -resize. Note the double hyphen.
ReplyDeleteOne thing I noticed was not mentioned is when trying to re-size the vdi, if it fails with out of memory errors, most likely your anti-virus/fire wall is interfering. I just had the issue and was able to successfully resize the vdi when I turned my anti-virus/fire wall off.
ReplyDeleteFor windows computers I used EaseUS partition manager (free version) to expand the windows boot partition (basic disk) without having to reboot. works really well and is very quick.
ReplyDeleteNote that for resizing a static virtual hard disk it's necessary to clone it first, which creates a dynamic disk, and then resize this one.
ReplyDeletevboxmanage clonehd olddrive.vdi newdrive.vdi
vboxmanage modifyhd --resize 8000 newdrive.vdi
Also, if you have multiple snapshots it's easier to first clone the entire virtual machine and then resize the vdi from that machine!
reference: http://www.streamwave.com/systems-administration/how-to-extend-your-virtualbox-virtual-hard-drive/2/
Hi! very nice tip I did it once, and also I change from virtual disk as written here:
ReplyDeletehttp://runakay.blogspot.com/2013/04/changing-disk-location-in-virtualbox.html
Great Post, very helpful, many thanks
ReplyDeleteThis post is an amazing post about virtual hard disk. These images are so helpful to understand more clearly. Thanks for the great post.
ReplyDeletei was downloading mac os x 10.8 mountain lion in virtual box on my windows7,bt i've given destination folder to local disk 'D',bt after instaling mac os,my virtual box seems to be in local disk'C'.i cnt getting any strikes of thogths why this is hapenning,wud any1 please help me?
ReplyDeleteThank you, few second to expand my vbox on redhat
ReplyDeleteThanks. Really very useful post.
ReplyDeleteAwesome! I had to delete the swap, because it wouldn't resize the left side even after turning it off. So if anyone has that issue, deleting it is fine. You just create a new swap at the end of the disk afterwards. First create an extended partition, then put the swap in the extended partition. Easy.
ReplyDeleteHi! I'd prefer to do this:
ReplyDeletehttp://runakay.blogspot.com/2013/12/changing-size-of-virtualbox-disc.html
Awesome. This was super helpful. Thanks!
ReplyDeleteGreat post!
ReplyDeleteFor Ubuntu 13.10 as guest, I had to modify some steps while resizing/moving swap. These are as follows:
- Your instructions indicated that the user would need to select the extended partition that contained the swap partition, move the right edge to the very end, and then move the left edge to the right until the size was restored. This did not work directly for me because the left edge of the extended partition wasn't allowed to be moved.
- Instead, I had to do it in 3 sub-steps:
-- Select the extended partition. Click resize/move. Move the right edge all the way to the right. Click on done. At this point you have a really large extended partition with swap space all the way up front inside it.
-- Select the swap partition. Click resize/move. Move the right edge all the way to the right. Move the left edge all the way to the right until size of swap is restored. Note that while attempting to move the left edge, you may get a warning screen indicating that your device may fail to boot if /boot is contained in this partition. If so accept and move it anyway. Click on done after the move. At this point you have a really large extended partition with unallocated space up front and a tiny swap space at the end.
-- Select the extended partition again. This time you can move the start of the extended partition, until it matches the start of the swap partition. Click on done.
With this, unallocated space is in front of the swap and extended partitions. You can continue with your instructions on adding it to dev/sda1.
Worked great... thanks a lot... you saved some time without uninstalling & installing the os...
ReplyDeleteWorked out well when I was partitioning my Ubuntu VM on VirtualBox, on my Mac OS X 10.9 Thanks!!!
ReplyDeleteThis was very helpful, thank you. (Did it for VBox 4.3.10 on Windows 7 Pro, guest = Ubuntu Precise (12.04.4)
ReplyDeleteBest guide ever. Thanks!
ReplyDeleteThanks!!!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks! Thanks! Thanks!
ReplyDelete