Linux Commands

LVM: How to Create Logical Volumes and Filesystems

Logical Volume Management, or LVM, is a system used to manage logical volumes and filesystems. LVM has much more advanced features than the other volume management tools (like gparted) used to divide a disk into one or more partitions. In order to play around with LVM, we need to have concepts about the following terms.

Physical Volume is the actual hard disk drive.

Volume Group gathers all the logical and physical volumes into one group

Logical Volume is the equivalent of disk partition conceptually in a non-LVM system.

File Systems are created on logical volumes, and after creating file systems, we can mount these filesystems on the machine.

This tutorial will use lvm, an apt package, to create logical volumes and filesystems.

Installing LVM

The lvm package does not come pre-installed on ubuntu. Install the lvm package using the apt command-line tool.

ubuntu@ubuntu:~$ sudo apt-get install lvm2 -y

After installing, check the version of lvm to verify the installation.

ubuntu@ubuntu:~$ lvm version

Creating Physical Volume, Volume Group, and Logical Volume

In order to create a logical volume on a block device, a physical volume and a volume group must be created. In this section, we will create a physical volume /dev/sdc; then, we will create a volume group (/dev/vg01) from that physical volume. After this, we will create a logical volume (/dev/vg01/lv01) in this volume group.

Creating Physical Volume

So before creating any physical volume, letā€™s display all the available physical volumes on the machine. Use pvs, pvscan or pvdisplay command to display all the physical volumes.

ubuntu@ubuntu:~$ sudo pvs

OR

ubuntu@ubuntu:~$ sudo pvscan

OR

ubuntu@ubuntu:~$ sudo pvdisplay

So there is no physical volume already initialized from any block device on the machine. Before creating a physical volume from a block device, list all the block devices available on the machine, which can be used to create physical volumes. Use the lvmdiskscan command to list all the block devices on the machine.

ubuntu@ubuntu:~$ sudo lvmdiskscan

We will initialize /dev/sdc as our physical volume using the pvcreate command. A block device can not be initialized as physical volume if it is mounted on the machine. Use umount command to unmount a block device.

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

Now initialize the block device as a physical volume using the pvcreate command.

ubuntu@ubuntu:~$ sudo pvcreate /dev/sdc

After initializing the block device as a physical volume, now again list all the physical volume using the pvdisplay command, and the recently created physical volume will show up there.

Creating Volume Group

So far, we have created a physical volume; now, we will create a volume group (vg01) from the physical volume we just created. Before creating any volume group, display all the available volume groups using the vgdisplay or vgs command.

ubuntu@ubuntu:~$ sudo vgs

OR

ubuntu@ubuntu:~$ sudo vgdisplay

There is no volume group on the machine, so create a new one from the physical volume (/dev/sdc), which we just created in the previous step. The vgcreate command will be used to create a volume group.

ubuntu@ubuntu:~$ sudo vgcreate vg01 /dev/sdc

The above command will create a volume group (vg01) from the /dev/sdc physical volume.

NOTE: We can create a volume group from more than one physical volume by using the vgcreate command as follows.

ubuntu@ubuntu:~$ sudo vgcreate vg01 /dev/sdc /dev/sda /dev/sdb

Now again, display all the volume groups using the vgdisplay command, and the recently created volume group vg01 will be listed there.

ubuntu@ubuntu:~$ sudo vgdisplay

In the above figure, we can see that the physical extent (PE) size is 7679, a maximum number of physical volumes is 0 (as we have not set its value), and the maximum number of logical volumes is 0 (as we have not set its value). We can set these parameters while creating the volume group by using the following flags.

-s: physical extent size

-p: maximum number physical volumes

-l: maximum number of logical volumes

So now we have one volume group, vg01, and we can activate and deactivate this volume group using the vgchange command. To activate the volume group, set the value of -a flag to y and deactivate the volume group, set the value of the -a flag to n along with the vgchange command.

ubuntu@ubuntu:~$ sudo vgchange -a n vg01

The above command has deactivated the volume group vg01. To activate the volume group, use the following command.

ubuntu@ubuntu:~$ sudo vgchange -a y vg01

Creating Logical Volume

After creating a physical volume and volume group, now create the logical volume in the volume group. Before creating the logical volume, list all the available logical volumes by using the lvs, lvscan or lvdisplay command.

