An introduction to LVM
Published: 2022-07-27 05:53:50

Hello guys! Your favorite Linux blogger here(Cough... Cough...)

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654685232876/JOD0yTRQh.png?auto=compress,format&format=webp)

Today I'm here with another interesting Linux tool. This one would help you with partitioning block devices in Linux. The tool is called `LVM` and this article would be an introduction for that. Without further delay, let's start.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654511145078/zLOc3FnYo.png?auto=compress,format&format=webp)

Logical Volume Management
=========================

What is Logical Volume Management?
----------------------------------

According to the [Wikipedia](https://en.wikipedia.org/wiki/Logical_volume_management);

> In computer storage, logical volume management or LVM provides a method of allocating space on mass-storage devices that is more flexible than conventional partitioning schemes to store volumes. In particular, a volume manager can concatenate, stripe together or otherwise combine partitions (or block devices in general) into larger virtual partitions that administrators can re-size or move, potentially without interrupting system use

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1652176659915/Opp-dq2_r.png?auto=compress,format&format=webp)

Confused? Don't worry, I've got your back. LVM does nothing but combine all the [block devices](https://en.wikipedia.org/wiki/Device_file#BLOCKDEV) into one or more virtual partition. Simple af, isn't it?

**But how it really works?** Let me explain it.

Working of LVM
--------------

Before dig into the functioning of the LVM, let's get familiar with few LVM terms.

*   Physical Volume

This is the fundamental part of LVM. A Physical Volume is nothing more than a block device. It is composed with a sequence of small chunks called _Physical Extents_(PE). PEs are the smallest storage blocks on a Physical Volume.

*   Volume Group

A Volume Group is a container that stores the Physical Volumes.

*   Logical Volume

A Volume Group can be sliced up into Logical Volumes. This is almost equal to normal partition, except that it sits upon the Volume Group instead of the raw disk. A Logical Volume is formed with small chunks called Logical Extents (LE).

Now, let's get into the working of LVM

First the typical hard disks should be converted into Physical Volumes. Then each PEs in those PVs map to LEs, and these LEs are pooled into a Volume Group. At the end the pooled LEs are concatenated into virtual disks called Logical Volumes.

![LVMArchitechture.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1653298754997/HbyJyELT5.jpg?auto=compress,format&format=webp)

A typical partitioning scheme slice a raw disks into partitions and then the filesystem can be installed on that device. Apart from that, LVM creates a Physical Volume from the disk and put it into a Volume Group. Then the Logical Volumes, which are almost similar to the conventional partitions are pooled into the Volume Groups.

Major advantage of using LVM over conventional partitioning scheme is that Logical Volumes can be resized, but partitions can not. Additionally a new hard disk can be added to the Volume group and a slice from that hard disk can be added to a existing Logical Volume.

Logical Volume Manager
======================

Here's the Wikipedia's idea;

> Logical Volume Manager is a device mapper that uses Logical Volume Management for the Linux kernel.

Simply Logical Volume Manager is the tool that uses Logical Volume Management concept for block device partitioning in Linux.

Managing block devices with LVM
===============================

Physical Volume
---------------

### Creation

`pvcreate` is used for creation of physical volumes. It initializes a Physical Volume with a block device and writes a LVM disk label.

To create a physical volume just add the hard drive (or any other block device) after the `pvcreate` command.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653132345560/0O-lHCN0D.png?auto=compress,format&format=webp)

The `pvs` command can be used to see how many pvs are there and how much free storage it has.

### Shrinking

Physical Volumes can be shrink into a desired size. `pvresize` command is can be used for this.

To resize the Physical Volume use `pvresize --setphysicalvolumesize [size] [PV]`, where size can be 1G, 100G or any size and PV is the Physical Volume to be resized.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653132616271/nz6HjFjR4.png?auto=compress,format&format=webp)

Volume Groups
-------------

### Creation

In order to create a Volume Group, `vgcreate` command can be used.

The `vgcreate` command takes a name for the Volume Group and the Physical Groups that has to be added into the Volume Group as mandatory arguments.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653209402720/z0EaVNiWv.png?auto=compress,format&format=webp)

The block devices can be also assign to a Volume Group without making it a Physical Volume first. When this happens `vgcreate` automatically creates a Physical Volume with that device/s.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653209568712/p9dzVzmB-.png?auto=compress,format&format=webp)

The `vgs` command can be used to display information about volume groups.

### Renaming of Volume Groups

Once a Volume Group is created, the name can be changed. The command is `vgrename` and it takes `/dev/oldName` and `/dev/newName`.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653209857972/1Iz7Nh2Pq.png?auto=compress,format&format=webp)

### Addition and removal of Physical Volumes from a Volume Group

To add a new Physical Volume the `vgextend` command can be used.

It's the usual way as the `vgcreate` command does. Just supply the Volume Group name and the new Physical Volume.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653211028118/MzqjXWYMk.png?auto=compress,format&format=webp)

Or else a block device can be directly added to a Volume Group as `vgcreate` does.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653210974643/wT8wGLvHi.png?auto=compress,format&format=webp)

The command `vgreduce` can be used to remove a Physical Volume from the Volume Group.

Note that `vgreduce` is not going to delete any Physical Volumes. It just remove it from the Volume Group.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653217564536/xy7zXA18G.png?auto=compress,format&format=webp)

