- blog.up-link.ro - https://blog.up-link.ro -

Linux: How to shrink a Logical Volume with LVM

LVM is a logical volume manager for the Linux kernel. It manages disk drives and similar mass-storage devices, in particular large ones. The term "volume" refers to a disk drive or partition thereof. LVM was originally written in 1998 by Heinz Mauelshagen, who based its design on that of the LVM in HP-UX.

The LVM can:
- Resize volume groups online by absorbing new physical volumes (PV) or ejecting existing ones.
- Resize logical volumes (LV) online by concatenating extents onto them or truncating extents from them.
- Create read-only snapshots of logical volumes (LVM1).
- Create read-write snapshots of logical volumes (LVM2).
- Stripe whole or parts of logical volumes across multiple PVs, in a fashion similar to RAID 0.
- Mirror whole or parts of logical volumes, in a fashion similar to RAID 1.
- Move online logical volumes between PVs.
- Split or merge volume groups in situ (as long as no logical volumes span the split). This can be useful when migrating whole logical volumes to or from offline storage.

In this article I'll show you how to shrink a LVM Volume Safely.

Logical Volume Management allows you to decrease the size of a volume without recreating it completely.
First, as is always the case when you're modifying disk volumes, partitions, or file systems, you should really have a backup.

All of the required steps must be performed on an unmounted volume. If want to reduce the size of a non-root volume, simply unmount it. For a root volume, you'll have to boot from a CD or USB drive. Any modern live or rescue CD should work fine. I personally prefer System Rescue CD [1]. It includes almost any disk management programs you might need. After booting from a CD, you may have to issue:

vgchange -a y

This makes any logical volumes available to the Linux. Most boot CD's will do it automatically some time during the boot process. Next, force a file system check on the volume in question (make sure the volume is not mounted):

e2fsck -f /dev/VolGroup00/LogVol00

Device names for LVM volumes follow the convention: /dev/volume-group/logical-volume/. In this case, my volume group is named VolGroup00 and the volume I'm going to shrink is named LogVol00. Next, resize the file system:

resize2fs /dev/VolGroup00/LogVol00 18G

Replace 18G with about 90% of the size you want the final volume to be. For example, in this case, I want the final volume to be 20 gigabytes, so I will reduce the file system to 18 gigabytes. Why is this necessary? When we reduce the size of the actual volume in the next step, it is critical that the new size is greater than or equal to the size of the file system. After reading the documentation for both resize2fs and lvreduce, I still haven't been able to find out whether they're using standard computer gigabytes (1024^3 bytes) or drive manufacturer gigabytes (1000^3 bytes). In this case, the difference is very important. To be on the safe side, we'll just shrink the file system a bit more than necessary and expand it to use the full space available later. Next, reduce the size of the logical volume:

lvreduce -L 20G /dev/VolGroup00/LogVol00

In this case, use the actual size you want to the volume to be. Finally, grow the file system so that it uses all available space on the logical volume:

resize2fs /dev/VolGroup00/LogVol00

It's recommended to run a file system check again.