原文:http://equivocation.org/node/103


Logical volume management (LVM) is the default method of partitioning on Fedora Linux. It provides a more flexible way of partitioning disks and media on top of the traditional Linux partitions. However, in a similar manner to traditional partitions, resizing a LVM partition (or "volume" in LVM speak) is not necessarily a simple task.

Before resizing can take place the partition/volume must be unmounted (i.e. not be in use). This may be simple for some volumes, but for the root volume, which contains the running operating system, it is generally not possible to unmount it while the operating system on it is running. The usual method to get around this is to boot from a "rescue" disk/CD/DVD, which usually runs a small operating system resident in memory, and does not need or mount the root LVM volume. The Fedora install CD/DVD can be booted into a "rescue" mode. Note, during the boot of the Fedora 8 DVD in rescue mode, the option to mount the system disks is given — this should not be done.

Shrinking the root LVM volume

In this example I want to shrink my root volume to free up some space for creating an additional LVM volume. By default the file system (usually ext2 or ext3 in Fedora) created on the LVM volume will occupy the entire volume. Before the LVM volume can be shrunk, the file system must be shrunk. This is important, without shrinking the file system, reducing the LVM volume could corrupt the file system and lead to loss of data.

Before we can resize the file system we need to gain access to the unmounted root volume. The Fedora rescue system provides all the lvm tools necessary. The LVM command vgchange changes attributes of volume groups. What needs to be "changed" to access the volume is the "availability" — i.e. to make the kernel realize the LVM volumes are there. This can be done by the command:

#lvm vgchange -a y

Note, in rescue mode the system command is lvm and the LVM command that is run is vgchange. The -a y argument sets the availability to "y" or yes. As there are no specified LVM volume groups, this command will make all LVM volumes found available to the rescue kernel.

Once the kernel is aware of all LVM volumes they will be automatically mapped as devices. These are usually located under /dev/VolGroup (where "VolGroup" is the name of the Volume Group — the default volume group name for a Fedora system is "VolGroup00"). There are usually a number of devices under this directory:

#ls /dev/VolGroup00/ LogVol00 LogVol01

In this example there are only two volumes on the Fedora system (LogVol00, the root volume; LogVol01 the volume used for swap). This is the LVM partitioning in a default Fedora 8 install.

Before shrinking the file system, it is good practice to check it is all in order. The e2fsck command can be used to do this:

#e2fsck -f /dev/VolGroup00/LogVol00

After the check, resize2fs can be used to shrink the file system on the LVM volume. Here I reduce the root volume to 15GB:

#resize2fs -f /dev/VolGroup00/LogVol00 15G

The e2fsck check command could be run again to make sure the, now significantly smaller, root file system is OK. The next step is to shrink the volume it sits on.

The lvreduce command reduces the size of LVM volumes. The -L option allows the new size to be explicitly given. If the option to this argument does not begin with a minus sign, it is taken as the size to reduce the volume to. For example, -L 15G will set the reduced volume size to 15 GB. Alternatively, a minus sign can precede the size option to indicate how much to reduce the volume by. For example, -L -2G will reduce the volume by 2GB. In the current example, setting the reduced volume size to 15GB is exactly what I want:

#lvm lvreduce -L15G /dev/VolGroup00/LogVol00

In this example, there is now spare/unused disk that could be turned into a new LVM volume, or added to an LVM volume that needs to grow.