2014-04-06 17:54

After searching a bit I could not find a simple and good howto to do that.
The following method should work for any Linux distribution (Ubuntu, Debian, Manjaro, Archlinux, Fedora…). 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)
  • To speed up data transfer: good quality ethernet cables (one cable between the 2 computers is OK), or a usb key/drive with a BIG ext4 partition. You can try over wifi, but it may be slow.

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

  1. 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).
  2. On source system, as root, create a simple /etc/rsyncd.conf file:
    uid = root
    gid = root
    use chroot = no
    
    [all]
        path = /
    
  3. Then start the rsync daemon server: rsync --daemon
  4. 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.

  1. Mount the USB partition on source system (mount /dev/sdbX /mnt/usb)
  2. For each partition:
    rsync -avHX /mnt/slash/ /mnt/usb/slash/
    
  3. umount, unplug and remount the USB disk on the target system.
  4. 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.

2014-04-06 17:54 · Tags: , ,

11 Comments

  1. 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!

    Reply

  2. I forgot: the new uuid of the SSD partition has also been edited in the fstab.

    Reply

  3. worked like a charm for ubuntu 14.04. thanks a lot

    Reply

  4. Worked exellent with ubuntu 14.04. Great guide, thanks.

    Reply

  5. Thank you. This is an easy but not too much shared thing i love.
    Rsync power ;)

    Reply

  6. Thank you for the tutorial, really useful!
    I just used it to clone a debian wheezy ( without X ) installation, and i had to do one step more to set up properly the system:
    after the first boot of the cloned SO, i had ethernet and wifi cards named as eth1 and wlan1 instead of eth0 and wlan0.
    That was because of the saved mac addresses in /etc/udev/rules.d/70-persistent-net.rules

    so in had 2 lines about the source pc mac addr plus two lines with the cloned one:

    SUBSYSTEM==”net”, ACTION==”add”, DRIVERS==”?*”, ATTR{address}==”OLDMACADDR”, ATTR{dev_id}==”0×0″, ATTR{type}==”1″, KERNEL==”eth*”, NAME=”eth0″
    (same for wlan0)

    SUBSYSTEM==”net”, ACTION==”add”, DRIVERS==”?*”, ATTR{address}==”NEWMACADDR”, ATTR{dev_id}==”0×0″, ATTR{type}==”1″, KERNEL==”eth*”, NAME=”eth1″
    (same for wlan1)

    i just had to remove the firs 2 lines, and change the seconds to eth0 and wlan0 .
    hope it helps

    thaks again

    Andrea

    Reply

  7. How to do this in a UEFI computer?

    Reply

  8. Daniel Oliveira

    Thank you very much my friend. This help me a lot. I create a MPI cluster cloning all systems with your instructions.

    Reply

  9. César Del Panta

    Thank you very much!!! It worked like a charm in ubuntu studio 14.04.

    Because i have /home and /var in two differents partitions in another drive, first to launch chroot command i had mounted these partitions
    mount -t ext4 /dev/sdaX /mnt/slash/var #mount my /var partition type ext4
    mount -t ext4 /dev/sdaX /mnt/slash/home #mount my /home partition
    ### and then
    chroot /mnt/slash
    ### Install grub
    grub-install /dev/sda
    update-grub

    Otherwise if i do not mount the /var partition when before chrooting, the command ‘update-grub’
    fails.

    Thanks a lot.

    Reply

  10. Thank you so much!!! you saved my life, very usefull, tanks

    Reply

  11. Thanks a lot! You safed me! I had an old Linux computer and got rid of the old hardware. I was able to create a virtual machine with your help. Thanks again.

    Reply

Leave a Reply to tybodoudou Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>