Clone a live root file system using tar
Let's say you want to clone a live Ubuntu root file system to an unused
partition /dev/sdc5
on the same disk. /dev/sdc5
size must be no less than the
data occupied by the root partioned being cloned. Let's assume that an EFI
partition is /dev/sdc1
.
- Format the destination partition and use a label to identify it
mkfs.ext4 -L Ubuntu-sdc5 -J size=128 /dev/sdc5
mkdir /tmp/{dstRoot,srcRoot}
mount /dev/sdc5 /tmp/dstRoot # destination root
mount --bind / /tmp/srcRoot # source root
- Clone the current root to
/dev/sdc5
mounted partition
tar -C /tmp/srcRoot -cf - . | tar -C /tmp/dstRoot -xf -
- Edit
/tmp/dstRoot/etc/fstab
to reflect the new label assigned to /dev/sdc5
:
LABEL=Ubuntu-sdc5 / ext4 errors=remount-ro 0 1
mount /dev/sdc1 /tmp/dstRoot/boot/efi
- Clone the current Ubuntu EFI folder
mkdir /tmp/dstRoot/boot/efi/EFI/ubuntu2
rsync -av /boot/efi/EFI/ubuntu/ /tmp/dstRoot/boot/efi/EFI/ubuntu2/
- Edit
/tmp/dstRoot/boot/efi/EFI/ubuntu2/grub.cfg
to reflect the new label
Ubuntu-sdc5
and partition gpt5
search.fs_label Ubuntu-sdc5 root hd2,gpt5
- Mount the necessary folders for
grub-mkconfig
for i in dev dev/pts proc sys run; do mount -B /$i /tmp/dstRoot/$i; done
- Reconfigure grub on the cloned root file system
chroot /tmp/dstRoot
grub-mkconfig -o /boot/grub/grub.cfg
exit # exit chroot
for i in run sys proc dev/pts dev; do umount /tmp/dstRoot/$i; done
umount /tmp/dstRoot/boot/efi
umount /tmp/dstRoot
umount /tmp/srcRoot
rmdir /tmp/{dstRoot,srcRoot}
- Reboot into the EFI setup menu and add a new boot entry to boot options
systemctl reboot --firmware-setup
NOTE: The EFI boot software should automatically detect a new boot entry, but
if it doesn't, boot to the original root and try to add it manually:
efibootmgr -c -d /dev/sdc -p 1 -L "Ubuntu2" -l "\EFI\ubuntu2\shimx64.efi"
- Reboot again into the EFI setup menu and add a new boot entry to boot options
ssh user@failingsys "tar cfz - /" > oldsys.tar.gz