Hello guys! Your favorite Linux blogger here(Cough... Cough...)
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.
Logical Volume Management
What is Logical Volume Management?
According to the Wikipedia;
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
Confused? Don't worry, I've got your back. LVM does nothing but combine all the block devices 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.
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.
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.
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.
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.
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
.
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.
Or else a block device can be directly added to a Volume Group as vgcreate
does.
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.
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.
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.
-
-l
- Thel
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 for more info on l
option with %
.
Here is an example of using the 100% of the free storage in the Volume Group.
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.
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.
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;
- 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.
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
.
-
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.
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.
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, Instagram, dev.to and Hashnode.
Top comments (0)