Friday, January 27, 2012

Resizing VirtualBox and Linux Partitions with GParted

VirtualBox 4 has an option to modify the logical size of the virtual disk after it's been created without affecting the physical size. The size specified in this option is the new overall size of the disk not the increment. If the current size is 100MB and resizing to 200MB would make the disk 200MB not 300MB (100 + 200). Once the size is increased it cannot be decreased (yet).

Current size of the disk
-rw------- 1 root root  18G Jan 27 16:43 rac4.vdi
The size of disk seen by the OS using this virutal disk
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Size of the partitions
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              16G   13G  1.8G  88% /
/dev/sda1              99M   12M   83M  13% /boot
tmpfs                1006M     0 1006M   0% /dev/shm
Increase the size with modifyhd option
VBoxManage modifyhd rac4.vdi --resize 40960
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Physical size is unaffected. This will grow as the space is used.
-rw------- 1 root root  18G Jan 27 16:45 rac4.vdi
But the size of the disk seen by the OS has changed
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
The additional space is unallocated and linux partitions are not affected after this increase.
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              16G   13G  1.8G  88% /
/dev/sda1              99M   12M   83M  13% /boot
tmpfs                1006M     0 1006M   0% /dev/shm
Using the Gnome Partition Editor (GParted) it is possible to resize the linux partition and allocate the unused space to it effectively increasing the size of it.

Download the Live CD iso of gparted and boot the virtual box server from it.

Select the default option and complete the startup.



GParted shows the size of current partitions (15.9 GB) and the unallocated space (20 GB).

Right click on the partition to resize and select resize/move item from the popup menu.

Resize the partition by either dragging the highlighted box or adjusting the numerical spinners.



Click apply to complete the resizing.



Restart the virtual server and see the size of linux partition changed.
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              35G   13G   21G  39% /
/dev/sda1              99M   12M   83M  13% /boot
tmpfs                1006M     0 1006M   0% /dev/shm
Size of the new partition is 35 GB (15 GB previously allocated and 20 GB added with GParted).