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 LV Logical Volumes

Logical volumes (LVs) are an essential part of the Linux Logical Volume Manager (LVM) system. LVM allows for more flexible disk management by enabling you to create, resize, and delete logical volumes without having to repartition the physical disk. In this tutorial, we'll discuss the basics of creating, managing, and removing LVs using LVM.

1. Prerequisites

Before starting this tutorial, ensure you have LVM installed and that you have a volume group (VG) with free space. To check if you have LVM installed, run:

lvm version

To list your volume groups and available space, run:

vgdisplay

2. Creating a Logical Volume

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

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

3. Formatting the Logical Volume

Before you can use the newly created LV, you need to format it with a filesystem. Commonly used filesystems are ext4, XFS, and Btrfs. To format the LV with ext4, run:

sudo mkfs.ext4 /dev/VG_NAME/LV_NAME

For example:

sudo mkfs.ext4 /dev/my_vg/my_lv

4. Mounting the Logical Volume

To mount the LV and start using it, create a mount point directory and use the mount command:

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

To automatically mount the LV at boot, add an entry to /etc/fstab. Open the file with a text editor and add the following line:

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

5. Resizing a Logical Volume

LVM makes it easy to resize LVs. To extend the size of an LV, first ensure that the volume group has enough free space. Then, use the lvextend command followed by the -L option and the new size:

sudo lvextend -L NEW_SIZE /dev/VG_NAME/LV_NAME

For example, to resize "my_lv" to 15GB:

sudo lvextend -L 15G /dev/my_vg/my_lv

Next, resize the filesystem to use the new space:

sudo resize2fs /dev/my_vg/my_lv

To reduce the size of an LV, first shrink the filesystem and then use the lvreduce command:

sudo resize2fs /dev/my_vg/my_lv NEW_SIZE
sudo lvreduce -L NEW_SIZE /dev/VG_NAME/LV_NAME

6. Removing a Logical Volume

To remove an LV, first unmount it and then use the lvremove command:

sudo umount /mnt/my_lv
sudo lvremove /dev/VG_NAME/LV_NAME

For example:

sudo umount /mnt/my_lv
sudo lvremove /dev/my_vg/my_lv

In conclusion, using LVM and logical volumes in Linux allows for more flexible disk management.

  1. Managing LVs with LVM commands:

    LVM provides several commands to manage Logical Volumes. Common commands include lvcreate, lvresize, lvremove, and lvdisplay.

    Example:

    lvcreate -L 10G -n mylv myvg
    
  2. Creating and resizing LVs in Linux:

    • Create a new LV:

      lvcreate -L 5G -n mylv myvg
      
    • Resize an existing LV:

      lvresize -L +2G /dev/myvg/mylv
      
  3. Adding and removing physical volumes in LVM:

    • Add a new physical volume to a volume group:

      pvcreate /dev/sdb1
      vgextend myvg /dev/sdb1
      
    • Remove a physical volume from a volume group:

      vgreduce myvg /dev/sdb1
      
  4. Extending logical volumes in Linux:

    Extend a logical volume to utilize additional space:

    lvextend -L +3G /dev/myvg/mylv
    
  5. Snapshot volumes in LVM:

    Create a snapshot of a logical volume for backup or testing purposes:

    lvcreate --size 2G --snapshot --name mysnapshot /dev/myvg/mylv
    
  6. LVM striping and mirroring explained:

    • Striping:

      lvcreate --type striped -L 20G -n striped_lv myvg
      
    • Mirroring:

      lvcreate --type mirror -m 1 -L 10G -n mirrored_lv myvg
      
  7. Troubleshooting LVM and LV issues in Linux:

    • Check LV status and details:

      lvdisplay
      
    • Repair a damaged VG metadata:

      vgcfgrestore myvg
      
    • Activate a VG:

      vgchange -ay myvg
      
    • Check LV consistency:

      e2fsck /dev/myvg/mylv