VirtualBox: Resize Linux Filesystem

This page specifically describes a way to address the following condition on an OEL 7 VirtualBox virtual machine where the "/" filesystem is on a Logical Volume, however the concepts should apply to other situations...

df -h | grep -v tmpfs


Filesystem Size Used Avail Use% Mounted on/dev/mapper/ol-root 17G 17G 447M 98% //dev/sdb 50G 4.0G 43G 9% /mnt/u01/dev/sdc 50G 53M 47G 1% /mnt/u02/dev/sdd 20G 1.2G 18G 7% /mnt/AHF/dev/sda1 1014M 330M 685M 33% /boot

i.e "/mnt/u01", "/mnt/u02" and "/mnt/AHF" are directly mounted disks (virtual disk images) whereas "/" is a filesystem on a logical volume that is nearly full.

Using the output of lsblk in conjunction with the df output from above we can see that "/" is a 17GB filesystem on a 17GB Logical Volume called "ol-root" which is on a 19GB partition called "sda2" which is on a 20GB disk...

lsblk


NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsdd 8:48 0 20G 0 disk /mnt/AHFsdb 8:16 0 50G 0 disk /mnt/u01sdc 8:32 0 50G 0 disk /mnt/u02sda 8:0 0 20G 0 disk ├─sda2 8:2 0 19G 0 part │ ├─ol-swap 252:1 0 2G 0 lvm [SWAP]│ └─ol-root 252:0 0 17G 0 lvm /└─sda1 8:1 0 1G 0 part /boot

How do we add space so we can install other software in "/opt", for example?

Logical Volume Manager

Stop VirtualBox Machine

Identify HDD to Modify

vboxmanage list vms


"OracleLinux7" {795d8e0f-8c8b-46ca-a29e-1f10be15daa4}

vboxmanage showvminfo OracleLinux7 | grep UUID


UUID: 795d8e0f-8c8b-46ca-a29e-1f10be15daa4Hardware UUID: 795d8e0f-8c8b-46ca-a29e-1f10be15daa4SATA (0, 0): /media/${USER}/HDD1/VirtualBox/OracleLinux7//OracleLinux7/OracleLinux7.vdi (UUID: 8fa057cf-3a16-445c-8157-5850da18f7f6)SATA (1, 0): /media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/u01.vdi (UUID: b7db77ab-4d4f-452c-9e49-c1d5dea44e25)SATA (2, 0): /media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/u02.vdi (UUID: 84864b53-0741-42e1-92aa-fc7164e03f48)SATA (3, 0): /media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/AHF.vdi (UUID: c24f7088-09cd-4ecc-b0ba-ba4a88d1522a)

In this case we can infer that "u01.vdi", "u02.vdi" and "AHF.vdi" map directly to "/mnt/u01","/mnt/u02" and "/mnt/AHF", therefore we can focus on "OracleLinux7.vdi"...

vboxmanage showmediuminfo disk /media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/OracleLinux7.vdi


UUID: 8fa057cf-3a16-445c-8157-5850da18f7f6Parent UUID: baseState: locked writeType: normal (base)Location: /media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/OracleLinux7.vdiStorage format: VDIFormat variant: dynamic defaultCapacity: 20480 MBytesSize on disk: 17731 MBytesEncryption: disabledIn use by VMs: OracleLinux7 (UUID: 795d8e0f-8c8b-46ca-a29e-1f10be15daa4)

Other useful commands...

vboxmanage list hdds

Increase HDD Size

We will increase from 20GB to 50GB...

vboxmanage modifymedium disk "/media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/OracleLinux7.vdi" --resize 51200

Start VirtualBox Machine

All of the following steps should be run on the target Virtual Machine as 'root'

Information Gathering

Using lsblk we can already see that the "sda" disk has increased in size to 50GB...

lsblk


NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsdd 8:48 0 20G 0 disk /mnt/AHFsdb 8:16 0 50G 0 disk /mnt/u01sdc 8:32 0 50G 0 disk /mnt/u02sda 8:0 0 50G 0 disk ├─sda2 8:2 0 19G 0 part │ ├─ol-swap 252:1 0 2G 0 lvm [SWAP]│ └─ol-root 252:0 0 17G 0 lvm /└─sda1 8:1 0 1G 0 part /boot

We can use fdisk to get further information...

fdisk -l /dev/sda


Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000e55cb
Device Boot Start End Blocks Id System/dev/sda1 * 2048 2099199 1048576 83 Linux/dev/sda2 2099200 41943039 19921920 8e Linux LVM

We can use pvs and pvdisplay to understand how /dev/sda2 is used by the Logical Volume Manager (LVM)...

pvs /dev/sda2


PV VG Fmt Attr PSize PFree /dev/sda2 ol lvm2 a-- <19.00g 0

pvdisplay /dev/sda2


