-
Hello. It's Dorian. And today, I'm going to
-
cover Logical Volume Management
-
or LVMs. I'm going to cover what it is,
-
how to use it, the basics, and why you should use it.
-
So let's get started.
-
[Music].
-
Now, I'm sure many of you have run into
-
the dreaded low disk space notification,
-
and then you have to, you know, start
-
going through your downloads, delete all
-
your large files, delete your games or whatnot.
-
And then you have to think about
-
expanding. So, are you going to add
-
another hard drive and mount your home partition to it or
-
start moving files over to there? Or are
-
you going to buy a larger drive, copy all
-
your partitions over, then extend the partitions?
-
Lots of choices, lots of decisions to
-
make there. But one of the things you could do
-
is install your Linux distribution on an LVM partition.
-
Now, when you read about LVM, it
-
kind of sounds like a RAID array where
-
you have multiple disks and the data is spanned across that.
-
In a way, yeah. Okay. It can be, but it
-
doesn't provide any redundancy or anything, but it does extend your
-
partitions across multiple disks, similar to
-
RAID. And what you can do is just keep adding disks to
-
continue to expand that partition on the fly.
-
So, in the description down below, I've got a link to an LVM
-
cheat sheet, and it basically goes
-
through what I'm going to go through today
-
step by step with the commands. And you
-
could follow along with that if you want--
-
watch the video at the same time, whatever.
-
And I have it in three different formats
-
so you can print it up and use it as a hard copy. But to get started,
-
what is LVM? Well, what it does is it'll take a
-
group of physical disks and create a volume
-
group. And then, from within that volume group,
-
you create logical volumes. So you can have your
-
swap partition, your root partition, and a home partition.
-
Then, once you start running out of room,
-
it's just a matter of adding another disk, and then you can expand your volume group.
-
And then, within the volume group, you can expand your
-
logical volumes. So, I'm going to go through that right now and show you.
-
What I have here is a basic install of Ubuntu. I didn't do anything
-
other than add a couple of favorites and install GParted.
-
So, you can see here this is the main hard drive. It has an EFI system
-
partition and the LVM partition.
-
Now, I set this when I installed it. It was the regular install
-
just like any other install, except you would click on the
-
Advanced button, and then there's an option here to
-
make it an LVM partition. You can also encrypt it with a password.
-
That's up to you if you want to do that or not. I don't do that because it
-
complicates things. And if you're trying to fix something
-
and it can't access it, it just causes another
-
layer of issues. But you can turn that on
-
if you want to. That's completely up to you.
-
And then you just go ahead and proceed
-
with the install like you normally would.
-
If you're trying to convert an existing system into LVM,
-
there is a way to do it, but it involves creating an
-
LVM and copying your existing files over
-
to it and then changing some configurations to make it boot properly
-
and then deleting the old one. It's not ideal. It can get messy and confusing,
-
and sometimes it just won't work
-
depending on how your system is set up.
-
So, I would say just start fresh--a clean install with LVM
-
from the beginning. So, this is a fresh install.
-
I mean, there's a lot of free space and there isn't even a lot of stuff
-
installed here. But let's just say I am starting to run out of room on my LVM
-
partition. It's full of games, movies, music, and what have you.
-
So, what I'm going to do is add another
-
hard drive into my computer, which I have done. So, if I go
-
down here, I have sdb. So, I have a 223 GB SSD in here, and I added another 120 GB. It shows up as a 111, but
-
this is what I added into my system.
-
And it doesn't matter what's on it because you're going to wipe it
-
and everything anyway. So, I just have
-
two EXT4 partitions in here.
-
So, the thing you're going to want to do is wipe everything out
-
that's on it. You could do it with fdisk,
-
but if you're already running a Linux
-
distribution, you might as well just use
-
GParted or some type of graphical application like this.
-
So basically, I'm just going to delete both of these, and I'm going to
-
create a new partition, take up all the space,
-
and it's going to be an LVM to PV as it shows up in GParted.
-
It might just say "Linux LVM" in whatever program you're using, but the important thing is
-
you're creating the LVM file system. So, add that,
-
apply, get that done, and then we can
-
finally get started and start extending our space.
-
So, next thing you're going to do is open up a terminal window. And I know
-
a lot of people are going, "Oh, no. Not the terminal." But it's not--
-
it's not that hard. And you can use
-
that cheat sheet that I have, and it's not a big deal. I'll walk you
-
through everything here.
-
So, some of the commands you're going to use here are pvs.
-
And before I get started, anything that you use here is going to be
-
sudo. So, you can do sudo su to become root so you don't have to
-
keep typing it, but you do have to be root to be able to do it.
-
So, now this is listing our physical
-
volumes. This is whatever disks that you
-
have in your system that have an LVM partition.
-
So, you can see sda2 is the partition that we're actually using for Ubuntu,
-
and you can see here it's mounted as vgubuntu,
-
which is here. Another command to show what's
-
currently going on is you could do vgs,
-
and it's going to show you the vgubuntu
-
volume group. And it is using one physical volume with
-
one disk, and it contains two logical volumes.
-
So, let's go ahead and look at lvs, logical volumes. So, the logical volumes--
-
the two that are listed up here--are root and swap. So, I have the root partition,
-
which is 222 GB, and I have a swap partition,
-
which for some reason Ubuntu only made
-
1 GB, and I'm going to expand that.
-
There's actually a difference in expanding a swap partition than a
-
regular partition, so I'm going to go through those steps as well.
-
So now, if you've used fdisk or another utility and it is not showing up here,
-
then the command you're going to want to use is pvcreate--
-
so physical volume create. And you're basically just going to
-
tell it which physical volume you want added.
-
Now, this isn't going to do anything because it was already successfully created.
-
So, if I do sudo pvs, you can see it's there. It was there
-
before. It's not assigned to any volume group, but if it didn't show up
-
the first time, it should now show up.
-
Okay. So, at this point, we have two physical volumes, and we want to
-
extend our volume group across both
-
disks. So now what you're going to do here is
-
sudo vgextend vgubuntu because that is the volume group we want to extend,
-
and then /dev/sdb1 because we want vgubuntu to extend onto
-
sdb1. So, we'll do that and successfully extend it.
-
So, we can go ahead and do pvs,
-
and now we can see that the volume group
-
is on both physical hard drives.
-
So now, at this point, we haven't changed anything with the
-
root and swap partitions. They are the same size. They are both
-
still on sda2, the original hard drive. What we have to do now is
-
extend those into the new space that is
-
available on the hard drive.
-
Now, as I mentioned before, I only had the 1 GB of swap partition, which to
-
me is fine. I actually don't use
-
any, but if you wanted to resize a swap partition,
-
this is how you're going to do it. The first thing you're going to do is
-
turn the swap off. So, the command is swapoff
-
-a, which will turn off all swap partitions, because you could
-
have more than one. And now we can resize that swap partition.
-
So, let's have a look at the logical volumes--the lvs--and you can see that swap_1 is 1 GB.
-
We need to extend that logical volume now that we have additional space
-
on the drive. So now we're going
-
to resize the logical volume of the swap partition.
-
But one thing that you're going to need to know is the path of
-
where that volume is physically on the drive.
-
So I'll explain this here by running lvdisplay. So Logical Volume Display shows
-
you a lot of information about the logical volumes in your volume group.
-
Now, you can see here the swap partition is
-
here: /dev/vgubuntu.
-
So vgubuntu is acting as a hard drive.
-
Normally, you'd have /dev/sda1 and whatnot.
-
Well, this is vgubuntu because it's a volume group, and then swap_1
-
within that volume group. If you found that confusing, don't worry about it.
-
The biggest thing you need to know is
-
you need to know this path in order to resize the partition.
-
So now we're going to do lvresize
-
and the path of what you want to
-
extend. And you're going to do -L (dash capital L), and then you're going to use disk
-
space here, like gigabytes, megabytes. So, I want an additional
-
7 gigabytes. So I do +7G. You could do
-
M if you want an extra 7 megabytes for some reason, but
-
I'm using gigabytes. So I want an extra 7 gigabytes
-
added to the current size of that swap
-
partition. So I'm going to do that, and the logical volume of swap_1
-
is successfully resized.
-
So you can see it went from 976 MB to 7.95 GB--
-
close enough to 8 GB. Now, because it's a swap partition, you need to do
-
mkswap, and you're going to put that same path again.
-
It's going to format it--no, it's not going to format it because
-
you also need to be sudo. Alright, so that's done. And now we can do swapon again:
-
-a. And if we look at our system monitor, my swap is now showing as 8 GB.
-
So, perfect. Now, that's swap partitions. That is not
-
EXT4 root partitions. That's just something I wanted to cover because it's
-
a different way to do it--because you have to unmount,
-
recreate the swap filesystem, and remount.
-
Resizing the root partition is actually a little bit easier.
-
So again, let's just do lvdisplay. And this is the one we want to resize.
-
So I'm just going to copy that. And now here, we're going to sudo lvresize,
-
just like before. And just like before, I could do
-
-L 100G and then the path.
-
Now, you'll notice before I put the options here
-
after--it doesn't really matter if you put it before or after; it will
-
understand. So I just want to show you that
-
it doesn't matter how you do it. And this should actually be
-
a +100G, but I'm not going to do
-
this. What I want to do is take up all the available remaining space on
-
that drive, because I took 7 GB of the new drive for the swap partition.
-
So now the rest of the space--I want to take it all up.
-
So I'm going to change this to lowercase l, and then I'm going to go
-
+100%FREE. This means I want to take 100% of the remaining free
-
space on the new drive--the new
-
volume group which is now extended onto the new drive, which gave us
-
extra free space. So now I want to resize this
-
root partition to take up all of it. You can see what is available
-
here also by doing a
-
sudo pvs, and you can see here there's 104 GB
-
free on the volume group because we've taken
-
7 away for the swap partition. So this command down here
-
in the other window is going to add 104 GB to our total free space.
-
So you can see here the size of root: 218 GB, 202 GB free.
-
So we're going to go ahead and run our resize command.
-
And you can see root has changed from 222 GB to 326 GB.
-
There's one thing that I forgot,
-
because I'm not reading my own notes on my cheat sheet:
-
you have to resize the filesystem within the logical volume as well.
-
So there's a -r that has to go in there as well.
-
So just run that, and it will also
-
resize the filesystem within the logical volume.
-
So now if we go ahead and
-
run this again, we can see here that root
-
used to be 218 GB with 202 GB free, and it is now 321 GB with
-
301 GB free. So now we have extended
-
our partition for root and swap across two physical drives.
-
So now, going back to GParted here--when this drive
-
fills up and it's out of space, the data is just going to start
-
spilling over into sdb1, the new hard
-
drive that you just added. You can see here the mount point for the LVM
-
partition is vgubuntu, and if you look on sdb,
-
the mount point is also vgubuntu.
-
And then after this, if you fill this disk up,
-
you do the same thing and you just add
-
another drive and extend onto that one as well.
-
However, if that happens on the drive you've added,
-
I would strongly consider getting a much larger hard drive
-
if you're going to add one so that it doesn't happen again.
-
So this is very handy for if you're running a server
-
and you're just collecting data, if you have a lot of pictures
-
and you have a computer that you just
-
use for working on pictures or videos,
-
and you just keep, you know, collecting large files
-
over and over and you just run out of space--well, throw in another hard drive.
-
And run out of space? Throw in another hard
-
drive. Now, the big downside to this is
-
shrinking--going the opposite, if
-
you want to remove one of your physical disks.
-
It's a little more complicated, and I'm
-
not going to go through it because I have tried it, and
-
three out of four times, the system wouldn't boot. Because you can
-
shrink your volumes and you can move the data off the physical disks.
-
But there's still a really good chance that you're going to lose data,
-
and something's going to go wrong. And three out of four times,
-
something went wrong, and the system wouldn't boot. So I just kept
-
trying and trying and trying. Actually, I did try another time.
-
So three out of five times, it failed. Two succeeded, but it took some
-
messing around with, and I don't know.
-
It's not--it's not something you want to do. This is better for just
-
expanding and expanding. So all this
-
in a nutshell--well, a long nutshell--but I tried to keep it short and simple.
-
All this is how you would continue expanding your drives with LVM.
-
Key points are: start with an LVM to begin with right
-
off the bat, get a larger hard drive than
-
you think you would need if you're adding one,
-
and remember that shrinking is not a
-
good option and terrible things can happen.
-
And yeah, I hope this helps you out. So don't forget to subscribe.
-
Don't forget to like the video. Share it on your social media. And until next time, bash on.
-
[Music].