Disclaimer :
The original version of this article was first published on IBM
developerWorks, and is property of Westtech Information Services. This
document is an updated version of the original article, and contains
various improvements made by the Gentoo Linux Documentation team.
This document is not actively maintained.
|
Learning Linux LVM, Part 1
1.
Storage management magic with Logical Volume Management
LVM intro
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 fdisk, which are
used to create and delete primary and extended partitions on hard disks. Hard
disk partitioning is an annoying but accepted part of the process of getting an
operating system up and running.
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.
Enter LVM
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:
Figure 1.1: A volume group is created out of physical volumes |
 |
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 lvcreate command,
specifying the name of our new volume, the size we'd like the volume to be, and
the volume group that we'd like this particluar logical volume to be part of.
The LVM system will then allocate storage from the volume group we specify and
create our new volume, which is now ready for use. Once created, we can put an
ext2 or ReiserFS filesystem on it, mount it, and use it as we like.
Figure 1.2: Creating two logical volumes from our existing volume group |
 |
Extents
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:
Figure 1.3: Adding additional extents from our volume group, expanding the size of our logical 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 resize_reiserfs, this filesystem expansion can also happen while
the volume is mounted and being used! Truly amazing -- with LVM and online
filesystem expansion utilties, it's no longer necessary to reboot your system
or even drop to runlevel 1 to change your storage configuration.
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.
Setting up LVM
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 Resources later in this article) and download the most
recent version of the LVM tarball (currently lvm_0.9.1_beta3.tar.gz) you can
find. The LVM tarball contains all the user-space tools, as well as a bunch of
kernel patches. Here is where things get interesting.
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 (/usr/src/linux) and
create a directory called extras. Then, enter this directory and
extract your LVM tarball:
Code Listing 1.1: Extracting the patches |
# 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
LVM that contains another directory named after the version of LVM
that you just unpacked. Enter these two directories to get to the LVM sources:
Code Listing 1.2: Getting to the LVM sources |
# 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 INSTALL file; I'll guide you
through this process. First, we'll want to run the configure script, as
follows:
Code Listing 1.3: Configuring the LVM sources |
# ./configure --prefix=/ --mandir=/usr/man
|
Patching
After executing this command, the Makefiles will be created and configured to
install all the LVM tools in /sbin and the man pages in
/usr/man. If your man pages are in /usr/share/man (as
per FHS 2.1), then adjust the above path accordingly. And, if your kernel
sources aren't in /usr/src/linux, then add a
--with-kernel_dir=/path/to/usr/src/linux option to the line as well.
Once the configure script completes, we're ready to install the tools and
generate a patch for your current kernel. Let's patch the kernel first. Enter
the PATCHES directory:
Code Listing 1.4: Changing directories |
# cd PATCHES
|
Now, we're going to type make. The Makefile will generate a patch
specifically for our particular 2.4-series kernel sources:
Code Listing 1.5: Creating the patch |
# make
|
The patch will be named lvm-[lvmversion]-[kernelversion].patch. For
example, since I'm using version 0.9.1_beta3 of LVM and kernel 2.4.0-ac11, my
patch is called lvm-0.9.1_beta3-2.4.0-ac11.patch. You'll find it in your
current directory. Now, it's time to apply the patch. To do this, you'll want
to change directories to the location of your kernel sources and use the patch
command as shown:
Code Listing 1.6: The patch command |
# 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
-l option to patch. This option allows the patch program to compensate
for any changes in whitespace (such as minor indentation changes) that would
ordinarily cause some hunks of the patch to fail. If the above command
completes without any "FAILED" lines, then you're ready to install the
user-space tools. If not, you'll need to scan your /usr/src/linux
directory for .rej files and insert the rejected hunks into the sources
by hand using a text editor -- ick! However, in most all situations the patch
will apply cleanly and you'll be ready to go.
Configuring, compiling, and installing
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:
Code Listing 1.7: Configuring the kernel |
# 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:
Code Listing 1.8: RAID and LVM support |
[*] Multiple devices driver support (RAID and LVM)
|
. . . you'll see the following option, which you should also enable:
Code Listing 1.9: Enable LVM support |
<*> 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:
Code Listing 1.10: Creating the user-space tools |
# 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:
Code Listing 1.11: Editing the startup rc script |
/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:
Code Listing 1.12: Editing the shutdown rc script |
/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 vgscan and vgchange -a
y as root before your logical volumes are available for use.
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!
Resources
-
Download the LVM
tarball from RedHat.
-
The impatient will want to check out Heinz
Mauelshagen's LVM HOWTO, which shows you how to set up physical
volumes, volume groups, and logical volumes. This is something we'll cover
in my next article.
-
There's also an interesting HOWTO that shows you
how to set up your root filesystem on a logical volume.
-
Andreas Dilger is involved with the Linux LVM project and has a
nice-looking online ext2
filesystem resizer.
-
ReiserFS is an excellent
filesystem (especially in combination with LVM). If you're using ReiserFS,
you'll want to grab the reiserfs-utils tarball, which contains a program
called reiserfs_resize -- allowing online resizing of ReiserFS
filesystems.
-
For more information on setting up Linux software RAID volumes, see Part 1 and Part 2 of Daniel's
series on software RAID.
-
For a refresher, see Daniel's tutorial on compiling
the Linux kernel.
|