Sunday, May 26, 2013

How To fix the Out Of Space Problem for Virtual Box with unused large hard disk space

Increasing the size of a VirtualBox disk has now become much easier than before. It is directly supported in VirtualBox 4.0+.  Sometimes the VirtualBox has been allocated huge hard disk space but the storage does not seem to expand automatically even if we have specified the "dynamically allocated storage" option.

This occurs mainly because main partition is followed by a smaller Linux SWAP partition.  This small partition behaves like a boundary between the main partition and the empty unallocated space after the SWAP partition.

The VirtualBox is not smart enough to allocate more memory to the main partition by skip this small partition.  In such scenario we have to move the swap partition a little bit and allocate more memory to the main partition manually.

In this post we are going to look at a way in which we can manually allocate more memory to the main partition using a tool called GParted.

General Disclaimer: Before doing anything mentioned in this post please take a backup of your virtual machine, because if something goes wrong then it can cause a lot of damage to your data and Author cannot be held responsible for that :)

How Do They Do It?

These are the steps involved to manually allocate more space to the virtual hard drive.

Resize the logical capacity of the virtual disk if required

This step is only required if your logical capacity of the virtual disk is smaller than what you actually want.  VirtualBox 4.0+ now directly supports resizing the logical size of the virtual disk. Run the following commands on the host OS to resize the logical capacity of the virtual disk
This command should show you details about the virtual disk.

As we can see the current logical size of the disk is around 108 GB.  Let's increase the logical from 108 GB to around 500 GB.  Run the following command to achieve this.
Remember that the parameter to the --resize option is the new size in MB's its not the delta but actually the new size in MB's.

After running this command if we run the showhdinfo command again we should see the change has taken place
As we can see the logical size of the virtual disk has been increased from around 108 GB to 500 GB.  We have achieved what we intended to do in this step.  Let's move on with the next step.

Download and mount the GParted ISO

Download the GParted ISO from here.  We don't need to burn it to a disk just keep the ISO download to the host OS.

Open the VirtualBox GUI and open the settings of the desired VM.  Under the Storage section click the "Add CD/DVD device" option.  A dialog will open up asking whether you want to keep the CD/DVD drive empty or choose an ISO to mount.  At this step click "Choose Disk" and mount the downloaded GParted ISO.
Adding the CD/DVD Drive to the VM
Now navigate to the System --> Motherboad section of the Settings dialog and make the CD/DVD as the first option in the Boot Order.  This will make sure that the GParted will be given an opportunity to load before The Guest OS is booted.
Making Sure that Boot from CD/DVD is the first option

We are all set, now lets do the actual resizing.

Boot the GParted

Now start the VM, since the GParted ISO is mounted on the CD/DVD drive, it will show us an option to Boot the GParted Live.  Just press enter on "GParted Live (Default Settings)".

The GParted Live Boot Menu
After a few anxious moments you should see a screen similar to this one, do not change anything just click OK
Dont change anything on this screen, Click OK
Next screen lets you select language (English is selected by default).  If you want to change it then enter the number against the language name to choose that language and then press Enter.
Choose Language
Next screen will let you choose the mode in which GParted will be shown.  Unless you call yourself some sort of a Linux Ninja choose the "(0) Continue to start X to use GParted automatically" option which is selected by default.
Mode for start GParted, select 0
 If everything goes as per plan you should see a screen similar to the one shown below. 
The Home screen of GParted
This should mean that we are all setup to actually do the resizing of partitions.

Resizing the main partition

As show in the image the main partition is about 39 GB, its followed by a smaller Linux SWAP partition.  After the Linux SWAP partition there is a huge unallocated space.  Our next task is to increase the size of the main partition.  This cannot be done by simply resizing the main partition since its blocked by the Linux swap partition.  Then how should we go about it?  Well patience people, patience!

First we will need to move the SWAP partition a bit.  We have to move it in such a way that there is enough unallocated space between the main partition and SWAP partition.

Select the SWAP partition and Click the "Resize/Move" button at the top.  A dialog will be shown that will let us move the SWAP partition.
Dialog to resize SWAP partition
Using the Mouse move the SWAP partition towards the right size or change the value of "Free space preciding (MiB)" value to the desired value.
Moving the SWAP partition towards right

After you are satisfied with the size click the "Resize/Move" button.  This will show a warning dialog like this one, just click OK.
Warning informing abou the side effects of the moving SWAP partition
Now the partitions may look something like this
Notice the Free Space between Main and SWAP partitions.
 As we can see there is now some unallocated space between the main partition and SWAP partition.  This unallocated space can now be taken up by the main partition.  Click on the main partition and then click the "Resize/Move" button.  
Now there is space to expand the Main partition.
This time around drag the right size of the partition all the way so that it occupies the entire unallocated space.
Expand the Main partition to occupy the entire unallocated space
Click "Resize/Move" and click OK on the warning dialog.
Once you come out of the Resize dialog, the disk should look something like this
Updated disk sizes, notice that the Main has expanded
Click Apply.  This might take a while depending on how much data is already present in your main partition and how much do you want to expand it.  It might take anything from 2 minutes to 2 hours, be patient, grab a cup of coffee or grab some lunch.
Doing the processing when Apply is clicked
Once this completes you should see the summary of the changes done
Done Applying the Change
That's it, we have now resized our main partition.  All that is left to do is Click Exit on the GParted menu and shutdown the VM.
Shutdown GParted
Before rebooting the VM change the Boot Order again so that Hard Disk is the first option.
Making Hard Disk as the first option to boot from
Once the VM is booted you can check the disk size using the following command
That's all folks!  Until we meet again, Have Fun!

No comments:

Post a Comment

Have some Fun!