Home Linux Disk Management Linux Logical Volume Manager (LVM) Guide For Beginners

Linux Logical Volume Manager (LVM) Guide For Beginners

Linux LVM Commands

By Karthick
Published: Updated: 6.4K views

In this tutorial, we are going to learn about Linux Volume Management. We will see what is LVM in Linux, advantages of LVM, how to create Volume group and Logical volumes in Linux with examples.

What is Logical Volume Manager (LVM)?

Logical Volume Manager (LVM) is used to manage block storage in Linux. LVM handles storage management differently as well as efficiently when compared with standard partitions.

LVM accumulates spaces taken from the partitions or entire disks (known as Physical Volumes) to form a logical container (known as Volume Group). The Volume Group is further divided into logical partitions called Logical Volumes.

To put this simply, LVM groups all your storage space into the pool and allows you to create volumes (Logical Volumes) from that pool.

The advantage of using LVM over standard partition is, LVM offers you more flexibility and features. It allows online resizing of Logical Groups and Logical Volumes. So If any of your logical partitions run out of space, you can easily increase the partition size by using the space available in the storage pool.

You can also export and import partitions. LVM also supports mirroring and snapshotting of logical volumes.

We will go through all the features separately in the upcoming articles. I suggest you also look at the following guides to understand how standard partitions are created and mounted.

Layers Of Abstraction In LVM

LVM provides an abstraction of layer between the physical storage and the file system, enabling the file system to be resized, span across multiple physical disks, and use arbitrary disk space.

LVM uses three layers of abstraction to create partitions.

  • Physical Volume,
  • Volume Group,
  • Logical Volume.

PHYSICAL VOLUME

It all starts with a physical disk. Physical volume is the first layer of abstraction LVM uses to identify the disk tagged for LVM operations. To put it in simple terms, if you wish to work with LVM, your disk should be initialized as the physical volume. It can be an entire disk or standard partitions created from that disk.

VOLUME GROUP

Volume group is the combination of all the physical volume. Let's say you have five separate disks with a size of 1 TB each. You will first initialize the five disks as physical volume and then add them to the volume group.

Volume group will hold 5TB of space which is the space available from all the physical volume. From the volume group, you can create logical partitions.

LOGICAL VOLUMES

From the pool of space (Volume Group) you can create logical volumes. Think of this as equivalent to a standard disk partition.

Enough with the basics. Let us get started with Linux LVM commands.

I am using the Ubuntu server running in virtualbox for demonstration. If you are learning LVM for the first time, do your testing on any virtual machine.

Heads Up: LVM commands require root privilege. Either run all the commands as root user or with sudo privilege.

Step 1 - Initialize Physical Volume

I have added three disks with different sizes and it totals 10G.

$ lsblk /dev/sd[b-e]

Sample output:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 2G 0 disk
sdc 8:32 0 3G 0 disk
sdd 8:48 0 5G 0 disk

To initialize any disk as physical volume, use the pvcreate command, with the device name as the argument.

$ sudo pvcreate /dev/sdb /dev/sdc /dev/sdd

Sample output:

Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
Physical volume "/dev/sdd" successfully created.

To check the list of physical volumes, you can run any of the following commands. Each command will give you a different output.

PVDISPLAY - The pvdisplay command will give you detailed information about each physical volume, under which volume group it is, the unique ID, and the size available.

$ sudo pvdisplay
Pvdisplay Command Output
Pvdisplay Command Output

PVS & PVSCAN - These two commands will give you information like physical volume, volume group, allocated, and free size.

$ sudo pvs

Pvs Command Output
Pvs Command Output
$ sudo pvscan
Pvscan Command Output
Pvscan Command Output

Step 2 - Creating Volume Group

Now I have three disks initialized as a physical volume with a total size of 10GB. These physical volumes should be added to a storage pool known as a volume group.

Run vgcreate command to create a volume group. You have to pass a name for the volume group. Here I am using "ostechnix_files" as the volume group name.

$ sudo vgcreate ostechnix_files /dev/sd[b-d]
Volume group "ostechnix_files" successfully created

Run any of the following commands to check the volume group details.

$ sudo vgdisplay

Or,

$ sudo vgdisplay <volume group name>
Vgdisplay Command Output
Vgdisplay Command Output

Step 3 - Display Volume Group Details

The vgs and vgscan commands will give you information related to all available volume groups, number of physical volumes, and number of logical volumes, allocated and free sizes from the volume group.

$ sudo vgs
$ sudo vgscan
Vgs And Vgscan Commands Output
Vgs And Vgscan Commands Output

Step 4 - Creating Logical Volumes

As I mentioned earlier, logical volume is similar to disk partitions. Now we have close to 10GB of free space in the "ostechnix_files" pool (volume group). On top of this volume group, we will create logical volumes, format the volume with the ext4 file system, mount and use the volume.

To create the logical volume, you can use the lvcreate command. The general syntax of lvcreate command is given below.

$ sudo lvcreate -L <volume-size> -n <logical-volume-name> <volume-group>

Here,

  • -L <volume-size> => size in KB, MB, GB
  • -n <logical-volume-name> => Name for your volume
  • <volume-group> => Which volume group to be used

Now I am creating a logical volume with 3GB size. I named the logical volume "guides".

$ sudo lvcreate -L 3GB -n guides ostechnix_files
  Logical volume "guides" created.

Step 5 - Display Logical Volumes Information

You can use any one of following commands for viewing logical volume information.

The lvdisplay command gives you detailed information about the logical volume, the associated volume group, volume size, logical volume path, etc.

$ lvdisplay

Or, mention the logical volume's name explicitly:

$ lvdisplay guides
Lvdisplay Command Output
Lvdisplay Command Output

The lvscan and lvs commands will also provide some basic information about the logical volumes.

$ lvscan
$ lvs
Lvscan And Lvs Commands Output
Lvscan And Lvs Commands Output

Step 6 - Formatting and Mounting Logical Volumes

You need to format the logical volume with a file system and mount the volume. Here I am formatting the volume with the ext4 file system and mounting it under the /mnt/ directory.

You should be seeing the device file for logical volume under /dev/volume-group/logical-volume. In my case, the device file will be /dev/ostechinix_files/guides.

$ sudo mkfs.ext4 /dev/ostechnix_files/guides

Sample output:

mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 786432 4k blocks and 196608 inodes
Filesystem UUID: a477d1b6-e806-451f-ab34-4be9978c1328
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

To mount the directory run the following command. You can mount the directory to any location as you wish.

$ sudo mount /dev/ostechnix_files/guides /mnt/

To view the mounted volumes, run:

$ mount | grep -i guides
/dev/mapper/ostechnix_files-guides on /mnt type ext4 (rw,relatime)

You can also run the df command to check info about the mounted file system. You can see the file system is named with volume name. It will be quite easy for you to understand the underlying volume and its group with this naming convention.

$ df -h /mnt/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/ostechnix_files-guides 2.9G 9.0M 2.8G 1% /mnt

To make the mount persistent across reboots, you should add an entry to the fstab. If you have no idea about fstab, I suggest you look at our comprehensive article on fstab.

Conclusion

In this introductory article, we have seen LVM abstraction and how to create volume group and logical volumes using LVM in Linux. In the next article, we will take a look at how to expand and shrink volume space.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More