Extend a CentOS Oracle VirtualBox Image with more Disk Space

virtualbox logoUPDATE: A lot has change over the years. I post a lot on medium now (bookmark it here, no excuse!). Also I have 80+ free webcasts on youtube (make sure to subscribe). Then all my slides went to speakerdeck. Yes. A lot is about THE cloud these days :-). For news: twitter.

This started as a simple note to myself. Hopefully good enough for me to reuse it one day. Over time it turned into a post that made a lot of people happy (see comments section) because it really sucks when you run out of disk space in a VirtualBox.

So the following instructions will probably only give you a rough idea if you found this page looking for help on Google.

Anyway, these steps worked fine for me. What a bliss not having the file systems 98% full when starting a project. Somehow, I always run out of disk space when I install SOA Suite. No matter how big I initially size it.

The challenge certainly is to get VBoxManage, fdisk and lvm right without completely messing up your system.

Warning!

fdisk and to a lesser extend lvm are razor sharp tools that can easily cause bleeding wounds. So make sure you have a backup (<- repeat this last sentence after me).

On Host side (e.g. Windows 7 here)

VBox instance has to be shut down for the following steps.

Extend the VBox image

Run the from the command line on the host system. Specify the new size in MB.

Example 1 with host system windows:

C:\Users\frank>"\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd --resize 29696 "\01_work\30 vms\virt
box\CentOS 6.4 64b STUDENT\CentOS 6.4 64b STUDENT.vdi"

Example 2 with host system MacOS:

# change to dir with vbox-manage
$ cd /Applications/VirtualBox.app/Contents/Resources/VirtualBoxVM.app/Contents/MacOS
$ VBoxManage modifyhd --resize 55000 /Users/frank/Virtual\ Boxes/ProjectA/project-disk1.vdi

On Guest side (e.g. CentOS 6.4 here)

Stay calm

Double check that you are on the guest side. Runing fdisk accidentially on the host side can destroy your whole computer, whereas running it on the guest side typically reduces the risk to destroy your virtual image only.

Note that this is not a kindergarten party, you have to run the commands with root privileges.

fdisk

Use fdisk -l to list devices, then e.g. fdisk /dev/sda to add another primary partition (command n), next free number (e.g. 3), of type 8e (Linux LVM) (t), print part table (p), write table (w). Reboot…

Add new partition as physical volume (PV)

[root@ccloud12 ~]# lvm

lvm> pvcreate /dev/sda3
 Physical volume "/dev/sda3" successfully created

Extend existing volume group (VG)

lvm> vgextend vg_ccloud12 /dev/sda3
 Volume group "vg_ccloud12" successfully extended

Find out partition name

lvm> lvdisplay
 --- Logical volume ---
 LV Path /dev/vg_ccloud12/lv_root

Extend logical volume

lvm> lvextend -L+8.48G /dev/vg_ccloud12/lv_root
 Rounding size to boundary between physical extents: 8.48 GiB
 Extending logical volume lv_root to 25.56 GiB
 Logical volume lv_root successfully resized

Resize the File System (Guest System)

resize2fs -F /dev/vg_ccloud12/lv_root

That’s it

Try df -h and enjoy your new diskpace.

Let me know if you had any success eg. using Oracle Enterprise Linux. Any super secret tips how to improve or shorten this are welcome!

Note: now I am using Mac OS as a host system, it works the same way.