Linux

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 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.

3 comments
apisair
apisair

How to use a linux...any explanation or download hard copies..?

Timothy J. Bruce
Timothy J. Bruce

Linux is an open source operating system. It is composed of a Kernel (the central controller for the Operating System developed by Linus Torvalds), the GNU utilities, and a variety of applications. There are several linux distributions (aka Distros) available that have different features. If you're new to Linux (both the operating system and applications), check out a live disto like Knoppix or Ubuntu that can be run directly from a CD-ROM/DVD-ROM (or RW) drive. Links to most distros can be found on Distrowatch (http://www.distrowatch.com). Documentation is available with the distribution. I also recommend looking at some of the documentation available from TLDP (The Linux Documentation Project - http://www.tldp.org). Particularly NAG (Network Administration Guide), SAG (System Administration Guide) and LAME (Linux Administration Made Easy). Other documentation exists on TLDP as well. And there are a variety of Linux / Unix resources out there on the web...usually the home of your distro of choice. Tim

Larry the Security Guy
Larry the Security Guy

Linux is an operating system/environment, as is Windows and Mac and many others. Using it would be no different than using any other OS. To better understand Linux and what it can and cannot do for you, I recommend you check out www.linux.org. It has lots of useful information.