How to extend a LVM volume adding a new disk

In this how-to I’m going to explain the way to increase the disk space by adding a new hard disk and extending the LVM volume. After that more free disk space to the system is available.

System preparation

Firstly, after you connected the new disk, take a look to the the system with lsblk command.

What is lsblk?

From: https://www.man7.org/linux/man-pages/man8/lsblk.8.html

lsblk lists information about all available or the specified block devices. The lsblk command reads the sysfs filesystem and udev db to gather information. If the udev db is not available or lsblk is compiled without udev support, then it tries to read LABELs, UUIDs and filesystem types from the block device. In this case root permissions are necessary. The command prints all block devices (except RAM disks) in a tree-like format by default.

pippo@pluto:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 465.8G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 464.3G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 464.3G 0 lvm /
sdb 8:16 0 238.5G 0 disk
├─sdb1 8:17 0 100M 0 part
├─sdb2 8:18 0 16M 0 part
├─sdb3 8:19 0 237.9G 0 part
└─sdb4 8:20 0 520M 0 part
pippo@pluto:~$

In my case I have a disk with 4 partitions under the device “/dev/sdb” and we must delete the non necessary partitions with fdisk in order to create the new one to use.

What is fdisk?

From: From: https://www.man7.org/linux/man-pages/man8/fdisk.8.html

fdisk is a dialog-driven program for creation and manipulation of partition tables. It understands GPT, MBR, Sun, SGI and BSD partition tables.

pippo@pluto:~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Partition number (1-4, default 4):

Partition 4 has been deleted.
Command (m for help): d
Partition number (1-3, default 3):

Partition 3 has been deleted.
Command (m for help): d
Partition number (1,2, default 2):

Partition 2 has been deleted.
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.


Command (m for help): n
Partition number (1-128, default 1):
First sector (34-500118158, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-500118158, default 500118158):

Created a new partition 1 of type 'Linux filesystem' and of size 238.5 GiB.

Partition #1 contains a vfat signature.

Do you want to remove the signature? [Y]es/[N]o: y
The signature will be removed by a write command.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

After the steps above we created the condition necessary to proceed with LVM volume extention part. Performing a new check with lsblk we can verify if the behaviour is as expected.

pippo@pluto:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 465.8G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 464.3G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 464.3G 0 lvm /
sdb 8:16 0 238.5G 0 disk
└─sdb1 8:17 0 238.5G 0 part
pippo@pluto:~$

LVM taks

Firstly create a physical volume with the pvcreate command and the new partition path /dev/sdb1.

pippo@pluto:~$ sudo pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
pippo@pluto:~$

After that we need to retrieve the Volume Group Name (VG Name) with the vgdisplay command, in this case is “ubuntu-vg“.

pippo@pluto:~$ sudo vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <464.26 GiB
PE Size 4.00 MiB
Total PE 118850
Alloc PE / Size 118850 / <464.26 GiB
Free PE / Size 0 / 0
VG UUID VpkffP-MEmJ-6CU6-eQfo-h0QW-af1K-3Xesz3

Secondly we need to use the vgextend command to extend the volume group “ubuntu-vg” into “/dev/sdb1“.

pippo@pluto:~$ sudo vgextend ubuntu-vg /dev/sdb1
Volume group "ubuntu-vg" successfully extended
pippo@pluto:~$

After that we can verify that the Free PE is equal to the size of the added disk with vgdisplay command, then find the Logical Volume Path (LV Path) with the lvdisplay command.

pippo@pluto:~$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/ubuntu-vg/ubuntu-lv
LV Name ubuntu-lv
VG Name ubuntu-vg
LV UUID whhBcQ-XLri-IRxE-z23h-NUWG-HB2M-pxlvHo
LV Write Access read/write
LV Creation host, time ubuntu-server, 2021-09-18 12:33:24 +0000
LV Status available
# open 1
LV Size <464.26 GiB
Current LE 118850
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

Thirdly extend the volume with the lvextend command to the desired size.

pippo@pluto:~$ sudo lvextend -L+238G /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from <464.26 GiB (118850 extents) to <702.26 GiB (179778 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
pippo@pluto:~$

The last step, but not least, is to resize the filesystem via resize2fs command.

pippo@pluto:~$ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 59, new_desc_blocks = 88
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 184092672 (4k) blocks long.
pippo@pluto:~$

Conclusion

In conclusion those are the steps necessary how to extend a LVM volume adding a new disk, I made this tutorial on Ubuntu 18.04 LTS but it is valid for all the distros. Moreover, if you have free space on the disk, you can start from LVM task paragraph.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s