Skip to content

πŸ“ˆ Extending Disk Using LVM in Linux

In this tutorial, we will cover how to extend a logical volume in Linux using LVM (Logical Volume Management). We'll go through the necessary steps and provide a practical script with detailed comments for clarity.


πŸ“‹ Table of Contents

  1. Understanding LVM and Extending Volumes πŸ”
  2. Steps to Extend a Logical Volume πŸ“
  3. Practical Script πŸ“œ

1. Understanding LVM and Extending Volumes πŸ”

LVM allows you to manage disk space flexibly. You can easily extend logical volumes when you need more space without needing to repartition your physical drives. The process involves:

  1. Adding additional space to a physical volume (either through a new disk or increasing the size of an existing one).
  2. Extending the logical volume.
  3. Resizing the filesystem on the logical volume to utilize the additional space.

2. Steps to Extend a Logical Volume πŸ“

Here’s a step-by-step guide to extend a logical volume:

  1. Identify the Current Logical Volume: Use the lvdisplay command to check the current size and information about the logical volumes.
lvdisplay
  1. Add Space to the Physical Volume: If you are adding a new disk, follow the steps to create a physical volume and add it to the existing volume group. If you're increasing the size of a current disk, ensure it's resized accordingly.

  2. Extend the Logical Volume: Use the lvextend command to add space to the logical volume. For example, to add 10GB to a logical volume named lv_storage in the volume group vg_data:

lvextend -L +10G /dev/vg_data/lv_storage
  1. Resize the Filesystem: After extending the logical volume, resize the filesystem to take advantage of the new space. For example, if you're using ext4:
resize2fs /dev/vg_data/lv_storage

3. Practical Script πŸ“œ

Here’s a complete script to demonstrate how to extend a logical volume using LVM, including comments at each stage:

#!/bin/bash

# Stage 1: Identify the current Logical Volumes
echo "Identifying current logical volumes..."
lvdisplay

# Stage 2: Check available space in the Volume Group
echo "Checking available space in the Volume Group..."
vgdisplay vg_data  # Replace with your actual volume group name

# Stage 3: Extend the Logical Volume
# Specify the size you want to add (e.g., 10G)
SIZE_TO_ADD="10G"
LV_PATH="/dev/vg_data/lv_storage"  # Replace with your actual logical volume path

echo "Extending Logical Volume $LV_PATH by $SIZE_TO_ADD..."
lvextend -L +$SIZE_TO_ADD $LV_PATH

# Stage 4: Resize the Filesystem
echo "Resizing the filesystem on $LV_PATH..."
resize2fs $LV_PATH

# Stage 5: Verify the changes
echo "Verifying the changes..."
lvdisplay $LV_PATH
df -h $LV_PATH  # Check the filesystem size

echo "Logical Volume extended successfully!"

πŸ“ Notes:

  • Run the script with root privileges (use sudo).
  • Modify the script according to your actual volume group and logical volume names.
  • Make sure to back up important data before making changes to disk partitions or logical volumes.