How to use LVM in Linux

Let’s walk through a basic example of using Logical Volume Management (LVM) in Linux. In this example, we’ll set up LVM to manage disk space and perform operations such as creating logical volumes, resizing, and removing them.

Step 1: Prepare Your Disk

For this example, let’s assume we have a new disk /dev/sdb that we want to use for LVM.

Step 2: Initialize the Physical Volume

We need to initialize /dev/sdb as a physical volume (PV) for LVM.

sudo pvcreate /dev/sdb

Step 3: Create a Volume Group

Once the physical volume is created, we can create a volume group (VG) using it.

sudo vgcreate my_vg /dev/sdb

Step 4: Create Logical Volumes

Now, within the volume group my_vg, we can create logical volumes (LVs).

# Create a logical volume named "my_lv1" with a size of 10 gigabytes
sudo lvcreate -n my_lv1 -L 10G my_vg

# Create another logical volume named "my_lv2" using the remaining space in the volume group
sudo lvcreate -n my_lv2 -l 100%FREE my_vg

Step 5: Format Logical Volumes

Once the logical volumes are created, we need to format them with a file system. Let’s assume we want to use ext4.

sudo mkfs.ext4 /dev/my_vg/my_lv1
sudo mkfs.ext4 /dev/my_vg/my_lv2

Step 6: Mount Logical Volumes

After formatting, we can mount the logical volumes to directories in the file system.

sudo mkdir /mnt/my_lv1
sudo mkdir /mnt/my_lv2

sudo mount /dev/my_vg/my_lv1 /mnt/my_lv1
sudo mount /dev/my_vg/my_lv2 /mnt/my_lv2

Step 7: Resizing Logical Volumes

If you need to resize a logical volume later, you can do so using the lvresize command.

# Resize my_lv2 by adding 5 gigabytes
sudo lvresize -L +5G /dev/my_vg/my_lv2

Step 8: Removing Logical Volumes, Volume Groups, and Physical Volumes

If you want to remove a logical volume, volume group, or physical volume, you can do so using the following commands:

# Unmount logical volumes if they are mounted
sudo umount /mnt/my_lv1
sudo umount /mnt/my_lv2

# Remove logical volumes
sudo lvremove /dev/my_vg/my_lv1
sudo lvremove /dev/my_vg/my_lv2

# Remove volume group
sudo vgremove my_vg

# Remove physical volume
sudo pvremove /dev/sdb


This example demonstrates the basic workflow of Logical Volume Management in Linux. By using LVM, you can dynamically manage disk space, resize volumes as needed, and handle storage resources more efficiently in your Linux system. Always remember to exercise caution when performing operations that involve data loss risk, and ensure you have proper backups before making significant changes to your storage configuration.

Leave a Comment