- 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:
- pvcreate – To create physical volume
- pvdisplay – Show physical volume info
- vgcreate – To group physical volumes into a volume group
- vgdisplay – Show volume group info
- lvcreate – To create logical volume from partitions in each physical volume
- lvdisplay – Show logical volume info
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.
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
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 UUID=123e4567-e89b-12d3-a456-426614174000 # 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