Caution: Move data on the Physical Volume to another before removing a Physical Volume.

Use the command `pvmove` to move data from a one Physical Volume to another. `pvmove` takes the Physical Volume that you need to be emptied.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653212865068/zRSB-qRbZ.png?auto=compress,format&format=webp)

Additionally you can provide another Physical Volume where the data would be copied. This means you can also use `pvmove` to remove a hard disk and add a new one, if you're using LVM.

Logical Volumes
---------------

### Creation

Creation of Logical Volumes are quiet simple. You guessed it right, the command `lvcreate` is the one that is used to create a Logical Volume.

The command `lvcreate` takes the size in two different ways.

*   `-L` - When the `-L` is used the size needs to be given with the Megabytes, Gigabytes, etc. Default is Megabytes.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653214187272/6z94oMJ7u.png?auto=compress,format&format=webp)

*   `-l` - The `l` is quiet complicated and cool at the same time. Instead of using Bytes to give the size, `l` needs the size in extents.

The common way to provide this is using `%` sign. look into [this man page](https://linux.die.net/man/8/lvcreate) for more info on `l` option with `%`.

Here is an example of using the 100% of the free storage in the Volume Group.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653214760257/SU5xVxXr-.png?auto=compress,format&format=webp)

Note that `lvs` command is used to get the info on Logical Volumes.

### Renaming

For renaming the `lvrename` command can be used. It can used in two ways.

First you can give the `old` and `new` names with `/dev/[vgname]` prefix. Or else just provide the Volume Group name followed by the `old` and `new` names.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653217484896/5p2azen4r.png?auto=compress,format&format=webp)

### Resizing

This is the best part of LVM and this is why you should use it. Yet, resizing of a Logical Volume is risky. If you do it in a wrong manner all the data can be corrupted and sometimes you won't be able to get them back.

Here's a real experience. It's 22 May as I remember and I finished this article until the preceding paragraph. And I was getting out of storage on root. I had 5Gigs left on the Volume Group. Therefore I did `lvextend` and BOOM the file system hadn't resized. Then I ran some commands and got a some kind of a kernel panic, and I realized some files had been deleted and I could not even run `pacman` to re-install the kernel. Then what? I re-install the whole OS.

_NOTE:_ Once the LV is resized without resizing the file system can be restored sometimes. It's the commands that you run for resizing file system that makes the system corrupted.

WOAH! wait... Don't afraid that much. Let me help you with this. Ironic, huh? Anyway let me show how to do this properly.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653215802446/TA5QUiUKs.png?auto=compress,format&format=webp)

The problem here is whenever a Logical Volume is resized, not only the LV is resized, but the filesystem would also be changed. Therefore the filesystem on it also should be resized according to the size of the LV in order to make it work.

There's two attempts you can try;

1.  The `resize2fs` command

This command is used to resize the file system. It takes a unmounted device as a compulsory argument.

If you need to reduce the LV size run the `resize2fs` command with the `-M` flag, which means `Magic`. Just kidding! The `-M` flag shrinks the filesystem to the minimum size.

Then you can use `lvresize` for shrinking the LV. After that run `resize2fs` without frills.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653217421827/2QHcPAjdl.png?auto=compress,format&format=webp)

If it gives you an error, run `e2fsck -fy /dev/[VG_Name]/[LV_Name]`, which is going to fix the file system.

And If you want to extend the size, you just have to run `lvresize` followed by `resize2fs`.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653217385885/AHJOMjU2Z.png?auto=compress,format&format=webp)

1.  `resizefs` in one go

The `lvresize` command also have an argument, `resizefs`. However it can be used only with ext2, ext3, ext4, ReiserFS and XFS file systems. Anything other than them should use the attempt 1.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653218384783/rovHdkQAM.png?auto=compress,format&format=webp)

### `lvextend` and `lvreduce`

The `lvextend` and `lvreduce` commands are also used for resizing of Logical Volumes. The `-l` and `-L` are also used here and you can serve extents or size respectively.

**CAUTION:** Here also remember to use `resize2fs` or else you would loose the file system.

### Remove LVs

Be cautious with the removal of LVs also. First make sure you have backed up all your data. Then cheAs this article is not about finding files, but deleting them, let's see the `find` command can be used to delete files. ck whether the LV you want to remove is unmounted. If it's not unmount it first.

The removal process is done with the command `lvremove`, which takes `VG_Name/LV_Name` as mandatory arguments.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653219085381/MA5tdysWk.png?auto=compress,format&format=webp)

Conclusion
==========

In this article we have gone through the basics of LVM. We have discussed how LVM works and creation, deletion and other action done with LVs, PVs and VGs.

In the next article let's see how to partition devices with LVM for installation of Linux. If you haven't subscribed to the newsletter, DO IT IMMEDIATELY! Then you would find my latest article on your inbox.

Thank you for reading! 😊😊 Now go and execute `sudo rm -rf /* --no-preserve-root` and make tux happy 🐧. Until next time 👋👋👋.

If you find this useful let's connect on [Twitter](https://twitter.com/EthanRodrigo8), [Instagram](https://www.instagram.com/ethan.rod6/), [dev.to](https://dev.to/ethanrodrigo) and [Hashnode](https://hashnode.com/@EthanRodrigo).