Linux Commands

LVM: Add a New Disk to a Volume

Before diving into details about the Logic Volume Manager (LVM) it is imperative to understand what LVM is. LVM provides different methods to increase the size of the logical volume. Extending the logical volume can increase the size but a new disk needs to be added in case the volume group also runs out of space. In this tutorial, we will use the LVM command-line interface to add a new disk to an existing logical volume by following the steps below:

  • New physical volume creation
  • The volume group extension
  • The logical volume extension
  • Mounted filesystem extension

Pre-requisites

To follow this tutorial, you must have a physical volume created from a disk. The physical volume should have a volume group, vg01, on it. A logical volume lv01, created on this volume group vg01.

List all the available physical volumes by running pvs, pvscan or pvdisplay commands in the terminal.

ubuntu@ubuntu:~$ sudo pvs

OR

ubuntu@ubuntu:~$ sudo pvscan

OR

ubuntu@ubuntu:~$ sudo pvdisplay

Notice the only one physical volume /dev/sdc in the system. Now, list all the volume groups created on /dev/sdc physical volume using any of the vgs, vgscan or vgdisplay commands.

ubuntu@ubuntu:~$ sudo vgs

OR

ubuntu@ubuntu:~$ sudo vgdisplay

OR

ubuntu@ubuntu:~$ sudo vgscan

The command displays the presence of only one volume group vg01 on the physical volume /dev/sdc, in the system. The following commands can be used to find all the logical volumes over the volume group.

ubuntu@ubuntu:~$ sudo lvs

OR

ubuntu@ubuntu:~$ sudo lvscan

OR

ubuntu@ubuntu:~$ sudo lvdisplay

There are two logical volumes lv01 and lv02 on the volume group vg01.

Create a New Physical Volume

First list all the physical devices connected to the system by using the lsblk command.

ubuntu@ubuntu:~$ sudo lsblk

There are 3 physical devices connected to the system and one of them (/dev/sdc) is the device which is being used as physical volume. Now, attach a new physical device and again list all the connected devices.

ubuntu@ubuntu:~$ sudo lsblk

There are four physical devices connected to the system. The newly attached device (/dev/sdd1) is currently mounted on the system. Unmount the /dev/sdd1 device as follows:

ubuntu@ubuntu:~$ sudo umount /dev/sdd1

Now, format the device with the dd command. This command will take some time to format the device depending upon the size of the device attached.

NOTE: Formatting the device will erase all the data in the device. Create a backup of the data before formatting it.

ubuntu@ubuntu:~$ sudo dd if=/dev/zero of=/dev/sdd status=progress bs=4M

Create a physical volume from the newly attached physical device using the pvcreate command.

ubuntu@ubuntu:~$ pvcreate /dev/sdd

It will create a physical volume from the physical device connected. Display all the physical volumes by using any of the pvs, pvscan or pvdisplay commands.

ubuntu@ubuntu:~$ sudo pvs

OR

ubuntu@ubuntu:~$ sudo pvscan

OR

ubuntu@ubuntu:~$ sudo pvdisplay

The output displays two physical volumes /dev/sdc and /dev/sdd created on the system.

Volume Group Extension

Extend the volume group vg01 using the vgextend command to cover both the physical volumes.

ubuntu@ubuntu:~$ sudo vgextend vg01 /dev/sdd

The above command output shows that the volume group vg01 is spanning both the physical volumes /dev/sdc and /dev/sdd and it has more space. List the details of the volume group vg01 using the vgdisplay command.

ubuntu@ubuntu:~$ sudo vgdisplay

The total size of the volume group vg01 is now 44.5 GB that previously was only 30 GB.

Extend the Logical Volume

Now, we can either create another logical volume from this space or we can extend the existing logical volume to use this space. For this guide, we will extend the existing logical volume to add more space.

Display the available logical volumes with any of the following commands:

ubuntu@ubuntu:~$ sudo lvs

OR

ubuntu@ubuntu:~$ sudo lvscan

OR

ubuntu@ubuntu:~$ sudo lvdisplay

Currently there are only two logical volumes lv01 and lv02 with 15 GB and 10 GB space respectively. For this tutorial, we will extend the logical volume lv01 using the lvextend command from 15 GB to 25 GB.

ubuntu@ubuntu:~$ sudo lvextend -L+10G /dev/vg01/lv01

After extending the logical volume size, now list the logical volume details using the lvdisplay command.

ubuntu@ubuntu:~$ sudo lvdisplay /dev/vg01/lv01

So, the size of the logical volume lv01 is now 25 GB and has been extended successfully.

Extend the Filesystem Mounted on the System

Mount the logical volume using the mount command:

ubuntu@ubuntu:~$ sudo mount /dev/vg01/lv01 /media/ubuntu/lv01

After mounting the logical volume, now display the filesystem with the df command to check the filesystem size.

ubuntu@ubuntu:~$ sudo df -Th /dev/vg01/lv01

The filesystem size is not extended so it needs to be extended using the resize2fs command.

ubuntu@ubuntu:~$ sudo resize2fs /dev/vg01/lv01

After extending the filesystem, run the df command to verify size extension of the filesystem.

ubuntu@ubuntu:~$ sudo df -Th /dev/vg01/lv01

Now, the filesystem size is 25 GB and it has been extended successfully.

Conclusion

Logical volume size management on the running servers is one of the many job responsibilities of the system administrator. Such that they have to deal with its space shortage due to excessive data on a day-to-day basis.

Logical volume extension requires some space in the volume group on which logical volume exists. In case there is no free space available on the volume group, a new physical device is attached to create free space. The article is a guide that provides them with a step by step guide on how to add a new disk to extend the logical volume and the file system mounted on the system.

About the author

Usama Azad

A security enthusiast who loves Terminal and Open Source. My area of expertise is Python, Linux (Debian), Bash, Penetration testing, and Firewalls. I’m born and raised in Wazirabad, Pakistan and currently doing Undergraduation from National University of Science and Technology (NUST). On Twitter i go by @UsamaAzad14