VirtualBox: Increase Size of RHEL/Fedora/CentOS/Scientific Guest File System

Tired of running out of space on your VM and running through complicated steps in order to get your VM resized?  This article provides the steps to take without you having to boot live CDs, clone VDI’s, or anything else complicated or time consuming.  We will simply leverage VirtualBox manager commands on our host and system commands on the guest.

First, shutdown the VM you plan to work on.  Then we will run the manager tool. the

cd /path/to/vm
VBoxManage modifyhd vm.vdi --resize <size>
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

The <size> parameter should be the desired size to make your VDI, in MB.  For example, if you wish to resize your 10GB disk to 25GB, then you would use 25600 (1024*25). This should resize the VDI appropriately, however, if you boot up your VM, you will soon see that you are still at your original disk space.

cd /path/to/vm
VBoxManage modifyhd vm.vdi --resize 25600
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

If you open a partition tool, such as gparted, you can easily see that there is the extra available disk available, but it is set to unallocated.  If your guest OS is using LVM (this is all of the Red Hat flavors), then you cannot simply extend the volume as it will be locked.  Other volume managers may let you resize the partition.  Some may let you if you boot from a live CD.

With LVM we can actually make all these changes to the logical volume on a live file system.  It never hurts to backup your filesystem, or at least anything important, but it is not required.

The following steps will allow you to resize your partition.  In a terminal run the following commands in bold (you need root permissions to run fdisk):

  • df
    • Take note of the logical volume mapping (ex. /dev/mapper/fedora-root)
  • fdisk -l
    • Take note of the filesystem partition of your physical volume sits (ex. /dev/sda2)
  • fdisk /dev/sda
    • Run fdisk of this phyisical volume
  • d
    • Delete a partition
  • 2
    • Select your partition (we are using 2 from /dev/sda2)
  • n
    • Create a new partition
  • p
    • Make it a primary partition
  • 2
    • The partition number to make it on (same as we deleted)
  • <return>
    • Set the starting block (keep the default as it is usually correct)
  • <return>
    • Set the ending block (keep the default as it is fine for our use case)
  • w
    • Write the partition (will also exit fdisk shell)
  • reboot
    • We must reboot in order to have the new partition table loaded
  • pvresize /dev/sda2
    • Resizes the physical volume
  • pvscan
    • Use to verify the new size
  • lvextend -l +100%FREE /dev/mapper/fedora-root
    • Extend the logical volume to take all free space
  • resize2fs /dev/mapper/fedora-root
    • Resize the file system
  • df
    • See your newly sized volume

And that is it! With the last df command, you should see that your volume increased! In our example, let’s say you had 9GB of the 10GB disk and you resized it to 25GB.  In the first df command, you would have seen 90% full.  After the resize, you should see the system as only 36% full, thus verifying the resize success!

The commands are reposted below for easier reference:

df
fdisk -l
fdisk /dev/sda
d
2
n
p
2
<return>
<return>
w
reboot
pvresize /dev/sda2
pvscan
lvextend -l +100%FREE /dev/mapper/fedora-root
resize2fs /dev/mapper/fedora-root
df

 

 

Share on Facebook+1Share on Twitter Share
Posted in Programming Tagged with: , , , , , , , , , , , , , , ,
21 comments on “VirtualBox: Increase Size of RHEL/Fedora/CentOS/Scientific Guest File System
  1. edwin says:

    Excellent post, worked a treat!…lifesaver

  2. Alfredo says:

    It was a lifesaver for me too, thanks! :)

  3. Marc says:

    Thanks! Got me 95% of the way there. Ran into a problem when I tried to run the lvextend command:
    [root@centos-vm ~]# df
    Filesystem 1K-blocks Used Available Use% Mounted on
    /dev/mapper/VolGroup00-LogVol00
    6983168 6015536 607184 91% /
    /dev/sda1 101086 18068 77799 19% /boot
    tmpfs 257432 0 257432 0% /dev/shm
    [root@centos-vm ~]# lvextend -l +100%FREE /dev/mapper/VolGroup00-LogVol00
    Volume group “mapper” not found
    Volume group mapper doesn’t exist

    After some research, I found that I needed to format it like this:
    [root@centos-vm mapper]# lvextend -l +100%FREE /dev/VolGroup00/LogVol00
    Extending logical volume LogVol00 to 10.88 GB
    Logical volume LogVol00 successfully resized
    [root@centos-vm mapper]# resize2fs /dev/VolGroup00/LogVol00
    resize2fs 1.39 (29-May-2006)
    Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
    Performing an on-line resize of /dev/VolGroup00/LogVol00 to 2850816 (4k) blocks.
    The filesystem on /dev/VolGroup00/LogVol00 is now 2850816 blocks long.

    [root@centos-vm mapper]# df
    Filesystem 1K-blocks Used Available Use% Mounted on
    /dev/mapper/VolGroup00-LogVol00
    11046136 6017316 4458708 58% /
    /dev/sda1 101086 18068 77799 19% /boot
    tmpfs 257432 0 257432 0% /dev/shm

    And that was that. Thanks for the great writeup!

  4. gunfus says:

    Excellent! Excellent.. it got me what I needed using RH6.2 straight forward article.. just the necessary information, to understand what is happening and make it work

  5. david says:

    Great article! Thank you.

  6. Soren Lloyd says:

    Thanks, this worked perfectly. If you have trouble resizing your vdi then it is most likely due to it being a Fixed size disk. If this is the case you can clone it and then resize it. Cloning create a dynamic disk.

  7. Mlagadec says:

    Thank’s a lot you save my day !

  8. patagu says:

    Great!!! Thanks a lot!

  9. KB, Norway says:

    Thanks a lot! Although my setup is a CentOS client OS on a Win7 host, the info on this page saved me for a lot of work.

  10. Itay Avilevich says:

    Thanks a lot!
    Worked like magic in the most critical time for me…

  11. JamesH says:

    Helped me also. I was a bit confused on which partition I needed to delete, and how to tell if it should be sda1 or sda2. But sda2 was correct like you mentioned.

    Thanks for the crystal clear instructions. Worked like a charm.

  12. Nico says:

    Great, worked like a charm!

  13. Milind Kulkarni says:

    one of the best posts I have seen – clean, quick, crisp and working accurate. Precisely provides what is needed to increase the filesystem size and which works even for a layman without having to understand details of partition tables and LVM.
    I cant thank you enough,great stuff !

  14. Owieboy says:

    Thanks a lot! This worked. I was stuck for hours trying to figure out why after resizing my vdi to 30GB it doesn’t reflect on on the actual size and still shows 3GB of free space. The instructions are clear and easy to follow.

  15. Mike Westaway says:

    Awesome!

    Many thanks – that was tricky stuff perfectly explained.

    Mike

  16. adamf says:

    Old post but very useful.

    For CentOS 7 etc you need:

    xfs_growfs /dev/centos/root
    not
    resize2fs /dev/mapper/fedora-root

  17. MarkW says:

    This works perfectly on Centos 7 as well, with one small change due to it using xfs. Instead of resize2fs, use: xfs_growfs /dev/mapper/centos-root

  18. Darío says:

    Thanks thanks thanks!! You are the best!

  19. Pod-Soubenn says:

    Thank you very much for this tutorial, and all comments !!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">