Logical Volume Manager (LVM) provides several benefits such as dynamic resizing, volume snapshots, and simplified disk management. In this Part, we'll walk through how to achieve these tasks.
Remember to exercise caution as these commands can affect your storage and preferably use a virtual machine and virtual disks to test around as example shown below.
You can use the lsblk
command to check the disk layout of your system
lsblk
Creating Physical Volumes
To create physical volumes on existing partitions, use the following pvcreate
command. This will erase any data in the respective disks :
pvcreate <...list of block devices...>
example:
pvcreate /dev/sdb1 /dev/sdb2 /dev/sdc1
List the created physical volumes
pvdisplay
Creating Volume Groups
Create a volume group with the vgcreate command
vgcreate volume_group_name <...list of physical volumes... >
example
vgcreate vgvolume /dev/sdb1 /dev/sdb2 /dev/sdc1
Display information about the volume group
vgdisplay
Creating Logical Volumes
Create logical volumes using lvcreate
:
lvcreate -n name -L size[G,M] volume_group_name
example
lvcreate -n LV_main -L 7G vgvolume
lvcreate -n LV_secondary -L 1G vgvolume
Display information about the logical volumes
lvdisplay
we can mount the logical volume's by using the mount
command
mount /dev/vgvolume/LV_main /mnt/my_mount_point
Extending LVM
Extending Logical Volumegives a flexible way to manage storage by dynamically resizing logical volumes and effectively utilizing newly added physical storage.
Adding New Physical Volumes:
When additional physical storage is available, connect a new drive to the system. Create a new physical volume on this drive using the pvcreate
command
example:
pvcreate /dev/sdd
Extending the Volume Group:
Extend the existing volume group to incorporate the newly created physical volume
vgextend vgvolume /dev/sdd
Replace vgvolume with the name of your volume group and the physical volume with appropriate name .
Extending Logical Volumes:
Extend the size of the desired logical volume to utilize the added space. In following example command, where LV_main
is the name of the logical volume and i am adding 10GB to LV_main
lvextend -L +10G LV_main
Resizing the Filesystem:
After extending the logical volume, the filesystem within it needs to be resized to utilize the additional space. For ext2/3/4 filesystems, use the resize2fs command or xfs_growfs for XFS filesystems
resize2fs /dev/vgvolume/LV_main
Snapshots with LVM
Snapshots in Logical Volume Management (LVM) offer a powerful way to capture a point-in-time copy of a logical volume, allowing for data preservation, testing, and recovery.
LVM allows you to create a snapshot of a logical volume. In the following example command, you can see how to create a snapshot named snapshot_name of the LV_main logical volume, with a size of 1GB
lvcreate -L 1G -s -n snapshot_name /dev/vgvolume/LV_main
To see the used space of all snapshots, run
lvs
This command provides information about logical volumes, including snapshots and the orgin of the snapshot (ie. the orginal logical volume)
You can mount a snapshot like any other block device. Use this command to mount the snapshot at a specified mount point
mount /dev/vgvolume/snapshot_name /mnt/snapshot_mount_point
And to restore a snapshot, first unmount the original logical volume if it's currently mounted
umount /mnt/snapshot_mount_point
Then, restore the snapshot back to the original logical volume using
lvconvert --merge /dev/vgvolume/snapshot_name
This operation merges the changes made to the snapshot back into the original volume. After restoring a snapshot, you may need to deactivate and reactivate the logical volume to ensure proper functioning
lvchange -an /dev/vgvolume/LV_main
lvchange -ay /dev/vgvolume/LV_main
Using LVM snapshots can provide a safety net when making changes to data or systems, allowing you to experiment without fear of losing critical information. However, keep in mind that snapshots consume space, and frequent or large snapshots can affect overall storage performance. Always monitor your storage usage and consider proper management strategies
Conclusion
In conclusion, Logical Volume Manager (LVM) is a versatile tool that offers dynamic storage management capabilities, making it an indispensable asset for system administrators, DevOps professionals, and anyone involved in managing storage resources efficiently. With LVM, you can resize volumes on the fly, create snapshots for data safety, and simplify disk management tasks. Remember that while LVM is a powerful tool, proper management practices are essential.
Top comments (0)