Logical Volume Management (LVM) Operations


LVM allows you to manage disk space efficiently by creating, resizing, and managing logical volumes dynamically.


1. Create LVM (Logical Volume Manager)

Step 1: Create Physical Volume (PV)

A Physical Volume (PV) is a raw storage device (disk or partition) that LVM can use.

pvcreate /dev/sdX # Replace /dev/sdX with your disk/partition name

Example:

pvcreate /dev/sdb

Step 2: Create Volume Group (VG)

A Volume Group (VG) is a pool of storage made from one or more PVs.

vgcreate my_vg /dev/sdb

To check available VGs:

vgdisplay

Step 3: Create Logical Volume (LV)

A Logical Volume (LV) is created inside a VG and used like a normal partition.

lvcreate -L 10G -n my_lv my_vg

Alternatively, create using the percentage of VG size:

lvcreate -l 100%FREE -n my_lv my_vg

To verify:

lvdisplay

Step 4: Format the Logical Volume

Before using, format the LV with a filesystem (e.g., ext4):

mkfs.ext4 /dev/my_vg/my_lv

Step 5: Mount the Logical Volume

Create a mount point and mount the LV:


mkdir /mnt/mydata mount /dev/my_vg/my_lv /mnt/mydata

To make the mount persistent across reboots, add an entry in /etc/fstab:

echo "/dev/my_vg/my_lv /mnt/mydata ext4 defaults 0 0" >> /etc/fstab

2. Delete LVM

Step 1: Unmount the Logical Volume

umount /mnt/mydata

Step 2: Remove the Logical Volume

lvremove /dev/my_vg/my_lv

Step 3: Remove the Volume Group

vgremove my_vg

Step 4: Remove the Physical Volume

pvremove /dev/sdb

3. Resize (Extend & Reduce) LVM

A. Extend LVM (Increase Size)

  1. Extend the Logical Volume:

    lvextend -L +5G /dev/my_vg/my_lv

    or use all free space:

    lvextend -l +100%FREE /dev/my_vg/my_lv
  2. Resize the Filesystem:

    • If using ext4:

      resize2fs /dev/my_vg/my_lv
    • If using XFS:

      xfs_growfs /mnt/mydata

B. Shrink LVM (Reduce Size)

⚠ Shrinking is risky! Backup data before proceeding.

  1. Unmount the LV:

    umount /mnt/mydata
  2. Resize Filesystem (for ext4):

    resize2fs /dev/my_vg/my_lv 5G
  3. Reduce Logical Volume:

    lvreduce -L 5G /dev/my_vg/my_lv
  4. Remount the LV:

    mount /dev/my_vg/my_lv /mnt/mydata

4. Take LVM Snapshot

A snapshot is a read-only or read-write copy of an LV at a given point in time.

Create a Snapshot

lvcreate -L 2G -s -n my_snap /dev/my_vg/my_lv

To view snapshots:

lvdisplay

Mount the Snapshot


mkdir /mnt/snapshot
mount /dev/my_vg/my_snap /mnt/snapshot

Delete Snapshot

umount /mnt/snapshot lvremove /dev/my_vg/my_snap

Summary of Commands

OperationCommand
Create PVpvcreate /dev/sdX
Create VGvgcreate my_vg /dev/sdb
Create LVlvcreate -L 10G -n my_lv my_vg
Format LVmkfs.ext4 /dev/my_vg/my_lv
Mount LVmount /dev/my_vg/my_lv /mnt/mydata
Extend LVlvextend -L +5G /dev/my_vg/my_lv && resize2fs /dev/my_vg/my_lv
Shrink LVumount /mnt/mydata && resize2fs /dev/my_vg/my_lv 5G && lvreduce -L 5G /dev/my_vg/my_lv
Create Snapshotlvcreate -L 2G -s -n my_snap /dev/my_vg/my_lv
Delete LVlvremove /dev/my_vg/my_lv

Comments

Popular Posts