Extending LVM Partitions

Extending LVM Partitions #

This guide walks you through extending LVM partitions when you’ve added new disk space to your system.

LVM Structure #

LVM operates with three main components:

  • Physical Volumes (PV): The actual disk partitions
  • Volume Groups (VG): Collections of physical volumes
  • Logical Volumes (LV): The volumes you mount and use

Common Scenario #

You’ve added disk space to a virtual machine and need to extend the root filesystem. This is a common requirement in virtualized environments where storage needs grow over time.

Step-by-Step Process #

Let’s say I added 5 more gigabytes VM image. We want to extend the disk partition and the physical and logical LVM volumes.

First, let’s see the current state using lsblk:

NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0                  11:0    1 1024M  0 rom
vda                 252:0    0   15G  0 disk
├─vda1              252:1    0    1G  0 part /boot
└─vda2              252:2    0    9G  0 part
  ├─rhel_test-root  253:0    0    8G  0 lvm  /
  └─rhel_test-swap  253:1    0    1G  0 lvm  [SWAP]

As you can see, the disk shows 15G total, but partition vda2 is still only 9G.

Disk Partition #

Install growpart #

sudo dnf install cloud-utils-growpart

Extend Disk Partition #

[user@test ~]$ sudo growpart /dev/vda 2
CHANGED: partition=2 start=2099200 old: size=18872320 end=20971519 new: size=29358047 end=31457246

Now let’s verify the partition has been extended:

[user@test ~]$ lsblk
NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0                  11:0    1 1024M  0 rom
vda                 252:0    0   15G  0 disk
├─vda1              252:1    0    1G  0 part /boot
└─vda2              252:2    0   14G  0 part
  ├─rhel_test-root  253:0    0    8G  0 lvm  /
  └─rhel_test-swap  253:1    0    1G  0 lvm  [SWAP]

Great! The vda2 partition is now 14G instead of 9G.

Physical Volume #

Let’s check the current physical volume status:

[user@test ~]$ sudo pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/vda2  rhel_test lvm2 a--  <14.00g 5.00g

The PV shows 14G total with 5G free space available.

Extend Physical Volume #

[user@test ~]$ sudo pvresize /dev/vda2
  Physical volume "/dev/vda2" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

Let’s verify the PV is updated:

[user@test ~]$ sudo pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/vda2  rhel_test lvm2 a--  <14.00g 5.00g

Now we have 5GB of free space available in the volume group.

Logical Volume #

Let’s check the current logical volumes:

[user@test ~]$ sudo lvs
  LV   VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root rhel_test -wi-ao---- <8.00g
  swap rhel_test -wi-ao----  1.00g

The root logical volume is currently 8GB.

Extend Logical Volume #

[user@test ~]$ sudo lvextend /dev/rhel_test/root -l+100%FREE -r
  File system xfs found on rhel_test/root mounted at /.
  Size of logical volume rhel_test/root changed from <8.00 GiB (2047 extents) to <13.00 GiB (3327 extents).
  Extending file system xfs to <13.00 GiB (13954449408 bytes) on rhel_test/root...
xfs_growfs /dev/rhel_test/root
meta-data=/dev/mapper/rhel_test-root isize=512    agcount=4, agsize=524032 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=2096128, 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 2096128 to 3406848
xfs_growfs done
  Extended file system xfs on rhel_test/root.
  Logical volume rhel_test/root successfully resized.

The -l+100%FREE flag uses all available free space, and -r automatically resizes the filesystem.

Let’s verify the logical volume has been extended:

[user@test ~]$ sudo lvs
  LV   VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root rhel_test -wi-ao---- <13.00g
  swap rhel_test -wi-ao----   1.00g

The root volume is now 13GB instead of 8GB.

Verification #

Let’s verify that the added space is visible and the filesystem has been extended:

[user@test ~]$ df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  4.0M     0  4.0M   0% /dev
tmpfs                     tmpfs     886M     0  886M   0% /dev/shm
tmpfs                     tmpfs     355M  5.2M  350M   2% /run
/dev/mapper/rhel_test-root xfs        13G  4.1G  8.9G  32% /
/dev/vda1                 xfs       960M  424M  537M  45% /boot
tmpfs                     tmpfs     178M     0  178M   0% /run/user/1000

The root filesystem now shows 13G total space instead of the original 8G.

Conclusion #

The process to extend an LVM partition after adding disk space involves:

  1. Extend the disk partition using growpart
  2. Resize the physical volume using pvresize
  3. Extend the logical volume using lvextend with -r to auto-resize the filesystem

The -l+100%FREE flag is particularly useful as it uses all available space, and the -r flag automatically handles filesystem resizing, making the process much simpler than doing it manually.