One of the important things that is commonly done with logical volumes is resizing.
So you can make them bigger, you can also make them smaller.
If you want to resize, you need to start using VGS.
VGS is how you can verify the volume group as unused disk space.
If you want to make it bigger, you need disk space to be available in the volume group.
If there is no disk space available, you can use VG-extend to add one or more PVs to the
volume group to make it bigger.
And then you can use LV-extend to grow the logical volume.
When you use LV-extend, it's a good idea to always use minus R. Minus R will also grow
the file system that it is hosting.
And then we have minus uppercase L, which is followed by the size specification.
In this case, one gig is added.
If you forget the minus R option, then you can always resize the file system later.
Using resize to FS for the ext file systems or XFS grow FS, this can be used to grow an
XFS file system.
You should note that shrinking is only possible on ext file systems and not on XFS.
So if ever you foresee a need that you might need to reduce the size of a logical volume,
you cannot use XFS, you must use ext4.
Let me demonstrate how this works.
In this demo, we are going to start by creating a couple of partitions.
So once again, I need to get back to FDisk and add a new partition because we need two
of them.
And oh boy, I can see that my device names have changed again.
But hey, that doesn't really matter.
I'm just going to use this device, partition number two, adding a two gig partition.
And you know what?
I'm creating a few more so that we are done with it once and for all.
So another two gig partition and oops, that's a typo.
And let's create one more so that we have three new partitions.
So number four, first sector is okay, plus 2G to specify the size.
P is showing that we have now four partitions in total.
And I need to set the type for partition number two to LVM.
And also for partition number three, also LVM, as well as partition number four is also
going to be set to LVM.
Once again, P, and now we can see that we are okay.
So I need to use NVMe 0 and 1 P2, well, that will work.
So I am going to use VG create, the VG files, dev, NVMe 0 and 1 P2.
Now you might be wondering, hey, how about these device names that change all the time?
Doesn't that make a difference?
Well, the thing is that when you use VG create, directly this time on NVMe 0 and 1 P2, you
can see that the physical volume is successfully created, and the physical volume information
is written to the device.
And when your system is starting, your system is going to scan all devices for LVM metadata,
and then it will find all the devices.
So it doesn't matter if the device names change, it will always find the LVM metadata.
Now here I use VG create directly without separately using PV create before.
That's something that you can do.
The LVM software is pretty smart and has created the PV automatically for me.
So VGS at this time is showing VG files with 2 gigs available.
Then I'm using LV create minus L255.
I'm addressing the extents directly this time, so I'm telling it use 255 extents, which have
a default size of 4 megabytes.
This N, LV files, that will be the name, and I put it in VG files.
You can use dev VG files or VG files.
It will both work.
Next, I'm going to use MKFS.ext4 on dev VG files, LV files, and that creates the ext4
file system.
Then I can use DF minus H, and DF minus H is showing nothing because it's not mounted
yet.
I need to mount it first in order to see it.
So let me use mount of dev VG files, LV files, on the directory LV files.
I need to create it before using control A to move my cursor to the start of the command
line, and mkdir slash LV files.
I want to do it manually.
Too much work to reboot to have it created automatically.
And now again, DF minus H, and DF minus H right here is showing that I have 919 megs
available on LV files.
Then in VGS, we see that we have 1 gig available.
Now let's use this 1 gig that is available to change the size of the logical volume.
Now on the slide, you can read there is no available extents in the volume group.
Well, that's not exactly true because I had a 2 gig volume group instead of 1 gig volume
group.
But that doesn't matter.
I do want to show you VG extents anyway.
So I'm going to use VG extents, VG files, with the dev NVMe0N1P3.
And then we can see the VG is successfully extended.
Next when I use VGS again, what do we see?
We see that we have 3 gigs available in the volume group VG files.
And now I can use a command like LV extent minus R for resize minus L plus 50% free.
So when you use any size options in the LVM commands, there are two ways.
Either you use minus uppercase L, and you specify the size explicitly, or you use minus
lowercase L, where you can use a relative size or address the number of extents.
And here I'm using plus 50% free.
What does that mean?
Well, that means that the command is going to check and allocate 50% of free extents
from the volume group.
So I do need to specify where I want to do that.
I want to do that on dev VG files, LV files.
And now what do we see?
Well, we see that it has found an exe4 file system.
That's a minus R option taking care of the file system as well.
Then it has resized the logical volume.
Then it has extended the file system.
And in order to do so, it is called resize2fs in the background.
As I told you, the LVM commands are pretty smart.
So it has automatically activated the appropriate command.
And now when we use df minus h, df minus h is showing that dev mapper VG files, LV files
has grown bigger.
And that is how you can resize an LVM logical volume.