Logical Volume Management in Linux

LVM is a very powerful file system administration tool in Linux. It provides you with the ability to create, extend, resize, and even take snapshots of disk space on live systems. Here are my notes. I created a new hard drive within my test VM. When the server booted, it sees the new drive as /dev/sda. The disk that’s in use by Linux is /dev/vda. To start, we’ll need to partition /dev/sda. Note that you can only have four primary partitions on a single hard drive. Once you reach four primary partitions, if there is any space left on the disk, it will be unusable. Therefore, if you have a couple primary partitions, it’s best to start using logical partitions.

I then created a new 2GB primary partition on /dev/sda. This new primary partition is listed as a “Linux LVM” partition type Linux will see the partition as /dev/sda1. That leaves 6GB of free space in /dev/sda.

Before I start, I’m going to run the pvs, vgs, and lvs commands so that you can see that I haven’t modified anything, yet. Then I’ll add my /dev/sda1 to a “Physical Volume”.

Now is where you have to make your first decision. Do you want to create a new “Volume Group” or add the new drive to an existing “Volume Group”. Both commands are simple. Creating a new volume group utilizes the vgcreate command, whereas extending the phsyical drive to an existing volume uses the vgextend command.
To be able to show both methods, I first extended the physical volume into an existing volume group via the vgextend command. I then removed the the physical volume from the volume group so that I could demostrate creating a new volume group. You don’t need to run through both commands. It’s just a decision that you need to make when you’re creating your volumes.

From here on, I’ll continue with the new volume group “VolGroup01”. After we have extended or created a new volume group, we’ll need to create the actual logical volume. That includes needing to made a decision on the name of the volume, as well as the size of the volume. For this test, I’m going to create a volume called data and make it 1GB. That will leave 1GB for expansion or new volumes.

Once that is completed. You can create a file system, mount it, and start writing data to it.

Now let’s say that I wanted to resize the volume. That could be done with the lvresize, lvextend, or lvreduce commands. Depending on what exactly you’re wanting to do, but the lvresize is generally a better universal command as it will allow you to make the volume larger as well as shrink the volume. Shrinking a volume can be tricky, particularly if you have data on it. It’s always a best practice to fensure that you have a proper backup of the volume, before you resize it. One of the switches with the lvresize command that will make life easier is the ‘-r’ switch, which is the resize2fs, switch. When this switch is enabled, it will unmount the volume, which is needed to shrink the volume, but not extend the volume. It will also run a file system check on the volume and make sure that no data will get lost somewhere in translation, and remount the volume. Just be prepared to execute a restore if the process fails.

As you can see, I shrunk the volume by 500M, verified that the data remained in tact and then grew the volume to the full space (extents) remaining. One of the gotchas with shrinking a volume is to make sure that nothing is accessing the volume that you want to shrink. You can accomplish this with the lsof command. If there are applications or people accessing the volume, you’ll need to either stop the application or get the person to leave the volume as their working directory.
One of the other cool features with LVM is being able to take snapshots. This can be accomplished with the lvcreate command in conjunction with the ‘-s’ switch. You will need to have sufficiant space within your volume group to accomplish this.

As you can see, since I had previously extended the volume to encompass the full space, I had no space left over on the volume group. I then needed to reduce the size of the logical volume to make space available in the volume group for the snapshot creation.

October 1, 2012

Posted In: Filesystems, Linux, LVM, System Administration