Tuesday, October 19, 2010

Enlarging/Expanding a VirtualBox HDD

I've been  using VirtualBox as a development host for quite some time now. But I've recently ran into a problem: the HDD that I defined wasn't large enough to install all tools I need (it ran out of 20G of space when I decided I needed to install management tools for SQL Server Express 2008).

So I've looked around the net searching for how to do this. First, I'm quite disappointed that VirtualBox itself doesn't accompany a tool that does that. Secondly, most of the solutions I found were complex or involved downloading large LiveCD images. Until I found this blog, which not only compiles many links to other solutions, but provides a simple one -- the one I used and recommend.

If you're brave enough (you must be; We are dealing with disk partitions here) all you have to do is:

  1. Download GParted LiveCD (~150MB) from here.
  2. Define a new HDD in VirtualBox and set it to be your secondary partition.
  3. Mount the live cd as the primary IDE controller. Make sure the BIOS in your Virtual Machine is configured to boot from the CD first (and HDD next).
  4. Boot GParted in command line (shell) mode.
  5. Use fdisk -l to identify the old disk and the new one. Be careful! 
    1. Usually, your original partition will be /dev/hda*, where * is a number. It can also be /dev/sda*. The new partition can have another number (higher one, in most cases) or be something like /dev/sdb.
    2. Remember that GParted will have a partition of its on, mounted on /. Use mount to list mounted partitions.
    3. If you try to mount a unpartitioned hdd mount will tell that you need to inform its file format. That is a good clue you are talking about the right partition. 
  6. Once you identified the source and target devices, all you have to do is dd if=[source] of=[target]. It will take a long time, since its doing a byte-by-byte copy of your partition. 
    1. Assuming your source device is /dev/hda2 and your target is /dev/hdb, the command above should be: dd if=/dev/hda2 of=/dev/hdb

Once the copy is over, all you need to do is shutdown the system, map only the new drive as the main HDD of your VM, and boot it up. 

Good luck.