How to clone a Linux box using netcat | ||
---|---|---|
Prev |
To clone the original box to the destination box over network using netcat we are needed to follow two steps. First of all we transfer a partial portion of original disk to permit us to abandon the CD linux distribution and to make possible to boot the destionation box directly from hard disk.
When, on the destination box, it runs a Linux with the same users who exist on the original box it will be possible to tranfer all data with the safe that permissions and owners of these files will remain unchanged during the tranfer.
But before doing this we need to take familiarity with netcat.
Netcat or nc as the actual program is named is a simple utility that can work in two ways
read data from STDIN and output them through network
read data from network and output them to STDOUT
The variable that decides if netcat is in client or server mode is the option -l that, if present in the command line that runs nc told it to listen to a particular port that will be defined using the -p option. In client mode
nc host port
creates a TCP (or UDP if -u option is used) to the given port on the given target host, in server modenc -l -p port
put netcat in listening mode to the given port.Now that we know how netcat utility works we can start to partially tranfer original disk and then to boot destination box directly from hard disk.
First of all, in the destination box, move current directory to the mounting point of the empty hard disk and put netcat in listening mode to a randomly port, say 6060. Since we use tar/gz compressed archive to archive source data we redirect the output that netcat receives to tar.
root@0[root]# cd /mnt/hda2 root@0[hda2]# nc -l -p 6060 | tar zxvPpWe analyze in detail this last line. Like saying, the -l option says netcat to put it in listening mode binding it to port 6060 like says by the -p option. The pipe "|" redirect the STDOUT to next command. The p flag of the tar command says it to read archive data from STDIN and then, owing to the pipe, from STDOUT of netcat. The other tar options should be well known options; z says to uncompress data using gunzip before trying to explose archive (x flag) and v is to be verbose. This command continues indefinitely, until the network side of the connection shuts down.
We are now ready to magically clone a directory of the source linux box into the destination box using netcat over network. Let's go to the root directory of the source computer and runs this command
[root@K7 /]# tar czvOPp --same-owner /boot | nc -w 5 192.168.0.2 6060 /boot/ /boot/grub/ ... /boot/vmlinuz-2.4.18-3 [root@K7 /]#If we give a look to the terminal of the destination computer where we left netcat in listening mode we should see something like this:
root@0[hda2]# nc -l -p 6060 | tar zxvPp /boot/ /boot/grub/ ... /boot/vmlinuz-2.4.18-3 root@0[hda2]#and you can see two things, netcat process ends and the verbose messages that are appeared in the source console they are appeared in the destination console too. An ls in the destination box confirms us that the source directory is completely backuped to the destination hard disk
root@0[hda2]# ls -al boot/ total 4488 drwxr-xr-x 3 root root 4096 Jul 21 2002 . drwxr-xr-x 4 root root 4096 Aug 13 12:54 .. lrwxrwxrwx 1 root root 19 Aug 13 12:54 System.map -> System.map-2.4.18-3 -rw-r--r-- 1 root root 465966 Apr 18 2002 System.map-2.4.18-3 ... -rwxr-xr-x 1 root root 2835238 Apr 18 2002 vmlinux-2.4.18-3 lrwxrwxrwx 1 root root 16 Aug 13 12:54 vmlinuz -> vmlinuz-2.4.18-3 -rw-r--r-- 1 root root 1030147 Apr 18 2002 vmlinuz-2.4.18-3We more give a deepened glance to the command line we run in the source computer. Let's start from tar options:
c indicates that we are creating an archive
z says to compress archive using gzip
v is to be verbose
O since we wish to send archive to netcat we don't create an archive file but send archive stream directly to STDOUT
P indicates to not remove leading character
p indicates to unchange file permission
--same-owner try to set the same owner to files
We can now tranfer other directory of the original source to create minimun environment to run linux. To do this we need to transfer /dev /lib /etc /sbin /bin /usr/sbin /usr/bin /root /var so execute nc in server mode on destination box
root@0[hda2]# nc -l -p 6060 | tar zxvPpand copy these directories from source box
[root@K7 /]# tar czvOPp --same-owner /dev /lib /etc /sbin /bin \ /usr/sbin /usr/bin /root /var | nc -w 5 192.168.0.2 6060To run linux we also need to create /proc /initrd /mnt /tmp and so
[root@K7 /]# mkdir /proc /initrd /mnt /tmpNow we need to edit some files. First of all /etc/fstab to change partition device. The more important things are swap and root mounting point that, for how we have partitionated the destination hard disk must link to /dev/hda1 and /dev/hda2 and so /etc/fstab must be edit like this
/dev/hda2 / ext3 defaults 1 1 /dev/hda1 swap swap defaults 0 0 none /dev/pts devpts gid=5,mode=620 0 0 none /proc proc defaults 0 0 none /dev/shm tmpfs defaults 0 0 /dev/cdrom /mnt/cdrom iso9660 noauto,owner 0 0 /dev/fd0 /mnt/floppy auto noauto,owner 0 0Then we need to change networking information to avoid that at next boot the networking settings of cloned box will be in conflict with the source one. Open /etc/sysconfig/network-scripts/ifcfg-eth0 and change
... IPADDR=192.168.0.1 (The ip address of the source box) ...to
... IPADDR=192.168.0.2 (The ip address of the destination box) ...Let's change hostname too to avoid confusion. Open /etc/sysconfig/network and change
... HOSTNAME=source_host_name (The hostname of the source box) ...to
... HOSTNAME=dest_host_name (The hostname of the destination box) ...If network card is a different model it could be necessary to change /etc/modules.conf
... alias eth0 pcnet32 (The source network card module ...to another one. Common modules are
3c59x for 3Com EtherLink PCI III/XL cards
lance for AMD LANCE/PCnet cards
eepro100 for Intel i82557-559 Ethernet cards
ne2k-pci for NE2000 PCI cards
pcnet32 AMD PCnet32 cards
Before restart we need to install bootloader to hard disk boot sector. With GRUB boot loader this is very simple.
First of all take a look at /boot/grub/grub.conf. The important thing to look and eventually, to set is the root partition for grub and for kernel. GRUB use a not conventional cwnaming convention to identify hard disk partition. First of all, GRUB requires that the device name is enclosed with `(' and `)', device name is indicated using standard terminology, then there is an integer indicates the drive number and then a second integer indicates the partition number Partition and drive numbers are counted from zero, not from one. These are some examples
(fd0) the first floppy drive
(hd0,1) the second partition of the first drive (/dev/hda2)
(hd1,0) the first partition of the second drive (/dev/hdb1)
title Red Hat Linux (2.4.18-3) root (hd0,1) (that is /dev/hda2) kernel /boot/vmlinuz-2.4.18-3 ro root=/dev/hda2 initrd /boot/initrd-2.4.18-3.imgTo install GRUB into MBR
root@0[root]# grub --no-curses GRUB version 0.91 (640K lower / 3072K upper memory) [ Minimal BASH-like line editing is supported. For the first word, TAB lists possible command completions. Anywhere else TAB lists the possible completions of a device/filename. ] grub> root (hd0,1) grub> setup (hd0)We only note that we need to use option --no-curses to call grub because we still don't have a complete linux box and we can't use curses library support. We can now eject CD and reboot the computer without to be worry of some errors during startup process; we still don't have a complete linux environment. However, afert the startup process, we have a running linux box from HD and we are ready to complete the data transfer.
We can now transfer all the source box into the destination box. Destination box already have a working network stack so we can simply run again nc command in listening mode after to have obtained root privileges and
root@0[root]# cd / root@0[/]# nc -l -p 6060 | tar zxvpOn the source computer we can execute a tar of the complete archive. But owing to fact that we have modified some source file and owing to fact that /proc directory must not be copied we need exclude this items
[root@K7 /]# tar czvOPp --same-owner \ --exclude /proc \ --exclude /etc/fstab \ --exclude /etc/sysconfig/network-scripts/ifcfg-eth0 \ --exclude /etc/sysconfig/network \ --exclude /etc/modules.conf \ --exclude /boot/grub/grub.conf \ / | nc -w 5 192.168.0.2 6060So all source file will be copied into the destination one with same owner and permissions. We can now reboot the destination box and begin to really work on it.