четверг, 27 декабря 2012 г.

Resizing existing virtual disk in Virtual Box


Today I had to resize the existing virtual hard drive on the virtual machine with all stuff on it second time in my life. Unfortunately, the first time was some time back and I didn't bother to make notes for myself, so this time it again took me more than 1 hour to get it all done. This process sounds like something really simple and easy, something thousands of programmers probably want to know how to do, so you may assume all required steps are well-known, ordered, and show up in Google when you search for something like "virtual box existing disk resize". However, most of the instructions found are either wrong or incomplete, so it may take quite some time for googling before you really get it done.

So so I want to share it here in case.  Steps below were tested with Centos 6.3 as guest OS and Windows 8 as host OS (this time, and before I used to do it with Centos 6.2 as guest OS and Ubuntu 12.10 as host OS).\

1) Resize the physical file containing virtual hard drive in host OS:

 - on Linux host just run something like:

sudo VBoxManage modifyhd /.../mydrive.vdi --resize 20480

where last number is size in MB.

On Windows host, do something like:
C:\Program Files\Oracle\VirtualBox>VBoxManage modifyhd D:\VM\Ubuntu\Ubuntu.vdi --resize 20480

2) Allocate the space on the virtual hard drive using GParted or similar tool.

Download .ISO image of it from http://gparted.sourceforge.net/download.php, mount it in your VM settings as a live CD, load from it, then resize the needed partition (most likely it will be root partition you need to enlarge, something like /dev/sda2 or so)

3) Load the guest OS and resize the partition within LVM group (you'll need to find exact LVM device used for mapping! this is just example how it is on my Centos 6.3 VM):

sudo lvm lvresize /dev/mapper/vg_<myhostname>-lv_root –size 20G

4) Resize the filesystem on partition:

sudo resize2fs -p /dev/mapper/vg_<myhostname>-lv_root 20G

Now, run df command and you are all set- should see new size as output. 

Hope if will help someone.