Extending Root Partition Using LVM on Fedora

Preface

When configuring a Fedora VM, maybe because I’m lack of coffee or what, I’ve accidentally configured the wrong partition to be used for root directory.

Here’s what it looks like by using lsblk to check the partition size and df -h to check used storages in the filesystem:

[ndkprd@bastion ~]$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda               8:0    0  128G  0 disk
├─sda1            8:1    0    1M  0 part
├─sda2            8:2    0    1G  0 part /boot
└─sda3            8:3    0  127G  0 part
  └─fedora-root 253:0    0   15G  0 lvm  /
sr0              11:0    1 1024M  0 rom
zram0           252:0    0  7.7G  0 disk [SWAP]
[ndkprd@bastion ~]$
[ndkprd@bastion ~]$
[ndkprd@bastion ~]$ df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 4.0M     0  4.0M   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    1.6G  1.5M  1.6G   1% /run
/dev/mapper/fedora-root   15G   13G  2.6G  83% /
tmpfs                    3.9G  124K  3.9G   1% /tmp
/dev/sda2                960M  287M  674M  30% /boot
tmpfs                    790M     0  790M   0% /run/user/1000
 

I just realized this after damn 6 (SIX) month, because I thought that I purposefully set this VM to only have 16 GB of storage. After using this VM as my main jumphost/bastion for those half a year, I checked the VM configuration in ESXI, and just realized that I actually allocated 128 GB for this machine.

So if you’re stupid enough to land on the same position as me, here’s how I extended the root partition using the free space from other partition.

Preresquisites

You need terminal access with root privilege to the machine, of course.

Procedures

Check Volume Groups

vgs

Expected output:

[ndkprd@bastion ~]$ sudo vgs
  VG     #PV #LV #SN Attr   VSize    VFree
  fedora   1   1   0 wz--n- <127.00g <112.00g

This means I actually have 112 GB of unallocated space. If you don’t see your supposed free space even though you can see the partition in lsblk, check out the next steps.

Resize Physical Volume (PV)

Check if your partition is already set as a PV:

sudo pvs

Expected output:

[ndkprd@bastion ~]$ sudo pvs
  PV         VG     Fmt  Attr PSize    PFree
  /dev/sda3  fedora lvm2 a--  <127.00g <112.00g
 

This means the partition is already set as a PV. If the partition not appear, do the following command:

sudo pvresize /dev/sda3

Now, check vgs again and make sure that your free volume is now allocated to the volume group.

Extend Logical Volume

The following command extend the logical volume used as root partition with 100% free unallocated spaces in Volume Group. You can adjust accordingly.

lvextend -r -l +100%FREE /dev/mapper/fedora-root

Expected output:

[root@bastion ndkprd]# lvextend -r -l +100%FREE /dev/mapper/fedora-root
  Size of logical volume fedora/root changed from 15.00 GiB (3840 extents) to <127.00 GiB (32511 extents).
meta-data=/dev/mapper/fedora-root isize=512    agcount=4, agsize=983040 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=1 inobtcount=1 nrext64=0
data     =                       bsize=4096   blocks=3932160, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=16384, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 3932160 to 33291264
  Logical volume fedora/root successfully resized.
 

Conclusion

Do a final check by using df -h again:

[root@bastion ndkprd]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 4.0M     0  4.0M   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    1.6G  1.5M  1.6G   1% /run
/dev/mapper/fedora-root  127G   14G  114G  11% /
tmpfs                    3.9G  124K  3.9G   1% /tmp
/dev/sda2                960M  287M  674M  30% /boot
tmpfs                    790M     0  790M   0% /run/user/1000
overlay                  127G   14G  114G  11% /var/lib/docker/overlay2/072ed155fa970efb499e9a6683467ec8fabce73aad9c5eb7013148f97dbc98ef/merged
overlay                  127G   14G  114G  11% /var/lib/docker/overlay2/07854e99857149675776459421e35ddccb20cb9de54dfc8338fe3bdf5905a954/merged
 

Now that my workspace is little bit spacious, I felt a little bit happier. god I wish my company would buy me a macbook.