In this series, I'm going to show you how to install and use the new Logical Volume Management support built-in to the Linux 2.4 kernel. If you've never used a form of LVM before, you're in for a treat; it's a wonderful technology. Before we actually get LVM up and running, I'm going to explain exactly what it is and how it works. Then, we'll be ready to test out LVM and get the most out of it.
If you're like me, then your experience with UNIX and Linux began on a PC
platform, rather than on large, commercial UNIX servers and workstations. On
the basic PC, we've always had to deal with partitioning our hard drives. PC
people are generally well-acquainted with tools such as
Hard drive partitioning can be annoying because to do a good job you really need to accurately estimate how much space you'll need for each partition. If you make a poor estimation, your Linux system could possibly be crippled -- to fix the problem, it's possible that you might even need to perform a full system backup, wipe your hard drives clean, and then restore all your data to a new (and presumably better) partition layout. Ick! These are exactly the kinds of situations that sysadmins try their best to avoid in the first place.
While partitions were once static storage regions, thankfully, we now have a proliferation of PC repartitioning tools (PowerQuest's Partition Magic product is one of the most popular). These tools allow you to boot your system with a special disk and dynamically resize your partitions and filesystems. Once you reboot, you have newly resized partitions, hopefully getting you out of your storage crunch. These partition resizing tools are great and solve the problem storage management for some. But are they perfect? Not exactly.
Tools like Partition Magic are great for workstations, but aren't really adequate for servers. First of all, they require you to reboot your system. This is something most sysadmins desperately try to avoid doing. What if you simply can't reboot your machine every time your storage needs change, such as if your storage needs change dramatically on a weekly basis? What happens if you need to expand a filesystem so that it spans more than one hard drive, or what do you do if you need to dynamically expand or shrink a volume's storage capacity while allowing Apache to continue to serve Web pages? In a highly available, dynamic environment, a basic partition resizer just won't work. For these and other situations, Logical Volume Management is an excellent (if not perfect) solution.
Now, let's take a look at how LVM solves these problems. To create an LVM logical volume, we follow a three-step process. First, we need to select the physical storage resources that are going to be used for LVM. Typically, these are standard partitions but can also be Linux software RAID volumes that we've created. In LVM terminology, these storage resources are called "physical volumes". Our first step in setting up LVM involves properly initializing these partitions so that they can be recognized by the LVM system. This involves setting the correct partition type if we're adding a physical partition, and running the pvcreate command.
Once we have one or more physical volumes initialized for use by LVM, we can move on to step two -- creating a volume group. You can think of a volume group as a pool of storage that consists of one or more physical volumes. While LVM is running, we can add physical volumes to the volume group or even remove them. However, we can't mount or create filesystems on a volume group directly. Instead, we can tell LVM to create one or more "logical volumes" using our volume group storage pool:
Creating an LVM logical volume is really easy, and once it's created we can go
ahead and put a filesystem on it, mount it, and start using the volume to store
our files. To create a logical volume, we use the
Behind the scenes, the LVM system allocates storage in equal-sized "chunks", called extents. We can specify the particular extent size to use at volume group creation time. The size of an extent defaults to 4Mb, which is perfect for most uses. One of the beauties of LVM is that the physical storage locations of the extents used for one of our logical volumes (in other words, what disk they're stored on) can be dynamically changed while our logical volume is mounted and in use! The LVM system ensures that our logical volumes continue to operate perfectly while allowing the administrator to physically change where everything is stored.
Of course, since everything is created out of equally-sized extents, it's really easy to allocate some additional extents for an already-existing logical volume -- in other words, dynamically "grow" the volume:
Once the logical volume has been expanded, you can then expand your ext2 or
ReiserFS filesystem to take advantage of this new space. If you use a program
such as
The only time you need to shut down your system is when you need to add new physical disks. Once new disks have been added, you then can add these new physical volumes to your volume group(s) to create a fresh supply of extents.
OK, let's get LVM installed. LVM consists of two parts: a kernel part and a
suite of user-space tools. To start, head over to the main LVM page (see
If you already have a 2.4-series kernel installed, you may already have LVM support available on your system, and if not, it's a simple matter to recompile your kernel to enable LVM support. However, you may not want to use the LVM support included with your stock (or distribution-supplied) 2.4 kernel. If you want to use the latest LVM version, you'll want to apply patches from the LVM tarball to your current 2.4 kernel source tree. Here's how to do it.
To start, enter your kernel source directory (
# cd /usr/src/linux # mkdir extras # cd extras # tar xzvf /path/to/location/of/lvm_0.9.1_beta3.tar.gz
Once you've done that, you'll notice a new directory in extras called
# cd LVM/0.9.1_beta3 # ls ABSTRACT COPYING INSTALL Makefile README autoconf config.status kernel make.tmpl.in CHANGELOG COPYING.LIB KNOWN_BUGS Makefile.in TODO config.cache configure lvm_input_msg scripts CONTRIBUTORS FAQ LVM-HOWTO PATCHES WHATSNEW config.log configure.in make.tmpl tools
You'll see several text files, scripts, and source directories. You'll find the
installation instructions in the
# ./configure --prefix=/ --mandir=/usr/man
After executing this command, the Makefiles will be created and configured to
install all the LVM tools in
# cd PATCHES
Now, we're going to type
# make
The patch will be named
# cd /usr/src/linux # patch -l -p1 < /usr/src/linux/extras/LVM/0.9.1_beta3/PATCHES/lvm-0.9.1_beta3-2.4.0-ac11.patch
While the LVM INSTALL documentation doesn't mention it, I typically pass the
OK, you now have a kernel that's been patched so that it has the most current LVM code available. Now, you'll want to configure your kernel so that LVM support is enabled. I recommend that you compile LVM support directly into the kernel rather than configuring it to compile as a module. Fire up your favorite Linux kernel configuration method:
# cd /usr/src/linux # make menuconfig
You'll find the LVM options under the "Multi-device support (RAID and LVM)" section. Once you enable the first option:
[*] Multiple devices driver support (RAID and LVM)
. . . you'll see the following option, which you should also enable:
<*> Logical volume manager (LVM) support
Depending on your LVM version, there may be other LVM-related options that you'll want to enable as well. Once you're done, save your kernel configuration and perform your standard kernel compilation routine and reboot. Congratulations -- you now have kernel LVM support enabled. Now, we need to get the user-space tools compiled and installed. This step is easy:
# cd /usr/src/linux/extras/LVM/0.9.1_beta3 # make # make install
There's just one more step, and it's optional. If you're going to be doing more than just testing out LVM, you'll want to add the following lines to your startup rc scripts:
/sbin/vgscan /sbin/vgchange -a y
These lines will scan for all available volume groups and activate them. Then, add the following line to your shutdown rc script, and make sure that it executes after all filesystems have been unmounted:
/sbin/vgchange -a n
If you're just testing out LVM, then you can skip these steps. Just remember
that after every reboot, you'll need to type
That's it for this article. Next article, I'll show you how to create your own logical volumes and unleash the power of LVM. I'll see you then!