Extending LVM Partitions in Linux

In Linux, Logical Volume Manager (LVM) is a technology that provides logical volume management for the Linux kernel. You can use LVM in most Linux distributions to resize file system by increasing it or descreasing it as per your needs.

The concept is pretty simple. You have multiple physical volumes (physical disk attached to the system) forming a volume group and in a volume group you can create logical volumes. You then have the flexibility to add/remove the physical volumes to increase the volume group capacity, and as such, being able to add new logical volumes or extending the size of the existing ones as needed.

In this short tutorial we will discuss the steps to extend a file system that is running out of space. For this, let’s suppose we have a LVM partition (/dev/mapper/centos-root) mounted on / that is almost out of space and we want to increase the space available on the disk.

Listing the file system along with the available and free space

Above command shows /dev/mapper/centos-root has only 1.7G available.

$ df -h /dev/mapper/centos-root
Filesystem               Size  Used Avail Use{a30ec6af4236f4179fdacbfa14163c342445d6f2c9798eae9caee1ef22a84454} Mounted on
/dev/mapper/centos-root  9.4G  7.7G  1.7G  83{a30ec6af4236f4179fdacbfa14163c342445d6f2c9798eae9caee1ef22a84454} /

Checking the existing Physical Volumes and Volume Groups

From below command, we can see that there is one physical volume (/dev/sda2) into one volume group called centos.

$ sudo pvs
  PV         VG     Fmt  Attr PSize  PFree
  /dev/sda2  centos lvm2 a--  10.25g 4.00m

Checking the existing Logical Volumes

We have two logical volumes named root and swap inside the centos Volume Group.

$ sudo lvs
  LV   VG     Attr       LSize   Pool Origin Data{a30ec6af4236f4179fdacbfa14163c342445d6f2c9798eae9caee1ef22a84454}  Meta{a30ec6af4236f4179fdacbfa14163c342445d6f2c9798eae9caee1ef22a84454}  Move Log Cpy{a30ec6af4236f4179fdacbfa14163c342445d6f2c9798eae9caee1ef22a84454}Sync Convert
  root centos -wi-ao----   9.31g
  swap centos -wi-ao---- 956.00m

Checking the disks attached on the system

We have a raw disk (sdb) attached to our system with a size of 4GB that can be used to extend the centos Volume Group, then assign all available free space to the Logical Volume root.

$ lsblk
fd0               2:0    1    4K  0 disk
sda               8:0    0   20G  0 disk
├─sda1            8:1    0  476M  0 part /boot
└─sda2            8:2    0 10.3G  0 part
  ├─centos-root 253:0    0  9.3G  0 lvm  /
  └─centos-swap 253:1    0  956M  0 lvm  [SWAP]
sdb               8:16   0    4G  0 disk
sr0              11:0    1 1024M  0 rom

Turning the attached disk to a Physical Volume

Before being able to add sdb to the centos Volume Group, we need to mark it as a Physical Volume.

$ sudo pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.

Adding the /dev/sdb to the centos Volume Group

$ sudo vgextend centos /dev/sdb
  Volume group "centos" successfully extended

Volume Group centos has two disks now, /dev/sda2 and /dev/sdb.

$ sudo pvs
  PV         VG     Fmt  Attr PSize  PFree
  /dev/sda2  centos lvm2 a--  10.25g  4.00m
  /dev/sdb   centos lvm2 a--  <4.00g <4.00g

Extending the Logical Volume

The last step would be to assign the available free (4GB) to the root Logical Volume. The -r here is used to resize the file system on the fly along with the logical volume. This will avoid you to run resize2fs afterwards.

$ sudo lvextend -r -L +4G /dev/mapper/centos-root
  Size of logical volume centos/root changed from 9.31 GiB (2384 extents) to 13.31 GiB (3408 extents).
  Logical volume centos/root successfully resized.
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=610304 blks
		 =                       sectsz=512   attr=2, projid32bit=1
		 =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2441216, imaxpct=25
		 =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
		 =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2441216 to 3489792

Checking the updated file system free space

The file system size has been increased after adding 4GB and the free space available now is 5.7GB.

$ df -h /dev/mapper/centos-root
Filesystem               Size  Used Avail Use{a30ec6af4236f4179fdacbfa14163c342445d6f2c9798eae9caee1ef22a84454} Mounted on
/dev/mapper/centos-root   14G  7.7G  5.7G  58{a30ec6af4236f4179fdacbfa14163c342445d6f2c9798eae9caee1ef22a84454} /


Leave a Comment

Your email address will not be published. Required fields are marked *