Skip to content

Instantly share code, notes, and snippets.

@windli2018
Last active February 15, 2026 02:21
Show Gist options
  • Select an option

  • Save windli2018/43ebf52ac447b4e47d2afba9c4a07b65 to your computer and use it in GitHub Desktop.

Select an option

Save windli2018/43ebf52ac447b4e47d2afba9c4a07b65 to your computer and use it in GitHub Desktop.
shrink xfs filesystem

According to https://xfs.org/index.php/Shrinking_Support :
"Currently XFS Filesystems can't be shrunk."
You need backup the data and recreate the file system to shrink it.
But if you have a big xfs filesystem with not too much space used, For example, a 2T xfs volume with 100G data.
And you can take some risk to lose data. Here is a way to shrink it without using new space.
The idea is:
1.temporary reduce the logic volume size, you can still read from xfs filesystem even after the partition or volume is shrunk if the real data is not wrote at the shrunk part
2.create a new volume
3.copy data
4.drop old volume then rename volume name back.

Notice: You may lost your data. Don't try it on important data.

Check if filesystem has enough free space

You will break your file system if there's not enough free space in the filesytem.

./freecount.pl /dev/mapper/cl-data 

Stop all processes using the filesystem, then reduce the volume, free enough space to copy the files.

lvreduce -L -200G /dev/mapper/cl-data 

Create a new volume and mount it

use ext4 if you may need reduce it later

lvcreate -L 200G -n newdata cl
mkfs.ext4 /dev/mapper/cl-newdata 
mount /dev/mapper/cl-newdata /mnt

Use tar to quick copy data

tar --ignore-failed-read -C /data -cf - . | tar --ignore-failed-read -v -C /mnt -xf -

Unmount old and new voume

umount /data
umount /mnt

Remove old volume and rename new volume back

lvremove /dev/mapper/cl-data 
lvrename cl newdata data

Add some new space back to new volume

lvextend -L +200G /dev/mapper/cl-data
e2fsck -f /dev/mapper/cl-data
resize2fs /dev/mapper/cl-data
mount /dev/mapper/cl-data /data

Use the freed space as you wish

lvextend -L +200G /dev/mapper/cl-root
xfs_growfs /dev/mapper/cl-root 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment