Sunday, January 30, 2011

Resizing a VirtualBox Virtual Hard Disk

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.

  1. Create a new virtual disk.
  2. Clone the existing virtual disk into the new virtual disk.
  3. Replace the old virtual disk on your virtual machine with the new one.
  4. 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

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.

63 comments:

  1. 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:

    https://github.com/przemoc/vidma
    http://tinyurl.com/vbox-vidma

    ReplyDelete
  2. Oh, nice. That's an extremely helpful piece of information. I'll update the above post accordingly.

    ReplyDelete
  3. I add only that vidma since version 0.0.3 supports resizing dynamic VDI files (fixed ones were supported from the beginning).

    ReplyDelete
  4. it'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.

    I was unable to delete snapshots after modifyhd had been performed, but restored to my pre-modifyhd backup and then it worked.

    ReplyDelete
  5. 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!

    ReplyDelete
  6. Sorry, that is of course if you have a Windows guest VM. Otherwise, follow step 4.

    ReplyDelete
  7. Thank you very much, It has been very helpful.

    I had the same issue with snapshots as @JohnTeldiot. What I did instead was restore the snapshot and delete it afterwards.

    ReplyDelete
  8. Thank you! Super helpful!
    Thanks @keoko for the snapshot fix, got flummoxed then saved.

    ReplyDelete
  9. 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.

    ReplyDelete
  10. Hi, 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?

    Thanks in advance!

    ReplyDelete
  11. the file-path to the .VDI files for Vista is C:\Users\User\VirtualBox VMs\'guest name'\

    and the default install path for VBoxManage is

    C:\Program Files\Oracle\VirtualBox

    (so no change on the 2nd path)

    ReplyDelete
  12. 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?

    ReplyDelete
  13. 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.

    ReplyDelete
  14. Thanks. The swap partition stuff was my missing link.
    That's the last time I skimp on VM disk space.

    ReplyDelete
  15. That article really helped - thanks a lot!

    ReplyDelete
  16. Excellent post... Thanks a lot....your post consolidated everypiece in a single place.

    ReplyDelete
  17. Great post -- exactly what I needed to resize the disk space of my Ubuntu VM. Thanks lots!

    ReplyDelete
  18. A 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.
    Thanks and this was a fantastic find.
    S. Murad

    ReplyDelete
  19. "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd OldDisk.vdi --resize 30000

    A 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

    ReplyDelete
  20. Hey, thanks! It was much less work than what I expected when I first skimmed through this post.

    BTW, 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.

    ReplyDelete
  21. brilliant, thanks a tonne !

    ReplyDelete
  22. Thank you so much! 30 seconds to expand a Win2008 disk on a Ubuntu host.

    ReplyDelete
  23. Very helpful. I resize my Linux Mint guest's disk from 7G to 20G, and move the swap to the last. Works fine.

    Thanks a lot.

    ReplyDelete
  24. 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
  25. --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
    Also 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.

    ReplyDelete
  26. This comment has been removed by a blog administrator.

    ReplyDelete
  27. Thanks! It worked!

    ReplyDelete
  28. This 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.

    ReplyDelete
  29. Great Post. I was able to resize my Ubuntu VM with these steps.


    Quick 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.

    ReplyDelete
  30. thanks a lot! i like people who post stuff which works out of the box

    ReplyDelete
  31. I was having trouble with the swap in a VM with Lubuntu running as a guest on a Windows 7 host.

    1) 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.

    ReplyDelete
  32. sorry that was blah.vhd not vdi but it worked just the same.

    ReplyDelete
  33. This 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.

    ReplyDelete
  34. On 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

    My 2 cents....

    ReplyDelete
  35. 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:

    # 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.

    ReplyDelete
  36. Thank you! Great post, easy to follow and super helpful.

    ReplyDelete
  37. Worked fine as described. Thank you!

    ReplyDelete
  38. Thanks, 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.

    ReplyDelete
  39. I'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."

    Help would really be appreciated.

    ReplyDelete
  40. 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
    at."
    input is
    VBoxManage modifyhd "C:\Users\USER\VirtualBox VMs\gautamxp\64g1b.vdi" -resize 65536

    ReplyDelete
  41. Anonymous: You need to use --resize, not -resize. Note the double hyphen.

    ReplyDelete
  42. One 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.

    ReplyDelete
  43. For 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.

    ReplyDelete
  44. Note 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.
    vboxmanage 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/

    ReplyDelete
  45. Hi! very nice tip I did it once, and also I change from virtual disk as written here:
    http://runakay.blogspot.com/2013/04/changing-disk-location-in-virtualbox.html

    ReplyDelete
  46. Great Post, very helpful, many thanks

    ReplyDelete
  47. This post is an amazing post about virtual hard disk. These images are so helpful to understand more clearly. Thanks for the great post.

    ReplyDelete
  48. i 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?

    ReplyDelete
  49. Thank you, few second to expand my vbox on redhat

    ReplyDelete
  50. Thanks. Really very useful post.

    ReplyDelete
  51. Awesome! 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.

    ReplyDelete
  52. Hi! I'd prefer to do this:
    http://runakay.blogspot.com/2013/12/changing-size-of-virtualbox-disc.html

    ReplyDelete
  53. Awesome. This was super helpful. Thanks!

    ReplyDelete
  54. Great post!

    For 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.

    ReplyDelete
  55. Worked great... thanks a lot... you saved some time without uninstalling & installing the os...

    ReplyDelete
  56. Worked out well when I was partitioning my Ubuntu VM on VirtualBox, on my Mac OS X 10.9 Thanks!!!

    ReplyDelete
  57. This was very helpful, thank you. (Did it for VBox 4.3.10 on Windows 7 Pro, guest = Ubuntu Precise (12.04.4)

    ReplyDelete
  58. Best guide ever. Thanks!

    ReplyDelete
  59. This comment has been removed by the author.

    ReplyDelete

Note: Only a member of this blog may post a comment.