Linux – How to shrink an LVM partition on CentOS

centoslinux

I'm trying to reduce the size of a partition on a CentOS 5.8 VM as my predecessor allocated far too much space to it. However, I've tried a variety of different methods and have run into problems with all of them. The details of the current partitions is below:

Model: VMware Virtual disk (scsi)
Disk /dev/sda: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number   Start   End    Size   Type      File system   Flags
1        32.3kB  107MB  107MB  primary   ext3          boot
2        107MB   215GB  215GB  primary                 lvm

When I try to resize the partition using Parted (running off of a Knoppix Live-CD with the system partition unmounted) I get this error:

(parted) resize 2 107 30827

WARNING: you are attempting to use parted to operate on (resize) a file system. parted's file system manipulation code is not as robust as what you'll find in dedicated, file-system-specific packages like e2fsprogs. We recommend you use parted only to manipulate partition tables, whenever possible. Support for performing most operations on most types of file systems will be removed in an upcoming release.
Error: Could not detect file system.

I've tried to find a good guide on how to do this with e2fsprogs but everything I've come across isn't explaining how to resize the lvm partition like I need to.

I'm new to the Linux world so perhaps this is basic and I'm overanalyzing but any help would be greatly appreciated.

Thanks in advance!

Best Answer

Growing file system is easy. Shrinking the file system is trickier and not supported by all file systems. ext3 and ext4 do support shrinking, though, but BE VERY CAREFUL WITH IT. Make absolutely sure you have good backups. Then, verify once more you have good backups.

You should first shrink the file system itself with resize2fs /dev/yourdevice newsizegoeshere.

Next you need to shrink the logical volume: lvresize -L X /path/to/your/logical_volume, where X is the new size expressed in some way. I purposely left out the syntax, see man lvresize for available options to see your preferred way to tell the new size. You can subtract from the old size or just specify the new size.

Next you need to shrink the volume group itself with vgresize, if you see that necessary. Alternatively you can just create new logical volumes inside the volume group, now that the previous step with lvresize gave you some space to breathe.

If you took the vgresize route, you finally need to adjust the partition size with fdisk by deleting the partition, and recreating it with the exactly same starting block as before and making the size smaller than before.

EDIT: It appears that with LVM2 volume group shrinking is not that trivial thing to do. You may need to delete the volume group and recreate it... oh dear. So, how about my footnote, would it work?

If you and/or software fail in any of these steps, your data goes boof. So once more: MAKE SURE YOU HAVE GOOD BACKUPS.


Oh, and as your copy-paste mentions VMware: could you just create a new virtual disk in VMware, copy over your stuff from your over-sized disk to the new disk, nuke the over-sized disk from the orbit and call it a day?