< Return to Video

Logical Volume Management - What is LVM & how to use it (and WHY you should use it)

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

more » « less
Video Language:
English
Duration:
16:34

English subtitles

Revisions Compare revisions