Hey guys and welcome back.
So now what we're going to do in this nugget right here is to walk through the actual configuration
that we can use when we are setting up LVM.
Now what I'm going to do first is to add some disks to my virtual machine.
You've seen me do this before in a few different nuggets.
What I've done is I've actually reversed my snapshot so that I no longer have those additional
disks.
The reason being is that I just want to have different disks of different sizes to highlight
to you the utility of being able to group these together even if they do not have the
same size you know.
So I'll make sure my device is powered off.
I'll go to settings.
I'll go to storage.
I'll click on SATA and I'll add a new hard disk.
I'll create one.
I'll just say next.
I'll say next again and I'll make the size pretty small.
I'll just maybe make this one 200 or so meg.
I'll say create.
I will go down.
I'll select this and choose this.
So now we've added this one.
I'll add two more again hard disks.
Creating new one.
Next.
Let's maybe make this one a different value.
Maybe say around 400 meg roughly.
Let's create this.
Go down.
Select it and choose this.
This is now also being added and then lastly we'll go to this one here hard disk.
Let's create one.
Let's say next and we'll maybe make this one around 700 meg.
So let's create you.
We'll select this one here.
700 meg.
Let's click choose and we'll say okay.
So what I'll now do is I will power on my machine.
Okay.
So I have now uploaded or uploaded I have now rebooted my machine and we are back on
the command line.
So now that we have added these three new disks, let's actually check for their presence.
We can say LSBLK-F.
We can actually see they are here SDB, SDC and SDD or alternatively we can go into our
dev directory and wildcard search for all of our SD devices.
We can see we have SDB, SDC and SDD.
Perfect.
These three different disks as we know they are all of different sizes.
What we want to be able to do here is to be able to bundle these together into that volume
group.
And once we have that big chunk of logical storage, then we can carve that up and as
many pieces as we like and subdivide it into different partitions with different file systems.
If we so choose, all of those options will be available to us.
The first thing we want to be able to do here is we want to ensure that we can use LVM.
So what I'm going to say is I'm going to say sudo apt install and I'm going to install
LVM2.
So if I enter here and I type in my password and to enter, I'm going to say yes of course
and that should run through the installation of what we need LVM2.
Okay.
So we have now installed LVM2.
Now you may recall in the previous Nuggets when we talked about creating a radaray, one
of the things we had to do was to give that partition a particular label.
Now that label was going to be FD and we were able to do such a thing using FDisk.
Now we want to be able to do the same type of thing with respect to LVM.
Remember we talked about when creating a physical volume, we want to put some type of header
on that physical device.
This is what we're going to do right now.
So we're going to use FDisk and like I say, if I do an LS we have these three devices
SDB, SDC and SDD again, all of different sizes.
So what I shall do here is I'll say sudo FDisk and I'll first start out with SDB and I will
hit enter.
Okay.
So we'll say M for help.
We know to create a new partition.
We can say N.
We will choose a primary partition.
We'll have the partition number, just be the default one.
We will have the sectors at the defaults.
Now remember here is this part here.
Automatically FDisk is going to select a partition of type Linux.
Now this is not what we actually want right now.
What I want to do is I want to change this and give this a very particular type so that
we can have this LVM heading.
So I'll say T and hit enter.
Now we want to do capital L to list all the codes.
Now just like we did with the RAID, we chose FD.
This time the option is going to be Linux LVM and the code here is going to be 8E.
So we will say 8E and then I'll hit enter.
And now we can see here we've actually changed the partition from Linux to Linux LVM.
This is exactly what we want to do.
So we can say M for more help.
We can see how we can actually write these changes by saying W.
So that is exactly what we will do.
Perfect.
So now that is done, we want to repeat the process but for disk C.
So we'll do the same.
We'll say N, P for primary partition.
Default one, we'll go with the default sectors.
We'll change the type and the type will be 8E.
We hit enter.
We are now changing it to Linux LVM and we will write those changes.
And the last one with disk D, again new partition, primary partition, default, default, default.
Change the type to 8E.
Hit enter, Linux LVM and we will write those changes.
Perfect.
So now like I say, we have these three disks marked with the LVM type.
Now the very first command that we want to do is going to allow us to see any physical
volumes we have created.
Now one thing I want you to note here is that right now no physical volumes have been created.
They have been primed to be a physical volume.
So the command we can do is PV display.
But as you're going to see here, we actually need to use it as the root user.
So I will say sudo PV display.
And look at this magnificent output.
We see absolutely nothing.
And this is because we do not have any physical volumes quite just yet.
In order to be able to create a physical volume, we have a very particular command.
And that command is PV create.
So if we go into the man page here, we can see here, this is how we initialize physical
volumes for use by LVM.
Now if we scroll on down, we can see all these different options.
And again, I always encourage you to go through the man page so you can see the different
ways to utilize these tools.
For now, we'll press Q.
And let me show you the basic setup that we want to utilize here.
I'm going to say PV create.
And I'm going to select my disks now just for posterity.
Remember the dislocations.
We're going to use SDB1, which is the partition which is marked as LVM, SDC1 and SDD1.
These are the three ones we are targeting.
So what I'll say is sudo PV create.
And now all I'm going to do is specify those devices.
So dev SDB1, that's the first device that we want to include.
Then I'll say dev SDC1, the second device.
And then dev SDD1.
We can see a physical volume has been successfully created.
And now if we actually use our command PV display, and I don't match the keypad, that
would be a lot better.
And of course, we need to do sudo.
If I hit enter, check this out.
We can see the output right here.
New physical volume, we can see the name is dev SDB.
We can see the actual size here.
Remember that was the first size that we used when we created our disk, just over 200 meg.
Then we see the next disk, dev SDC.
We see the size here, just over 400 meg.
And the last one is SDD1, just over 700 meg.
So this is kind of like what we talked about.
In fact, not kind of like.
This is exactly like what we talked about.
We have ultimately added three different disks, ones around, like I say, 200 meg.
One was around 400.
And the third one, a little bit bigger at around 700 meg.
Very, very roughly, of course.
All of these have been marked with the correct header, and they've all been created to be
physical volumes.
That means that they are now primed and ready to be combined into a volume group, such that
we have one big logical volume of size, 1.3 gig.
So how on earth do we actually transfer these physical volumes so that we can combine them
into this volume group?
Well, it's a very, very similar process.
The first thing we will do is see how we can check if we do have any volume groups created.
The command we will use is very similar.
It's not PV display.
This time it's VG display, volume group display.
So if I hit Enter, again, we get this absolutely magnificent output, absolutely nothing at
all.
So what we have to do is, just like what we did with PV create, we now need to be able
to create that volume group.
Now, the command we're going to use this time is going to be VG create very, very similar.
And we can see here this command allows us to create a volume group and it says here,
as the description says, creates a new VG on block devices.
Now, one thing to note here is that even though we used the PV create command to be very explicit
because again, we want to know how these tools work for the purposes of the examination,
notice that if you happen to use block devices that have not previously been initialized
as physical volumes with the PV create command, VG create will actually initialize them and
make them PVs.
So what I'm saying here is, is that even though we want to be aware of the PV create command
and to be able to initialize it manually yourself, notice that the VG create command can actually
do this for us automatically.
So just understand this difference.
And if we scroll on down, we're going to be able to see the different options that we
can use with this command.
Again, I always say read the man page.
But for now, what we'll do is we will just dive in and create our first volume group.
What I'll do here, as I will say, pseudo VG create.
And the first thing that I want to do is I want to give my volume group a name.
So I'll just call it my storage chunk, call this anything you wish though.
There's nothing special about this name, obviously.
And now what I want to do is I want to specify which physical volumes are going to be part
of my volume group.
Well, we have devsdb1.
That is a PV that has been initialized and ready to go.
I also want to add SDC1, the second disk, and the third one was SDD1.
So if I hit Enter now, we can see a volume group called my storage chunk has now successfully
been created.
So you can imagine therefore, if I do a VG display, we're now going to see some information
relating to this.
So check this out then.
We now have a volume group called my storage chunk.
The format is LVM2.
We can actually see it is comprised of three different physical volumes.
And we can see the actual size of the VG.
We now have a logical size of just over 1.3 gig, as I said.
1.35, it happens to be.
And we can cut this up in as many pieces as small or as large as we choose and format
those pieces as we wish using different file systems if we need to use different file systems.
So the next component, now that we have our volume group, are those logical volumes.
That is the subdivisions that we can carve this volume group into.
So can you maybe guess what the command is to actually see the output?
If you happen to follow the same format and say LV display, you would be absolutely correct.
And as we expect, we get that same amazing output of absolutely nothing.
That is because of course, we haven't actually subdivided our volume group into logical volumes.
So we will do that now.
Again, can you maybe guess what the command is going to be if you say LVCreate?
You would be absolutely correct.
We can see here LVCreate allows us to create a logical volume.
And again, of course, we can see all these different options and been able to name a
particular logical volume.
So one so forth.
So again, have a skim through the man page.
But for now, what I'll do is I'll just press Q to go back and let's try subdividing this
volume group.
So let's create our very first chunk.
Okay, so I will say sudo LVCreate and I will say dash N to specify the name of the logical
volume I want to create.
I'll just call this one little chunk because it's going to be a little chunk from my volume
group.
What I want to do is to specify the size.
Now if I want to specify a fixed size, I can use the dash capital L flag.
I like to remember this as how large I want to make this size.
And I'll just take a 50 meg chunk.
Now whereabouts do I want to take my chunk from?
I want to take it from my volume group, which is called my storage chunk.
So if I hit enter, we can see the logical volume little chunk has now been created.
So if I say sudo LVDisplay and to enter, we can see here, we have actually created little
chunk.
It's taken from my storage chunk and the size is around 50 meg.
Perfect.
So notice the actual path here.
It's now shown the path is dev, then the name of the volume group and then the name
of the logical volume.
So check this out.
What if I wanted to create another chunk from my storage chunk?
Now what I did the first time, I specified a fixed size.
Okay.
I just said give me 50 meg.
Now if you recall, I do believe the size of the volume group was 1.35 gig.
We have now taken 50 meg from that.
So we should now have 1.3 gig left.
If I do a sudo VG display, we can actually see allocated.
We now have 52 meg and the free space is now 1.3 gig.
Perfect.
Now the first time, like I say, I specified a fixed size of around 50 meg.
What if I just wanted to take what is left, i.e. the 1.3 gig and just take a percentage
of that?
Just say, okay, whatever I've got left, give me 50% of that or give me 30.
30% of that.
Well, I can actually still do that using the LVCreate command.
The syntax is a little bit different.
Let me show you what that looks like.
We would do sudo LVCreate and I'll do N.
Let's just call this one medium chunk.
Okay.
And now I will do dash lowercase L and I'll make this one 40% of the free space.
So we now have 1.3 gig free.
I want to take 40% of that 1.3 and assign it to medium chunk.
Now, again, whereabouts am I going to take this chunk from?
We're going to take it from the volume group called myStorageChunk.
So if I hit enter, we can now see another logical volume has been carved out from myStorageChunk.
So if we do sudo LVDisplay and hit enter, at the very top we see lilchunk.
But now here we also have this new logical volume.
It's called medium chunk.
And again, notice the path.
It's dev, then the volume group, and then the logical volume.
Now check out the size of this.
It is now 528 meg.
That is actually 40% of 1.3 gig.
Quick calculator, 1.3 gig times that would be 0.4 equals 0.52, otherwise known as 520
meg.
That is pretty much what we have got right here.
See that?
So this is obviously a very, very nice way for you to be able to manage the size of your chunk
just by carving out neat little percentages of whatever is left over.
So like I say, we want to be making a note of these locations.
Dev myStorageChunk, lilchunk, and dev myStorageChunk, medium chunk.
What we're now going to do is we're going to put a file system on these logical volumes.
So what I'll do is I'll say MKFS.
In fact, they'll use the sudo command MKFS.
The first one I'll give an EXT3 file system, and I'll say dev myStorageChunk and then lilchunk.
Perfect.
So that is now being created.
And we'll do the same for sudo MKFS.
Let's give this one EXT4.
And we'll say dev myStorageChunk.
And this one is called medium chunk.
And if we hit enter, that one is now done.
If I do an LSBLK-F, we can see we have an LVM member two for this device and this partition.
Same again with this one and our third one.
But notice lilchunk right here has an EXT3 file system, whereas medium chunk here has an EXT4 file system.
Now you will notice of course that neither of these file systems happen to be mounted.
But we can do that no problem at all.
So what I could do here is I could maybe make a directory.
Let's just call this directory.
Let's just call it small one.
Oh, and I'll need to do sudo of course.
So let's go to my root directory, do an LS.
We can see we have the small one, directory, and I'll do sudo MKDR, medium one.
There we are there.
So let's mount these two logical volumes with different file systems in these different locations.
So what I will say is sudo mounts.
And the path of the first one will be dev my storage chunk for the volume group.
And then the logical volume will be lilchunk.
And whereabouts do I want to mount this?
I'm going to mount it in the folder called small one.
Perfect.
And if I do something very similar with the second one also, sudo mount dev my storage chunk.
And then medium chunk.
And we will mount that in the medium one directory.
Cool.
So for now say mounts and I grep for just the word chunk.
We can see here this is mounted now.
I want you to notice here is that when this is mounted, the format looks a little bit different.
We do want to be aware of this location.
forward slash dev forward slash mapper.
And then it's going to be the name of the volume group separated with a hyphen.
And then the name of the logical volume again.
Dev mapper volume group hyphen logical volume.
So now what we can do is I can go into say for example my small one directory.
And I'll just create a file called file one dot txt.
I'll just say this.
In fact, I better go into sudo sudo file one dot txt.
And I'll just say this is some data stored on lilchunk.
So save you go back out and I could go into medium one gain sudo nano file two.
So this is stored on medium chunk.
Save you write it out.
So as you can see here, what we've done is we've been able to take three different discs
of three different sizes and combine them to make one big logical chunk.
This was called my storage chunk.
And then what I did was I was able to carve off a little slice called lilchunk.
Give it a particular file system in that case ext three.
And I could mount that file system in a particular directory.
And I could begin populating this partition file system with data as I so needed.
Now it just so happens again, I just put on a simple text file.
But you can imagine you could put whatever type of data you wanted on.
And then like I say, I happened to carve out another chunk from this that took up 40%
of what was remaining after we created the first logical volume.
And we were able to add a completely different file system on this one.
Ext four.
And just like we saw before, we were able to mount this and then add in data.
Once again, the only data we added was simple text data.
But there is nothing stopping you from adding images or grips or MP3 files, whatever you so need.
The point is, is we were able to aggregate that data from this, even though they had different sizes,
create this big chunk and subdivide that chunk in as many ways as we wished.
So as you can see here, LVM is very, very useful.
The commands to be able to configure LVM are very, very similar when you're dealing with
physical volumes, volume groups and logical volumes.
But there are some more cool features that we want to explore with respect to LVM.
We'll get to talk about those in the very next nugget.
So I hope this has been informative for you and I'd like to thank you for viewing.