Cloning Linux machines PDF Print E-mail
Tuesday, 19 January 2010 11:51

Below is a (fairly primitive) method for cloning a Linux machine.  In the example below the machine in question is running Ubuntu 9.10 from a 4GB SDCard so backing it up is fairly easy as you just stick the SDCard into another linux machine and finding 4GB of diskspace is easy enough.  For IDE drives or whatever the principle is the same but the method may need to be tweaked, and for really large drives a more intelligent method is probably better.


CAVEATS:  Before we start its worth mentioning a couple of gotchas

  1. Sudo requires a password.  Do not do the dd command as your first sudo command as it is backgrounded and you will never get the prompt!  If you don't need to remove 70-persistent-net.rules then do a sudo ls or something equally benign before you start.  Alternately just log in as root.
  2. Make sure you backup the entire disk and not just the root partition!  In the example above make sure dd uses /dev/sdb and not /dev/sdb1


Step 1:  Backing up the disk image

  • Plug in the SDCard into another Linux machine and mount it.  I'm using an Ubuntu 9.10 laptop so the SDCard will auto-mount.  As you can see from the last line below the root partition of the SDCard is /dev/sdb1, which means the SDCard itself is /dev/sdb:
    • cianer@dorfl:~$ mount
      /dev/sda1 on / type ext4 (rw,errors=remount-ro)
      proc on /proc type proc (rw)
      none on /sys type sysfs (rw,noexec,nosuid,nodev)
      none on /sys/fs/fuse/connections type fusectl (rw)
      none on /sys/kernel/debug type debugfs (rw)
      none on /sys/kernel/security type securityfs (rw)
      udev on /dev type tmpfs (rw,mode=0755)
      none on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
      none on /dev/shm type tmpfs (rw,nosuid,nodev)
      none on /var/run type tmpfs (rw,nosuid,mode=0755)
      none on /var/lock type tmpfs (rw,noexec,nosuid,nodev)
      none on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
      /dev/sda3 on /home type ext4 (rw)
      binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev)
      gvfs-fuse-daemon on /home/cianer/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=cianer)
      usbfs on /proc/bus/usb type usbfs (rw)
      /dev/sdb1 on /media/03317d9a-5e3f-4886-88e4-2d06a71c346c type ext4 (rw,nosuid,nodev,uhelper=devkit)
      cianer@dorfl:~$
  • Most versions of Linux use udev to create persistent network names for interfaces so they survive across reboots/hot-swaps/etc.  This causes problems when cloning as the NIC on the master machine is permanently stored on the cloned machines hence they end up with eth1 being the active interface instead of eth0.  To prevent this remove the auto-generated network rules file from the master disk:
    • cianer@dorfl:~$ sudo rm /media/03317d9a-5e3f-4886-88e4-2d06a71c346c/etc/udev/rules.d/70-persistent-net.rules 
      [sudo] password for cianer:
      cianer@dorfl:~$
  • Finally unmount the disk (very important!) and use dd to back up the entire SDCard to a file on your hard-drive.  Using the syntax given below allows you to get status updates by sending a kill -USR1 signal to the running process as shown:
    • cianer@dorfl:~$ umount /media/03317d9a-5e3f-4886-88e4-2d06a71c346c 
      cianer@dorfl:~$ sudo dd if=/dev/sdb of=ubuntu_9.10_backup& pid=$!
      [1] 15084
      cianer@dorfl:~$
      cianer@dorfl:~$ sudo kill -USR1 $pid
      cianer@dorfl:~$ 279777+0 records in
      279776+0 records out
      143245312 bytes (143 MB) copied, 10.6103 s, 13.5 MB/s

      cianer@dorfl:~$
  • This status information is also printed when dd completes.  My 4GB SDCard takes 290s, or roughly 5 minutes, to back up.


Step 2:  Restoring the image to a new disk

  • This is basically the previous steps with the dd command reversed.  Remember to umount the SDCard if it got auto-mounted when you plugged it in!
    • cianer@dorfl:~$ umount /media/newdisk
      cianer@dorfl:~$ sudo dd if=ubuntu_9.10_backup of=/dev/sdb& pid=$!
      [1] 15448
      cianer@dorfl:~$
      cianer@dorfl:~$ sudo kill -USR1 $pid
      cianer@dorfl:~$ 75921+0 records in
      75921+0 records out
      38871552 bytes (39 MB) copied, 11.1882 s, 3.5 MB/s

      cianer@dorfl:~$
  • As before you can use 'kill -USR1' to get a progress report.  Note writing to an SDCard is far slower than reading.  My 4GB SDCard takes 1931s, or roughly 30 minutes, to burn the image.
 
Joomla Templates by Joomlashack