How to Shrink an XFS Filesystem: A Step-by-Step Guide for Linux Administrators


2 views

XFS is a high-performance, journaling filesystem that excels in handling large files and high-throughput scenarios. However, one notable limitation is that XFS does not natively support shrinking a filesystem. Unlike ext4 (with resize2fs) or ReiserFS, tools like xfs_growfs only allow expanding the filesystem.

To effectively shrink an XFS partition, you'll need to:

  1. Backup your data
  2. Recreate the filesystem with a smaller size
  3. Restore your data

Here's a practical example:

# 1. Backup the data
rsync -av /mnt/xfs_partition/ /backup/

# 2. Unmount and recreate partition
umount /mnt/xfs_partition
mkfs.xfs -f /dev/sdX1

# 3. Remount and restore
mount /dev/sdX1 /mnt/xfs_partition
rsync -av /backup/ /mnt/xfs_partition/

If your XFS filesystem is on LVM, you can:

# Shrink the logical volume
lvresize -L -10G /dev/vg0/lv0

# Then follow the backup/recreate/restore process

While XFS is excellent for performance, consider these factors for long-term storage:

  • Pros: Crash recovery, scalability, and excellent large file handling
  • Cons: No native shrinking, limited fsck capabilities

For pure archival storage, ZFS or Btrfs might offer better features like built-in compression and checksumming.

For frequent resizing needs, consider scripting the backup/recreate process:

#!/bin/bash
# Simple XFS resize script
BACKUP_DIR="/backup/xfs_$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"
rsync -av /mnt/xfs_partition/ "$BACKUP_DIR"
umount /mnt/xfs_partition
mkfs.xfs -f /dev/sdX1
mount /dev/sdX1 /mnt/xfs_partition
rsync -av "$BACKUP_DIR/" /mnt/xfs_partition/

XFS was designed with scalability in mind, but unlike ext4 or btrfs, it doesn't natively support filesystem shrinking. This architectural decision stems from XFS's focus on high-performance operations in enterprise environments where storage expansion is more common than reduction.

While there's no direct xfs_shrinkfs command, these methods can achieve similar results:


# Method 1: Backup and recreate
mkfs.xfs -f /dev/sdX  # Create new smaller filesystem
xfsdump -J - /mnt/xfs_old | xfsrestore - /mnt/xfs_new

# Method 2: Using LVM (if filesystem is on LVM)
lvresize --size -10G /dev/vg0/lv0  # Reduce LV first
xfs_growfs /mnt/xfs  # Will fail if smaller than current

The XFS on-disk structure uses B+ trees for metadata and extents for file data. Shrinking would require:

  • Relocating all data blocks beyond new end
  • Reorganizing metadata trees
  • Potential corruption risks during process

For long-term bulk storage where resizing flexibility is crucial, consider:


# ZFS example with easy resize
zfs set quota=10T pool/dataset  # Both grow/shrink supported
zfs list -o name,used,quota

If you absolutely need to reduce an XFS filesystem:

  1. Always backup first with xfsdump
  2. Use LVM thin provisioning if possible
  3. Consider adding new smaller partition rather than shrinking