π 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
- Understanding LVM and Extending Volumes π
- Steps to Extend a Logical Volume π
- 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:
- Adding additional space to a physical volume (either through a new disk or increasing the size of an existing one).
- Extending the logical volume.
- 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:
- Identify the Current Logical Volume:
Use the
lvdisplay
command to check the current size and information about the logical volumes.
-
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.
-
Extend the Logical Volume: Use the
lvextend
command to add space to the logical volume. For example, to add 10GB to a logical volume namedlv_storage
in the volume groupvg_data
:
- 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
:
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.