To understand one of the main advantages of LBM, the logical volume manager,
let's first take a look at a common problem.
Imagine we have this set up
one partition at the beginning of the disk, one in the middle and some free space.
At the end,
it's unpartitioned space.
We wanna make partition two bigger we can easily do.
So we just resize it and expand it to the right
since it has all that free space available next to it.
But what if we want to grow partition one?
Well, this is a problem.
There's no free space at the left or the right,
we can't resize it.
But with LDM, this is easy to solve.
Instead of directly partitioning the disk, we first add it into L VM.
Now imagine we have the same setup but in an L VM managed disk,
if we want to grow the first partition, the following will happen.
The logical volume manager searches for some free space.
It sees that there's some space at the end of the disk.
So it just makes the first partition use the first part of the disk,
same as it did before.
But it also attaches the free space at the end of the disk to the same first partition.
This is how the disk is really used, but this is how L VM presents it to the system.
Basically the first partition is made out of two different areas on the disk now.
But to the operating system,
L VM will present this as a regular continuous partition.
This way, Linux can use it normally without any problems
and we as administrators can resize our partitions without any headaches.
But this is just the core concept of LBM. This tool can do much more.
So let's go through some practical exercises to understand it better.
Now,
we'll need to add three virtual disks to
our virtual machine to have something to work with
these three disks will each be five gig in size.
Now, as you can see on most
Ubuntu systems, this package is already preinstalled.
We'll often see four terms related to LBM PV, BGLV and pe
these abbreviations stand for physical volume,
volume group, logical volume and physical extent.
It's a good idea to memorize the abbreviations and what they represent.
That's because most L VM commands start with one of these two letters.
So let's analyze them in order
PBS first physical volumes,
these represent the real storage devices that L VM will work with.
A PV will often consist of an entire disk or SSD.
Sometimes A PV can also be a partition
to examine what PBS we can use, we enter this command.
This gives us a nice view of what disks and partitions
are available and also what is already used in LBM structures.
For example,
we can see here that slash DEV slash SD
A three is already used as an LBM physical volume
that was created automatically when we installed
Ubuntu. So we'll ignore it in our lesson.
Let's add two of our new disks as physical volumes to LBM.
All we need to do is pass the block
device files that point to these disks or partitions
type in pseudo PV, create
slash DEV slash S DC slash DEV slash SDD.
Now to see what physical volumes are currently attached to L VM
P size shows us the total size of the PV
P three shows us how much of this is
still unpartitioned or not used by logical volume.
Now LDM has the physical devices where it can store data, the PV S.
Next, we need to tell it how to use the storage capacity
to do. So we add PV S to AB G which is a volume group.
The command we'll use to do so is called BG create.
Now, its syntax is BG create
the name of the group and the name of the PV S to use,
for example, to add the slash DEV slash S DC PV to a volume group called my volume.
We would use something like this
but let's do something more interesting and add two PV S at once. Instead of just one,
we can think of a volume group as sort of a virtual disk in L VM.
In our scenario, we added two disks to L VM
slash DEV S DC and slash DEV SDD.
These are two separate five gig disks.
But once we add them to a volume group,
they will be seen as a single continuous 10 gig disk.
Now, why is this useful?
Because we can keep growing our VG indefinitely just by adding more disks
to the server and then adding them to this VG or volume group.
It's like having a single disk that can keep growing as time goes on.
And our server needs to store more data
with a real disk with a fixed size.
We would have to replace it if our data doesn't fit anymore.
This means powering off the server moving data around which is inconvenient
with a VG. We can expand our storage without ever needing to power off the server.
For example, let's say that 10 gig is not enough for our VG. We need more space
to expand our VG. First, we need to add a new physical volume to L VM.
Then we use the BG XTEND command and add this
new PV to the volume group we created earlier.
So now we can take a look at our available volume groups
and there's our volume,
the output tells us that it uses three PV S
and it has 14.99 gig in total size and 14.99 still free because there are
zero LV logical volumes on it to remove a physical volume from a volume group.
We use the BG reduce command.
Of course, we can only remove it if L VM didn't store any data on it yet.
Now, if we don't need this PB anymore, we can also remove it from LBM
by using PV, remove.
OK. So we went from PV S to BGS
and a VG is like a single disk in L VM.
What's missing
this disk or VG is not partitioned yet.
So we need to add an L VA logical volume is similar to a partition in L VM.
Now to create our first LV, we'll use this command.
We've created a two gig lb with a simple name partition one.
At the end,
we specified the volume group where we want to create our logical volume on.
Now, if we inspect our volume groups,
we see we now have one LV in this VG and around eight gig of free space left.
Now let's create a second volume group.
Now let's remember the problem that we had
earlier with a partition scheme like this,
we couldn't grow partition one because it had no free space on its right.
In L VM, we have a similar setup. Now
the first LV is two gig. The second one is six gig
on our 10 gig VG, we should still have two gig of free space left.
So how do we grow our first LV to use all available space
data? And L VM is divided into multiple pe S or physical extents.
For example, imagine we have two incredibly small disks, four megabytes each.
And we add these two PV S to 18 megabyte VG.
We create a logical volume of eight megabyte on this BG.
And now we write an eight meg file
to the application.
It looks like it writes this file to a
file system that exists on a single contiguous disk.
But in reality, L VM has split this data into two physical extents. A
four meg pe will be written on the first hard
disk and another four meg pe will be written on the
second hard disk to tell L VM to expand our first
logical volume to all the extents that it has available.
We'll use this command
note the 100% BG there with dash dash extents, 100% BG.
We basically said use 100% of the storage space available in this VG or volume group.
At the end
my volume slash partition one,
we specified the name of the volume group followed by
a slash and then the name of the logical volume,
we can see our first LV called partition one has been properly resized.
Notice how easy it was to resize a partition logical
volume on the LDM compared to a normal partition.
Let's shrink our first LV back to the original size two gigabytes
with the dash dash size option. We can shrink or enlarge an LV to any custom size.
We want
not empty logical volume is like an empty partition. It's not useful in any way
to be able to store files and directories on an LV.
We need to create a file system on it.
We know that partitions can be found at locations like slash DEV slash SD A one.
But where are logical volumes found
a path to an LV is pretty intuitive.
It kind of looks like this slash DEV slash
name of volume group slash name of logical volume.
Now, we saw how easy it was to resize an LV.
But now that we have a file system on it,
we need to be more careful if we want to resize an LV with a file system on it.
We should not use a command like this.
This would only resize the LV from two gig to three gig,
but the ext four file system would still use two gig.
So we can tell L VM to resize both the LV and the file system on it.
If we pass the dash dash resize FS option.
Keep in mind though some file systems can be enlarged
but they can't be shrink if you ever forget all these commands
either open up the LVM manual
and scroll all the way to the end of it
or you can think about what you want to work with. For example BG A volume group,
you can type that in a terminal BG, then press tab twice to get useful suggestions.
And this concludes our lesson on managing and configuring LDM storage.