This tutorial provides a comprehensive guide to using the Linux Volume Manager (LVM) on Ubuntu. You’ll learn how to create, manage, and optimize logical volumes for efficient storage management. Each step builds on the previous one, with practical examples and engaging exercises to reinforce your learning.
Step 1: Step 1: Install LVM Tools
Before using LVM, ensure the necessary tools are installed. On Ubuntu, you can install the lvm2 package using the following command:
sudo apt update
sudo apt install lvm2Practice Exercise
Check if LVM is already installed on your system. If not, install it and verify the installation by running a command to display the LVM version.
Show Solution
# Check if LVM is installed
lvm version
# If not installed, run:
sudo apt update
sudo apt install lvm2
# Verify installation
lvm versionStep 2: Step 2: Create Physical Volumes
Physical volumes (PVs) are the foundation of LVM. They are storage devices or partitions that LVM can manage. Use the pvcreate command to initialize a physical volume.
sudo pvcreate /dev/sdb1Practice Exercise
Create a physical volume on a new partition (e.g., /dev/sdc1). Verify the creation using the pvs command.
Show Solution
# Create a physical volume
sudo pvcreate /dev/sdc1
# Verify the physical volume
sudo pvsStep 3: Step 3: Create a Volume Group
A volume group (VG) is a collection of physical volumes. Use the vgcreate command to create a volume group and add physical volumes to it.
sudo vgcreate my_volume_group /dev/sdb1Practice Exercise
Create a volume group named data_vg and add two physical volumes (e.g., /dev/sdb1 and /dev/sdc1) to it. Verify the volume group using the vgs command.
Show Solution
# Create a volume group
sudo vgcreate data_vg /dev/sdb1 /dev/sdc1
# Verify the volume group
sudo vgsStep 4: Step 4: Create Logical Volumes
Logical volumes (LVs) are the usable storage units within a volume group. Use the lvcreate command to create a logical volume.
sudo lvcreate -L 10G -n my_logical_volume my_volume_groupPractice Exercise
Create a logical volume named backup_lv with a size of 20GB in the data_vg volume group. Verify the logical volume using the lvs command.
Show Solution
# Create a logical volume
sudo lvcreate -L 20G -n backup_lv data_vg
# Verify the logical volume
sudo lvsStep 5: Step 5: Format and Mount Logical Volumes
After creating a logical volume, format it with a filesystem (e.g., ext4) and mount it to a directory for use.
sudo mkfs.ext4 /dev/my_volume_group/my_logical_volume
sudo mkdir /mnt/my_mount_point
sudo mount /dev/my_volume_group/my_logical_volume /mnt/my_mount_pointPractice Exercise
Format the backup_lv logical volume with the ext4 filesystem and mount it to /mnt/backup. Verify the mount using the df -h command.
Show Solution
# Format the logical volume
sudo mkfs.ext4 /dev/data_vg/backup_lv
# Create a mount point
sudo mkdir /mnt/backup
# Mount the logical volume
sudo mount /dev/data_vg/backup_lv /mnt/backup
# Verify the mount
df -hStep 6: Step 6: Extend a Logical Volume
LVM allows you to resize logical volumes dynamically. Use the lvextend command to increase the size of a logical volume.
sudo lvextend -L +5G /dev/my_volume_group/my_logical_volume
sudo resize2fs /dev/my_volume_group/my_logical_volumePractice Exercise
Extend the backup_lv logical volume by 10GB and resize the filesystem to use the new space. Verify the changes using the lvs and df -h commands.
Show Solution
# Extend the logical volume
sudo lvextend -L +10G /dev/data_vg/backup_lv
# Resize the filesystem
sudo resize2fs /dev/data_vg/backup_lv
# Verify the changes
sudo lvs
df -hStep 7: Step 7: Reduce a Logical Volume
You can also shrink a logical volume using the lvreduce command. Ensure you back up data before reducing the size.
sudo umount /mnt/my_mount_point
sudo e2fsck -f /dev/my_volume_group/my_logical_volume
sudo resize2fs /dev/my_volume_group/my_logical_volume 5G
sudo lvreduce -L 5G /dev/my_volume_group/my_logical_volume
sudo mount /dev/my_volume_group/my_logical_volume /mnt/my_mount_pointPractice Exercise
Reduce the size of backup_lv to 15GB. Ensure you unmount the volume, check the filesystem, and resize it before reducing the logical volume. Verify the changes.
Show Solution
# Unmount the logical volume
sudo umount /mnt/backup
# Check the filesystem
sudo e2fsck -f /dev/data_vg/backup_lv
# Resize the filesystem
sudo resize2fs /dev/data_vg/backup_lv 15G
# Reduce the logical volume
sudo lvreduce -L 15G /dev/data_vg/backup_lv
# Remount the volume
sudo mount /dev/data_vg/backup_lv /mnt/backup
# Verify the changes
sudo lvs
df -hStep 8: Step 8: Add a Physical Volume to a Volume Group
You can expand a volume group by adding new physical volumes. Use the vgextend command to add a physical volume to an existing volume group.
sudo vgextend my_volume_group /dev/sdc1Practice Exercise
Add a new physical volume (e.g., /dev/sdd1) to the data_vg volume group. Verify the addition using the vgs command.
Show Solution
# Add a physical volume to the volume group
sudo vgextend data_vg /dev/sdd1
# Verify the addition
sudo vgsStep 9: Step 9: Remove a Physical Volume from a Volume Group
To remove a physical volume from a volume group, use the vgreduce command. Ensure the volume group has enough free space to accommodate the removal.
sudo vgreduce my_volume_group /dev/sdb1Practice Exercise
Remove the physical volume /dev/sdb1 from the data_vg volume group. Verify the removal using the vgs command.
Show Solution
# Remove a physical volume from the volume group
sudo vgreduce data_vg /dev/sdb1
# Verify the removal
sudo vgsStep 10: Step 10: Backup and Restore LVM Configuration
Back up your LVM configuration using the vgcfgbackup command. This ensures you can restore your setup in case of issues.
sudo vgcfgbackup my_volume_groupPractice Exercise
Back up the configuration of the data_vg volume group. Simulate a failure by deleting the volume group, then restore it using the backup.
Show Solution
# Backup the volume group configuration
sudo vgcfgbackup data_vg
# Simulate a failure by deleting the volume group
sudo vgremove data_vg
# Restore the volume group from the backup
sudo vgcfgrestore data_vg -f /etc/lvm/backup/data_vg
# Verify the restoration
sudo vgs