Using LVM - Comprehensive Tutorial
Introduction to LVM
Logical Volume Manager (LVM) is a device mapper framework that provides logical volume management for the Linux kernel. LVM allows you to create, resize, and delete logical volumes, making it easier to manage storage space on your system.
Installing LVM
First, ensure that LVM is installed on your system. You can install it using your package manager.
sudo apt-get install lvm2
sudo yum install lvm2
Creating Physical Volumes (PVs)
Physical volumes are the basic building blocks of LVM. They are typically entire hard drives or partitions.
To create a physical volume, use the pvcreate
command.
sudo pvcreate /dev/sda1
Physical volume "/dev/sda1" successfully created
Creating a Volume Group (VG)
A volume group is a collection of physical volumes. To create a volume group, use the vgcreate
command.
sudo vgcreate my_volume_group /dev/sda1
Volume group "my_volume_group" successfully created
Creating Logical Volumes (LVs)
Logical volumes are created from volume groups and can be resized easily. Use the lvcreate
command to create a logical volume.
sudo lvcreate -n my_logical_volume -L 10G my_volume_group
Logical volume "my_logical_volume" created
Formatting and Mounting the Logical Volume
After creating a logical volume, you need to format it with a filesystem and mount it to a directory.
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_point
Resizing a Logical Volume
One of the advantages of LVM is the ability to resize logical volumes. To increase the size of a logical volume, use the lvextend
command.
sudo lvextend -L +5G /dev/my_volume_group/my_logical_volume
sudo resize2fs /dev/my_volume_group/my_logical_volume
Size of logical volume my_volume_group/my_logical_volume changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents). Logical volume my_volume_group/my_logical_volume successfully resized.
Removing Logical Volumes, Volume Groups, and Physical Volumes
To remove a logical volume, use the lvremove
command. Similarly, you can remove volume groups and physical volumes using vgremove
and pvremove
.
sudo umount /mnt/my_mount_point
sudo lvremove /dev/my_volume_group/my_logical_volume
sudo vgremove my_volume_group
sudo pvremove /dev/sda1