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 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 ;-):

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:

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.

Tomas,
Thanks for the pointer, I’ll try Disk Manager later today on a 2008 Server. One question though: how can Disk Manager increase the size of a partition if the virtual disk is already at its size limit?

Hey! Your Post “ng a VirtualBox Virtual Disk Image File at Nick Heppleston’s BizTalk Blog” is very interesting for me. Unfortunately my written English is not so good so I write in German: Dir, meinem liebsten, geh

Can these same steps be used to copy a fixed-size disk to a dynamically expanding one? I can’t see any reason why not, but after trying it, I get a “A disk read error occurred” error when I try to boot from the new disk…

Jules,
I don’t know whether a dynamically expanding disk needs some ‘special’ configuration within the disk itself, so maybe it isn’t possible…. Out of interest, are you sure that you marked the partition as bootable before you tried to use it?

Let me add a bit to the end of the process – I did this with an XP image, and though everything appears to work fine, I did see one issue. During the boot process, Chkdsk ran, didn’t find anything and booting continued. Once XP got to the login screen, VirtualBox reported a fatal error and was “stuck”. VB locked up good at that point so I killed it and restarted. 2nd time was the charm. The VM came right up, and once logged in, the Add Hardware process picked up the new disk.

Thanks, very awesome tutorial, this worked for me! However I’d recommend using the gparted live cd instead, which is what I used. It is 95 megs instead of 230, and automatically boots into a graphical environment after choosing the keyboard, so it shouldn’t be as off-putting as you aren’t ever manually typing things into a terminal. Additionally there is a shut-down icon on the desktop so you don’t have to manually halt it either. By the way this worked fine on a dynamic image (.vdi) file. Thanks again!

Great post man. I was definitely skeptical about having to download GParted and use it, but it actually turned out to be a great learning experience. I’m not really new to Unix or Linux, but I’m not expert at it either. The GUI and your instructions on using it were spot on and very helpful.

@Antoine Leclair

Thanks for the advice for Vista. By following your additional instructions, I was able to get a Vista 64 bit guest running on a Vista 64 bit host.

This may alarm some people, so I think it’s worth a mention: CHKDSK ran when Windows started up the 1st time, and after that it (of course) booted fine.

have tried it today.
unfortunately I had the same problem than Jules and got a “A disk read error occurred” error.
I have tried it with an dynamically expanding one and a fixed one.
Both of them create the same error message.

This was great! Thank you so much. I was not paying enough attention on my initial install. How annoying that clonevsi doesn’t accommodate this.
After following these instructions my experience was a little different:

* upon boot into Vista after making a larger partition and otherwise following these instructions, I got an error from windows about winload.exe not being found.

* I booted with my vista disk, hit repair and let it reboot and do something to the disk and it’s all good now.

Nick,
Thanks! These instructions worked like a charm. I had the same problem with the Vista-32 reboot as Rob W. and Antoine, but the Windows “Repair” was quick and painless.
Host: Ubuntu 9.04 – 64bit
Client: Vista-32 Ultimate

Although I made the disk as “boot” as instruction, but it failed to start normally. I tried to repair it from by windows installation CD and it didn’t also work out! May be it is better to make a whole backup when from OS running , not just simply copy the image of system (VDI) which doesn’t help at this stage.

Have tried this with Windows Server R2 guest on Windows 7 host with no success, including Rob’s suggestion for using the repair option. When Gparted runs it shows TWp partitions in the existing single 20GB vdi drive. the /dev/hda1 is quite small and the main disk (showing as 20GB) is dev/hda2. I copied the hda2,set its boot flag and then applied Rob’s Server 2008 x64 trick to do a repair from installation media for bootfix. IT applied this and said successful but then on reboot get the “BOOTMGR” is missing. My guess is that this is the small partition (/dev/hda1)that I did not copy. I cannot see how within Getparted that I can clone a disk to be both partitions or copy them sequentially as it looks as the second will over right the first.

Any suggestions to copy both partitions – wonder about using a disk cloning tool from within Server 2008R2

