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 the necessary folders to run
grub-mkconfig on the clone
for i in dev dev/pts proc sys run; do mount -B /$i /tmp/dstRoot/$i; done
- Rebuild grub menu on the clone
chroot /tmp/dstRoot
update-grub
exit # exit chroot
- Unmount the previously mounted folders and cleanup tmp folders
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}
- Rebuild grub menu on the source root file system to include the cloned partition
update-grub
- 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