Wednesday, December 5. 2007

This article is part of System Recovery Week, examining techniques used to perform maintenance or recovery on a Fedora system in special circumstances.

Logical Volume Management (LVM) is a powerful storage system layer which abstracts the logical view of storage from the actual physical layout. It is automatically configured in the default Fedora storage configuration. LVM enables you to grow and shrink volumes, add storage from new devices to existing volumes, and migrate volumes between storage devices, all without taking the system offline. In fact, the system-config-lvm package provides a convenient way to perform all of these operations using a graphical interface.

However, you can't (yet) shrink a filesystem while it is mounted. This isn't a limitation of LVM itself, but of most filesystems, including ext3. To shrink a partition that is always mounted when the system is running -- such as the root filesystem -- it's necessary to boot from another medium, which is where rescue mode comes in (see yesterday's article). It may also be necessary to use rescue mode to recover from some particularly nasty corruption or misconfiguration issues.

To do LVM and filesystem-resizing work within the Fedora rescue mode, boot the rescue mode from disc as usual, but select Skip when the system offers to mount your hard-disk partitions. Unfortunately, this will mean that your volume groups will not be detected or activated, so you will need to do that manually.

The commands normally used for logical volume management such as vgdisplay, pvcreate, and lvreduce are actually symbolic links to a single executable named lvm. These symbolic links are not available in rescue mode, so you must explicitly use the lvm command followed by the operation you wish to perform: if you wish to do a vgscan, for example, enter the command lvm vgscan.

Therefore, to discover and activate all volume groups, you must execute these commands:

sh-3.2# lvm vgscan Reading all physical volumes. This may take a while... Found volume group "VolGroup00" using metadata type lvm2sh-3.2# lvm vgchange -ay 2 logical volume(s) in volume group "VolGroup00" now active

You can display information about the logical volumes using lvm lvs (or lvm lvdisplay for a more verbose display):

Note that the LV and VG names are not very descriptive -- which is why it's a good idea to override the default names during system installation. In this case, we know the approximate size of the LV containing the root filesystem, so we can determine that it is LogVol00 in VolGroup00.

Armed with this information, you can now check (fsck) the filesystem, shrink the filesystem within that logical volume, and then shrink the LV. Because of the potential for rounding errors, it's best to shrink the filesystem to a size slightly smaller than the new LV size, resize the LV, and then grow the filesystem to fully fill the LV:

Note that the operations following the first resize2fs do not need to be performed in rescue mode -- you can reboot the system at that point and continue after the system has booted normally. Earlier versions of the Fedora rescue mode may not set up the symbolic link /dev/VolumeGroup/LogicalVolume, so you may need to refer to /dev/mapper/VolumeGroup-LogicalVolume (e.g., use /dev/mapper/VolGroup00-LogVol00 in place of /dev/VolGroup00/LogVol00).

Yes -- "online resizing" is available for ext3, but unfortunately only for growing filesystems, not for shrinking them, as the article indicates. I know the filesystem folks are working on this and really look forward to seeing it land!

Thank you this is just what I needed.
I followed your steps, and reduced my /dev/VolGroup00/LogVol00 to 70 GB, freeing up 40GB.
I do have a question how to proceed, because the LogVolume00 is now reduced in size, but the VolGroup00 is not yet.

Freeing space in a VG creates space that you can use for other LVs, but it's not easy to use that to create free partition space.

In other words, you can easily use the space you've freed up to create another LV for use with Linux (for another filesystem, or for swap space), but can't as easily free it up for use by some other operating system that doesn't use LVM (or uses a different volume management scheme).

To create an LV named "test", for example, you could use the command: lvcreate VolGroup00 --size 40GB --name test

To free up space for another partition -- again, only required if you want to use it for a non-Linux operating system-- you could use another disk (or external disk) and use pvcreate/vgextend/pvmove/vgreduce to move the data off the main disk temporarily. Then you could use fdisk/pvcreate/vgextend/pvmove/vgreduce to migrate the data off the external disk onto a smaller PV partition, enabling you to use the freed-up space for another operating system. These operations can be done graphically, while the system is running, using the system-config-lvm tool.

Thank you for the tip regarding vgscan and vgdisplay to activate the LVM in system rescue mode. I would like to add a note regardng LVM on top of software RAID as adding the command "raidstart /dev/md*" is needed to start the RAID. I was able to use your procedure to run fsck on my broken CentOS 5 RAID1, LVM home file server that would not restart after a failed suspend/resume event. The system would kernel panic a few lines into the INIT process without any messges I could read( too fast). Using fsck on the filesystem after following your procedure worked to repair the problem. Thanks again.

Welcome!

TheFedora Daily Package exists to highlight lesser-known Fedora Linux packages each weekday-- with a special article each Wednesday taking a behind-the-scenes look at some of the configuration options and packaging details that make Fedora tick. For more information, please see the Fedora Daily Package Welcome posting.

For information on the Fedora package management system and how to install, update, and remove pacakges, see the postings from Package Management Week (especially Using Yum).

To suggest a future Fedora Daily Package, use the Suggest a Package box below.