sections in this module City College of San Francisco - CS260A
Unix/Linux System Administration

Module: Filesystems II
module list

Logical Volume Manager

As we have seen, there are several good reasons to partition a system, most importantly data protection and data separation. However, partitioning has one big drawback: it is difficult to partition efficiently. If you allocate a partition large enough to fit a dataset no matter the developments, a large amount of space is wasted. If we could easily change the size of a partition after it is created we could overcome this drawback.

A logical volume manager (LVM) places an additional layer of software between the physical partitions of the disk and the system 'seen' by the operating system. This layer of software allows filesystems on 'logical volumes' (created using the logical volume manager) to be mounted just like those on physical paritions. However, the size of these logical volumes and their filesystems can be changed (increased or decreased) later if needed. If you trust your LVM completely, you can even increase the size of a filesystem on a logical volume while the filesystem is mounted, thus avoiding even brief interruptions in critical applications.

Note: Because we want students to be familiar with physical volumes, we do not use logical volumes on the class system, although they are now the default on most linux distributions. I have, frankly, not used them a lot, but have had no trouble with them. The reader is referred to the Linux Documentation Project, particularly the LVM Howto In particular, however, I do not recommend using a logical volume manager to manage your /boot partition. (You cannot use an LVM for /boot unless you are using the newer version of grub that understands LVMs.)

Overview of logical volumes

Logical volume creation requires several steps. Below, these are introduced. We will examine each of these in more detail in the remainder of this section.

Creating physical volumes

Normally, a physical volume is created from a disk partition. This is simply done by pvcreate device  where device is the device file of the partition. 

Entire disks can also be physical volumes. In this case, the partition table on the disk must first be erased using dd. It is probably simpler to create a single partition on the disk and use this.

As near as I can figure, all pvcreate does is place a label on the device to indicate it is for use by the LVM.

Creating a volume group

After the physical volumes have been initialized, one or more of them are used to create a named volume group using vgcreate

vgcreate volumegroupname physicalvolume1 [ physicalvolume2 ... ]

Creating a logical volume

A logical volume can then be created from an existing volume group by giving it a name, and indicating the size wanted and the name of the volumegroup to carve it out of

lvcreate --name logicalvolumename --size N  volumegroupname

Here, N is the size of the volume. The units of size are indicated by a suffix, m, g or g. (The correponding upper-case letter is for the 1000x unit, the lower-case letter is for the 1024x unit. Thus g is gibibytes while G is gigabytes. Does anyone else think this is backwards?).

Creating a logical volume create a device file for it under /dev/V/L, where V is the volumegroupname and L is the logicalvolumename. (Actually this is a symlink to the real device file.)

Accessing the logical volume

All that remains is to create a filesystem on the logical volume, and mount it. Simply use the logical volume's device file just like a physical partition's device file.

Special features

A logical volume can be mirrored, for redundancy, or striped, for performance improvement. These attributes are specified when the logical volume is created.

Other commands

The status of physical and logical volumes and of volume groups can be examined using the nine commands

{pv,vg,lv}{s,scan,display}

To extend an existing volume group or logical volume, simply use {vg,lv}extend

In fact, there are a huge number of LVM commands. To see them, execute the following commands

An example

In this example, we will create two logical volumes of size 6 and 7 GB starting with three existing physical partitions of size 5GB each. The three partitions are hda12, hda13, and hda16

# pvcreate /dev/hda12 /dev/hda13 /dev/hda16
  Physical volume "/dev/hda12" successfully created
  Physical volume "/dev/hda13" successfully created
  Physical volume "/dev/hda16" successfully created
#
Note that these physical partitions are not adjacent on the disk.
# vgcreate vg01 /dev/hda12 /dev/hda13 /dev/hda16
  Volume group "vg01" successfully created
#
# lvcreate --name lv01 --size 6G vg01
  Logical volume "lv01" created
# lvcreate --name lv02 --size 7G vg01
  Logical volume "lv02" created
#
Note that both lv01 and lv02 span physical partitions.
# lvscan
  ACTIVE            '/dev/vg01/lv01' [6.00 GB] inherit
  ACTIVE            '/dev/vg01/lv02' [7.00 GB] inherit
