Clone a Linux system installation to another computer
The following method should work for any Linux distribution. Source and target systems must be on the same processor architecture (though transfer from 32bit to 64bit should work).
What you need:
- 2 live USB keys (or cds)
- Good quality ethernet cables (one cable between the 2 computers is OK), or a usb key/drive with a BIG ext4 partition.
1. Boot source and target machines on live USB/CD
Any live USB/CD should be OK.
On the target computer, you will need a tool to partition your hard drive, like gparted.
rsync is also required for data transfer: it’s included in many live systems.
Ubuntu live cd is OK, Manjaro live cd too.
2. Partition your target hard drive
Use a tool like gparted to partition the target hard drive, with the same partitions as your source system (slash, swap, home…).
I recommend you to assign LABELs to your partitions: for the fstab, it’s easier than UUIDs.
3. Mount all partitions on both machines
On both systems, open a root terminal. Then, for each data partition (you can ignore swap):
mkdir /mnt/slash
mount /dev/sdaX /mnt/slash
If you have a home partition:
mkdir /mnt/home
mount /dev/sdaY /mnt/home
4. Transfer the data (network or usb)
This part may be tricky. Choose the method you prefer.
Network
Setup the network. Test the connectivity with ping command.
The easier is to plug the PCs on a DHCP network (like your ISP box) so that you get automatic IP addresses. If you linked the 2 pcs with a single cable, you’ll have to setup the IPs with NetworkManager (static ips, or adhoc network).
On source system, as root, create a simple /etc/rsyncd.conf file:
uid = root
gid = root
use chroot = no
[all]
path = /
Then start the rsync daemon server: rsync --daemon
On target PC, for each partition:
rsync -avHX SOURCE_IP::all/mnt/slash/ /mnt/slash/
Don’t forget ‘/’ at the end of paths. -a will preserve many file attributes like owner and permissions, -H will preserve hardlinks if any, -X will preserve extended attributes like setuid. You may also add -A if you are using acls. What is good with rsync is that you can stop and restart the transfer whenever you want.
USB
Prepare a USB drive with a BIG ext4 partition.
Mount the USB partition on source system (mount /dev/sdbX /mnt/usb)
For each partition:
rsync -avHX /mnt/slash/ /mnt/usb/slash/
umount, unplug and remount the USB disk on the target system.
For each partition:
rsync -avHX /mnt/usb/slash/ /mnt/slash/
5. Change fstab on target system
As root, edit /mnt/slash/etc/fstab
For each partition (including swap), replace the first field with the new UUID or LABEL (it’s straightforward with LABELs):
UUID=the-long-uuid, or LABEL=yourlabel
2 ways to get the UUIDs / LABELs:
ls -l /dev/disk/by-uuid/
blkid /dev/sdaX
6. Reinstall Grub
We will use a chroot (changed root environment) to be able to call the grub install inside the migrated system.
First, bind mount some system directories needed by grub, then chroot:
mount --bind /proc /mnt/slash/proc
mount --bind /sys /mnt/slash/sys
mount --bind /dev /mnt/slash/dev
mount --bind /run /mnt/slash/run
chroot /mnt/slash
Then install grub in the Master Boot Record of your hard drive, and update grub config file (with the new uuids…):
grub-install /dev/sda
update-grub
7. Reboot target machine
That’s it! Your system should be working on the new computer now.
Feel free to comment if you encounter problems.
-----------------------------------------------
Frank
2014-08-10 at 14:05
Based on your guide, I cloned an actively running Arch system (X server logged off) successfully the following way:
->A primary partition has been created on the new SSD using fdisk, then it has been formatted with EXT4 using mkfs.ext4 and mounted to /mnt/new
->The partition has been cloned using rsync, but /mnt has to be exluded, otherwise it will be copied recursively until the new disc is full:
rsync -avHX –exclude ‘mnt’ / /mnt/new/
->arch-chroot has been run directly at the /mnt/new without the blind mounts.
->grub-mkconfig -o /boot/grub/grub.cfg
->grub-install /dev/sda
->mkinitcpio -p linux#
Reboot…works!
I forgot: the new uuid of the SSD partition has also been edited in the fstab.