Logical Volume Manager
The course is part of this learning path
The Logical Volume Manager introduces extra layers of abstraction between the disks or storage devices presented to a Linux system and the file systems placed on them. This course explores the service and its components. This course is part of the Linux Administration Bootcamp learning path, designed to get you up and running with Linux.
- Understand what the LVM is and how it works
- Create and remove physical volumes (PVs), volume groups (VGs), and logical volumes (LVs)
- Extend volume groups and extend and mirror logical volumes
- Migrate Data from one storage device to another
- Anyone with basic knowledge of Linux who wants to learn more
- Professionals who want to learn more about Linux to enhance their career prospects
This is an intermediate-level course so some knowledge of Linux is expected. If you're just starting out, then try our Linux Fundamentals course first.
At a high level, the process for creating a logical volume is this. First you create one or more physical volumes or PVS. Then you create a volume group from those one or more physical volumes. And then finally, you can create one or more logical volumes from that volume group. Because we're going to be working on system administration type of tasks that will require root privileges, I'm just going to switch to the root user first.
Before you can create a physical volume, you need to know what storage devices are available. The easiest way to do that is with the LVM disk scan command. This command shows you all the storage devices that have the ability to be used with LVM. At least one of these storage devices is currently being used to host the root file system, but the output we see here doesn't really tell us which one that is. We could guess, but let's, don't do that. Let's find out some more information about our block devices with the lsblk command.
Here you can see that sda1 is actually mounted on slash, also known as the root file system. If you wanna display full pass to the devices shown with lsblk, use the -p option. Now it's clear that sda1 is really /dev/sda1. We can further confirm that by using the DF command which shows disk space usage. I like using the -h option to force DF to display sizes in a human readable format. Let me do that here. And you can see here that dev/sda1 is mounted on slash. So LVM disk scan is a great way to see what disks are available.
You can also use the fdisk-l command to see the storage that's attached to your Linux system. Some of that data scrolled off your screen but you can see disk/dev/sde is 100 gigabytes. And the next stanza you can see dev/sda is 10 gigabytes and it shows that sda1 is a partition that's of a Linux type and so on. Now, personally, I prefer the LVM disk scan output, but again, there are multiple ways to view the attached storage devices.
To create a PV, a physical volume, we use the PV create command and give it the path to the storage device. So we know dev SDA is taken but dev SDB and so on are free. So let's create a PV on dev SDB. The PV create command initializes the disk for use by the logical volume manager. You can view the list of PVs with a PVS or PVS command. We can use RPV to create our VG, which is a volume group. I'm going to pretend that I'm creating this volume group for an application. So I'm going to name it VG_app. I like to start the name of all my volume groups with VG_, but this is not required. You can name your volume groups anything you want, but I like the convention of VG_.
So we'll use VG create, the name of the volume group follows the VG create command, which is VG_app. Then we have to provide at least one physical volume, and the PV we just created is dev/SDB. So we'll hit that. Now at the bottom of your screen you can see VG_app was successfully created as a volume group. Now, we can view our volume groups by using the VGs command, VGS and press enter. So here, this output shows us that we have one physical volume that's under the PV column, and the size of this volume group is 50 G or 50 gigabytes. And we have 50 gig free in that volume group.
If we look at our PV, so the PVs command, we should now see what volume group our physical volume belongs to. So sure enough, it says dev SDB belongs to the VG VG_app. Now that we have a pool of storage and a volume group, we can use that storage to create a logical volume. Let's create a logical volume that is 20 gigabytes in size and name it LV_data. Again, I like the convention of LV_ as part of the name of the logical volume, but you don't have to do that if you don't want to. So the command to create a logical volume is LV create, and we need to give it a size. So we do that with dash L, and then we can specify our size in a human readable format. Here I'll use 20 G for 20 gigabytes. If you wanted it to be 100 megabytes, you would specify 100m. If you wanted to do it in kilobytes, you could do K or terabytes, T and so on.
Anyway, so we're going to use 20 G for 20 gigabytes. Next, we need to name our logical volume. And we do that with a dash in option and then provide the name. Like I said, I wanted to name it, LV_data. And then, finally, what we need to provide is the name of the volume group from which this logical volume will be created. And that volume group is VG_app in this demonstration. Just like we can view physical volumes with the PVS command and volume group. So the VGs command, I bet you can guess how we can view logical volumes. That's right, we'll use the LVS command.
By the way, there's another way to view your logical volumes and that is with the LV display command. So let's do that here. The reason you would want to use the LV display command is that it provides a different output. For example, notice the LV path highlighted here at the top. It's /dev/vg_app/lv_data. Now, that's the format for it, /dev followed by the volume group name, followed by the logical volume name. It's easy to tell that the LV_data logical volume belongs to the VG_app volume group, for example.
Now that we have a logical volume, we can treat it like we would any normal disk partition. So let's put a file system on our logical volume and then mount that file system. So we'll use mkfs to create a file system. We'll do the type of ext4 and then supply a path. Okay, it created the file system on our logical volume. Now let's create our mount point. Next we mount that logical volume in the file system on that mount point. So we give mount the path here, and then the path to our mount point. So here you can see that /data has 20 gigabytes available because that's the size that we created our logical volume.
Now let's create a place to store the actual application files for this fictitious application here. Let's call its logical volume LV_app, and let's give it five gigabyte of space. Again, -L, size in a human readable format, 5G, four or five gigabytes. We'll give it a name with the dash in option, and then finally specify the volume group from which we want to create this logical volume. So if we run the Lvs command, we can now see we have LV_app at five gigabyte in size and LV_data at 20 gig in size. Again, we just treat this logical volume like any other partition, and we can put a file system on it and mount it. So let's do that here now. Mkfs-t, ext4 dev/vg_app.
Now let's create a mount point for that. Again, like I said, we just treat this like any other partition, but this time let's put it in the fs tab so that it gets mounted at boot time. So let's see this, edit, let's see, fs tab. So the first column in the fs tab is a path to the storage device, and that device is de/vf_app, LV_app since we're using logical volumes. And then we need to supply a mount point, and we're gonna mount it on /app. File system type, any special settings we want, and we don't really want any special settings, we'll just use the defaults. And then these last two columns here. Going to save my changes. And then now I can just mount that like I would any other file system here, mount/app.
Now we'll read the SEFs tab and no to mount/dev/vg_app/lv_app to /app. Okay, there, you can see it mounted there, and we can even see all of our data here with df-h. By the way, you can access your logical volume through the device mapper path as shown here in the DF output. For example, /dev/vg_app/lv_app can be accessed via /dev/mapper/vg_app-lv_app. So let's just look at all these devices here. So that particular path is assembling to dm-one. Let's look at the mapper path. Okay, it points to the exact same place which is dev/dm-1. Be aware, however, that the /dev/dm-1 might change between reboots. So don't refer to it directly when mounting or configuring the fs tab and so on.
Again, use the path as displayed in the LV display output, which is /dev/ the volume group name, and finally /the logical volume name. So let's look at this here, allow the display, and the LV path is /dev/vg_app/lv_app. So that is the path we want to use. Okay, so let's see how much space we have left in our volume group. And we can do that with the VGs command. This output says we have one PV, two LVS, and that we have 25 gigabytes of free space.
Let's also pretend that our fictitional application generates logs, and we want to keep those log files separate from the other data. So we need to create a logical volume for those logs. If we use -L 25G to specify the size, we are going to get an error. So let me show you that, and then I'll explain to you why we get that error. So we'll do this lvcreate-L 25 gig. We'll name it LV_logs and the VG app volume group. We're about to wait off into the weeds and dive into some very fine details. If you're more of a big picture type of person, just bear with me for the next couple of minutes. But if you like to know every single detail, then you're going to love the next couple of minutes.
Okay, so there is yet another layer of obstruction that we haven't yet talked about. Well, actually, it's two layers of obstruction. So each of our logical volumes is actually divided up into LEs which stands for logical extents. Or if we look at it from the other direction, a collection of logical extents makes up a logical volume. This is how LVM allows us to expand or shrink a logical volume. The logical volume manager just changes the number of underlying logical extents for that logical volume.
Anyway, to view information about these logical extents, use the LV display command. Look at the line that begins with current LE. These are the number of logical extents that comprise this logical volume. That number is 1,280. You can use the -M option to LV display to show a map of the logical volumes. And let's do that here now. So we'll use -M, and this map view tells us that the logical extents for the LV_app reside on the dev SDB desk. Just like an LV is divided into LE, a PV is divided into PEs, which stand for physical extents. So there's this one-to-one mapping of logical extents to physical extents.
Now let's look at the map from the view of the disk using the PV display-M command Physical extents, zero through 5,119 belong to the LV_data logical volume. And they correspond to the zero through 5,119 logical extents of that logical volume. Now, physical extends 5,120 through 6,399 belong to the LV_app logical volume. And they correspond to the zero through 1,279 logical extents of that logical volume. Finally, we see PE 6,400 to 12,798 are unused. This is actually the space we want to use for our new logical volume. If we back up just a bit, we can see that the size of each of the PEs and LEs is four megabytes. That's on the PE size line. Just below the PE size line is the total number of physical extents, which for this physical volume is 12,799. Now, if we were to do the math on that, which would be four times 12,799, we would end up with 51,196 megabytes, which is actually four megabytes shy of 50 gigabytes.
You'll also notice on the PV size line, a message that reads not usable four megabytes. Now, the reason about that four megabyte space being unusable is due to LVM metadata, the alignment of disk sectors and so on. So more or less, you can just think about this little bit of data as a rounding error. This little bit of data is really insignificant in the grand scheme of things. The most important thing here is to know how to get around this error we saw when we tried to create our new logical volume.
Now, let's go back to that LV create command and display that error again. So now, you know why this error message is being given and exactly what it means. We're asking for 25 gigabytes of space, which works out to 6,404 megabyte extents, but we only have 6,399 extents available to allocate. By the way, to be thorough, you can see the number of extents available in the volume group by using the VG display command.
So let's do that really quickly On the free PE/size line, we see that we have 6,399 extents available in our volume group, which is about 25 gigabytes of space. At this point, we can use the lowercase -l option to the LV create command to specify the size and number of extents. Now, let me show you that here, LV create --help to get some help. And let's look at these two lines. So what we've been using is the -L option, and then using a human readable size.
Now, the other way, as I just said, was the dash -l option, which allows you to specify a number of extents. So we could run LV create-_l, 6,399 extents, and do it that way. Or we could use a percentage. So one common pattern is to use 100% of the remaining free space like this. So we can use 100% free, and then we continue with our command. So again, if you look back up here, you can use a number and then percentage of the volume group or free space.
So I'm gonna use 100% free space that we have left, and then continue my command like we were doing earlier, lv_logs will be the name. And then of course, we specify the volume group and hit enter. So that command worked. Now we can look at our logical volumes with the LVS command. Okay, so that's exactly how you can squeeze every possible bit of space out of your volume group and put it into a logical volume.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.