#
mkfs -t ext4 /dev/vg01/lv01
mkfs -t ext4 /dev/vg01/lv02
# mount /dev/vg01/lv01 /vol1
# mount /dev/vg01/lv02 /vol2

# df /vol1 /vol2
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/vg01-lv01
                       6192704     45092   5833040   1% /vol1
/dev/mapper/vg01-lv02
                       7224824     48924   6808900   1% /vol2
#

All that is left to do is to insert the mount directive in /etc/fstab

Resizing a logical volume

It is possible to resize a logical volume at the command-line, but it is this is much more easily done using the GUI (if it is available). The GUI interface to the logical volume manager will easily let you add physical volumes to the volume group and either decrease or increase the size of logical volumes. 

If you resize a logical volume using command-line tools, here is the sequence of events:

to increase a logical volume's size: 1) increase the lv size using lvextend 2) resize the underlying filesystem using resize2fs or xfs_growfs

to decrease a logical volume's size (much more dangerous - only possible with ext filesystems): 1) check to see how much space your filesystem is currently using with df. 2) resize the underlying filesystem using resize2fs 3) decrease the lv size using lvreduce

Be careful using resize2fs. The units of M, G mean 1024-based units. There are no parallel 1000-based units!

Removing a Logical Volume

It is possible to remove a logical volume, but there are some important caveats. First we will explain the steps to successful removal. Of course this assumes that you no longer want the data that is on the logical volume.

First, detach the LV from current system-use:

It is important to check this second step using swapon -a or mount -a (followed by cat /proc/swaps or mount) to ensure that the removal of the automated access has been successful.

Once the logical volume is not in-use, simply use lvremove, giving it the logical volume path. You will get a message about an 'active logical volume', but that just means the LVM is currently managing it. You should not be allowed to remove it if it is currently in-use.

If the logical volume is used by the kernel during the boot process, you must make further adjustments. There are three situations in which this might be true, but all of them are unusual

  1. the LV holds the /boot partition and you have moved the boot partition to somewhere else. This means your grub root is incorrect, as are all the paths in /boot/grub/grub.conf. I have never tried to make this work, but this would be difficult. Since the current version of grub does not support /boot on a LV, this may not be an issue (on 6.5) I wouldn't even attempt this on 7
  2. the LV holds the / partition. I cant imagine a situation where you could successfully reconfigure a system to move the / partition unless the system was offline. In this case, you must adjust the kernel parameter that indicates where the root is (not the grub root directive, the kernel option that points to the actual / partition - see root= below) I wouldn't even attempt this on 7
  3. the LV holds the swap partition. In this case you should remove the kernel option from the grub configuration file that accesses the swap partition from the LVM. This requires an edit of the grub configuration file (for 6.5), or (for 7), an edit of the /etc/default/grub file followed by a rebuild of the configuration file using grub2-mkconfig (dont forget to redirect the output!)

Here is a copy of part of a 6.5 grub configuration file with the indicated parts highlighted

splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Scientific Linux (2.6.32-358.11.1.el6.x86_64)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-358.11.1.el6.x86_64 ro root=UUID=3104318e-f797-42af-9501-13f3f5d62acd rd_NO_LUKS LANG=en_US.UTF-8  rhgb crashkernel=no quiet rd_LVM_LV=vg01/vg01swap rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-358.11.1.el6.x86_64.img

Using the LVM interactively

The logical volume manager provides an interactive program you can start at the command line. Just invoke lvm and issue the LVM commands from within it. lvm has a rudimentary help facility (yes, just by typing help) for a list of commands, and a help facility on each individual command (help commandname)

The LVM GUI tool

If you prefer clicking, use the GUI LVM tool. Although there are some things you can only do at the command-line, the GUI allows access to most LVM operations. Simply create your physical partitions, then invoke the LVM GUI. (At the time of this writing, I dont know where the LVM GUI tool is for 7, or even if an equivalent tool exists.)

If you are sure you want to resize a logical volume to increase its size, the GUI tool makes it very easy. Be sure, however, to always back your data up first.


Prev This page was made entirely with free software on linux:  
the Mozilla Project, Kompozer,
and Openoffice.org      
Next

Copyright 2016 Greg Boyd - All Rights Reserved.