Cloning

From Smop.co.uk

Jump to: navigation, search

Earlier today I cloned a Linux machine (we ran out PCI slots in the first machine). As the machine in question is our firewall I wanted to do this live so I decided to use the old favourite of "tar over the network".

Reading the filesystem of the source machine was not a problem, however how could I write the data to the new machine if the box was booted into Linux and using its hard disks? <a href="http://www.knoppix.org">Knoppix</a> to the rescue! Knoppix is a bootable CD that has a full Linux implementation on it - which runs from the CD.

So here are the steps I performed to clone the machine:

  1. booted target machine using Knoppix CD (I type "knoppix 2" at the boot prompt to avoid the overhead of starting the graphical interface)
  2. "loadkeys uk" will load a UK keyboard map and "setterm -blength 0" will turn keyboard beeps off
  3. network configured automatically using DHCP (but you could always use ifconfig)
  4. use cfdisk to partition disk (use "sudo cfdisk" in order to run cfdisk as root if you are in the graphical interface)
  5. create filesystems as appropriate (mke2fs /dev/....)
  6. convert to ext3 filesystems if wanted (tune2fs -j /dev/....)
  7. make swap device if needed (mkswap /dev/....)
  8. for each filesystem
    1. mount filesystem
    2. change directory to mountpoint
    3. as root run "nc -l -p 12345 | tar -xf -" (12345 is a random port number)
    4. on the source machine change directory into the same filesystem and then type:
tar -clf - . | nc -w 10 target-machine 12345
    1. sit back and enjoy, watch "vmstat 1" or "df -k" on the target machine to watch the progress.
  1. some distributions give the netcat utility a name of netcat rather than the traditional nc so try that if nc cannot be found
  2. you may like to add a "z" into the tar comamnds (after the c or x) which will compress the data before sending it over the network

We now need to perform some tidying up on the destination machine as partitions have moved around. Ensure the files you modify are those on the new filesystems (i.e. those in /mnt/etc not the Knoppix ones in /etc). Ill assume the new root filesystems is mounted on /mnt):

  1. edit /mnt/etc/fstab and ensure the partitions are correct for both swap areas and filesystems. Filesystems may also need their filesystem types changing (if the source machine was reiserfs but the target machine was ext3 for example)
  2. edit /mnt/etc/raidtab if that is different (ignore this if it means nothing to you)
  3. edit /mnt/etc/lilo.conf with the correct boot and root sections. root should match what / is in /mnt/etc/fstab (as usual)
  4. run "/mnt/sbin/lilo -r /mnt" - this will install lilo but will do a "chroot" to /mnt first
  5. reboot, remove the Knoppix CD and enjoy!
  6. remember that having the source and target machines up on the same network may cause problems - for instance if they both try and use the same IP address

It so happened that in our case, the source machine used normal IDE disks but the target machine used SCSI disks. Here then are details for SuSE Linux:

  1. type lspci and find the module for your SCSI card (aix7xxx in our case)
  2. edit /mnt/etc/sysconfig/kernel, listing the required modules in the INITRD_MODULES line
  3. "chroot /mnt" - you will notice some things now look wierd (like df)
  4. run "mk_initrd" - if this complains about the temporary directory, ensure /var/tmp is mounted
  5. run "lilo"

If you are using the Grub rather than Lilo as your bootloaded, dont ask me! You will probably need to just do the initial Grub installation (so that it can install itself in the bootsector of the hard disk).

If you wish, you might prefer to simplify this whole setup and just copy the raw disk from one machine to the other. If you want to do this, ensure the target disk is large enough, then on the target machine run: "nc -l -p 12345 > /dev/..." and on the source machine type "nc target-machine 12345 < /dev/...". (NB: here the ... means something like "hda" (the whole disk) rather than the hda1 (a single partition) that you normally use in formatting commands.

Finally, I was doing this on a 166MHz Pentium box with 96MB ram (target) and 200MHz Pentium Pro 128MB (target). Both boxes are easily up to the task (the initial box was normally 99% idle whilst firewalling a 690Kbps connection according to vmstat!)

Personal tools