How to Resize XFS Partitions: Shrink /home and Extend / on CentOS 7 LVM


2 views

When working with CentOS 7's default XFS filesystem on LVM, traditional resize tools like resize2fs won't work because:

# df -T
Filesystem              Type  Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs    50G   50G  341M 100% /
/dev/mapper/centos-home xfs   500G   20G  480G   4% /home

XFS filesystems can only be expanded, not shrunk while mounted. This requires a different approach than ext4 filesystems.

1. Create Backup of Critical Data

Before any partition operations:

# tar czvf /tmp/home_backup.tar.gz /home
# tar czvf /tmp/root_backup.tar.gz --exclude=/home --exclude=/tmp --exclude=/dev --exclude=/proc --exclude=/sys /

2. Unmount the /home Partition

# umount /home
# lvchange -an /dev/mapper/centos-home

3. Check Filesystem Integrity

# xfs_repair /dev/mapper/centos-home

4. Shrink the XFS Filesystem

Unlike ext4, we need to:

  1. Create a new smaller filesystem
  2. Copy data to it
# mkdir /mnt/temphome
# mkfs.xfs -f -L home /dev/mapper/centos-home
# mount /dev/mapper/centos-home /mnt/temphome
# tar xzvf /tmp/home_backup.tar.gz -C /mnt/temphome

5. Resize the Logical Volume

Reduce the LV to desired 400G:

# lvreduce -L 400G /dev/mapper/centos-home
# xfs_growfs /dev/mapper/centos-home

6. Extend the Root Partition

First check available space:

# vgdisplay centos | grep Free
  Free  PE / Size       64.00m / 64.00m

Extend root filesystem:

# lvextend -L +100G /dev/mapper/centos-root
# xfs_growfs /dev/mapper/centos-root

Confirm the new layout:

# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  150G   50G  100G  34% /
/dev/mapper/centos-home  400G   20G  380G   5% /home

For minimal downtime:

# lvcreate -L 400G -s -n home_snap /dev/centos/home
# mkfs.xfs -f /dev/centos/home
# mount /dev/centos/home /home
# dd if=/dev/centos/home_snap of=/dev/centos/home bs=4M
# lvremove /dev/centos/home_snap
  • If xfs_growfs fails, check filesystem with xfs_repair
  • Ensure no processes are using /home with lsof /home
  • For large partitions, consider performing at system boot from rescue mode

After resizing:

# xfs_admin -defrag /dev/mapper/centos-home
# xfs_fsr /dev/mapper/centos-home

When working with CentOS 7 systems using XFS filesystems, traditional resizing tools like resize2fs won't work since they're designed for ext filesystems. The situation gets trickier when you need to reallocate space between logical volumes, especially when your root partition is full (100% usage in this case).

From your df -h output, we can see:

/dev/mapper/centos-root   50G   50G  341M 100% /
/dev/mapper/centos-home  500G   20G  480G   4% /home

And from LVM information:

VG centos has 557.26G total with only 64.00M free
Currently allocated:
- root: 50G
- home: 499.38G
- swap: 7.81G

Before proceeding:

  1. Backup all important data (though we won't touch /home data, better safe than sorry)
  2. Ensure you have a recent backup of your system
  3. Verify filesystem integrity with xfs_repair
  4. Check disk space usage to confirm the desired resizing is feasible

1. Unmount the /home Partition

umount /home

If you get "target is busy" error, check for processes using /home:

lsof +D /home
fuser -vm /home

Kill or stop these processes before retrying the umount.

2. Reduce the /home Logical Volume

First, check filesystem size and integrity:

e2fsck -f /dev/mapper/centos-home
xfs_repair /dev/mapper/centos-home

Now shrink the filesystem (note: XFS cannot be shrunk! We must recreate it):

lvremove /dev/mapper/centos-home
lvcreate -L 400G -n home centos
mkfs.xfs /dev/mapper/centos-home

3. Remount /home and Restore Data

mount /home

If you had data in /home, restore it from backup now.

4. Extend the Root Partition

First check available space:

vgs

Now extend the root LV:

lvextend -L +100G /dev/mapper/centos-root

For XFS, the filesystem grows automatically after extending.

5. Verify the Changes

df -h
lvs

For systems where /home contains important data that needs to be preserved, consider this alternative:

  1. Create a temporary mount point: mkdir /mnt/temphome
  2. Mount /home there: mount /dev/mapper/centos-home /mnt/temphome
  3. Dump the filesystem: xfsdump -J - /mnt/temphome | xfsrestore -J - /new/home
  4. Proceed with LV resizing as above
  5. Restore the data to the new /home
  • If you get "volume group has insufficient free space" errors, double-check your calculations
  • For emergency recovery, have a CentOS 7 rescue disk ready
  • Consider using screen or tmux for long-running operations
  • Monitor progress with watch lvs or watch df -h in another terminal

After completing all steps, your df -h should show:

/dev/mapper/centos-root  150G   50G  100G  34% /
/dev/mapper/centos-home  400G   20G  380G   5% /home

Remember that these operations are destructive if not performed carefully. Always test in a non-production environment first.