The Linux Logical Volume Manager

One of the great strengths of the Linux operating system is its support for a wide variety of filesystem types. Users now have the choice of using several different production-level filesystems. For example, the recently released Reiser filesystem (reiserfs) has generated considerable interest and excitement among Linux users and system administrators.

Guru Guidance Disks new
Figure One: You can use Logical Volume Manager to combine physical disk partitions and create rather large and resizable filesystems.

One of the great strengths of the Linux operating system is its support for a wide variety of filesystem types. Users now have the choice of using several different production-level filesystems. For example, the recently released Reiser filesystem (reiserfs) has generated considerable interest and excitement among Linux users and system administrators.

Similarly, the Linux software RAID facility is in wide-use by sites needing a low-cost solution for fault-tolerance or higher performance disk I/O (via RAID-0 disk stripping). One capability that has been missing, however, is the ability to arbitrarily combine distinct disk partitions into a single filesystem.

This omission is addressed by the Linux Logical Volume Manager (LVM), which is coming into production readiness at the time of this writing. The LVM project has been in existence for several years (its homepage can be found at http://linux.msede.com/lvm), and support for the LVM has been recently merged into the latest versions of the Linux kernel. Veritas will offer a commercial product with similar facilities for Linux sometime later this year (see http://www.veritas.com/us/aboutus/pressroom/2000/00-01-24-0.html for more information).

Conceptually, the LVM allows you to combine and divide physical disk partitions in a completely flexible manner. The resulting filesystems are dynamically resizable, and their maximum size can exceed 1,000 terabytes.

Figure One illustrates the operations of the LVM in a high-level conceptual manner. Here we have three physical disks — two of which have two physical partitions each and one that has a single partition. These partitions are each designated to the LVM as physical volumes. Additionally, physical volumes may be combined into volume groups; a volume group is an entity that functions as a virtual disk. Accordingly, volume groups may be subdivided — that is, virtually partitioned — into logical volumes. Therefore, a logical volume can be thought of as a virtual partition upon which a filesystem can be built. The current version of the LVM can support up to 99 volume groups and 256 logical volumes.

In our example, the volume group vg0 contains physical partitions from two physical disks: one of the partitions from dev/hdc and the single partition on /dev/hdd. Similarly, vg1 consists of the other partition on /dev/hdc and the second partition on dev/hde. Finally, the volume group vg2 contains one of the partitions from /dev/hde.

We have gone on to divide these volume groups into logical volumes. Volume group vg0 contains three logical volumes, residing on two physical disk partitions from two separate disks. Volume group vg1 combined two distinct disk partitions (on separate disks) into a single logical volume and thereby into a single Linux filesystem. In the same way, volume group vg2 divides a single disk partition into two logical volumes and these two Linux filesystems. As you can see at the end of this example, we have created six Linux filesystems.

Installing the LVM

The LVM is included in some recent Linux distributions (for example, SuSE Linux 6.4). If it is not included in yours, installing it is quite straightforward:

* Download the LVM package and the appropriate kernel patch for your system from http://linux.msede.com/lvm/.

* Unpack and build the LVM package. This will install files for the LVM utilities into /sbin and other files into the standard shared library and main page locations.

Guru Guidance Kern
Figure Two: Using block devices to enable LVM support in the kernel.

* If necessary, patch the kernel source code and build a new kernel. This enables LVM support during the configuration process. One way to do the latter is to use the make xconfig command. Use the Block Devices button from the main menu; Figure Two illustrates the appropriate settings.

* If you selected modular support for LVM, add entries to /etc/ modules.conf to enable the modprobe command to load the LVM module at boot time. Here are the needed entries:

  alias block-major-58 lvm-mod
alias char-major-109 lvm-mod

* If you are not using modules, install the new kernel into the boot directory. Then, modify the LILO configuration file and install it with the LILO command.

* Modify the system startup and shutdown scripts to activate and deactivate configuration. Add these commands to the startup scripts:

vgscan # Search for volume groups
vgchange -a y # Activate all volume groups

Add this command to the shutdown script:

vgchange-an # Deactivate all volume groups

* Reboot the system using the new kernel.

Using the LVM

The LVM package includes a number of administrative utilities, each of which is designed to create or manipulate a specific type of LVM entity. For example, the commands vgcreate, vgdisplay, vgchange, and vgremove create, display information about, modify the characteristics of, and delete a volume group (respectively). You can also backup and restore the volume group configurations with vgcfgbackup and vgbfgrestore, change the size of a volume group with both vgreduce (decrease its size) and vgextend (increase its size by adding disk space to it), move a volume group between computer systems (vgexport and vgimport), divide and combine volume groups (vgsplit and vgmerge), search all local disks for volume groups (vgscan), and rename a volume group (vgrename). There are similar commands for other LVM entities:

* Physical volumes: pvcreate, pvdisplay, pvchange, pvmove, and pvscan.

* Logical volumes: lvcreate, lvdisplay, lvchange, lvremove, lvreduce, lvextend, lvscan, and lvrename.

Creating a Volume Group

Let’s examine some of these commands in action as we create a sample volume group and some logical volumes, and then build filesystems on them.

The first step is to set the partition type of the desired disk partitions to 0x8e. We use fdisk for this task; here is the process for the first disk partition:

# fdisk /dev/sdb1
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 8e
Command (m for help): w

The first time we use the LVM, we need to run vgscan to initialize the facility:

# vgscan
vgscan – reading all physical volumes
(this may take a while…)
vgscan – no volume groups found

Next, we designate the disk partitions as physical volumes by specifying the desired disk partitions as command arguments to the pvcreate command (/dev/sdc2 is the second partition we will be using in our volume group):

# pvcreate /dev/sdb1 /dev/sdc2
pvcreate – reinitializing physical volume
pvcreate – physical volume “/dev/sdb1″
successfully created

We are now ready to create a volume group, which we will name vg1:

# vgcreate vg1 /dev/sdb1 /dev/sdc2
vgcreate – INFO:using default physical extent size 4 MB
vgcreate – INFO: maximum logical volume
size is 255.99 Gigabyte
vgcreate – doing automatic backupof volume
group “vg1″
vgcreate – volume group “vg1″
successfully created
and activated

This command creates the vg1 volume group using the two specified disk partitions. In doing so, it creates/updates the ASCII configuration file /etc/ lvmtab (which holds the names of the system’s volume groups) and places a binary configuration file into two subdirectories of /etc: lvmtab.d/vg1 and lvmconf/vg1.conf. The latter directory will also store old binary configuration files for this volume group, reflecting changes to its characteristics and components. The vgcreate command also creates the special file /dev/vg1/group, which can be used to refer to the volume group as a device.

Now we can create two 800 MB logical volumes:

# lvcreate -L 800M -n chem_lv vg1
lvcreate – doing automatic backup of “vg1″
lvcreate – logical volume “/dev/vg1/chem_lv”
successfully created
# lvcreate -L 800M -n bio_lv -r 8 -C y vg1
lvcreate – doing automatic backup of “vg1″
lvcreate – logical volume “/dev/vg1/bio_lv” successfully created

We set the sizes of both logical volumes via the lvcreate command’s -L option. In the case of the second logical volume, bio_lv, we specify (via -r) that the read-ahead mode chunk size is eight sectors (the amount of data returned at a time during sequential access) and that the logical volume be created contiguous (via the -C y option).

Once again, two new special files are created, each named after the corresponding logical volume and located under the volume group directory in /dev (here, /dev/vg1).

Now, we can create a filesystem on a logical volume using an ordinary mke2fs command and specify the logical volume as the device upon which to build the new filesystem. For example, the following command creates an ext2 filesystem on the bio_lv logical volume:

# mke2fs

Once built, this filesystem may be mounted as usual.

In addition to the previously mentioned commands, the LVM provides both the e2fsadmin command (which can be used to increase the size of a logical volume) and the ext2 filesystem on a single, nondestructive operation. This utility requires the resize2fs utility originally provided by PowerQuest (as part of its PartitionMagic product), now available under the GPL at http://e2fsprogs.sourceforge.net.

Here is an example of its use; the command in Figure Three adds 100 MB to the bio_lv logical volume and the filesystem that it contains.

Note that the filesystem must be unmounted in order to increase its size.

Figure Three: Using e2fsadm to Increase Logical Volume Size

# e2fsadm /dev/vg1/bio_lv -L+100M
e2fsck 1.18, 11-Nov-1999 for EXT2 FS 0.5b,95/08/09
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg1/bio_lv: 11/51200 files (0.0% non-contiguous), 6476/819200 blocks
lvextend – extending logical volume “/dev/vg1/bio_lv” to 900 MB
lvextend – doing automatic backup of volume group “vg1″
lvextend – logical volume “/dev/vg1/bio_lv” successfully extended
resize2fs 1.19 (13-Jul-2000)
Begin pass 1 (max = 5)
Begin pass 3 (max = 25)
The filesystem on /dev/vg1/bio_lv is now 921600 blocks long.
e2fsadm – ext2fs in logical volume “/dev/vg1/bio_lv” successfully
extended to 900 MB

LVM Snapshots

The Logical Volume Manager includes a useful facility that is designed to ensure consistent backups of active filesystems. This features is referred to as filesystem snapshots. The scheme involves the creation of a temporary, additional logical volume that is used as the target location for storing file modifications within another logical volume’s filesystem during the time that a backup is actually running.

The facility works like this: When the snapshot logical volume is created, the current state of all of the files and directories within the target filesystem are recorded. As long as the snapshot logical volume exists, any changes to the filesystem are propagated to the snapshot; for example, modified files are copied there. The idea is that once the backup has completed, the snapshot volume itself can be backed up, and you will be guaranteed to have all of the data within the target filesystem, despite the fact that some files may have been modified while the backup was running.

The following is an example of creating a snapshot logical volume:

# lvcreate -L 100M -snapshot -name snap_bio
# mount /dev/vg1/snap_bio /somewhere

Note that snapshot volumes do not persist across system reboots.

Building Blocks

With the introduction of LVM, yet another barrier to Linux’s acceptance as an “enterprise ready” OS has fallen away. The 2.4 kernel will come ready with LVM support, but until then, have fun playing with the tools we’ve described in this article.

Æleen Frisch is the author of Essential System Administration. She can be reached at aefrisch@lorentzian.com.

Comments are closed.