How to increase the size of your VirtualBox HDD WITHOUT losing any Data
February 14, 2014
Has it ever happened that you are working on a dev Virtual Machine and suddenly you realised that you have run out of hard disk space ? And then, you had to do the tedious job of manually transferring all your changes, repos, config files, etc. to a new VM ? Well, in this post, I am going to tell you how to increase the size of your Virtual HDD, whether the HDD is in the native VDI format or a different format like VMDK, without losing ANY data and continue your work.
The steps mentioned in this post are for *NIX OSes like Linux and Mac OS. I have personally performed this on the Mac OS X 10.9 (Mavericks), but the exact same steps are applicable for Linux. I guess, there should not be any different for Windows either.
A quick background : The reason I wanted to increase my Virtual HDD size is because I ran out of space trying to download the Android source code. That’s right, I am trying to build Android from scratch and after downloading 17GB of source code from the Cyanogenmod github repos, over a period of 5 days, I suddenly ran out of disk space. The Android story is for another day, another post.
As mentioned earlier, I have performed this on a Mac OS X Host machine with Ubuntu 12.04.3 (32-bit) as my Guest OS. The VM had a mere 20GB HDD and that too I had configured as a VMDK disk as opposed to the native VDI (Don’t ask!!)
So, without further ado, here’s how the magic happens :
Step 1a) : Backup your disk
Can’t stress this enough. Always, …. Always backup your disk. If I had to download 17GBs again, I would have to kill someone first to justify the action.
Now, you can’t just “cp” your virtual HDD file and hope it will work with your VM. There’s a whole lot of UUID issues with VirtualBox. To cut a long story short, don’t “cp”. Execute the following command instead :
$ VBoxManage clonehd <path to>old_ubuntu_hdd.vdi <path_to>backup_ubuntu_hdd.vdi
With this, VirtualBox assigns a different UUID to the backed up vdi and you can use this HDD as if it were your old one.
Now, if I were you, I would do just that. To be on the safe side, Release your current HDD from the Virtual Media Manager GUI tool.
Then go to your VM’s settings and add the newly backed-up VDI as the HDD for your VM. Then boot into it and verify that everything looks good.
Ok, We’re done backing-up. Unless, you are like me and had configured your virtual HDD in Vmware’s VMDK format.In that case, you need to execute Step 1b) below rather than Step 1a) above.
Step 1b) Convert your disk to VDI and backup your disk
Actually, it’s exactly like Step 1a) above ,except that you have an additional parameter to VBoxManage. Execute the following command :
$ VBoxManage clonehd <path to>old_ubuntu_hdd.vmdk <path_to>ubuntu_hdd.vdi --format VDI
Again, as above and especially after the conversion, make sure that you bootup the backed-up HDD and ensure that you can everything is working.
With our HDD safely backed up, lets tread on to more dangerous waters !!
Step 2) Increase the size of your Virtual HDD
This step is actually the easiest step. Just execute the following command :
$ VBoxManage modifyhd <path_to>Ubuntu_Hdd.vdi --resize <size in MB>
For example, in my case , I wanted to increase the size to 100GB, so the command was something like this :
$ VBoxManage modifyhd ./AndroidDev.vdi --resize 102400
That’s it !! We are done increasing the HDD. You can check this my opening the VirtualBox GUI tool , selecting your VM, clicking on Settings, then Storage and checking-out the value of the “Virtual Size:”
In my case it shows 100GB.
Forward march !!!
Step 3) Increase the size of the Linux Guest OS partition
With the above step, if you try to boot, you might either get an error, or see the same disk size from the Guest OS. What we need to do is resize the filesystem of the GuestOS to match the size of the partition or HDD we just increased.
For this, you need another Live Linux CD/DVD/iso like Ubuntu or GParted. Actually, we are going to use the gparted utility to do the resizing. So, as long as you have any OS which has this tool, it’s ok.
Now, add the Live Linux CD/DVD/iso to your VM and boot into.
Fire up Gparted session and resize your filesystem
That’s it !! That’s truly, finally it !!!
You have successfully resized your virtual hard disk keeping intact all your data.
I, have successfully resumed downloading the Android source.