Logical Volume Manager for Linux
Rafeeq Ur Rehman
The Logical Volume Manager, or LVM, is widely used on commercial UNIX systems for online disk storage management. It adds an extra layer between kernel I/O and physical disk devices. In the traditional disk storage schemes, the disk is partitioned into fixed size segments. The LVM considers all installed disks as pools of data storage. Each disk is called a Physical Volume (PV) and is part of one pool of data storage known as a Volume Group (VG). A volume group may consist of multiple physical disks and is represented as one large storage space. The volume group is then divided into partitions called Logical Volumes (LV). A logical volume acts as one logical disk partition, although it can span multiple physical disks. Its size can be increased/decreased without any loss of data. A disk may be added to a volume group at any time when there is a need to increase data storage capacity. This provides a great flexibility for changing storage space demands and is useful for combining multiple small capacity disks into one large logical storage space.
HP-UX and Digital UNIX use the OSF version of LVM, while Solaris uses Veritas. Linux LVM was recently released and is similar to the LVM used on HP-UX. As Linux penetrates into business, use of LVM is inevitable in Linux systems. The LVM support has been included in new kernel versions. This article describes how to install, configure, and use the Linux version of LVM. I'll begin with a brief introduction for those who have not used LVM with other platforms. If you're familiar with LVM, you may wish to skip this introduction. All steps from download to installation and management of LVM will be presented in the article. For example configurations in this article, I have used a PC with three IDE disks. The first disk contains a traditional Linux installation, while the other two disks, each of 200-MB capacity, are used with LVM. A volume group named vg00 consisting of these two disks is used, having a total capacity of 400 MB.
Introduction to Logical Volume Manager LVM is a new type of storage management system. Traditionally, a computer comes with one or more disks. Each of these disks may be partitioned into one or multiple filesystems under Linux. These partitions are then used to create filesystems and are mounted on a directory under the root directory. This directory is called the mount point for the filesystem. In this scheme, the maximum size of a filesystem is limited by storage capacity on the physical disk where this filesystem resides. In addition to that, it is not possible to change the size of a filesystem without destruction of data. These two limitations are a major restriction in today's ever-changing storage requirements.
The logical volume manager is designed to overcome the restrictions imposed by size of physical disk space. It also solves problems related to increasing or decreasing filesystem sizes without destroying data. Here's how the LVM solves these problems.
Suppose you have three old disks in your system, each of 200-MB storage capacity. If you use these disks in the ordinary way, you can't create filesystems of more than 200-MB disk space. However, if you use the LVM, you are able to create a volume group consisting of these three disks (physical volumes in LVM terminology) to create a volume group of 600-MB capacity. This approach is shown in Figure 1.
Once you have created a volume group, you can use it as a single logical storage space of 600 MB. You can decide how to partition this storage space to create logical volumes (logical partitions). You can create a single logical volume in a volume group of capacity 600 MB, thus having a larger filesystem, which was previously limited to 200 MB. You can also create multiple logical volumes with a storage space of your choice, making effective use of the disk space. Figure 2 shows two logical volumes. One of these has 500 MB of storage space, and the other one has 50 MB. The remaining 50 MB of storage space is left empty and can be used to increase the size of any of these logical volumes at any later stage.
The logical volume 1 spans three physical disks (physical volumes). It takes 200 MB from physical volume 1, 200 MB from physical volume 2, and 100 MB from physical volume 3. While creating logical volumes, you can decide how to allocate disk space on different physical volumes. You can force contiguous disk space allocation or disk stripping. If contiguous allocation is not forced, space from different parts of a disk can be allocated to a logical volume.
The steps involved for configuring and using LVM are as follows:
1. Create physical volumes. This process checks physical volumes (disks) for consistency and integrity of storage space.
2. Create volume groups. During this process, you combine multiple physical volumes to create a volume group. A volume group may consist of only one physical volume. You can add more physical volumes to a volume group at a later stage to increase its capacity. For example, if you already have 20 GB of storage space and you need 10 GB more, you can simply add a disk of 10 GB to the volume group to make its total capacity 30 GB. This provides a lot of flexibility in handling storage space. In Linux, you can also use two partitions on the same disk to be added to a volume group.
3. Create logical volumes. When you have created a volume group, you can create one or more logical volumes. At this point, you don't see any physical disks. Instead, a volume group appears as single storage space, and you can create a logical volume of any size that fits into that storage space. Creating logical volumes in a volume group is just like partitioning a disk (however, you don't use fdisk for this purpose). Here you replace a disk with a volume group consisting of multiple physical disks. It is better to create logical volumes depending upon your current disk requirements and leave any extra space as unallocated. You can increase the size of any of the logical volumes using this unallocated space at a later stage as required.
4. Create filesystems. Once you have created logical volumes, you can create filesystems on these logical volumes. This process is exactly the same as creating filesystems on disk partitions. You must use the conventional mke2fs command for this purpose.
5. Mount filesystems. This process is also familiar. You have to create a mount point directory for each filesystem. After creating the mount point, you use the mount command to mount the newly created filesystem to the mount point. You also create an entry in /etc/fstab file for the new filesystems so that these are automatically mounted every time you boot up your system.
After mounting the filesystems, you can use the disk space on logical volumes in the usual way.
Installation I used Linux kernel version 2.3.49 for testing LVM, but you can also use other supported kernel versions. The current LVM package version at the time of writing is 0.8. You can download Linux kernel from a number of sites on the Internet. The latest version of LVM package is available at: http://linux.msede.com/lvm . You must download the LVM patch for your kernel as well as the LVM package. The LVM package consists of utilities and libraries required to compile these utilities, while the kernel patch contains a kernel driver for LVM. After downloading, copy all of these files into /usr/src directory. Then, untar the Linux kernel as follows:
gunzip linux-2.3.49.tar.gz
tar xvf linux-2.3.49.tar
The above commands will create a directory /usr/scr/linux-2.3.49. Note that 2.3.49 will be replaced by the version number of the Linux kernel you are using. Link this directory to /usr/src/linux, as shown below.
ln -s /usr/scr/linux-2.3.49 /usr/src/linux
If the /usr/src/linux link already exists, remove it before creating the above link. Now you can apply the LVM patch to the kernel. The command for applying the patch is shown below:
cd /usr/src
gzip -cd patch-2.3.49-LVM-0.8final.gz | \
(cd /usr/src/linux; patch -p0)
Note that you may have a different patch, and the above command will be changed accordingly with the correct patch name. After applying the patch, recompile the kernel using the usual procedure.
Reconfiguring the Kernel For kernel recompilation, go to /usr/src/linux directory and use the following kernel configuration and compilation steps.
cd /usr/src/linux
make config
make dep
make clean
make bzImage
When you use the make config command, you must answer a number of questions. For LVM, you will encounter the following two questions. Answer Y for both of these.
Logical volume manager (LVM) support \
(CONFIG_BLK_DEV_LVM) [Y/m/n/?]
LVM information in proc filesystem \
(CONFIG_LVM_PROC_FS) [Y/n/?]
The kernel compilation process starts when you use the make bzImage command. It may take some time depending upon your computer processing speed. When the process is complete, your new kernel is present in /usr/src/linux/arch/i386/boot directory. Copy the kernel to an appropriate location (e.g., to /boot directory as shown below).
cp /usr/src/linux/arch/i386/boot/bzImage /boot
Now you have to configure lilo so that the new kernel may be loaded at the boot time. Instead of replacing the old kernel with the new one, I recommend adding the following lines at the end of /etc/lilo.conf file, so you can boot your computer from the old Linux kernel as well as the new one.
image = /boot/bzImage
label = lvm
root = /dev/hda1
After adding these lines, execute lilo so that the configuration becomes effective. Now, when you reboot your system, you can use lvm at the lilo boot prompt. Reboot the system to load the new kernel.
Compiling LVM Utilities To compile the LVM utilities, first, untar the LVM package as follows. Remember you are in /usr/src directory where you have downloaded all files.
gunzip lvm_0.8final.tar.gz
tar xvf lvm_0.8final.tar
This will create a directory /usr/src/LVM/0.8final where the source code for the LVM utility and library files are copied. Next, edit the make.tmpl file and change the TOP macro to the directory where you untarred the LVM files. In this case, it is /usr/src/LVM/0.8final. You must make another change in tool/lib/liblvm file at line 50 for this version of LVM. Change gnu/types.h to sys/types.h at line number 50 for a smooth compilation. Use the following two commands to compile all LVM utilities and install these.
make
make install
The LVM utilities are LVM commands used to manage physical volumes, volume groups, and logical volumes. After successful installation of the new kernel and these utilities, you are ready to use LVM.
Using LVM On a Linux system, a separate directory is created for each volume group under the /dev directory. This directory has the same name as the volume group name. I have used volume group name vg00, so the volume group directory is /dev/vg00. Under this directory, a file named group is present, which is created when you add a volume group. Whenever you create a logical volume in a volume group, a device file for the logical volume is also created in this directory. The logical volume device files are used to create filesystems and mount them to mount points. For this article, I have installed Linux on my first IDE disk (/dev/hda1) and have used two other IDE drives (/dev/hdb and /dev/hdc) for LVM. Both hdb and hdc have 200-MB storage capacity. You have to use the fdisk command to create partitions on both of these disks with a partition ID 8e. This partition ID is necessary for using a disk with LVM. The following procedure is used for creating a partition on disk /dev/hdb; you can repeat the same procedure for all disks to be used with LVM.
[root@localhost /root]# fdisk /dev/hdb
Command (m for help): p
Disk /dev/hdb: 15 heads, 38 sectors, 723 cylinders
Units = cylinders of 570 * 512 bytes
Device Boot Start End Blocks Id System
/dev/hdb1 1 723 206036 83 Linux
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Unknown)
Command (m for help): p
Disk /dev/hdb: 15 heads, 38 sectors, 723 cylinders
Units = cylinders of 570 * 512 bytes
Device Boot Start End Blocks Id System
/dev/hdb1 1 723 206036 8e Unknown
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
WARNING: If you have created or modified any
DOS 6.x partitions, please see the fdisk manual
page for additional information.
[root@localhost /root]#
After creating partitions on both of the disks, I created the /etc/lvmtab file, which keeps a record of all volume groups and logical volumes. The vgscan command is used for this purpose, and its output is shown below.
[root@localhost 0.8final]# vgscan
vgscan -- reading all physical volumes
(this may take a while...)
vgscan -- no volume groups found
[root@localhost 0.8final]#
As seen in the above output, it did not find any volume group information. However, it created the /etc/lvmtab file, which will be used by other commands.
As a first step toward creating and using LVM, we create physical volumes on disks hdb and hdc, as shown below:
[root@localhost /root]# pvcreate /dev/hdb1
pvcreate -- physical volume "/dev/hdb1" \
successfully created
[root@localhost /root]# pvcreate /dev/hdc1
pvcreate -- physical volume "/dev/hdc1" \
successfully created
After creating the physical volumes, create a volume group that consists of these physical volumes. The following command creates a volume group vg00 that consists of two physical volumes /dev/hdb1 and /dev/hdc1. A volume group may or may not be active at any time. The vgcreate command automatically activates a volume group after creating it. (You can use a volume group only when it is active.)
[root@localhost /root]# vgcreate vg00 \
/dev/hdb1 /dev/hdc1
vgcreate -- INFO: using default \
physical extent size 4 MB
vgcreate -- INFO: maximum logical \
volume size is 255.99 Gigabyte
vgcreate -- doing automatic backup \
of volume group "vg00"
vgcreate -- volume group "vg00" \
successfully created and activated
[root@localhost /root]#
Capacity of a volume group is measured in physical extents (PE). A physical extent is the minimum disk space that can be allocated in LVM. The default physical extent size is 4 MB. Other information about the volume group can be displayed using the vgdisplay command as follows:
[root@localhost /root]# vgdisplay vg00
--- Volume group ---
VG Name vg00
VG Access read/write
VG Status available/resizable
VG # 0
MAX LV 256
Cur LV 0
Open LV 0
MAX LV Size 255.99 GB
Max PV 256
Cur PV 2
Act PV 2
VG Size 400 MB
PE Size 4 MB
Total PE 100
Alloc PE / Size 0 / 0
Free PE / Size 100 / 400 MB
[root@localhost /root]#
More detailed information can be displayed using the -v switch at the command line with the vgdisplay command, where information about physical volumes (disks) is also displayed. Now you have a volume group consisting of two physical volumes. The next step is to create logical volumes in the volume group. The following command creates a logical volume named lvol1, having a capacity of 300 MB.
[root@localhost /root]# lvcreate \
-L 300 vg00
lvcreate -- doing automatic \
backup of "vg00"
lvcreate -- logical volume \
"/dev/vg00/lvol1" successfully created
[root@localhost /root]#
Since both disks have 200-MB capacity, the logical volume lvol1 spans over both of the disks. A storage space of 200 MB is allocated from one disk and 100 MB from the other disk. You can use pvdisplay -v command to display all physical extents on a physical volume and their allocation to a logical volume. This command will show which physical extents are allocated to a particular logical volume. Note that the lvcreate command has chosen the logical volume name lvol1 by itself. If you create another logical volume, the command will choose lvol2 for the second logical volume, and so on. You can use a logical volume name of your own choice using command-line options with the lvcreate command.
After creating one or more logical volumes, you have to create filesystems on these volumes and mount them on appropriate mount points. The following three commands create a filesystem on lvol1 volume and mount it on /extra directory.
mke2fs /dev/vg00/lvol1
mkdir /extra
mount /dev/vg00/lvol1 /extra
After mounting, you can use df command to verify that the mount point does exist and has the same capacity as you allocated to it. For logical volume lvol1, output of the df command will contain a line like the following:
/dev/vg00/lvol1 297485 13 282112 0% /extra
You may also want to list device files in the /dev/vg00 directory, where you will find two files (named group and lvol1) at this stage. If you create another logical volume using the lvcreate command, another device file with the logical volume name will be created in this directory.
Activating LVM at Boot Time Before you mount logical volumes, volume groups must be activated. The vgchange command is used to activate and deactivate volume groups. Depending upon your Linux distribution, there are multiple ways to achieve this goal. Putting the following command in /etc/rc.d/rc.local file should do the job on all Linux systems.
vgchange -a -y
mount /dev/vg00/lvol1 /extra
Note that you must add a mount command for all logical volumes. You should add the umount command and vgchange -a -n command in the shutdown procedure to unmount all logical volumes and deactivate the volume groups. If you are using this approach, you will not create a record of logical volumes in /etc/fstab file.
LVM Commands There are a number of commands used for LVM management tasks. Some important commands with a short description are listed here.
lvcreate Creates a new logical volume.
lvdisplay Displays logical volume information.
lvextend Extends a logical volume size.
lvreduce Reduces a logical volume size.
lvremove Removes a logical volume.
pvcreate Creates a physical volume.
pvdisplay Displays physical volume information.
pvscan Finds all existing physical
volumes.
vgchange Activates/deactivates volume groups.
vgcreate Creates a volume group.
vgdisplay Displays volume group
information.
vgextend Extends a volume group by one or more physical volumes.
vgreduce Reduces a volume group by one or more empty physical volumes.
vgremove Removes an empty volume group.
vgscan Scans for volume groups.
You can also use the e2fsadm command if you have installed Ted Tso's resize2fs program on your system. A complete list of commands can be found in the Abstract file that comes with the LVM package. More information about LVM is available in BeginnersGuide-to-LVM and LinuxLVM-FAQ files included in the package. Man pages for LVM commands are also available.
Conclusion LVM is a very important and useful feature, which was available only on high-performance commercial UNIX systems until now. It provides flexible disk management features. Using LVM, filesystems can be managed dynamically, and one filesystem can span multiple physical disks. Without LVM, a filesystem size is limited by the size of a disk. The size of filesystems can be increased and decreased depending upon changing storage requirements without any loss of data, which is another important aspect. After its inclusion in new kernel releases, it will help Linux to strengthen its foothold in corporate business.
Acknowledgements The author is thankful to the Linux community for creating such a wonderful operating system, especially to the Linux Laboratory at the Department of Electrical Engineering, University of Engineering and Technology, Lahore. Special thanks to Patricia Lickliter at Dedicated Technologies Inc. and Randy Mather for their continuous support.
About the Author
Rafeeq U. Rehman received Bachelor's and Master's degrees in Electrical Engineering from the University of Engineering and Technology, Lahore. He is the author of many articles on Linux and a book on HP-UX system and network administration. His interests are network management, security, and C programming. He can be reached at: rurehman@yahoo.com.
|