XFS is the default file system on CentOS 7 and RHEL 7 as a very viable alternative to ext3 and ext4, particularly with awesome new freeze and snapshot features of xfs (xfs_freeze, snapshot, xfs_unfreeze, delayed allocation, etc). However, unlike conventional ext containers, a major limitation of XFS is inability to shrink or reduce this file system container. Instead of recreating your volume partitions from scratch, a workaround is to unmount your desired partition to be shrunk, recreate a new smaller and xfsdump all your existing data into your new smaller partition.
Here is an example on how you go about doing it to resize my /home partition in the vg00 Logical Volume.
- Backup all your data if you’ve not done so.
- Install dependences if you have not: yum install xfsdump
- Note the following:
- Your Volume Group using the “pvs” command to display your device “/dev/sdb1” and its associated Volume group, in this example it’s “vg00”.
- Your file system size which you will use to compare with the new partition to ensure all your files are transferred, do a “df -h /home/” and the the size of your partition to shrink.
- Your UUID doing cat /etc/fstab and note your /home partition UUID. You can similarly list your partitions and their respective UUID using sudo blkid. Make a note of the UUID of your partition to shrink (/home). This is to ensure your shrunk partition is mounted at boot.
- Backup the data using xfsdump (backing up /home to dump at the -f parameter)
xfsdump -f /tmp/home.dump /homeDo ensure that you have enough space in /tmp to hold the entire partition you are dumping, or substitute “/tmp” to a partition of your liking with enough space and remember to delete it thereafter when done.
- Unmount your old home partition:
cd /
umount /home - Delete your old /home partition:
lvremove /dev/vg00/home - Recreate your new /home volume with a smaller size, say 5GB:
lvcreate –name home -L 5GB vg00 - Format your newly-created XFS /home partition
mkfs.xfs /dev/vg00/home - Mount your new /home partition:
mount /dev/vg00/home /home - Restore the data using xfsrestore, substituting “/tmp” otherwise if you changed it in Step 3:
xfsrestore -f /tmp/home.dump /home - Get your new UUID for fstab.
sudo blkid - Update your /etc/fstab with the UUID to ensure that your new shrunk /home partition is auto-mounted on boot. And verify your /hom parition size using “df -h /home/” again.
And there you have it, all your /home partition files in a smaller XFS container. A thing to learn from this is to adopt a best practice to create your XFS LVMs using the smallest possible volume sizes during the point of OS install, where you can easily expand them quickly as needed using xfs_growfs using your utilized disk space.
Repeat steps 4-10 for every XFS volume you wish to resize.
Hope this quick guide helps and happy Linux-ing!
[…] 除了直接减小分区大小外,还有一种方法,先移除/dev/centos/home,再添加,可以参考如何在 LVM 中减少/收缩 Linux XFS 卷 – ShaunChng.com […]