Increasing the size of a root partition on a Linux VM

Last updated on 12th March 2018

When you create a new virtual machine in Oracle VM VirtualBox, most often you create it with the default settings which means you'll have a virtual hard disk of size anywhere between 2GB - 12GB depending up on the version of Linux that you selected. Usually this default disk size will be just enough to do a full install of your selected operating system. However if you have to install several additional packages then you'll run out of space in your root volume.

For example, if you select Red Hat as the Linux version then the default size of your virtual disk will be 8GB. Then when you install a Linux Operating System on this virtual machine with default partitioning scheme you'll have around 6GB of space on your root partition (/) and remaining space taken by boot and swap partitions. Typically 6GB root partition could be just enough for a full install of Red Hat / CentOS operating system but if you want to install additional software on top of it say, Oracle client, Node, meteor, React etc. finally you will reach a situation where there is no more space left on your filesystem. This article shows you how to increase the size of the LVM root partition on a Linux Virtual Machine running on Oracle VM VirtualBox.

Warning!! Incorrect use of fdisk utility and LVM commands may result in data loss or may even damage the entire virtual disk. Remember to save a backup copy of your Virtual Machine disk files before you make any changes.

Step 1: Extend the virtual disk file size

  1. Open a command prompt and navigate to the Virtualbox installation folder.

    cd "c:\Program Files\Oracle\VirtualBox"
  2. List all virtual hard disks and take note of the Location of the disk you want to resize.

    c:\Program Files\Oracle\VirtualBox> vboxmanage list hdds
    UUID:           3d0ed16a-0649-4f61-9ecc-5a86c42b6f53
    Parent UUID:    base
    State:          created
    Type:           normal (base)
    Location:       C:\Users\<user>\VirtualBox VMs\<vm>\NewVirtualDisk1.vdi
    Storage format: VDI
    Capacity:       8192 MBytes
    Encryption:     disabled
    
  3. Resize the disk.

    c:\Program Files\Oracle\VirtualBox> vboxmanage modifymedium disk "C:\Users\<user>\VirtualBox VMs\<vm>\NewVirtualDisk1.vdi" --resize 3000
    

    The size is specified in MB.

  4. Verify disk size.

    c:\Program Files\Oracle\VirtualBox> vboxmanage list hdds
    UUID:           3d0ed16a-0649-4f61-9ecc-5a86c42b6f53
    Parent UUID:    base
    State:          created
    Type:           normal (base)
    Location:       C:\Users\<user>\VirtualBox VMs\<vm>\NewVirtualDisk1.vdi
    Storage format: VDI
    Capacity:       11192 MBytes
    Encryption:     disabled
    

    Note:To be able to resize a disk it should be created as a dynamically allocated disk. You cannot resize of virtual disk that is created as a fixed size disk.

    To convert a fixed disk to dynamic, run

    c:\Program Files\Oracle\VirtualBox> vboxmanage clonemedium disk "C:\Users\<user>\VirtualBox VMs\<VM>\NewVirtualDisk1.vdi" "C:\Users\<user>\VirtualBox VMs\<vm>\NewVirtualDisk12.vdi" --variant Standard
    

Step 2: Add new partition

List disk devices and partitions.

# fdisk -l
 Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    16777215     7339008   8e  Linux LVM

The above output shows there are two partitions on the disk /dev/sda. To add a new partition, run

# fdisk /dev/sda

In the fdisk command prompt,

  • Enter n to create a new partition.
  • Enter p to create primary partition.
  • Press ENTER to accept default for first sector.
  • Press ENTER to accept default for Last sector. This will create a partition with all the remaining available space on the disk. Alternatively you could specify the size, for example +2G will add a partition of size 2GB.
  • Enter t to set the type of the new partition to Linux LVM.
  • Select partition number 3.
  • Enter 8e as the Hex code.
  • Finally enter the command w to save the changes to disk and exit fdisk.

Step 3: Reboot the Virtual Machine and Initialize LVM

To initialize LVM on the newly created partition, run

