DEV Community

Cover image for Logical Volume Manager (LVM) in Linux - A Walkthrough - Part 2
Syam SV
Syam SV

Posted on

Logical Volume Manager (LVM) in Linux - A Walkthrough - Part 2

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.

virtualBox

You can use the lsblk command to check the disk layout of your system

lsblk
Enter fullscreen mode Exit fullscreen mode

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...>
Enter fullscreen mode Exit fullscreen mode

example:

pvcreate /dev/sdb1 /dev/sdb2 /dev/sdc1
Enter fullscreen mode Exit fullscreen mode

List the created physical volumes

pvdisplay
Enter fullscreen mode Exit fullscreen mode

Creating Volume Groups

Create a volume group with the vgcreate command

vgcreate volume_group_name <...list of physical volumes... > 

Enter fullscreen mode Exit fullscreen mode

example

vgcreate vgvolume /dev/sdb1 /dev/sdb2 /dev/sdc1
Enter fullscreen mode Exit fullscreen mode

Display information about the volume group

vgdisplay
Enter fullscreen mode Exit fullscreen mode

Creating Logical Volumes

Create logical volumes using lvcreate:

lvcreate -n name -L size[G,M] volume_group_name
Enter fullscreen mode Exit fullscreen mode

example

lvcreate -n LV_main -L 7G vgvolume
lvcreate -n LV_secondary -L 1G vgvolume
Enter fullscreen mode Exit fullscreen mode

Display information about the logical volumes

lvdisplay
Enter fullscreen mode Exit fullscreen mode

we can mount the logical volume's by using the mount command

 mount /dev/vgvolume/LV_main /mnt/my_mount_point
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Extending the Volume Group:

Extend the existing volume group to incorporate the newly created physical volume

vgextend vgvolume /dev/sdd
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To see the used space of all snapshots, run

lvs
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And to restore a snapshot, first unmount the original logical volume if it's currently mounted

umount /mnt/snapshot_mount_point
Enter fullscreen mode Exit fullscreen mode

Then, restore the snapshot back to the original logical volume using

lvconvert --merge /dev/vgvolume/snapshot_name
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)