Converting EXT4 Root Filesystem to XFS (Arch Linux)
XFS offers more modern features compared to EXT4 and performs better when handling large numbers of files
References: XFS vs EXT4, Why did CENTOS 7.0 choose XFS as the default filesystem? What are the advantages of XFS over ext?, Ext4 vs XFS – Which one to choose?
NOTE: Since XFS cannot shrink partitions, it’s recommended to use LVM for flexibility
Prerequisites: Arch Linux system with LVM+EXT4 disk management
Technical references: Change Root File System from Ext4 to Xfs on Archlinux, XFS (简体中文), LVM (简体中文)
Preparation
Backup system data and create Arch Linux Live environment
Preparing Filesystem
Reboot into Live environment
Shrink EXT4 Logical Volume
Compress Ext4 Partition
Reference: Resizing EXT4 partition safely
e2fsck -f /dev/<volume_group>/<physical_volume>
# Check the partition to be resized (required by EXT4 tools)
resize2fs /dev/<volume_group>/<physical_volume> 300G
# Resize filesystem to 300G
Shrink LVM Logical Volume
Reference: Shrinking logical volumes and their filesystems simultaneously
lvresize -L 300G /dev/<volume_group>/<physical_volume>
# Resize logical volume to 300G
Create LVM Logical Volume for XFS
Reference: Creating logical volumes (LV)
lvcreate -L 300G <volume_group> -n <xfs_physical_volume>
# Create a 300G logical volume
mkfs.xfs /dev/<volume_group>/<xfs_physical_volume>
# Format the volume with XFS filesystem
Data Migration
NOTE: Do not use cp for data copying as it changes setuid bits and follows hard links.
Using tar to Copy All Files
- Mount XFS and EXT4 partitions
tar -cf - <EXT4_mount_path> | ( cd <XFS_mount_path> ; tar -xpvf - )
PS: You can use pv to monitor progress
Since tar cannot preserve SELinux labels (if any), ACLs and xattrs, use rsync for exact file copying.
Reference: How can a filesystem be copied exactly as is?
rsync -aviHAXKhPS <EXT4_dir> <XFS_dir>
System Configuration
Update GRUB2 Configuration
Mount Root and EFI Partitions
mount /dev/<volume_group>/<xfs_physical_volume> /mnt # Mount XFS partition
mount /dev/<EFI_Partition> /mnt/boot/efi # Mount EFI partition
arch-chroot /mnt # Enter chroot environment
Verify Swapfile
If you use swapfile for system hibernation, reconfigure GRUB2 accordingly
Rebuild GRUB2 Configuration
grub-mkconfig -o /boot/grub/grub.cfg # Rebuild GRUB2 config
Update Fstab File
Exit chroot environment
genfstab -U /mnt >> /mnt/etc/fstab # Update file
vim /mnt/etc/fstab # Remove/comment old EXT4 mount points
System Verification
Reboot and check if system boots normally
Check XFS Filesystem Status
Reference: XFS_(简体中文)#Data corruption
sudo xfs_scrub / # Online error checking
sudo xfs_db -c frag -r / # Check disk fragmentation
NOTE: If any errors are found, immediately enter Live environment for offline repair
xfs_repair -v /dev/<volume_group>/<xfs_physical_volume>
Check Error Logs
journalctl -p 3 -xb
Troubleshooting
LVM Mount Failure
Reference: LVM replace hard drive and move data smoothly
lvm vgchange -ay # Activate all LVM groups
mount /dev/mapper/YOUR_LVM_NAME /new_root # Replace YOUR_LVM_NAME with your LVM group name
exit # Exit rescue mode and boot into system
XFS Fragmentation Issues
Enter Live system to repair filesystem
mount /dev/<volume_group>/<xfs_physical_volume> /mnt # XFS requires mounting first
umount /mnt
xfs_repair -v /dev/<volume_group>/<xfs_physical_volume> # Repair
Attempt defragmentation:
mount /dev/<volume_group>/<xfs_physical_volume> /mnt # Defrag requires mounted partition
xfs_db -c frag -r /dev/<volume_group>/<xfs_physical_volume> # Check fragmentation
xfs_fsr /dev/<volume_group>/<xfs_physical_volume> # Defragment
XFS Mount Failure
Reference: mount: Structure needs cleaning. How to repair without losing data?
Enter Live system to repair filesystem:
mount /dev/<volume_group>/<xfs_physical_volume> /mnt # Will fail due to corruption
xfs_repair -v -L /dev/<volume_group>/<xfs_physical_volume> # Force clear journal and repair (may lose data)
NOTE
XFS cannot shrink partitions, but never attempt to shrink only the LVM logical volume without shrinking the partition.