Re-sizing a VirtualBox Virtual Disk Image File

Update 17th April 2009: This post has been updated to include the latest suggestions posted to the comments, including using the gparted Live CD instead of the Gentoo Linux System Rescue CD.

In my previous post I extolled the virtues of Sun’s desktop virtualisation software, VirtualBox. One thing niggled me though – I couldn’t easily expand a Virtual Disk Image (VDI) and was regularly reaching the space limits of the modest 20Gb disks I was creating; I needed an easy way of expanding disks before I could use it as my main virtualisation platform and felt comfortable in recommending it to my readers.

Given that there is very little information out there on how to perform this task – apart from a single obscure forum post – here is my attempt to walk you through the process of expanding a virtual disk image. This guide assumes that you are trying to expand a disk configured with Windows, however the procedure should be pretty much the same for a Linux/OpenSolaris etc. based disk.

Getting Started

  • Ensure that the Virtual Machine that uses the disk you want to re-size is shutdown.
  • Remove any snapshots you may have – VirtualBox can go a bit wonky when re-sizing a VDI with snapshots attached (thank to Darren for pointing this one out).
  • Take a backup of your VDI that you want to resize (by copying and pasting the .vdi file in Windows Explorer) – if we mess our resize up we can recover back to this backup.
  • While the VDI file is copying, download the latest stable release of the gparted Live CD ISO from Sourceforge (approx 95Mb).
  • While the ISO is downloading, create a new empty Virtual Disk in the VirtualBox console that is the size of the larger disk you need.
  • Attach the new disk to your virtual machine that needs its disk expanding as the slave disk.
  • Once the System Rescue CD download has completed, mount the ISO on the virtual machine CD drive.

Starting the Disk Image Utility

  • Boot the virtual machine from the mounted ISO – you may need to reconfigure your VM to ensure that you boot from the CD-ROM drive before the HDD, so change the VM settings as shown below or hit F12 at boot and change there (given that VirtualBox is currently under development, the screenshot below will always be out of date, but I’m sure you get the idea ;-):

virtualboxbootorder

  • During the boot process the gparted Live CD will prompt you to select the correct keymap – if you are using a QWERTY keyboard, simple select the ‘Don’t Touch Kepmap’ option; next, you will be prompted for the language settings to be used – select the appropriate language code, in my case ’02’ for British English; finally, you will be prompted for the X-Windows mode – select ‘0’ to automagically start gparted in an X-Windows session.
  • Once gparted starts, you will be presented with a graphical representation of your disks – left-click the left-to-right bar named /dev/sda1 (your primary hard disk that is to be expanded) and then click on the Copy icon.
  • Select the drop-down-box to the right of the tool-bar and select the second (currently empty) disk – /dev/sdb (possibly /dev/hdb in your environment), the graphical representation of your disks will change to show you the second slave disk which is currently empty. Click on the Paste icon.
  • gparted will will prompt you that all data on the new partition will be erased and if you’re happy, subsequently prompt you on how the disk should be formatted. For a Windows environment, select MSDOS (this will give you an NTFS partition, trust me!).
  • gparted will finally present you with a slider dialog indicating the desired size of the new disk. Drag the slider to the right to select the maximum size of the new partition on this new disk (I’d just drag it so the partition consumes the whole disk), as shown in the screenshot below:

selectsizeofnewpartitiongparted

  • Click the Apply icon, you’ll be presented with something along the lines of the screenshot below as the contents of the source disk are copied to the new, larger, disk:

  • Once the copy has completed (approx. 35 mins to create a 30Gb disk from an original 20Gb disk), you will need to mark the new disk as bootable (if this is to be a bootable partition – if not, simply skip the next step).
  • To mark the partition as bootable, right-click the graphical representation of the new disk and left-click Manage Flags. In the dialog that appears, select Boot and click Ok to close. gparted will apply the necessary flag and re-scan your disks.
  • Close gparted and click the Exit icon to shutdown the system.

Completing the Re-Sizing

  • Once the virtual machine has powered off, re-configure the hard disks to use the newly created/copied disk as the primary and remove the old primary disk from the system; finally, unmount the System Rescue ISO from the CD-ROM.
  • Power on your new VM and you should be presented with the the usual Windows boot sequence; if you are just presented with a black screen with a flashing cursor at the top left-hand corner of the screen, there isn’t a boot sector on the disk, so restart gparted and add the boot flag as directed above.
  • Hopefully, your virtual machine will start without issue. Windows may perform a check of the disk during boot. Once logged-in, open Windows Explorer and confirm that the newly created drive is the new larger size.

The procedure described above has been tested on Windows Server 2003 and works without issue (although the first time around I forgot to apply the boot flags…), so it should work seamlessly on Windows XP, Vista and Windows Server 2008.

I appreciate that this procedure does involve running a flavour of Linux to acheive the desired results, however its very straightforward and shouldn’t be off putting to a Linux novice.

Reblog this post [with Zemanta]
About these ads

106 thoughts on “Re-sizing a VirtualBox Virtual Disk Image File

  1. Hey guys,

    I had the same problem with win2008 r2 on win7. I tried Rob’s idea and it didn’t work initially, but only because it wasn’t set as my primary partition.
    in order for it to work u need to right click your new image file inside the settings>storage and choose SATA port 0. Then load your windows setup again, choose repair and either use the command line with bootrec /fixboot or let it automatically fix it for you.

  2. Ran across this and thought I would add my 2 cents.

    I think someone else mentioned this but figured I would state it again. I am running Mac OS X (not by choice) and set up a Windows 7 Home Premium virtual os. Of course I stupidly took the default 20 GB dynamic disk option. After installing some of my development stuff, I was basically out of space.

    So these were the steps I did to resize the partion.

    1) Shutdown the virtual Windows OS.
    2) In terminal, ran “VboxManage modifyhd –resize 80000 nameofimage.vdi”
    3) Startup the virtual Windows OS again.
    4) Administrative Tools -> Computer Management -> Storage -> Disk Management
    5) Disk 0 showed the 100 MB partition, 20 GB NTFS C:, and 60 GB unallocated.
    6) Right click the “C:” partition (either up in the list or the graphical representation) and select “Extend Volume”.
    7) It came up with 1 or 2 screens that I did not make any changes on and just clicked through them (sorry didn’t think it would work so didn’t keep track of their names and such).
    8) Partition now extended to 80 GB (in just a few seconds).

    I didn’t need to run any other utilities except VboxManage. Also this was from a default partitioning of a Win 7 install. I hadn’t converted the partition to a dynamic disk or anything else.

    Hope this helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s