sections in this module | City
College of San Francisco - CS260A Unix/Linux System Administration Module: Filesystems II |
module list |
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
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.
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
Here is a copy of part of a 6.5 grub configuration file with the indicated parts highlighted
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 |