Increasing the size of a root partition on a Linux VM

Posted 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

Nothing yet..be the first to share wisdom.