--- Physical volume --- PV Name /dev/sda2 VG Name ol PV Size <19.00 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 4863 Free PE 0 Allocated PE 4863 PV UUID fliLf2-aAfr-hFC3-Aw6k-SdpT-9RI6-JsoJDV

We can see that /dev/sda2 is in the "ol" Volume Group and therefore we can use vgs and vgdisplay to get more information about that Volume Group...

vgs ol


VG #PV #LV #SN Attr VSize VFree ol 1 2 0 wz--n- <19.00g 0

vgdisplay ol


--- Volume group --- VG Name ol System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size <19.00 GiB PE Size 4.00 MiB Total PE 4863 Alloc PE / Size 4863 / <19.00 GiB Free PE / Size 0 / 0 VG UUID uUrPcu-lWgS-13Oo-sDK0-yJWK-X4yB-HICPuY

We can use lvs and lvdisplay to get information about the Logical Volumes in the "ol" Volume Group...

lvs ol


LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root ol -wi-ao---- <17.00g swap ol -wi-ao---- 2.00g

lvdisplay ol


--- Logical volume --- LV Path /dev/ol/swap LV Name swap VG Name ol LV UUID z4n6St-G0in-uGXZ-We4P-pHsK-7iBH-G2CYcJ LV Write Access read/write LV Creation host, time localhost, 2019-09-24 16:51:17 +0100 LV Status available # open 2 LV Size 2.00 GiB Current LE 512 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 252:1 --- Logical volume --- LV Path /dev/ol/root LV Name root VG Name ol LV UUID NPni07-Yrq6-Jsdb-LeHl-bSBP-jxrP-vuGdQp LV Write Access read/write LV Creation host, time localhost, 2019-09-24 16:51:17 +0100 LV Status available # open 1 LV Size <17.00 GiB Current LE 4351 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 252:0

Add More Space

To add the extra 30GB we created at disk level to the "/" Filesystem, we need to add more space to the Logical Volume "/dev/ol/root". Before we can do that we need to increase the size of the Volume Group by adding Physical Volume space. We can achieve this in one of two ways:

  1. Increase the size of the "/dev/sda2" partition

  2. Add a new "/dev/sda3" partition.

Option 2 would make sense if we needed to use multiple Virtual Disk Images (e.g. vdi files on different disks of our host system). This option is not yet documented here.

To increase the size of /dev/sda2 to fill the newly extended vdi (now 51200MB) use parted...

NOTE: The default unit for parted is MB (this can be misleading when the prompt shows a size in GB)

parted


GNU Parted 3.1Using /dev/sdaWelcome to GNU Parted! Type 'help' to view a list of commands.(parted) print Model: ATA VBOX HARDDISK (scsi)Disk /dev/sda: 53.7GBSector size (logical/physical): 512B/512BPartition Table: msdosDisk Flags:
Number Start End Size Type File system Flags 1 1049kB 1075MB 1074MB primary xfs boot 2 1075MB 21.5GB 20.4GB primary lvm
(parted) resizepartPartition number? 2 End? [21.5GB]? 51200(parted) print Model: ATA VBOX HARDDISK (scsi)Disk /dev/sda: 53.7GBSector size (logical/physical): 512B/512BPartition Table: msdosDisk Flags:
Number Start End Size Type File system Flags 1 1049kB 1075MB 1074MB primary xfs boot 2 1075MB 51.2GB 50.1GB primary lvm
(parted) quit Information: You may need to update /etc/fstab.

Note that at this stage the output of pvs, vgs and lvs will not have changed, but lsblk should show that the new size of the "sda2" partition...

lsblk


NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsdd 8:48 0 20G 0 disk /mnt/AHFsdb 8:16 0 50G 0 disk /mnt/u01sdc 8:32 0 50G 0 disk /mnt/u02sda 8:0 0 50G 0 disk ├─sda2 8:2 0 46.7G 0 part │ ├─ol-swap 252:1 0 2G 0 lvm [SWAP]│ └─ol-root 252:0 0 17G 0 lvm /└─sda1 8:1 0 1G 0 part /boot

We can now call pvresize without options to resize the "sda2" physical volume to the size of the "sda2" partition...

pvresize /dev/sda2


Physical volume "/dev/sda2" changed 1 physical volume(s) resized or updated / 0 physical volume(s) not resized

We can see the change in the output of pvs and pvdisplay...

pvs /dev/sda2


PV VG Fmt Attr PSize PFree /dev/sda2 ol lvm2 a-- <46.68g 27.68g

pvdisplay /dev/sda2


--- Physical volume --- PV Name /dev/sda2 VG Name ol PV Size 46.68 GiB / not usable <2.13 MiB Allocatable yes PE Size 4.00 MiB Total PE 11950 Free PE 7087 Allocated PE 4863 PV UUID fliLf2-aAfr-hFC3-Aw6k-SdpT-9RI6-JsoJDV

