The script is presented here to show the commands in a formal way and to propose a way to reduce the possibility of damages resulting from mistyping. You have to change the variables tftp_server, nic_module, major_a, family_a and image_a according to your environment and application.
Please note that the arrays major_a and family_a are corresponding. Wrong major number for a given family name will mislead the user. You can locate the major and minor numbers of the devices of interest (whole disks and partitions) by listing the /dev directory. The major and minor number are where the size of a regular file is, in the output of the command ls -l, separated by a comma.
# ls -l /dev/fd0 /dev/hda /dev/hda1 /dev/hdc brw-rw---- 1 root disk 2, 0 Apr 11 11:25 /dev/fd0 brw-rw---- 1 root disk 3, 0 Apr 11 11:25 /dev/hda brw-rw---- 1 root disk 3, 1 Apr 11 11:25 /dev/hda1 brw-rw---- 1 root disk 22, 0 Apr 11 11:25 /dev/hdc
The command set -e instructs the shell to abort the script should any command return non-zero code. The message"Kernel panic: Attempted to kill init!" will follow, as in case of normal end. Don't panic! This is normal, given the circumstances. Just turn off the computer. Press Ctrl-Alt-Del to have a smooth reboot before exiting the script to avoid this ugly message.
The command insmod will load the network interface module and the command dhcpcd will start DHCP client. Note that the fact that Grub used DHCP during its boot has nothing to do with Linux doing the same.
The script makes a big loop and, for each iteration, it asks for one of three operations: Copy from network to device, Copy from device to network or Run fdisk. Then the script asks which block device to use. The array major_a holds the major number for the block devices allowed to be used and the array family_a the respective names for the device families. Next, the script asks the minor number of the block device to be used.
If one of the copy operations is selected, the script asks for the name of the image to be saved or restored. The image name is limited to the elements of the array image_a. A named pipe with the same name as the image is created if doesn't exist. Finally, dd and tftp are invoked simultaneously to transfer the image. Unlike regular ftp, tftp puts and gets named pipes just like regular files.
If fdisk command is selected it is invoked for the block device. fdisk is normally run for the whole disk as opposed to one partition. Note that what normally is called /dev/hda will be called /dev/hda0 by the clone script. The input of fdisk could be put in the script to make automatic creation of partitions if desired.