Logical Volume Manager (LVM) is used on Linux to manage hard drives and other storage devices. As the name implies, it can sort raw storage into logical volumes, making it easy to configure and use.
In this guide, you’ll learn how LVM works on Linux systems. There’s no better way to learn about LVM than simply running through an example, which is exactly what we’ll do in the steps below. LVM works the same on any Linux distribution, so you can use any of the commands below on your own system.
Follow along with us as we use LVM to create partitions, physical volumes, a virtual group, logical volumes, and filesystems on a hard disk. We’ll also show how to mount, extend, and remove our newly created logical volumes. By the end of this tutorial, you’ll have a full understanding of how to use LVM and apply your own configurations.
In this tutorial you will learn:
- How to install LVM on major Linux distros
- How to create partitions
- How to create physical volumes
- How to create a virtual group
- How to create logical volumes
- How to create a filesystem on logical volumes
- How to edit fstab to automatically mount partitions
- How to mount logical volumes
- How to extend a logical volume
- How to remove a logical volume
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux systems|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Install LVM on major Linux distros
Your Linux system may already have LVM installed, but it doesn’t come installed by default on every distro. Use the appropriate command below to install LVM with your system’s package manager.
$ sudo apt install lvm2
$ sudo dnf install lvm2
$ sudo pacman -S lvm2
The first thing we will do is create partitions on our disk. This is to facilitate the creation of physical volumes in the next section, which can either be created on raw, unpartitioned block devices or single partitions. For the sake of this tutorial, we will work on the latter.
For this example, the disk we’ll be working with is
/dev/sdb, which is a 5GB (and currently unpartitioned) hard disk. Refer to the diagram at the beginning of this guide to visualize the configuration we’ll be setting up.
We can see our
/dev/sdb disk and its pertinent details with the following command.
# fdisk -l
Next, let’s partition the disk with
# cfdisk /dev/sdb
An interface will open in your console, which is quite intuitive to use. We’ve created the following two per partitions, as per our diagram in the beginning.
Finalize your changes by choosing “write,” then exit the utility when done. We can now see our partition listed when we execute
fdisk -l again.
Create physical volumes
We can now create physical volumes on our new partitions by using the
# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created. # pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created.
pvdisplay command to see information about all the physical volumes on your system, or specify a particular volume you wish to view details about.
# pvdisplay OR # pvdisplay /dev/sdb1
Create a virtual group
At this stage we need to create a virtual group which will serve as a container for our physical volumes. In this example, we’ll call our virtual group “mynew_vg” which will include the
/dev/sdb1 partition, with the following Linux command:
# vgcreate mynew_vg /dev/sdb1
Or, to include both partitions at once:
# vgcreate mynew_vg /dev/sdb1 /dev/sdb2
Use the following command to display information about the virtual group(s).
We can add more physical volumes to the group by using the
# vgextend mynew_vg /dev/sdb2 Volume group "mynew_vg" successfully extended
Create logical volumes
Now we can move on to creating logical volumes. It may help to think of our virtual group as a “big cake,” from which we can cut “pieces” (logical volumes) that will get treated as partitions on our Linux system.
The following command will create a logical volume named
vol01 with a size of 400MB.
# lvcreate -L 400 -n vol01 mynew_vg
Then, we’ll create another volume named
vol02 with a size of 1GB. Again, refer to the diagram above to help visualize the configuration.
# lvcreate -L 1000 -n vol02 mynew_vg
Finally, we can use the
lvdisplay command to see the logical volumes we’ve just created.
As you can see from the screenshot below,
vgdisplay shows us that we still have 3.6GB of free space in the
mynew_vg virtual group.
Create a filesystem on logical volumes
The logical volume is almost ready to use. All we need to do is to create a filesystem on it with the
# mkfs.ext4 -m 0 /dev/mynew_vg/vol01
-m option specifies the percentage reserved for the super-user, we can set this to 0 to use all the available space (the default is 5%).
Edit fstab to automatically mount partitions
For the filesystem to be automatically mounted, we should add an entry for it into the
/etc/fstab file. This will mount the partitions for us when the computer boots up in the future.
# nano /etc/fstab
The entry you add should look something like the screenshot below.
Mount logical volumes
In order to use our new volumes, we’ll need to mount them. Don’t forget to also create the mount point first.
# mkdir /foobar # mount -a
Extend a logical volume
The biggest advantage of a logical volume is that it can be extended any time we are running out of space. For example, to increase the size of a logical volume and add other 800 MB of space, we can run this command:
# lvextend -L +800 /dev/mynew_vg/vol01
Notice in the screenshot below that the command doesn’t actually increase the size of the filesystem, but only that of the logical volume.
To make the filesystem grow and use the added space we need to resize the filesystem with the following command.
# resize2fs /dev/mynew_vg/vol01
On some systems, especially older ones, you may be required to unmount the volume and run
e2fck before being able to extend it.
# umount /foobar # e2fck -f /dev/mynew_vg/vol01 # resize2fs /dev/mynew_vg/vol01
Remove a logical volume
lvremove can be used to remove logical volumes. We should make sure a logical volume does not have any valuable data stored on it before we attempt to remove it. Moreover, we should make sure the volume is not mounted.
# lvremove /dev/mynew_vg/vol02
In this guide, we took you through the step by step instructions to create two physical partitions within the same volume group using LVM. We also saw how to add a filesystem, mount the partitions, and extend the logical volumes. This should give you a good understanding of how LVM works, and how to use it to apply your own storage configurations.