re: server 2008R2. I have now got both partition on the new larger drive (using Gparted) and have marked the first one -it is 100MD only- as boot i.e. it mirrors the stup of original.Have run the bootfix trick as per Server 2008 but still will not boot. Get an error message saying it is missing a Driver and asking to reboot from installation media and do a repair but not sure what else I should repair. Any suggestions – not familiar with the repair command line.

If all you need to do is increase the size of a virtual box disk image, I have found this to be slightly more effective.

1) Create a new expanding disk image of the appropriate maximum size
2) Attach this as the slave image
3) Mount a Ubuntu (or whatever) live disk
4) Boot into the live disk
5) run “dd if=/dev/sda of=/dev/sdb” (double check the correct /dev/sd’s, they should be right but this could kill your disks very quickly if you have if and of backwards).
6) Power off the vm, remove the small disk from the machine, set the large disk as master
7) Boot the machine.

Instructions work great running a win xp on virtual box on an iMac. One thing I had to do to get the image to boot into gparted iso was to remove the guest editions mounted iso file (the gparted iso came up as a second cd drive). Otherwise perfect!

This post was excellent. I was linked here from http://www.ubuntuforums.org, and was happy they knew of this gem. I just followed the steps one by one (some parts are outdated with the current version of VirtualBox), but all in all, it worked beautifully. I just rebooted, and was now finally able to finish the XP updates on my virtual install.

I’m having the same issue as Paul. I open gparted on a Windows 7 image to see three partitions,
unallocated 1.00 MiB
/dev/hda1 System Reserved 100.00 MiB boot
/dev/hda2 9.90 GiB

I feel like I could live without ‘unallocated,’ but I don’t seem to be able to copy both of the others at once, and I’ll take Paul’s word for it that copying them sequentially won’t work. How do I proceed?

If your root partition is lvm type and you are using LVM try this out:
——————————————————————————————————————————————–
Use LiveCD with dd and fdisk utilities:
—————————————-
1) dd if=/dev/hda of=/dev/hdb # /dev/hda – old disk with smaller size, /dev/hdb – new disk with bigger size
2) fdisk -u /dev/hdb
3) Steps from 2) to 6) (incl.) described here – http://www.linuxquestions.org/questions/fedora-35/lvm-partition-resizing-666683/#post3281556
4) Shutdown the Virtual machine and remove the old disk and the LiveCD from the virtual CDROM

Hi All,
I’ve tested on 2008 R2, and I’ve also encountered with the “BOOTMGR is missing error”. Just wanted to share how i resolve the issue and the image is working perfectly fine for me. The following steps from http://www.binarywar.com/ resolves my issue:-

1. Put in the 2008 R2 installation cd
2. In the System Recovery Options dialog box, click Command Prompt
3. Type Bootrec /RebuildBcd, and then press ENTER.
4. If the Bootrec.exe tool runs successfully, it presents you with an installation path of a Windows directory. To add the entry to the BCD store, type Yes. A
5. Next, I booted the server from a Windows 7 ISO and ran the Startup Repair option.
6. After running the startup repair, on the first boot, do let the check disk run. After which, you are able to logon to your Windows 2008 R2 server as per normal with your storage size enlarge.

I got this working on my Windows 7 Ultimate 64 bit box. I did the following
1) Copy the small boot partition (I gave it 200 MiB) Paste to the new disk
2) Copy the system partition, paste to the new disk (I was going from 20 to 50 GB so I just gave it the rest of the vdi)
3) Apply
4) Manage Flags on the small partition – set boot flag
5) Power off
6) Remove old vdi from the machine, set the new one to Primary Master
7) Here’s the catch – when I booted up windows detected that something was wrong. I had to dig out my windows CD and boot.
8) I then selected repair system – this took only a few minutes.
9) Now everything worked fine.

Thanks a lot! I noticed it is important to boot virtualbox *without* the original disk attached in VirtualBox. I booted the original disk with the copy disk attached and the guest Windows (at least XP did) saw two disks with the same GUIDs. It then assigned a new drive letter to the copy disk, leaving me with an effectively non-bootable copy disk because during boot, all references to C: will no longer be valid.

