Logical Volume Management (LVM) is a system in Linux that allows for flexible management of disk space by abstracting physical storage into logical volumes. It makes it easier to manage disk space, allowing resizing, creating, and deleting partitions without worrying about the underlying hardware configuration.
Key Concepts of LVM
- Physical Volume (PV):
- A Physical Volume is a physical storage device (like a hard drive or a partition) used in LVM. You can create one using an entire disk or a specific partition.
- Example:
/dev/sda1,/dev/sdb, etc.
- Volume Group (VG):
- A Volume Group is a pool of storage that combines multiple physical volumes. It acts as a container where logical volumes are created. VGs can span across multiple physical volumes, making it easier to expand the storage.
- Example:
vg01
- Logical Volume (LV):
- A Logical Volume is the partition created from the volume group. It acts as the usable space for file systems and can be resized dynamically.
- Example:
lv_data,lv_home
- Physical Extents (PE):
- Physical Extents are the smallest units of space (typically 4MB or 32MB) that the physical volumes are divided into. These extents are then assigned to logical volumes.
Benefits of LVM
- Flexibility: You can resize volumes easily, adding or removing storage without worrying about disk boundaries.
- Snapshots: You can take snapshots of logical volumes for backups or testing purposes.
- Efficient Space Utilization: Multiple physical devices can be combined into a single volume group, making space management much easier.
- Dynamic Resize: Logical volumes can be resized dynamically without the need for repartitioning the disk.
Steps to Create and Manage Logical Volumes
1. Creating Physical Volumes (PVs)
The first step is to initialize physical volumes, which are required to create a volume group.
Initialize Physical Volumes:
Use the pvcreate command to initialize disks or partitions as physical volumes.
sudo pvcreate /dev/sdb /dev/sdc
View Physical Volumes:
You can check the list of physical volumes using the pvdisplay command.
sudo pvdisplay2. Creating a Volume Group (VG)
Once you have initialized physical volumes, you can create a volume group.
Create a Volume Group:
Use the vgcreate command to create a volume group by specifying the name of the group and the physical volumes you want to include.
sudo vgcreate my_volume_group /dev/sdb /dev/sdcView Volume Groups:
Use the vgdisplay command to list the available volume groups.
sudo vgdisplay3. Creating Logical Volumes (LV)
After creating the volume group, you can create logical volumes from it.
Create a Logical Volume:
Use the lvcreate command to create a logical volume within the volume group. You need to specify the size of the logical volume and the name.
sudo lvcreate -L 10G -n my_logical_volume my_volume_groupThis command creates a logical volume named my_logical_volume of size 10GB within the volume group my_volume_group.
View Logical Volumes: Use the lvdisplay command to list the logical volumes.
sudo lvdisplay4. Formatting and Mounting the Logical Volume
Now that you have created a logical volume, you can format it with a file system and mount it.
Format the Logical Volume:
Use the mkfs command to format the logical volume with a file system (e.g., ext4).
sudo mkfs.ext4 /dev/my_volume_group/my_logical_volume
Create a Mount Point:
Create a directory to mount the logical volume.
sudo mkdir /mnt/mydataMount the Logical Volume:
Mount the logical volume to the mount point.
sudo mount /dev/my_volume_group/my_logical_volume /mnt/mydataCheck Mounted File Systems:
Use the df -h command to verify that the logical volume is mounted correctly.
df -h5. Resizing Logical Volumes
LVM allows you to resize logical volumes dynamically.
Extend a Logical Volume
To extend a logical volume (and thus increase the file system size), use the lvextend command. After extending, you may need to resize the file system using resize2fs.
sudo lvextend -L +5G /dev/my_volume_group/my_logical_volume sudo resize2fs /dev/my_volume_group/my_logical_volumeReduce a Logical Volume:
Reducing a logical volume requires unmounting the volume first, resizing the file system, and then reducing the logical volume size.
sudo umount /mnt/mydata sudo e2fsck -f /dev/my_volume_group/my_logical_volume sudo resize2fs /dev/my_volume_group/my_logical_volume 8G sudo lvreduce -L 8G /dev/my_volume_group/my_logical_volume sudo mount /dev/my_volume_group/my_logical_volume /mnt/mydata6. Removing Logical Volumes, Volume Groups, and Physical Volumes
Unmount the Logical Volume:
sudo umount /mnt/mydataRemove the Logical Volume:
sudo lvremove /dev/my_volume_group/my_logical_volumeRemove the Volume Group:
sudo vgremove my_volume_groupRemove the Physical Volumes:
sudo pvremove /dev/sdb /dev/sdcLab Exercise: Working with Logical Volumes
Objective
Create and manage logical volumes using LVM, including creating, extending, and removing them.
Steps
Initialize Physical Volumes:
sudo pvcreate /dev/sdb /dev/sdcCreate a Volume Group:
sudo vgcreate lab_vg /dev/sdb /dev/sdcCreate a Logical Volume:
sudo lvcreate -L 5G -n lab_lv lab_vgFormat and Mount the Logical Volume:
sudo mkfs.ext4 /dev/lab_vg/lab_lv sudo mkdir /mnt/lab sudo mount /dev/lab_vg/lab_lv /mnt/labExtend the Logical Volume:
sudo lvextend -L +2G /dev/lab_vg/lab_lv sudo resize2fs /dev/lab_vg/lab_lvUnmount and Remove the Logical Volume:
sudo umount /mnt/lab sudo lvremove /dev/lab_vg/lab_lv sudo vgremove lab_vg sudo pvremove /dev/sdb /dev/sdc 