How to Migrate a Linux System to Another Partition While Preserving File Attributes and Links


14 views

Migrating a live Linux system involves more than simple file copying. The operation must preserve:

  • File permissions and ownership
  • Special files in /dev
  • Hard and symbolic links
  • Extended attributes (xattr)
  • Sparse files

While cpio can work, these methods generally provide better results:

# Using rsync (recommended for most cases)
rsync -aAXHv --numeric-ids --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /mnt/sdb5

# Using tar for complete archiving
(cd / && tar -cvpzf - .) | (cd /mnt/sdb5 && tar -xvpzf -)

After copying the files, these configuration updates are essential:

# Update fstab
blkid /dev/sdb5
# Edit /mnt/sdb5/etc/fstab with new UUID

# Reinstall bootloader
chroot /mnt/sdb5
grub-install /dev/sdb
update-grub

Before switching systems, verify:

  • Run find /mnt/sdb5 -type l -exec ls -l {} \; to check links
  • Verify special files with ls -l /mnt/sdb5/dev
  • Test boot from new partition using a temporary grub entry

For LVM or btrfs users:

# LVM snapshot approach
lvcreate -L 10G -s -n root_snapshot /dev/vg/root
dd if=/dev/vg/root_snapshot of=/dev/sdb5 bs=4M

Moving an active Linux installation to a new partition while preserving all system attributes requires careful handling of:

  • Symbolic and hard links
  • Special device files in /dev
  • File permissions and ownership
  • Filesystem-specific attributes (xattr)

While cpio can work, these alternatives provide better guarantees:

# rsync example with full attribute preservation
rsync -aAXHv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /mnt/sdb5

# tar alternative with similar capabilities
tar --xattrs --acls -cvpf - / | (cd /mnt/sdb5 && tar --xattrs --acls -xvpf -)

1. Prepare target partition:

mkfs.ext4 /dev/sdb5
mount /dev/sdb5 /mnt/newroot

2. Copy system files (using rsync for example):

rsync -aAXHv --numeric-ids --delete \
  --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} \
  / /mnt/newroot

3. Update boot configuration:

chroot /mnt/newroot
grub-install /dev/sdb
update-grub
exit

Before switching to the new partition:

  • Verify file attributes with lsattr and getfacl
  • Check symlinks with find /mnt/newroot -type l -ls
  • Test boot using GRUB command line or temporary boot entry

Problem: Device files not copied properly
Solution: Recreate device nodes in chroot:

mount --bind /dev /mnt/newroot/dev
chroot /mnt/newroot
mount -t proc proc /proc
mount -t sysfs sys /sys
mount -t devpts devpts /dev/pts

Problem: SELinux contexts lost
Solution: Relabel filesystem:

touch /mnt/newroot/.autorelabel