- 02 Mar 2023
- 2 Minutes to read
- Print
- DarkLight
How to expand an LVM (Logical Volume Manager)
- Updated on 02 Mar 2023
- 2 Minutes to read
- Print
- DarkLight
After expanding an existing partition that’s part of LVM you will need to tell LVM about the changes.
The steps are broken down into multiple parts as shown below.
Extend the PV
After running lsblk we can see that sda2 has 248.6GB but centos-root LV only has 145.8GB of space.
As such, we’ll need to ensure that the /dev/sda2 pv is extended to the maximum size.
[root@server ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 250G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 248.6G 0 part
├─centos-root 253:0 0 145.8G 0 lvm /
└─centos-swap 253:1 0 3.2G 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom
loop0 7:0 0 4G 0 loop /var/tmp
When we check the physical volume we can see that the space has not been allocated yet. This is done using pvs as shown below:
[root@server ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <149.00g 0
We can extend the /dev/sda2 PV (physical volume) to the maximum size by using pvresize as shown below:
[root@server ~]# pvresize /dev/sda2
Physical volume "/dev/sda2" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
[root@server ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <248.59g 99.59g
You can see after we have done the pvresize that the results of pvs now show that the /dev/sda2 PV has a new maximum size (<248.59g). Now that the PV has the maximum size allocated to it, we can move on to extending the LV.
Extend the LV
As the PV (physical volume) has been resized and there is more available space, we need to assign it to the LV (logical volume) by using the lvextend command. The below assigns 100% of the free space in the PV (physical volume) to the root LV (logical volume) (/dev/centos/root).
[root@server ~]# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from <145.80 GiB (37324 extents) to 245.39 GiB (62820 extents).
Logical volume centos/root successfully resized.
Running an lsblk will show that the LV (logical volume) centos-root has now been extended to 248.6GB.
[root@server ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 250G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 248.6G 0 part
├─centos-root 253:0 0 245.4G 0 lvm /
└─centos-swap 253:1 0 3.2G 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom
loop0 7:0 0 4G 0 loop /var/tmp
Once the LV (logical volume) has been extended we can look at extending the filesystem so that the space becomes usable.
Check the Filesystem Type
First, check the filesystem that the machine is currently using with either of the following commands.
You’ll want to grep for the name of the LV that has the root filesystem on it.
In this case, the LV (logical volume) ‘centos-root’ will have a name of ‘root’.
[root@server ~]# grep 'root' /etc/fstab
/dev/mapper/centos-root / xfs defaults,uquota 0 0
or by using the following grep command:
[root@server ~]# mount | grep 'root'
/dev/mapper/centos-root on / type xfs (rw,relatime,attr2,inode64,usrquota)
Grow the Filesystem
This is the final piece of the puzzle.
If the filesystem is XFS then use the below command. This is assuming the name of the lv is ‘centos-root’.
xfs_growfs /dev/centos/root
If the LV had a different name, for example ubuntu-main
the command will be different. For example:
xfs_growfs /dev/ubuntu/main
Otherwise, if the filesystem is an EXT filesytem e.g. ext4, then use the following:
resize2fs /dev/centos/root
Once the filesystem resize is finished, the space is now usable and your job is complete.