Understanding LVM
Logical Volume Manager, or LVM, is a storage management solution that allows administrators to divide hard drive space into physical volumes (PV), which can then be combined into logical volume groups (VG), which are then divided into logical volumes (LV) on which the filesystem and mount point are created.
As shown in Figure 7.1, because a logical volume group can include more than one physical volume, a mount point can include more than one physical hard drive, meaning the largest mount point can be larger than the biggest hard drive in the set. These logical volumes can be resized later if more disk space is needed for a particular mount point. After the mount points are created on logical volumes, a filesystem must be created on them.
Figure 7.1 How Logical Volume Manager Works
LVM is used by default during installation for all mount points except the /boot partition, which cannot exist on a logical volume. This section discusses how to perform LVM operations after installation such as creating a physical volume for a newly added hard drive, expanding logical volumes, and generating LV snapshots.
Table 7.1 summaries the LVM tools available after installation.
Table 7.1. LVM Tools
LVM Tool |
Description |
pvcreate |
Create physical volume from a hard drive |
vgcreate |
Create logical volume group from one or more physical volumes |
vgextend |
Add a physical volume to an existing volume group |
vgreduce |
Remove a physical volume from a volume group |
lvcreate |
Create a logical volume from available space in the volume group |
lvextend |
Extend the size of a logical volume from free physical extents in the logical volume group |
lvremove |
Remove a logical volume from a logical volume group, after unmounting it |
vgdisplay |
Show properties of existing volume group |
lvdisplay |
Show properties of existing logical volumes |
pvscan |
Show properties of existing physical volumes |
Adding Additional Disk Space
One big advantage of using LVM is that the size of a logical volume can be increased and logical volumes can be added to create additional mount points. To modify the LVM configuration post-installation, the lvm2 package needs to be installed. Refer to Chapter 3, “Operating System Updates,” for details on installing packages.
To increase the size of an existing logical volume or to add a logical volume, you first need free disk space. This free disk space can either be disk space that already exists in the system as unpartitioned space (not part of an existing logical volume), an unused partition, physical volume that is not already a member of a logical volume, or disk space as a result of installing one or more additional hard drives to the system. The disk space can come from removing a logical volume to create space in the logical volume group, however, this is not common because if the LV already exists, it is most likely already being used and cannot be easily deleted without losing data.
After deciding which free disk space to use, the basic steps for increasing the size of a logical volume are as follows:
- Create new physical volume from free disk space.
- Add physical volume to the logical volume group.
- Expand the size of the logical volume to include the newly added disk space in the volume group.
- Expand the filesystem on the logical volume to include the new space.
To add a logical volume, use the following steps:
- Create new physical volume from free disk space.
- Add physical volume to the logical volume group.
- Create a logical volume with the new space in volume group.
- Create a filesystem on the logical volume.
- Create a mount point.
- Mount the logical volume.
- Test the filesystem.
- Add the new mount point to /etc/fstab.
Creating a Physical Volume
To create a new physical volume from free hard drive space or a hard drive partition, use the pvcreate command:
pvcreate <disk>
Replace <disk> with the device name of the hard drive:
pvcreate /dev/sda
or the partition name:
pvcreate /dev/sda1
The <disk> specified can also be a meta device or loopback device, but using an entire hard disk or partition is more common. After creating a physical volume, you can either add it to an existing volume group or create a new volume group with the physical volume.
Creating and Modifying Volume Groups
A volume group can be created from one or more physical volumes. To scan the system for all physical volumes, use the pvscan command as root. It displays all PVs on the system. If the PV is part of a VG, it will display the name of the VG next to it.
To create a VG, execute the vgcreate command as root, where <vgname> is a unique name for the volume group and <pvlist> is one or more physical volumes to use, each separated by a space:
vgcreate <vgname> <pvlist>
For example, to create a VG with the name DatabaseVG from the first and second SCSI hard drives:
vgcreate DatabaseVG /dev/sda /dev/sdb
If a volume group already exists but needs to be expanded, use the vgextend command to add additional physical volumes to it:
vgextend <vgname> <pvlist>
To remove a physical volume from a volume group:
vgreduce <vgname> <pvlist>
Use caution when reducing a volume group because any logical volume using the PVs are removed from the VG and can no longer be accessed.
Creating and Modifying Logical Volumes
Now that the physical volumes are formed into volume groups, the volume groups can be divided into logical volumes, and the logical volumes can be formatted with a filesystem and assigned mount points.
Use the lvcreate command to create a logical volume. Each LV must have a unique name. If one is not specified with the -n <name> option, a name will be assigned to it. To create a logical volume from the volume group <vgname> of a certain size, specify the size unit after the value of the size such as 300G for 300 gigabytes:
lvcreate -n <lvname> --size <size> <vgname>
Each physical volume consists of physical extents, which are 4 megabytes in size by default. When the size is given in gigabytes, this size must be converted to physical extents, meaning that some amount of disk space may not be used. So, the number of physical extents to use when creating the logical volume can be given with the -l <numpe> option:
lvcreate -n <lvname> -l <numpe> <vgname>
To determine the number of physical extents in a logical volume group, issue the following command as root:
vgdisplay <vgname>
The Total PE line shows the number of physical extents for the volume group. The output should look similar to Listing 7.5, which shows a total of 1189 physical extents. Look for the Free PE / Size line to determine whether any free PEs are available to allocate to a new logical volume. Listing 7.5 shows 220 free physical extents.
Listing 7.5. Example vgdisplay Output
--- Volume group --- VG Name VolGroup00 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 5 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 37.16 GB PE Size 32.00 MB Total PE 1189 Alloc PE / Size 969 / 30.28 GB Free PE / Size 220 / 6.88 GB VG UUID N60y5U-2sM2-uxHY-M1op-Q1v3-uVV2-Zkahza
By default, logical volumes are created linearly over the physical volumes. However, they can be striped over multiple PVs:
lvcreate -i<stripes> -I<stripesize> -l <numpe> -n <lvname> <vgname> <pvlist>
The -i<stripes> option sets the number of stripes, or physical volumes to use. The -I<stripesize> is the stripe size, which must be 2^n, where n is an integer from 2 to 9. Provide the number of PEs to use with the -l <numpe> option or give the size of the LV with the --size <size> option. The -n <lvname> option specifies the LV name, and <vgname> represents the name of the VG to use. Optionally, list the PVs to use, <pvlist>, at the end of the command separated by spaces. The number of PVs listed should be equal to the number of stripes.
After creating the logical volume, you must create a filesystem on it. To create an ext3 filesystem, execute the following as root:
mke2fs -j /dev/<vgname>/<lvname>
If the LV is to be used as swap, execute the following as root instead:
mkswap /dev/<vgname>/<lvname>
Next, still as the root user, create an empty directory as its mount point with the mkdir command, and use the mount command to mount the filesystem:
mount /dev/<vgname>/<lvname> /mount/point
If it mounts properly, the last step is to add it to /etc/fstab so it is mounted automatically at boot time. As root, add a line similar to the following, replacing with the appropriate values:
/dev/<vgname>/<lvname> /mount/point ext3 defaults 1 2
To extend a logical volume, expand the volume group if necessary, and then use the lvextend command. Either specify the final size of the logical volume:
lvextend --size <size> /dev/<vgname>/<lvname>
or specify how much to expand the logical volume:
lvextend --size +<addsize> /dev/<vgname>/<lvname>
Just like physical volumes are composed of 4KB physical extents, logical volumes consist of logical extents, which also have a default size of 4KB. Instead of specifying the size or amount of space to add in gigabytes, it is also possible to use the -l <numle> to provide the final number of logical extents or -l +<numle> to expand the logical volume by a certain number of logical extents.
After extending the logical volume, the filesystem on it must be expanded as well. If it is an ext3 filesystem (default filesystem for Red Hat Enterprise Linux), it can be expanded while it is still mounted (also known as online). To do so, execute the following as root:
resize2fs /dev/<vgname>/<lvname>
The filesystem is expanded to fill the entire logical volume unless a size is listed after the logical volume device name (be sure to list the size unit such as G for gigabyte after the size):
resize2fs /dev/<vgname>/<lvname> <size>
To remove a logical volume from a volume group, first unmount it with the umount command:
umount /dev/<vgname>/<lvname>
and then use the lvremove command:
lvremove /dev/<vgname>/<lvname>
To view the existing logical volumes along with information about them such as what VG they are a member of, the number of logical extents, and their size in gigabytes, execute the lvdisplay command as root as shown in Listing 7.6.
Listing 7.6. Example lvdisplay Output
--- Logical volume --- LV Name /dev/VolGroup00/LogVol00 VG Name VolGroup00 LV UUID tugMFo-PESp-3INs-nrGF-K0Wh-s3U0-l9FsTc LV Write Access read/write LV Status available # open 1 LV Size 12.94 GB Current LE 414 Segments 1 Allocation inherit Read ahead sectors 0 Block device 253:0 --- Logical volume --- LV Name /dev/VolGroup00/LogVol01 VG Name VolGroup00 LV UUID fdKfYP-wIP9-M4Da-eoV3-pP99-w8Vb-0yhgZb LV Write Access read/write LV Status available # open 1 LV Size 78.12 GB Current LE 2500 Segments 1 Allocation inherit Read ahead sectors 0 Block device 253:1 --- Logical volume --- LV Name /dev/VolGroup00/LogVol02 VG Name VolGroup00 LV UUID bzr4Ag-rDKT-y8zY-F3e8-SaBI-QY51-r6lJ3T LV Write Access read/write LV Status available # open 1 LV Size 1.94 GB Current LE 62 Segments 1 Allocation inherit Read ahead sectors 0 Block device 253:2
Creating Snapshots
With LVM, it is possible to take a snapshot of a logical volume while the LV is still in read-write mode and being accessed by the system. As the root user, issue the following command:
lvcreate --size <size> -s -n <snapshotname> <lvname>
The lvcreate command is used to create a new logical volume, meaning there must be free physical extents in the logical volume group to create a snapshot. The -s option means that the LV is a snapshot, <snapshotname> is the name of the new LV created, and <lvname> is the name of the LV from which to create the snapshot.
A snapshot is not a copy of the entire LV. Instead, it keeps track of the changes from the time the snapshot is taken and the present time. Thus, the size of the snapshot LV does not need to be as large as the LV from which it is created. It just needs to be as big as all the changes from the time the snapshot is taken until the snapshot is used. Snapshots are not intended to be left around for long periods of time. Reasons to create snapshots include performing backups (most common), creating virtual machines using the Virtualization feature (refer to Appendix B, “Creating Virtual Machines”), creating a duplicate testing system, and transferring data from one logical volume group (and possibly a different hard drive) to another.
If a snapshot LV reaches disk capacity, it will become unusable. When the backup or data transfer has been completed, the snapshot logical volume should be unmounted and removed with the lvremove /dev/<vgname>/<lvname> command. Because the snapshot LV is storing a copy of all changes made to the original LV, performance for the original LV can be reduced because of this copy process.