Search:  
Gentoo Wiki

Clone_a_device

Contents

About

This article deals with the case where you want to install a machine with exactly the same configuration as another machine by cloning the device over network. Assuming you're on the same network, boot both machines using a Linux Live CD.

Note: You may need to get yourself a copy of Netcat the easiest way to do this is jump onto an existing Gentoo box and build netcat:

emerge gnu-netcat

Once netcat has been built on your Gentoo box copy the binary over to both the box you want to clone (the server) and the client (box you want to create.) of course replace the IP address with the IP address of the appropriate machine.

scp /usr/bin/netcat 192.168.1.1:/tmp/nc

If you did not already have nc on your livecd you can now use it by replacing nc with /tmp/nc throughout the rest of the guide.

Now on the machine you are copying from (assuming the hard drive you want to clone is /dev/hda):

dd if=/dev/hda conv=sync,noerror bs=64k | nc -l -q 0 -p 5000

This begins a low level read of the hard drive and pipes it to netcat, which just listens on port 5000 for a connection.

Next, we must setup the client:

nc 192.168.1.1 5000 | dd of=/dev/hda bs=64k

Replace "192.168.1.1" with the IP address of the machine you are copying from.

This will take a long time.

Problems

This only really works well when both machines are exactly the same or you are using a less customized OS, such as Fedora or Microsoft Windows. Gentoo Linux Kernels are generally customized to the exact spec of the machine. If the kernel is the only problem and not the partition layout, you could just compile a new kernel on the server and copy it to /boot on the client in place of the current one.

If however your partitions are different you could copy the / partition and make a new /boot wherever you want.

A good way to get around this is to only copy the / partition. Instead of the above use:

dd if=/dev/hda3 conv=sync,noerror bs=64k | nc -l -q 0 -p 5000

Replace "hda3" with your / partition.

You must then setup the partitions on your client machine to be similar to the server. The / partition must be the same or larger than the original.

Then type this on your client:

nc 192.168.1.1 5000 | dd of=/dev/hda3 bs=64k

You can then do the same for /boot and copy a new kernel across like above. You will also need a new Master Boot Record so that the client knows to boot using Grub.

To do this, you'll need to chroot into your new / partition, which hopefully has grub installed if it came from a Linux machine.

From the Live CD:

mkdir /mnt/hda3
mount /dev/hda3 /mnt/hda3
chroot /mnt/hda3 /bin/bash
grub
root (hd0,0)
setup (hd0)

Substitute (hd0,0) for the location of /boot (don't forget grub's weird naming habits) and (hd0) for the hard drive itself.

The only thing left to do is edit /boot/grub/grub.conf to make sure the settings are correct for the new machine.

Extras

Compression

It is also possible to compress the output during transfer using gzip. This may or may not make a real difference to the transfer rate but you would use something like this:

dd if=/dev/hda conv=sync,noerror bs=64k | gzip -c | nc -l -q 0 -p 5000
nc 192.168.1.1 5000 | gzip -cd | dd of=/dev/hda bs=64k

Resizing the / partition after the clone

First get the relevant information using fdisk -l (of course, your information will be different):

Code: fdisk -l /dev/hda
Disk /dev/hdb: 20.0 GB, 20003880960 bytes
255 heads, 63 sectors/track, 2432 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/hda1            1           5       40131   83  Linux
/dev/hda2            6          68      506047+  82  Linux swap / Solaris
/dev/hda3           69        2432    18988830   83  Linux

Useful information is:

We can now call reiserfs_resize as follows (in a BASH console... so that we can let BASH do the calculations):

resize_reiserfs -s $[(2432-69)*8225280] /dev/hda3

The output should look something like this:

Code: resize_reiserfs 3.6.19
ReiserFS report:
blocksize             4096
block count           4745199 (1118512)
free blocks           4295716 (669139)
bitmap block count    145 (35)

Syncing..done


resize_reiserfs: Resizing finished successfully.

Resizing the / partition after the clone (GUI method)

if you don't feel confortable with units size and cylinders, you can take a live-cd and launch gparted.

Gparted is a graphic utility that allow you to resize easily your partitions; in my expirience was pretty stable, but take your backup near you ;-)

Obviuosly exist also a qt version named qtparted; and almost every live-cd has one of this tool.

Retrieved from "http://www.gentoo-wiki.info/Clone_a_device"

Last modified: Sat, 06 Sep 2008 14:58:00 +0000 Hits: 20,311