ubuntu@ubuntu:~$ sudo lvs

OR

ubuntu@ubuntu:~$ sudo lvscan

OR

ubuntu@ubuntu:~$ sudo lvdisplay

There is no logical volume, so create a logical volume of size 10GB in the vg01 volume group using the lvcreate command.

ubuntu@ubuntu:~$ sudo lvcreate -L 10G -n lv01 vg01

After creating the logical volume, now list all the logical volumes by using the lvdisplay command.

ubuntu@ubuntu:~$ sudo lvdisplay

Creating Filesystems

After creating the logical volumes, now the final step is to create a filesystem on top of the logical volume. After creating a filesystem, mount it on a directory to be accessible and can be used to store data in it. There are different file system formats (like FAT16, FAT32, NTFS, ext2, ext3, etc.) that can be used to create a filesystem. Create an ext4 filesystem by using the mkfs command.

ubuntu@ubuntu:~$ sudo mkfs.ext4 /dev/vg01/lv01

After creating the filesystem, mount it on a directory to access it. Create a directory ā€˜/media/$USER/lv01ā€™.

ubuntu@ubuntu:~$ sudo mkdir /media/$USER/lv01

Mount the filesystem on this directory using the mount command.

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

Now the /dev/vg01/lv01 filesystem can be accessed from the ā€˜/media/$USER/lv01ā€™ directory, and data can be stored on this location. In order to mount the filesystem automatically on reboot, add the entry for this filesystem in the ā€˜/etc/fstabā€™ file. Open the ā€˜/etc/fstabā€™ file in nano editor and append the line in the file.

ubuntu@ubuntu:~$ sudo nano /etc/fstab

/dev/vg01/lv01 /media/ubuntu/lv01 ext4 defaults 0 0

After creating and mounting the filesystem, we can use different commands like fdisk, df, or lsblk to display the file system.

ubuntu@ubuntu:~$ sudo lsblk | grep lv01

OR

ubuntu@ubuntu:~$ sudo df -h | grep lv01

OR

ubuntu@ubuntu:~$ sudo fdisk -l | grep lv01

Using Graphical Tool For LVM

So far, we have used lvm through the command-line interface, but there is a graphical tool (kvpm) available used to create logical volumes and filesystems using a nice graphical user interface. Install the kvpm by using the apt command-line tool.

ubuntu@ubuntu:~$ sudo apt-get install kvpm -y

After installing kvpm, open the tool from the terminal by typing the following command.

ubuntu@ubuntu:~$ sudo kvpm

In the storage devices tab, it is showing all the block devices available on the machine. For /dev/sdc block device, it is showing 20GiB of space remaining from a total of 30GiB space because we have created a 10GiB filesystem on this block device. Alongside the ā€˜Storage Devicesā€™ tab, there is the ā€˜Group: vg01ā€™ tab, and it has all the data related to the volume group we created in this tutorial.

This window has all the information about the volume group, logical volume, and the filesystem created. In order to create a new logical volume using the kvpm tool, click on ā€˜New volumeā€™ in the volume group tab.

Specify the volume name and size of the logical volume.

It will create a new logical volume named lv02, and the volume will be listed there.

Confirm whether the logical volume is created or not by using the lvs command in the terminal.

ubuntu@ubuntu:~$ sudo lvs

To create a filesystem on lv02 using a graphical tool, select the logical volume lv02 on which you want to create a filesystem and click on ā€˜mkfsā€™.

It will ask for confirmation and then create the file system after entering the file system format.

In order to confirm whether the filesystem was created or not, use the following commands to list all the filesystems.

ubuntu@ubuntu:~$ sudo fdisk -l | grep lv02

OR

ubuntu@ubuntu:~$ sudo lsblk | grep lv02

Now, this file system can be mounted on any directory and can be used to store and retrieve data.

Conclusion

To manage data on a system, we need to store it in an organized way. Some files need to be stored in one filesystem and some files in another depending upon the requirement. In this scenario, we have to manage filesystems on our block devices in the machine. In this tutorial, we learned how we could create logical volumes and different filesystems on block devices. We discussed how to initialize block devices as physical volumes and how volume groups, logical volumes, and filesystems can be created using a command-line interface and a graphical user interface tool.

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