Open Source

Set up Logical Volume Manager in Linux

You can use the Logical Volume Manager (LVM) in Linux to create virtual drives, and when used with RAID, LVM provides redundancy. Vincent Danen's tip will show you how to create your first volume.

Logical Volume Manager (LVM), is a mechanism to create virtual drives out of physical drives. These virtual (or logical) drives can then be manipulated in interesting ways: They can be grown or shrunk, and they can span more than one physical disk. An LVM in and of itself is exciting because it allows you to turn a number of disks into one massive volume, but it becomes even more interesting when throwing RAID into the mix.

Using LVM with a RAID-1 mirroring system provides large devices with redundancy. This is important because if one drive in an LVM volume set dies, it could leave your data in an inconsistent (or entirely gone) state. Using LVM over RAID is really no different than using LVM on a physical disk; instead of adding physical volumes to your LVM set, you would add md devices, using /dev/md0 rather than /dev/hda1.

To begin, creating an LVM set from physical disks is quite easy. The following commands will get you started. This also assumes you're using a fairly recent Linux kernel; most distributions have LVM available for install.

# modprobe dm-mod
# vgscan
# fdisk /dev/hda

The first step is to partition the drive. You don't have to give the entire drive over to LVM if you don't want to. Create a partition, say hda1, and give it the type 8e, which is for Linux LVM. Do the same with the second disk (say, hdb). After that, execute:

# pvcreate /dev/hda1
# pvcreate /dev/hdb1

These commands will make the partitions available to LVM for use. The next step is to create the volume group:

# vgcreate data /dev/hda1 /dev/hdb1

This will create a volume group called "data" and assign /dev/hda1 and /dev/hdb1 to it. If you wish to add a third drive to the group later, you would use vgextend data /dev/hdc1. To obtain information on your volume group, use vgdisplay and the volume group name. For information on the physical volume, use pvdisplay. You need to use vgdisplay to find out how many physical extents are available for use. Here, we'll assign them all to one large logical device:

# vgdisplay data | grep "Total PE"
# lvcreate -l 10230 data -n files

The number of physical extents available was 10230, and all were assigned to the logical volume "files." Now you can format, manipulate, and mount the volume just as any other device, except the device name will be /dev/data/files or /dev/mapper/data-files:

# mke2fs -j /dev/data/files
# mkdir -p /srv/files
# mount /dev/data/files /srv/files

There is a lot more information about manipulating and maintaining LVM volumes out there, and it would be wise to become familiar with it, but it's quite straightforward to create your first LVM volume.

Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About Vincent Danen

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks

Free Newsletters, In your Inbox