logical volume

Introduction to LVM

402 VIEWS

·

Most people still use standard partitioning schemes on their Linux desktops and servers. But as with most things in Linux, you have choices.

An alternative to traditional partitioning is to use Logical Volume Manager, or LVM. This blog post explains what LVM does, and how to use it.

Key Advantages

  1. You can create single volumes from multiple physical hard disks/volumes.
  2. In the case of virtualisation, you can easily add additional space to a virtual machine without a reboot, and online resize your LVM/partition.
  3. Consistent backups can be taken whilst the volume is online by using LVM snapshots.
  4. LVMs can be created to include raid functionality, including Raid 1, 5 and 6.
  5. Entire LVs can be striped across multiple physical volumes, much like Raid 0.

Objectives

In order to learn what can be achieved with LVM, I’m going to assume you’re running a Debian- based distribution (e.g. Ubuntu, Debian); and we’re going to carry out the following:

  1. Create physical volumes with LVM
  2. Create volume groups with LVM
  3. Create logical volumes with LVM
  4. Perform an online resize of a logical volume

Create a physical volume with LVM

You can see if there are any existing volumes in place by running pvdisplay at the command line. If you get a command not found error, then you’ll need to install lvm with apt-get install lvm2.

Now we can move on to create a physical volume. You can find if any are spare by running fdisk -l and taking note of any physical devices without any partitions present. In my case, I have /dev/sdb and /dev/sdc free to use. As such, I will go ahead and run:

Now, I can see the existing physical volumes along with my newly created ones.

Create volume groups with LVM

Now we’re ready to create a volume group with our new physical volumes. I’ll choose to create a volume group called ‘test’ on the new physical volumes we’ve added.

:

Create logical volumes with LVM

For simplicity’s sake, I’m going to create a logical volume utilising 100% of the free space on the volume group by running the following:

The above command creates a logical volume named ‘test-lv’ on the volume group ‘test’.

We can see the result of our work by running lvdisplay :

At this point, we’ll want to create a filesystem on our logical volume to be able to utilise it within Linux. Simply run: mkfs.ext4 /dev/test/test-lv—From here we can simply mount the filesystem in a directory as one normally would by running mount /dev/test/test-lv /mnt/

We can see that the partition is mounted successfully by running df -h /mnt/test/ :

Perform an online resize of a logical volume

So, you’re all up and running with LVM and loving it…But Suzie from management says that a new project is coming your way that requires a whopping 60GB of space on the same server, and you can’t have any downtime. What do you do?!?! :O

This scenario is simple. If you’re running a physical server with hot swappable storage, you can simply add another disk whilst the server is running. Alternatively, as is often the case these days, you may be running a virtual machine where you can add a hard drive to the virtual machine without too much hassle. Once you’ve done this, search for it using fdisk -l the same way as before. Once you’ve found the new disk, simply run pvcreate as we did previously. In my case, it’s pvcreate /dev/sdd .

Now let’s extend our volume group by running vgextend test /dev/sdd where the name of the volume group we created earlier and /dev/sdd is the new physical volume we added:

We can see that we have grown our logical volume and its new size by running vgdisplay test :

At this point we can extend our logical volume by running lvextend -l 100%FREE -n /dev/test/test-lv :

To resize the filesystem resident on the logical volume, simply run resize2fs /dev/test/test-lv and it will perform an online resize!

Conclusion

There is a lot more you can do with LVM, but so far in my career, the functionality shown above has proven to be most useful. Further links below are included to assist you in your LVM journey!

Resources

  1. Further reading on LVM: https://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux)
  2. Old (but still relevant) examples of LVM usage: http://tldp.org/HOWTO/LVM-HOWTO/
  3. An overview of LVM on Debian’s website: https://wiki.debian.org/LVM

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Keith Rogers is an IT professional with over 10 years’ experience in modern development practices. Currently he works for a broadcasting organization in the DevOps space with a focus on automation.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu