How do I shrink a Linux filesystem?

WARNING: storage maintenance, especially reducing filesystems, can potentially result in errors or data loss. Plan and act accordingly. Always create backups and follow best practices.

For this example, we’ll be using the standard /dev/datavg/eclipse LV that’s mounted at /u2/eclipse. We’ll be shrinking the filesystem to 50GB.

Begin by unmounting the file system:

umount /u2/eclipse

Force a filesystem check:

e2fsck -f /dev/datavg/eclipse

Reduce the file system. The number specified here is the final, target size of the filesystem, not the amount by which it is reduced.

resize2fs /dev/datavg/eclipse 50G

The logical volume must also be reduced by the same amount:

lvreduce -L 50G /dev/datavg/eclipse

Mount the filesystem and run df to verify the change was made:

mount /u2/eclipse
df -h /u2/eclipse

Resources:

  • https://access.redhat.com/kb/docs/DOC-5531
  • http://blog.shadypixel.com/how-to-shrink-an-lvm-volume-safely/

Why can’t I remove a Linux logical volume?

If you are unable to unmount or lvremove a logical volume, verify that there are no processes holding the LV.

Locate the major/minor numbers for the logical volume you’re trying to remove (ie lvol0):

dmsetup info -c | grep lvol0

Take note of the 5th column, which indicates if a volume is “open,” and the 2nd and 3rd columns, which are the major and minor IDs, respectively. For example:

[root@eclipse ~]# dmsetup info -c | grep lvol5
datavg-lvol5         253  24 L--w    1    1      0 LVM-4WlscxDDEw5R9IvjgZYpu5FQeW9h835ADtQzXFr3cJ6SHEcgS4NFMxzaSKjUkedy
datavg-lvol5-cow     253  27 L--w    1    2      0 LVM-4WlscxDDEw5R9IvjgZYpu5FQeW9h835ADtQzXFr3cJ6SHEcgS4NFMxzaSKjUkedy-cow

Find any process attached to this volume by searching on the major and minor IDs discovered above:

lsof | grep "major,minor"

For example:

[root@eclipse ~]# lsof | grep "253,24"
beremote  14524      root   15r      BLK             253,24                1835186 /tmp/filehRrCD8 (deleted)

Shut down or kill any process still accessing the volume to continue unmounting and removal.

If no processes show as accessing the volume in lsof, check to make sure nothing is sharing the filesystem (i.e. rsync, nfs, or samba).

If you have verified that no processes are holding the LV open, verify that the LV is not mounted somewhere:

cat /proc/mounts

If the volume appears in the output, unmount it using the umount command.
For further assistance, please contact Red Hat for support