Logical Volume Manager (LVM) in Linux - A Walkthrough - Part 2
Tags: linux
Published: 2023-08-18 18:55:28

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxlwhahhjoe9s64lq3uk.png)

You can use the `lsblk` command to check the disk layout of your system
```bash 
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 :

```bash 
pvcreate <...list of block devices...>
```
example:
```bash
pvcreate /dev/sdb1 /dev/sdb2 /dev/sdc1
```
List the created physical volumes
```bash
pvdisplay
```

### Creating Volume Groups

Create a volume group with the vgcreate command
```bash
vgcreate volume_group_name <...list of physical volumes... > 

```
example 
```bash
vgcreate vgvolume /dev/sdb1 /dev/sdb2 /dev/sdc1
```
Display information about the volume group
```bash
vgdisplay
```

### Creating Logical Volumes
Create logical volumes using `lvcreate`:
```bash 
lvcreate -n name -L size[G,M] volume_group_name
```
example 
```bash
lvcreate -n LV_main -L 7G vgvolume
lvcreate -n LV_secondary -L 1G vgvolume
```
Display information about the logical volumes
```bash
lvdisplay
```
we can mount the logical volume's by using the `mount` command 
```bash 
 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: 
```bash 
pvcreate /dev/sdd
```
#### Extending the Volume Group:
Extend the existing volume group to incorporate the newly created physical volume
```bash 
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`
```bash
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
```bash
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

```bash
lvcreate -L 1G -s -n snapshot_name /dev/vgvolume/LV_main
```
To see the used space of all snapshots, run
```bash
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
```bash
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
```bash 
umount /mnt/snapshot_mount_point
```
Then, restore the snapshot back to the original logical volume using
```bash 
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
```bash 
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.