Create and delete logical volumes

5. Configure Local Storage

πŸ“˜Red Hat Certified System Administrator (RHCSA – EX200)


In Linux, Logical Volumes (LVs) are part of the LVM (Logical Volume Manager) system. LVM lets you manage storage more flexibly than traditional partitions. Think of it like creating storage units that can grow, shrink, or move without touching the physical hard drives directly.

Key Terms

  1. Physical Volume (PV) – This is a physical storage device, like a hard disk or SSD, that LVM can use.
  2. Volume Group (VG) – Combines one or more PVs into a single storage pool.
  3. Logical Volume (LV) – A β€œvirtual partition” created from the VG storage pool. Applications use LVs like normal disks.

For this section, you need to create and delete logical volumes.


Step 1: Check Existing Volume Groups

Before creating a logical volume, know what VGs exist and how much space is available.

  • Command:
vgs
  • Shows all volume groups and their free space.
VG     #PV #LV #SN Attr   VSize  VFree
vg_data 1 2 0 wz--n- 50G 20G
  • VFree is the free space in the VG that can be used for new LVs.

Step 2: Create a Logical Volume

The lvcreate command is used to create LVs.

Basic syntax:

lvcreate -L <size> -n <LV_name> <VG_name>
  • -L = size of the LV (e.g., 10G for 10 GB)
  • -n = logical volume name
  • <VG_name> = the VG where you want to create the LV

Example:
Create a 10 GB LV called lv_app in vg_data:

lvcreate -L 10G -n lv_app vg_data

Verify creation:

lvs
LV      VG      Attr   LSize
lv_app vg_data -wi- 10.00G
  • You can also create thin-provisioned LVs, but for the exam, standard LVs are usually enough.

Step 3: Format the Logical Volume

Before using the LV, it must have a filesystem.

  • Example (ext4):
mkfs.ext4 /dev/vg_data/lv_app
  • /dev/<VG_name>/<LV_name> is how LVs are referenced in Linux.

Step 4: Mount the Logical Volume

Once formatted, mount it to use:

mkdir /mnt/app_data
mount /dev/vg_data/lv_app /mnt/app_data
  • Now applications or services can store files on /mnt/app_data.
  • Optional: Add to /etc/fstab for automatic mounting on reboot:
/dev/vg_data/lv_app  /mnt/app_data  ext4  defaults  0  0

Step 5: Resize a Logical Volume (Extra Exam Tip)

Sometimes you need to grow or shrink LVs.

  • Grow LV:
lvextend -L +5G /dev/vg_data/lv_app
resize2fs /dev/vg_data/lv_app
  • Shrink LV:
    Shrinking is riskier; you must first shrink the filesystem, then the LV:
umount /mnt/app_data
e2fsck -f /dev/vg_data/lv_app
resize2fs /dev/vg_data/lv_app 5G
lvreduce -L 5G /dev/vg_data/lv_app
mount /dev/vg_data/lv_app /mnt/app_data

Step 6: Delete a Logical Volume

If you no longer need an LV, delete it to free space in the VG.

  1. Unmount the LV first:
umount /mnt/app_data
  1. Delete LV:
lvremove /dev/vg_data/lv_app
  • Confirm deletion when prompted.
  • Check using lvs to make sure it’s gone.

Real IT Environment Use

  • Application Storage: An application like a database can use an LV for its data files.
  • Temporary Expansion: If a VM needs more disk space, you can grow its LV without changing the underlying physical disk.
  • Snapshots: LVs can be snapshotted for backups before applying changes to production applications.

Quick Reference Commands for Exam

TaskCommand
List VGsvgs
List LVslvs
Create LVlvcreate -L <size> -n <LV_name> <VG_name>
Format LVmkfs.ext4 /dev/<VG>/<LV>
Mount LVmount /dev/<VG>/<LV> <mount_point>
Add to fstabEdit /etc/fstab
Extend LVlvextend -L +<size> /dev/<VG>/<LV> + resize2fs
Reduce LVlvreduce -L <size> /dev/<VG>/<LV> (after resizing filesystem)
Delete LVlvremove /dev/<VG>/<LV>

βœ… Exam Tips

  1. Always check free space in VG before creating LVs.
  2. Remember to format LVs before mounting.
  3. Deleting an LV is destructive; ensure it’s unmounted first.
  4. Know how to read output of lvs and vgs.
Buy Me a Coffee