Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux LVM Logical Volume Management

Linux Logical Volume Management (LVM) is a powerful and flexible disk management system that allows you to create, resize, and delete logical volumes (LVs) without the need to repartition physical disks. LVM works by abstracting physical storage devices into volume groups (VGs), which can then be divided into logical volumes. In this tutorial, we'll cover the basics of LVM and its usage.

1. Prerequisites

Before starting this tutorial, ensure that you have LVM installed on your system. To check if LVM is installed, run:

lvm version

2. LVM Components

LVM has three main components:

  • Physical Volumes (PVs): Physical storage devices or partitions used by LVM.
  • Volume Groups (VGs): Containers for physical volumes, which provide a pool of disk space for creating logical volumes.
  • Logical Volumes (LVs): Partitions within a volume group that can be formatted and mounted like regular disk partitions.

3. Create Physical Volumes

First, identify the disk or partition you want to use as a physical volume. You can use the lsblk command to list your available disks and partitions:

lsblk

Next, create a physical volume using the pvcreate command:

sudo pvcreate /dev/DEVICE_NAME

Replace DEVICE_NAME with the appropriate disk or partition name.

4. Create Volume Groups

After creating a physical volume, you need to create a volume group. Use the vgcreate command to create a volume group and add the physical volume to it:

sudo vgcreate VG_NAME /dev/DEVICE_NAME

Replace VG_NAME with a descriptive name for your volume group and DEVICE_NAME with the physical volume you created earlier.

5. Create Logical Volumes

To create a new logical volume within your volume group, use the lvcreate command. Specify the size with the -L option, the name with the -n option, and the volume group to create it in:

sudo lvcreate -L SIZE -n LV_NAME VG_NAME

For example, to create a 10GB logical volume named "my_lv" in the "my_vg" volume group, run:

sudo lvcreate -L 10G -n my_lv my_vg

6. Format and Mount Logical Volumes

Format the logical volume with a filesystem (e.g., ext4, XFS, or Btrfs):

sudo mkfs.ext4 /dev/VG_NAME/LV_NAME

Create a mount point and mount the logical volume:

sudo mkdir /mnt/my_lv
sudo mount /dev/my_vg/my_lv /mnt/my_lv

To mount the LV automatically at boot, add an entry to /etc/fstab:

/dev/my_vg/my_lv /mnt/my_lv ext4 defaults 0 0

7. Manage LVM Components

Use the following commands to manage your LVM components:

  • List physical volumes: sudo pvs or sudo pvdisplay
  • List volume groups: sudo vgs or sudo vgdisplay
  • List logical volumes: sudo lvs or sudo lvdisplay

8. Resize and Remove Logical Volumes

To resize an LV, use the lvextend or lvreduce commands:

sudo lvextend -L NEW_SIZE /dev/VG_NAME/LV_NAME
sudo lvreduce -L NEW_SIZE /dev/VG_NAME/LV_NAME
  1. Introduction to Logical Volume Management in Linux:

    LVM is a flexible storage management system that allows dynamic resizing of logical volumes. It consists of physical volumes (PVs), volume groups (VGs), and logical volumes (LVs).

  2. Creating logical volumes in Linux:

    • Create a physical volume:

      pvcreate /dev/sdb
      
    • Create a volume group:

      vgcreate myvg /dev/sdb
      
    • Create a logical volume:

      lvcreate -L 10G -n mylv myvg
      
  3. Expanding and resizing logical volumes in LVM:

    • Extend a logical volume:

      lvextend -L +5G /dev/myvg/mylv
      
    • Resize the filesystem on the logical volume:

      resize2fs /dev/myvg/mylv
      
  4. LVM snapshots and their use cases:

    • Create an LVM snapshot:

      lvcreate --size 2G --snapshot --name mysnapshot /dev/myvg/mylv
      
    • Use cases: Backups, testing, and data recovery.

  5. Managing physical volumes in LVM:

    • Display information about physical volumes:

      pvdisplay
      
    • Remove a physical volume from a volume group:

      vgreduce myvg /dev/sdb
      
  6. Volume groups in Linux LVM:

    • Display information about volume groups:

      vgdisplay
      
    • Extend a volume group:

      vgextend myvg /dev/sdc
      
  7. LVM mirroring and striping explained:

    • Mirroring:

      lvcreate --type mirror -m 1 -L 10G -n mirrored_lv myvg
      
    • Striping:

      lvcreate --type striped -L 20G -n striped_lv myvg
      
  8. Troubleshooting LVM issues in Linux:

    • Check the status of logical volumes:

      lvdisplay
      
    • Scan for and activate volume groups:

      vgscan
      vgchange -ay
      
    • Repair a damaged VG metadata:

      vgcfgrestore myvg