We can also see that there is free space available in the "ol" Volume Group using vgs and vgdisplay...

vgs ol


VG #PV #LV #SN Attr VSize VFree ol 1 2 0 wz--n- <46.68g 27.68g

vgdisplay ol


--- Volume group --- VG Name ol System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size <46.68 GiB PE Size 4.00 MiB Total PE 11950 Alloc PE / Size 4863 / <19.00 GiB Free PE / Size 7087 / 27.68 GiB VG UUID uUrPcu-lWgS-13Oo-sDK0-yJWK-X4yB-HICPuY

To extend the Logical Volume we can use lvextend. We will allocate all 7087 free PEs...

lvextend -l +7087 /dev/ol/root


Size of logical volume ol/root changed from <17.00 GiB (4351 extents) to <44.68 GiB (11438 extents). Logical volume ol/root successfully resized.

Use lsblk to see results...

lsblk


NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsdd 8:48 0 20G 0 disk /mnt/AHFsdb 8:16 0 50G 0 disk /mnt/u01sdc 8:32 0 50G 0 disk /mnt/u02sda 8:0 0 50G 0 disk ├─sda2 8:2 0 46.7G 0 part │ ├─ol-swap 252:1 0 2G 0 lvm [SWAP]│ └─ol-root 252:0 0 44.7G 0 lvm /└─sda1 8:1 0 1G 0 part /boot

For our final task we need to resize the filesystem to use the extra space in the Logical Volume.

First we need to identify whether this is an xfs or ext4 filesystem. This can be found in /etc/fstab...

cat /etc/fstab|grep "/dev/mapper/ol-root"


/dev/mapper/ol-root / xfs defaults 0 0

Use xfs_growfs

xfs_growfs -d /


meta-data=/dev/mapper/ol-root isize=256 agcount=4, agsize=1113856 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 spinodes=0 rmapbt=0 = reflink=0data = bsize=4096 blocks=4455424, imaxpct=25 = sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=1log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0data blocks changed from 4455424 to 11712512

Use df to check...

df -h | grep -v tmpfs


Filesystem Size Used Avail Use% Mounted on/dev/mapper/ol-root 45G 17G 29G 38% //dev/sdb 50G 4.0G 43G 9% /mnt/u01/dev/sdc 50G 53M 47G 1% /mnt/u02/dev/sdd 20G 1.2G 18G 7% /mnt/AHF/dev/sda1 1014M 330M 685M 33% /boot

ext4

Stop VirtualBox Machine

Identify HDD to Modify

vboxmanage list vms


"OracleLinux7" {795d8e0f-8c8b-46ca-a29e-1f10be15daa4}

vboxmanage showvminfo OracleLinux7 | grep UUID


UUID: 795d8e0f-8c8b-46ca-a29e-1f10be15daa4Hardware UUID: 795d8e0f-8c8b-46ca-a29e-1f10be15daa4SATA (0, 0): /media/${USER}/HDD1/VirtualBox/OracleLinux7//OracleLinux7/OracleLinux7.vdi (UUID: 8fa057cf-3a16-445c-8157-5850da18f7f6)SATA (1, 0): /media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/u01.vdi (UUID: b7db77ab-4d4f-452c-9e49-c1d5dea44e25)SATA (2, 0): /media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/u02.vdi (UUID: 84864b53-0741-42e1-92aa-fc7164e03f48)SATA (3, 0): /media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/AHF.vdi (UUID: c24f7088-09cd-4ecc-b0ba-ba4a88d1522a)

In this case we can infer that "u01.vdi", "u02.vdi" and "AHF.vdi" map directly to "/mnt/u01","/mnt/u02" and "/mnt/AHF", therefore we can focus on "OracleLinux7.vdi"...

vboxmanage showmediuminfo disk /media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/OracleLinux7.vdi


UUID: 8fa057cf-3a16-445c-8157-5850da18f7f6Parent UUID: baseState: locked writeType: normal (base)Location: /media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/OracleLinux7.vdiStorage format: VDIFormat variant: dynamic defaultCapacity: 20480 MBytesSize on disk: 17731 MBytesEncryption: disabledIn use by VMs: OracleLinux7 (UUID: 795d8e0f-8c8b-46ca-a29e-1f10be15daa4)

Other useful commands...

vboxmanage list hdds

Increase HDD Size

We will increase from 20GB to 50GB...

vboxmanage modifymedium disk "/media/${USER}/HDD1/VirtualBox/OracleLinux7/OracleLinux7/u01.vdi" --resize 51200

Start VirtualBox Machine

All of the following steps should be run on the target Virtual Machine as 'root'

The following command should show the new disk size...

lsblk

The df command should still show the old size but will also show the device name...

df -h

The following command will resize the filesystem on the specified device to the largest size possible...

resize2fs /dev/sdb

The df command should now show the new size...

df -h