# pvcreate /dev/sda3
Physical volume "/dev/sda4" successfully created.

Step 4: Add partition to volume group

To do this, first List physical volumes and note the VG Name from the output.

# pvdisplay
 --- Physical volume ---
 PV Name          /dev/sda2
 VG Name          centos
 PV Size          <7.00 GiB / not usable 3.00 MiB
 Allocatable      yes (but full)
 PE Size          4.00 MiB
 Total PE         1791
 Free PE          0
 Allocated PE     1791
 PV UUID          M46abM-BMhT-LSGm-sxGb-8wf8-lSV4-fjtei3

"/dev/sda3" is a new physical volume of "7.53 GiB"
 --- NEW Physical volume ---
 PV Name           /dev/sda3
 VG Name
 PV Size           7.53 GiB
 Allocatable       NO
 PE Size           0
 Total PE          0
 Free PE           0
 Allocated PE      0
 PV UUID           cFRLij-grti-jBeb-1OT6-4qRs-vTo8-Dhp5xy

Extend the root volume group.

# vgextend centos /dev/sda3
  Volume group "centos" successfully extended

Step 5: Extend the root logical volume

Identify the path for the root logical volume.

# lvdisplay
 --- Logical volume ---
 LV Path                /dev/centos/swap
 LV Name                swap
 VG Name                centos
 LV UUID                zhvC0n-EZ3P-Xec6-k08e-TFRh-95HQ-gVRf8m
 LV Write Access        read/write
 LV Creation host, time localhost, 2018-02-13 10:18:54 +0000
 LV Status              available
 # open                 2
 LV Size                820.00 MiB
 Current LE             205
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     8192
 Block device           253:1

 --- Logical volume ---
 LV Path                /dev/centos/root
 LV Name                root
 VG Name                centos
 LV UUID                0wEuQA-DSzu-1Syp-596R-T3Wb-O9Ld-DlMZJc
 LV Write Access        read/write
 LV Creation host, time localhost, 2018-02-13 10:18:55 +0000
 LV Status              available
 # open                 1
 LV Size                <10.10 GiB
 Current LE             2585
 Segments               2
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     8192
 Block device           253:0

Extend the logical volume and resize to filesystem.

# lvextend --size +2G --resizefs /dev/centos/root

Step 6: Verify disk size.

To verify the size of root volume, run

# df -h 

Post a comment

Comments

Robert | August 22, 2023 6:36 PM |

You are the best!. Been stuck for a full day searching for a solution. All Steps worked perfectly!

Herve | March 25, 2022 4:26 PM |

Step 2 to 6 work great with vsphere / vmware 6 after extend vmdisk size

White | December 28, 2020 3:40 AM |

Before step 5, it would be better to verify the free size of the new volumn by executing pvs.

Phoo Phoo | December 2, 2020 9:33 AM |

thank you. you saved my day.

D.F. | November 28, 2020 1:22 PM |

Thanks for this tutorial. Better command for step 5 (tested in CentOS 7): lvextend -l +100%FREE --resizefs /dev/centos/root This will automatically add all the free space to "root".

Francois | July 1, 2020 9:42 AM |

Thanks a lot. Considerable time saved

foxescross | November 14, 2019 7:00 PM |

Helpful - thanks. Recommend re-writing the windows bit into a powershell script.

AB | September 17, 2019 6:51 PM |

# pvcreate /dev/sda3 Physical volume "/dev/sda4" successfully created. Why sda4, not 3?

kiran | September 18, 2019 11:31 AM |

/dev/sda3 might be a symbolic link to /dev/sda4 ?

paul peterson | June 18, 2019 11:05 PM |

awesome article , thank you for posting this on internet. very useful one. issue resolved. Done.

halan | May 28, 2019 5:14 PM |

very usefull

GB | May 13, 2019 2:59 PM |

Worked perfectly

Sean | April 26, 2019 12:49 AM |

Wow. This is exceptional!!!

LinuxUser | October 10, 2018 8:47 AM |

I did Step 1 through Virtual Box GUI. Then followed the other steps. This worked to expand my /