Hmmm as mentioned before, resizing from 3GB to 4GB not worked. As a last try I changed from 3GB to 3.5GB and it worked! Host machne disk free space before creating additional virtual disk (4GB in first case and 3.5GB in second) was 4.7GB. Maybe it is related somehow…

I test this with my winxp pro OS running on the latest virtualbox 3.2.14.
I follow the instructions but got a few confusions and it ends up with some problems.

Step1. I backup the xp.vid file, which I think it better copy entire file folder to keep the original settings in the virtualbox for this virtual disk.

Step2. create a new file, xp2, which is with larger disk size. Now I have two virtual system: xp_original, xp2.
Step3. Here is my first question: where to mount the gparted-livecd.iso? xp_origninal or xp2?
I tried xp_original.
Step4. copy, paste, resize the partition, and set bootflag.
Step5. turn off the gparted-livecd and release xp.xvi in xp_original file.
Step6. start xp_original –> the system asks for disk scanning. done.
restart xp_original and the system is halted in the windows flag theme.

I tried to set xp.vid back to the xp_original, and the system can start up and run into the windows.
However the system is completely clean system without my settings with shared folders and installed program files.

What is going on exactly?
I am thinking to dig out my windows XP CD and try to repair the system.

You need to attach your new (larger) disk to your xp_original virtual machine; then mount the gparted ISO (again to xp_original) and boot the machine. Do your gparted copy from the original disk to the new disk. Shut down the xp_original vm and remove your original VDI and gparted ISO, so the vm only contains the newly copied disk.

to all having issues copying win7 partitions, I was trying to copy small 100mb partition then big partition but at the end I’ve got unreadable boot partition because gpart couldn’t make it 100mb so here is the fastest way to do it:
1 create new big disk
2 inside windows make disk image of system drives
3 attach new disk and switch new unformatted disk to be primary and old disk to be secondary
4 boot with setup cd and choose repair and select repair from backup image, this will create exact copy of old drive on new drive so you will get empty space at the end since this drive is bigger then the old drive
5 boot with gpart live cd and resize second partition to fill all empty space you’ve got
6 reboot, windows gonna run chkdsk and you’ll be able to login after that
entire process takes about 4 times less time then trying to copy it with gpart alone

P.S. Might be useful to add to the article… Before you delete your original VDI file, it’s worth getting rid of any snapshots associated with that vdi. Otherwise, VirtualBox gets in a bit of muddle and refuses to remove references to the original VDI.

Thanks for the instructions. I used this as a reference for my Mac-based VB installation. One variance that matter was when creating the ntfs clone in gparted. Your illustration shows “Round to Cylinder” However, the version of gparted I used let me choose between “Cylinder” “MiB” and one other choice. I first chose “MiB” and that rendered a useless partition.

Thank you for this article, and indeed for the helpful comments and contributions from others.

With the help of this I’ve successfully resized my Windows 7 x64 system drive from 20GB to 200GB (on an OSX 10.6.5 host). I did need to insert the install disk to perform a repair, but it was as trivial as selecting ‘repair’ and rebooting.

It’s a shame that Sun/Oracle/Virtual Box development team don’t build a process in to package to allow a little more dynamicity of disk volumes: until they do, this page has been the most helpful by far.

Thanks for the post. Although I’m a noob, i did succesfully went through all the steps except the ‘Completing re-sizing’

I am puzzled by whether I should delete the whole old Xp or the newly created one (I’m running windows xp in the virtualbox). I deleted the original hard diks (.vdi file). But now i still have two XP’s. One is the old one now extended with the bigger hard disk. And one is the new one, made to serve as slave. Both are working and have the big size. Does it matter which one I remove? Just to be sure I don’t mess up now it’s almost done.

Great write-up! I followed your instructions and was able to increase the size of my virtual disk from 10GB to 30GB. The current version of gparted was slightly different from your description, but it was easy enough to figure out. Thanks so much!

