Understanding Logical Volume Manager (LVM)

  • Normally, we would have a disk drive and create a partition on it. On top of a partition, we would install filesystem. However, with LVM we also have disk drive and partitions. But, instead of creating filesystem right away, LVM creates volume group, on top of which we would install logical volume, and only then a filesystem.
  • LVM is used to combine multiple physical partitions or physical disks called ‘physical volumes’ into virtual logical volumes. Physical disks are added in one Volume Group. Total size of Volume Group is like a pool of space that can be used to create multiple logical volumes. Each virtual volume is then treated as single component on the system that stores data. Each logical volume consumes space from volume group. On top of logical volumes, we can mount different filesystems.
  • Benefit of LVM is that you can add and/or remove more space when needed and create new logical volumes, or expand and shrink logical volumes to gain more free space.

Let’s digest the image above that explains LVM structure:

  • Physical volume – This is lowest part of the structure and it contains disks and/or partitions from which we create ‘volume group’
  • Volume group – Space used from physical volumes is combined into one area called ‘volume group’. From this pool of free space, we can create ‘logical volumes’ and create filesystem on top of each of them.
  • Logical volume – To create filesystem somewhere, we need logical volume. Logical volume pulls space from volume group and dedicates it to one particular logical volume. Then, we can install filesystem on top of logical volume and use it to store files.
  • Filesystem – filesystems will format logical volumes and create type of filesystem you choose. It is way for system to organize data on a logical volume (or in partitions when LVM is not present). Different types of filesystems have different features. The most common ones are ext4 and btrfs.

Here are the tools used for creating, modifying and deleting LVMs:

  1. pvcreate – To create physical volume
  2. pvdisplay – Show physical volume info
  3. vgcreate – To group physical volumes into a volume group
  4. vgdisplay – Show volume group info
  5. lvcreate – To create logical volume from partitions in each physical volume
  6. lvdisplay – Show logical volume info

Example 1:

Step 1) Create physical volume

# pvcreate /dev/sda

Step 2) Create volume group

We are using vgcreate to create new volume group, specify volume group name and physical disk which will be added:

# vgcreate My_VG /dev/sda

Step 3) Gather info

With vgdisplay we will examine what we have done so far. The vgdisplay will show all volume groups, if present.

# vgdisplay

Step 4) Create logical volume

We use lvcreate command to create new logical volume. Give it a name, with -L flag specify how much space this logical volume gets from volume group. The -n flag gives this logical volume name:

# lvcreate My_VG -L 10G -n My_LV

Step 5) Gather info

# lvdisplay

Step 6) Format logical volume and create filesystem

# mkfs.ext4 /dev/mapper/My_LV

Step 7) Mount new logical volume to the filesystem

# mkdir -p /mnt/lvm/
# mount /dev/mapper/My_LV /mnt/lvm

With this command, our logical volume is part of our filesystem – mounted.

Step 8) Automatically mount new logical volume

This will be done by adding UUID of logical volume to /etc/fstab file.

# blkid /dev/mapper/My_LV
# cp /etc/fstab /etc/fstab.BAK
# umount /mnt/lvm
# nano /etc/fstab
UUID=123e4567-e89b-12d3-a456-426614174000   /mnt/lvm   ext4   0   2
# mount -a

Leave a Reply