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.
Let's say that the lv_data logical volume is getting full and we need to extend it. If we look at our volume group we'll see that we've already allocated all of our space to the existing logical volumes. So I run vgs, now we get VFree equals zero. In this particular case we need to extend the volume group itself before we can extend the logical volume within that volume group. So lets repeat our initial process of looking for disk with the lvmdiskscan command. So we already know /dev/sda is taken, we just used /dev/sdb and it actually says, hey this is an LVM physical volume that's in use, so it looks like we have /sdc, /sdd, /sde available. So lets use /sdc and let's pvcreate that.
Now we can add this physical volume to our volume group with the vgextend command. We'll give it the name of the volume group and then the path to the disk we want to add or excuse me, the physical volume that we want to add to our volume group. Now if we look at the vgs command we can see we have 2PV under the PV column, 2 physical volumes and at the end we have v3 of 50 gigabytes which means we have 50 gigabytes of free space now. So if we look at our PVs we can now see that /sdb and /sdc both belong to VG_app volume group. We can also see that one PV is completely full and the other PV is completely free.
Now, before we extend our logical volume into this free space, lets look at this space from the file system level. So it's df -h on our data file system here. So it currently shows that the file system size is 20 gigabytes. Now let's use the lvextend command to add five gigabytes of space to that logical volume. In addition to increasing the size of the logical volume, we also need to grow the file system on that logical volume to fill up this new space, and that's what we'll be using the dash r option for. So we'll use lvextend, dash, capital L, plus 5G.
Now, this plus in text means add whatever amount of space we supply after it to the size of our logical volume. If we didn't use plus, we would have to specify the exact end resulting size. I like to use this plus in text because that way I don't have to think about it, I'm just thinking hey, I need to add some more disk space. How much more? Let's add five gigabytes so ill just use plus 5G. It's pretty simple that way. Like I stated just a second ago we need the dash r option to perform the resize of the file system that resides on that logical volume, and then finally we give the path to the logical volume itself. So, we're gonna do this on lv_data.
So if we read the resulting messages it says the logical volume was changed from 20 gigabytes in size to 25 gigabytes in size and it actually shows the number of extents that it needed to do that, and then right below that you see where the file system itself was extended. So now, lets look at the lvs command here, And we see that the logical volume size did in fact increase by five gigabytes.
So let's actually look at the file system level df -h of data. The file system also shows that it's 25 gigabyte in size and so that's really what you want, the file system and the logical volume to be the same size. By the way, if you forget to make lvextend perform the resize of the file system with the dash r option, you'll have to do that after the fact. So let me just demonstrate that. It's a very easy and common mistake to make.
So let's do this again, we'll use our lvextend command, but this time we'll leave off the dash r option. So we're adding another five gigabytes to this logical volume. So it says it went from 25 to 30 gigabytes, we can confirm with the lvs command and it does show 30 gigabytes in size there, however if we look at it from the file system's perspective, its going to be what it was, 25 gigabytes. The file system didn't change, just the logical volume. To fix this, you'll have to use a resize tool for the specific file system that you're working with. For ext file systems, that tool is resized to fs, and we just give it the path to the underlying device which is a logical volume here in our case. So we'll just use resize, 2fs, and then our path here.
Now let's look at the file system layer here. Now the file system has caught up to the size of our logical volume. Its showing 30 gigabytes. Lets look at the map for the this particular logical volume. Let's use lvdisplay dash m for map and then we can just isolate one logical volume by providing its path here. If we look at bottom of this output we see that some of the extents live on /dev/sdb, while the other extents live on /dev/sdc. Again, this is what makes lvm so powerful and flexible, you have one file system that actually spans multiple storage devices. That way, the file system sizes aren't limited to disk sizes.
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.