Hey, thank you sharing this post, this has helped me resize my virtual HDD without any flaws! By the way, I think there is a new option (VBoxManage modifyhd –resize) in the Virtualbox CLI tools which permit to resize the virtual disk directly. But as of version 3.2.8_OSE (under Ubuntu Maverick), this option is not available. So again thank you for this nice howto!

I write many step-by-step tutorials and read even more. Your guide on expanding a VirtualBox VDI is simply excellent! Even though gparted has changed a little bit since you wrote the guide, you still provided ample information that it was easy for the reader to remain on track and complete the task of resizing the drive. I am sure I will use this guide often in the future and appreciate your taking the time to figure it out an post it.

This problem is similar to the problem described in the FAQ where resizing your Vista boot partition renders your OS unbootable. In this case the Vista install media can be used to repair the problem.

I found that resizing a a Windows 2008 Server boot partition produces the same result. Booting off of the install media does not give you a menu that allows you to fix the problem however. What you get is a menu to bring up a command prompt or run memory diagnostics.

To fix the problem you need to select the command prompt option. From there change into the X:sourcesrecovery directory. There you will find a program called StartRep.exe. Run this program and your boot partition will be fixed and life will be good again. big_smile

Hi,
My few cents regarding Windows 2008 R2 guest machine – just a while ago I’ve expanded boot drive from 40 to 120 GB.
GParted operaton failed, because Windows has partitioned the drive in two chunks, one 100 MB , second 40Gb less 100MB. Copying both chunks with GParted methid failed, possibly by my mistake related to boot flags on wrong chunk…

Anyway:
My (very hard-core) solution was to use “dd if=/dev/sda of=/dev/sdb bs=512″ to duplicate the disk sector-by-sector instead of using GParted. Windows machine booted like charm, the only thing remaining was to extend volume using Windows Storage Manager (from Server manager console). Takes a bit longer than GParted, but worked for me.

Aside note is – few months ago I converted real machine to virtual one and discovered, that Windows Vista (and probably Windows 2008 too) does store crucial boot information… outside of the areas described by partition table information. No comments about, I would not believe, but have seen it by my own eyes. Proven. No tools were able to migrate real machine to virtual one. The only successful tool was…. “dd”. First, performed sector-by-sector copy, than later, used anything else to convert image to VHD/VDI/whatever. Went excellent.
I’ve lost a week trying numerous tools, before I discovered, that there is information outside of the partitions defined….

Anyway – thanks for posting. Your instruction is very good and does work excellent with Windows 2003 server quest. I just expanded another machine using your instruction. Just failed on 2008 R2 and had to go with “dd”.
Kind regards
Emil

I have a Windows 7 x64 system. I followed all the instructions. After restarting the system Windows refused to boot saying something like: “Hardware changed”. I shut the VM down, mounted an existing Windows ISO image and restarted the VM. Then I went to “Repair installation”. Windows installation program searched for existing installations, said that I had boot problems and offered to fix them automatically. I confirmed and after a couple of minutes it fixed the installation, restarted the VM and started chkdsk. Everything works now.

Thank you very much, Nick!

Advice:

1. Do a backup before the start. You can export appliance (requires less space but more time) or copy the whole VM.
2. Delete snapshots if you have them.
3. If you are paranoid do a backup again. Just in case.
2. Make sure that you have enough time.
For example in my case (AMD Phenom 9850 x4, RAM 8G, x2 7200rpm HDDs so my copy operations are faster than usual, VM size: 30Gb -> 80Gb):
– Export appliance: 20 min.
– Delete snapshots: 1 hour. I had four snapshots and it took from 15 to 20 minutes to delete each of them. 4 * 15 = 60 min
– Do a backup again: 10-20 min. I am paranoid. This time I copied the whole VM and it took 10 min.
– gparted operations ~30..40 min
– Windows restore and chkdsk activity: ~15 min
Total: 2h 15m to 2h 35m.

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.

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.