LVM For Beginners

Logical Volume Manager (LVM) is a powerful tool for managing storage on Linux systems. It allows you to create logical volumes that span across multiple physical disks, making it easy to resize, create, and manage storage. In this tutorial, we will cover the following topics:

  1. Introduction to LVM

  2. Prerequisites for using LVM

  3. Setting up LVM

  4. Creating and managing logical volumes

  5. Advanced LVM concepts

Let's get started!

Introduction to LVM

LVM is a logical volume manager for the Linux operating system. It allows you to create logical volumes that can span across multiple physical disks or partitions. This makes it easy to manage storage on your system and to add or remove storage as needed.

One of the main benefits of LVM is that it allows you to resize logical volumes on the fly, without having to unmount them or take them offline. This makes it ideal for use in environments where storage needs may change frequently.

LVM also allows you to create snapshot volumes, which are read-only copies of a logical volume. Snapshots are useful for making backups or for creating test environments.

Prerequisites for using LVM

Before you can use LVM, you will need to ensure that you have the following:

  1. A Linux system with LVM support. Most modern Linux distributions come with LVM support built in, but you may need to install the LVM utilities if they are not already present.

  2. At least one physical disk or partition that you can use for LVM. This can be a hard disk drive, a solid state drive, or a partition on either of these types of devices.

  3. A recent version of the LVM utilities. You can check the version of the LVM utilities by running the lvm version command.

Setting up LVM

Before you can create logical volumes with LVM, you will need to set up LVM on your system. This involves creating a volume group, which is a collection of physical volumes that you can use to create logical volumes.

To set up LVM, follow these steps:

  1. Initialize the physical volume. Run the pvcreate command, followed by the name of the physical volume that you want to use for LVM. For example, to initialize /dev/sda, you would run the following command:
pvcreate /dev/sda
  1. Create the volume group. Run the vgcreate command, followed by the name of the volume group and the name of the physical volume. For example, to create a volume group called vg0 using /dev/sda, you would run the following command:
vgcreate vg0 /dev/sda

You can add additional physical volumes to the volume group by running the vgextend command and specifying the name of the volume group and the name of the physical volume.

Creating and managing logical volumes

Once you have set up LVM, you can create logical volumes within the volume group. A logical volume is a virtual partition that you can use to store data.

To create a logical volume, follow these steps:

  1. Determine the size of the logical volume. You can specify the size of the logical volume in megabytes (MB), gigabytes (GB), or terabytes (TB).

  2. Run the lvcreate command, followed by the name of the volume group and the size of the logical volume. For example, to create a logical volume called lv0 with a size of 10GB in the vg0 volume group, you would run the following command:

lvcreate -L 10G -n lv0 vg0
  1. Format the logical volume. Once you have created the logical volume, you will need to format it with a filesystem. This is done using the mkfs command, followed by the type of filesystem and the name of the logical volume. For example, to format lv0 with the ext4 filesystem, you would run the following command:
mkfs.ext4 /dev/vg0/lv0
  1. Mount the logical volume. After formatting the logical volume, you will need to mount it to make it accessible. To do this, create a mount point (a directory where the logical volume will be mounted) and then run the mount command, followed by the name of the logical volume and the mount point. For example:
mkdir /mnt/lv0
mount /dev/vg0/lv0 /mnt/lv0

To make the logical volume available every time the system boots, you will need to add an entry to the /etc/fstab file. This file contains a list of filesystems that are mounted when the system boots.

Here is an example of an /etc/fstab entry for a logical volume:

/dev/vg0/lv0 /mnt/lv0 ext4 defaults 0 0

This entry tells the system to mount the /dev/vg0/lv0 logical volume to the /mnt/lv0 mount point, using the ext4 filesystem, with the default mount options. The 0 0 at the end of the line specifies that the filesystem does not need to be dumped and that the filesystem should be checked for errors at boot time.

You can also specify specific mount options for the logical volume. For example, to mount the logical volume with the noatime option, which disables the updating of the access time on the filesystem, you would use the following entry:

/dev/vg0/lv0 /mnt/lv0 ext4 noatime 0 0

To make the logical volume available at boot, you will need to ensure that the mount point directory exists. You can create the mount point directory using the mkdir command. For example:

mkdir /mnt/lv0

Once you have added the entry to the /etc/fstab file and created the mount point directory, you can test the entry by running the mount -a command, which mounts all filesystems listed in the /etc/fstab file. If the entry is correct, the logical volume should be mounted and accessible.

To remove a logical volume, you can use the lvremove command, followed by the name of the logical volume. For example:

lvremove /dev/vg0/lv0

Advanced LVM concepts

Here are a few advanced LVM concepts that you may find useful:

  • Snapshots: As mentioned earlier, snapshots are read-only copies of a logical volume. You can create a snapshot using the lvcreate command with the -s flag and specifying the size of the snapshot. For example:
lvcreate -L 1G -s -n snapshot /dev/vg0/lv0
  • Resizing logical volumes: You can resize a logical volume using the lvresize command. To increase the size of a logical volume, use the -L flag and specify the new size. To decrease the size of a logical volume, use the -L flag and specify the new size, followed by the --resizefs flag.

  • Mirroring: LVM allows you to create mirrored logical volumes, which are copies of a logical volume that are stored on separate physical devices. This can improve data availability and reliability. To create a mirrored logical volume, use the lvcreate command with the -m flag and specify the number of mirrors. For example:

lvcreate -L 10G -m1 -n mirror vg0

I hope this tutorial has helped you understand the basics of LVM and how to use it to manage storage on your Linux system. If you have any questions or need further assistance, don't hesitate to ask.

Did you find this article valuable?

Support Joshua Rosato by becoming a sponsor. Any amount is appreciated!