Linux Logical Volume Management

Linux Logical Volume Management

What is LVM

The Logical Volume Manager (LVM) is a powerful storage management tool in Linux that provides an abstraction layer over physical storage devices. It allows for the dynamic allocation, resizing, and management of disk space without disrupting system operations.

Why use LVM

Using LVM, you can create storage spaces that can grow or shrink based on your needs, rather than being stuck with fixed partitions that are hard to resize or adjust. LVM helps make storage management flexible.


Architecture

LVM acts as the layer between the physical devices and the file system. It is structured in three layers:

  1. Physical Volume (PV): A single disk or partition of a disk.

  2. Volume Group (VG): A combination of multiple PVs to create a large storage pool, providing flexibility to allocate storage across multiple logical volumes.

  3. Logical Volume (LV): Virtual partitions taken from the volume group for different purposes. They are flexible and can be resized or moved.


Key Components of LVM

  1. Physical Volumes (PVs):

    • The physical storage devices (disks or partitions) are formatted for LVM use.

    • Serve as the foundation for storage in LVM.

  2. Volume Groups (VGs):

    • Combine one or more physical volumes into a single storage pool.

    • Act as the primary space for creating logical volumes.

  3. Logical Volumes (LVs):

    • Virtual storage partitions are created from volume groups.

    • It can be resized or moved dynamically and is used for file systems or raw data.

  4. Physical Extents (PEs):

    • Fixed-size blocks of storage within physical volumes.

    • Form the building blocks for logical volumes.

    • The default size of PE is 4MiB.

  5. Logical Extents (LEs):

    • Mapped directly to physical extents.

    • Define the storage units within logical volumes for flexible management.

    • The default size of LE is 4MiB.


Commands

CommandDescription
pvsDisplay a summary of physical volumes.
vgsDisplay a summary of volume groups.
lvsDisplay a summary of logical volumes.
pvdisplayShow detailed information about a physical volume.
vgdisplayShow detailed information about a volume group.
lvdisplayShow detailed information about a logical volume.
pvcreateCreate a new physical volume.
vgcreateCreate a new volume group.
lvcreateCreate a new logical volume.
vgextendAdd a physical volume to an existing volume group.
lvextendIncrease the size of a logical volume.
lvreduceDecrease the size of a logical volume.
pvremoveRemove a physical volume from LVM management.
vgremoveRemove a volume group.
lvremoveRemove a logical volume.

Steps for LVM Management

Add a Hard Disk

First, add a hard disk of SCSI type of a specific disk size and create

  • Scan the hosts or reboot the system
echo "- - -" > /sys/class/scsi_host/host0/scan # scan all hosts in /sys/class/scsi_hosts
  • Verify that the disk is added


Creating Linux LVM

  1. Partition the Disk

     fdisk /dev/sdb
    


Creating Physical Volume

  1. Create Physical Volume:

     pvcreate /dev/sdb1
    
  2. Display/Verify:

     pvs
     pvdisplay
    


Creating Volume Group

  1. Create Volume Group:

     vgcreate vg001 /dev/sdb1
    
  2. Verify:

     vgs
     vgdisplay
    

Note: Without creating Physical Volume, you can directly create a Volume Group as they are almost identical (not recommended).


Creating Logical Volume

  1. Create Logical Volume:

     lvcreate -L 1GB -n lv001 vg001
    
    • -L: Logical Volume Size

    • -n: Logical Volume Name

  2. Verify:

     lvs
     lvdisplay
    


Formatting Logical Volume

  1. Format the Logical Volume:

     mkfs.ext4 /dev/vg001/lv001
    

  2. Verify with Partition List:

     parted -l
    


Mounting the Logical Volume

  1. Create a Mount Point:

     cd /
     mkdir data
    
  2. Edit /etc/fstab:

     /dev/vg001/lv001 /data ext4 defaults 0 0
    

  3. Mount the Filesystem:

     mount -a
     df -h
    

  4. Verify with lsblk:

     lsblk
    


Extending a Logical Volume

  1. Extend Logical Volume from 1GB to 2GB:

     lvextend -L +1GB /dev/vg001/lv001
    
    • This extra volume is taken from the volume group.

After extending the logical volume, the output of df -h will still not reflect the updated size. This is because the filesystem needs to be informed about the volume extension. Use the resize2fs command to update the filesystem.

  1. Update the Filesystem:

     resize2fs /dev/vg001/lv001
    

  2. Verify:

     lvs
     vgs
    

    You can see that the Logical Volume size is increased by 1GB and the Volume Group size is decreased by 1GB.


Reducing a Logical Volume

  1. Take Downtime:

    • Ensure no one is using the VG.
  2. Unmount Logical Volume: Comment the line form /etc/fstab.

     umount /data
    

  3. Organize Logical Volume Data:

    Running e2fsck ensures the filesystem is consistent and free of errors before reducing a logical volume. This prevents data corruption during the resize process

     e2fsck -f /dev/vg001/lv001
    
    • -f: Force checking even if the filesystem is marked clean.

  1. Resize the Filesystem:

     resize2fs /dev/vg001/lv001 1G
    

    The 1G is the size we want of our logical volume after resizing.

  2. Reduce the Logical Volume:

     lvreduce -L -1G /dev/vg001/lv001
    

  3. Re-mount the Logical Volume:

    • Uncomment /etc/fstab entry.
    mount -a


Deleting Logical Volume

  1. Delete or Comment /etc/fstab Entry.

  2. Unmount the Volume:

     umount /data
    
  3. Remove Logical Volume:

     lvremove /dev/vg001/lv001
    


Deleting Volume Group

  1. Remove the Volume Group:

     vgremove vg001
    


Deleting Physical Volume

  1. Remove the Physical Volume:

     pvremove /dev/sdb1
    


Optimizing Space Usage with Physical Extents (PEs) and Logical Extents (LEs)

  1. Create Physical Volume:

     pvcreate /dev/sdb1
    

  2. Create Volume Group Using PEs:

    • Calculate the required PE size. For example:

      • 1 PE = 4MiB (default).

      • For 5GiB, calculate as 5×1024 = 5120 MiB.

    vgcreate vg001 -s 5120 /dev/sdb1
  • -s: Specifies the size of each physical extent.

  1. Create Logical Volume Using LEs:

    • Logical Volume size calculation:

      • 1 LE = 4MiB (default).

      • For a 1GiB Logical Volume, calculate as 1×1024 = 1024 LEs.

    lvcreate -l 1024 -n lv001 vg001
  • -l: Specifies the number of logical extents.
  1. Format and Mount Logical Volume:

    • Format Logical Volume:

        mkfs.ext4 /dev/vg001/lv001
      
    • Create Mount Point and Edit /etc/fstab:

        mkdir /data
        vim /etc/fstab
      

      Add entry:

        /dev/vg001/lv001 /data ext4 defaults 0 0
      
    • Mount the Filesystem:

        mount -a
        df -h