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.

Comments

  1. you forgot the final command : resize2fs -F /dev/vg_ccloud12/lv_root

    thx !

  2. Awesome, worked like a charm and I am all new to Linux. Thanks for the post!

  3. Super, great post :), It was very helpful!

  4. SuperUser says

    Great job! Thanks for the info. After using fdisk you can invoke system-config-lvm and initialize the new partition, add it to the existing volume group and edit properties of the existing logical volume. That will re-size your file system also.

  5. Amazing… It worked..thanks

  6. Thanks. Perfect.

  7. Thank you very very much. This was very helpful.

  8. Awesome! After having read a lot of stuff that didn’t work, THIS finally worked!
    You saved me. I’m in an importan customer project.
    *tumbs_up* 🙂

  9. Thank you so much for this post. It helped me a lot!!!

  10. You saved my day man..! Thanks a lot from the heart.!

    I had setup development server, perfectly working steps to increase LogicalVolume of Linux Virtualbox HDD.

  11. vladimirfp says

    thanks a lot Man!

  12. Thanks! This is the only LVM resize recipe that worked for me on VirtualBox 4.3.

  13. Thanks so much for the article.
    3 elements of note from my experience tho I was able to get this working.
    Note, I’m recently returning to Linux so am very rusty but am suspect people using this guide are in the same boat.

    Firstly, most of the commands gave me permission errors but worked with sudo.

    Second, “vg_ccloud12” is the name of the existing volume group.
    Users should replace that with whatever their volume group name is. This can be found using vgs.

    Lastly, resize2fs errored for me.
    If it does for anyone else, I recommend you read this (http://stackoverflow.com/questions/26305376/resize2fs-bad-magic-number-in-super-block-while-trying-to-open) and subsequently try using xfs_growfs (“xfs_growfs /dev/vg_ccloud12/lv_root” in the above example).

    Maybe some of this stuff is trivial but wanted to comment since the rest of the article was spot on perfect.
    Hope that helps someone and thanks again!
    JD

    • thanks for your comments JD!

    • Hi,
      I followed the steps, but at the end after typing resize2fs -F /dev/vg_ccloud12/lv_root
      I got an error. Indeed xfs_growfs saved my struggle . Thanks for the blog @frank and thank you for the comment @JD.
      Best,
      Maher

  14. On Host side you used Windows 7 as an example. But my Host is Ubuntu (and Geust CentOS). Can I use the same steps abve to expand my CentOS VM? Thank you!
    Philip

  15. @frank, Thank you so very much! You rock!

    @philip, I just did this using Kubuntu 15.04 as the host system and Fedora as the guest. It will work no problem. The bulk of the operation here involves running LVM commands in the guest system, so it’s basically agnostic about what the host system is.

  16. For what it’s worth (and your procedure is perfectly fine), there’s another way to do this which I prefer. I shutdown the VM, create a second virtual hard drive attached to the machine, boot it back up and lsblk to verify the OS saw it. From there you can pvcreate the new disk and vgextend to add it to whatever volume group you want to add more space to, and the procedure is the same from that point. No need for messing with fdisk or resizing the original VHD.

  17. Developer says

    Thanks. I was able to use it.

  18. Thanks!!!! Saved me from reinstalling a whole lot of complicated software! gotta love VirtualBox 🙂

    • Daniel,

      this is one of my top 3 posting that I regularly look up and follow *myself* to extend VirtualBoxes 🙂
      Sometimes I wonder if somebody couldn’t inject some magic and make this much easier.

      Good to read it helped you and thanks for leaving a comment!

  19. in case of
    lvm> lvextend -L+8.48G /dev/vg_ccloud12/lv_root

    I use : lvextend -l+100%FREE /dev/VolGroup/lv_root

  20. Thanks for the post! It helped me!

Speak Your Mind

*