Logical Volume Management (LVM) Guide

Guide for creating and extending Logical Volume Management (LVM) on Linux.

Logical Volume Management (LVM) is a flexible method for managing storage on Linux systems. It allows you to dynamically adjust storage space without recreating partitions or deleting data. This guide explains how to create, extend, and permanently mount an LVM.

Create LVM

Create a Physical Volume (PV)

A Physical Volume (PV) is the foundation of LVM. It represents a physical disk or partition to be used by LVM. To create a PV, run:

1
pvcreate /dev/sdb

Explanation:

  • /dev/sdb is the disk you want to use for LVM. Replace it with the actual device name of your disk.
  • This step marks the disk as LVM-compatible and prepares it for use in a Volume Group.##

Create a Volume Group (VG)

A Volume Group (VG) is a collection of Physical Volumes. It provides the storage space from which Logical Volumes are created. To create a VG and add the previously created PV, use:

1
vgcreate vg_data /dev/sdb

Explanation:

  • vg_data is the name of the Volume Group. You can choose any name that makes sense for your setup.
  • /dev/sdb is the Physical Volume to be added to the Volume Group.##

Create a Logical Volume (LV)

A Logical Volume (LV) is the actual usable storage space allocated from a Volume Group. To create an LV that uses all available space in the Volume Group, run:

1
lvcreate -l 100%FREE -n lv_data vg_data

Explanation:

  • -l 100%FREE allocates all available space in the Volume Group to the Logical Volume.
  • -n lv_data assigns the name lv_data to the Logical Volume.
  • vg_data is the Volume Group from which the space is allocated.##

Create a Filesystem

To use the Logical Volume, you need to create a filesystem on it. In this example, we use the ext4 filesystem:

1
mkfs.ext4 /dev/vg_data/lv_data

Explanation:

  • /dev/vg_data/lv_data is the path to the Logical Volume.
  • The ext4 filesystem is a common choice for Linux systems, but you can use other filesystems like XFS or btrfs depending on your requirements.##

Extend LVM

If you need more storage space, you can extend an existing LVM. Follow these steps:

Resize the Physical Volume (PV)

If the underlying disk has been expanded (e.g., by adding more storage in a virtual machine), you need to update the Physical Volume:

1
pvresize /dev/sdb

Explanation:

  • This command adjusts the size of the Physical Volume to match the new size of the disk.
  • /dev/sdb is the disk you are resizing.##

Extend the Logical Volume (LV)

To use the additional storage space, extend the Logical Volume:

1
lvextend -l +100%FREE /dev/vg_data/lv_data

Explanation:

  • -l +100%FREE extends the Logical Volume by using all available free space in the Volume Group.
  • /dev/vg_data/lv_data is the Logical Volume you are extending.##

Resize the Filesystem

After extending the Logical Volume, you need to resize the filesystem to utilize the new space:

1
resize2fs /dev/mapper/vg_data-lv_data

Explanation:

  • resize2fs adjusts the size of the ext4 filesystem to match the new size of the Logical Volume.
  • /dev/mapper/vg_data-lv_data is the path to the Logical Volume.##

Add Entry to /etc/fstab

To ensure the Logical Volume is automatically mounted at system startup, you need to add it to the /etc/fstab file.

Display the UUID

Find the UUID of the Logical Volume:

1
lsblk -f

Explanation:

  • The UUID is a unique identifier for the Logical Volume. It is required to add the volume to /etc/fstab.##

Add Entry to /etc/fstab

Add the UUID to the /etc/fstab file so the volume is mounted automatically at boot:

1
echo "UUID=foo /mnt/data ext4 defaults 0 0" >> /etc/fstab

Explanation:

  • Replace foo with the actual UUID of the Logical Volume.
  • /mnt/data is the mount point where the Logical Volume will be accessible. Adjust this to your desired directory.
  • ext4 is the filesystem type. Use the appropriate type if you used a different filesystem.##

Reload Systemd

Reload the Systemd configuration to apply the changes:

1
systemctl daemon-reload

Explanation:

  • This ensures that Systemd recognizes the updated /etc/fstab configuration.##

Mount the Volume

Finally, mount the volume to verify that everything is working correctly:

1
mount -a

Explanation:

  • This command mounts all filesystems listed in /etc/fstab. If there are no errors, the Logical Volume should now be mounted and accessible.