This article assumes that the reader using the described setup is familiar with the technical concepts and commands. If you are not comfortable issuing the commands in this document or don't understand the clone script, don't try these instructions.
Additionally, if you do not know the difference between /dev/hda and /dev/hda1, please do not use this HOWTO. This may be the difference between restoring a partition and losing all your data. Please note that I take no responsibility for anything that may go wrong, even if the error resulting from any incorrectness in this article.
For the purpose of this document, the RedHat 7.3 distribution has been used in both server and client. Although this shouldn't make great difference, no test has been made on other distributions or custom setups.
Copyright (c) 2002 Guilherme Tupynambá
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License (GFDL), Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. The clone script is licensed in the terms of the GNU General Public License (GPL).
The main reason to clone disk images is to ease the installation of an operating system and a basic set of applications over a large number of machines. One standard machine is prepared and its pristine image is saved to be restored on other machines saving efforts and simplifying procedures.
Booting from hard disk would limit the possibilities of copying images. It wouldn't be possible, for instance, to safely copy to and from a partition mounted by the booted operating system. Also, the operating system may not be Linux, in which case free options to clone are not available.
The client machine boots from a Grub floppy disk. Then, using the Grub BOOTP support, it gets an IP address from a DHCP server. Next, the client machine downloads the kernel and initrd images from the TFTP server. Once the initrd image is mounted in memory, the initialization script is run, making use of the programs and files stored in this image. This script allows block devices contents to be saved in the TFTP server, and contents from the TFTP server to be written to the block devices.
A DHCP server is required to provide IP addresses for the clients when booting Grub (BOOTP) and later when booting Linux. A TFTP server is required to make the boot images available on the network for Linux to boot. The TFTP server is also necessary to make it possible to save and restore the disk images.
Details on DHCP are beyond the scope of this article. The "Linux Networking HOWTO" has a chapter on DHCP.
Setting up DHCP is very easy, but if you are in a network environment administered by someone else, it's advisable to use a preexisting DHCP server. If you "own" the network then you can follow this procedure.
Install DHCP, if not installed, from the rpm package, normally found in Linux distributions:
Edit the /etc/dhcpd.conf file to configure DHCP service. In our setup, the server has IP address 10.0.0.1 and provides IP addresses up to 253 clients. Configure /etc/dhcpd.conf according to your environment:
Start the dhcpd server:
Setting up TFTP is almost as easy as DHCP.
First install from the rpm package:
Create a directory for the files:
The directory /tftpboot is owned by user nobody, because this is the default user id set up by tftpd to access the files.
Edit the file /etc/xinetd.d/tftp to look like the following:
The changes from the default file are the parameter disable = no (to enable the service) and the server argument -c. This argument allows for the creation of files, which is necessary if you want to save boot or disk images. You may want to make TFTP read only in normal operation.
Then reload xinetd:
You can use the tftp command, available from the tftp (client) rpm package, to test the server. At the tftp prompt, you can issue the commands put and get.
It is possible to use different servers for DHCP and TFTP. This may be necessary if using a preexisting DHCP server. You can configure the next-server parameter in DHCP to point to the TFTP server or you can use the command tftpserver in Grub.
Now that the server is set up, you need to prepare the files to make the client boot. Two files are necessary: the kernel and the init ramdisk (initrd) which will be mounted by the kernel as the root file system. This document assumes that the procedures outlined in this section and the next are made in the client machine. Normally, when saving and restoring disk images, there is no need to have Linux installed on a local hard disk. To deploy disk images to a number of machines, start by installing a Linux distribution on one machine for each model. Use DHCP and have TFTP client to test the setup made in the previous section. Unless otherwise noted, commands are issued in the bash shell by the user root in a working directory.
Identify the compressed kernel file:
The version may vary, according to your system. Upload this file to the TFTP server, renaming it to vmlinuz:
You may want to keep the file name, or have different names for different hardware models or kernel versions. We use the same names for the kernel and initrd so we don't have to make another boot floppy disk after changing the kernel or the initrd images.
Next, make the root file system image for the client. The full listing of the files is in Appendix A.
These files have been taken from a working system as a minimum configuration for having powerful shell (bash), client network utilities (dhcpcd and tftp), and copying and compressing utilities (dd, gzip). Administrative commands (mknod, mount, fdisk and insmod) are also present.
In the working directory create a file named initrd.lst and put these file names on it. To check the existence of these files in your system, run the following command:
You should get an error output like this:
The first error is a script to be created in the working directory. The second error is the program tftp found in the directory /usr/bin instead of /bin. The third is the network interface card module (probably not yours) found in the directory /lib/modules/$(uname -r)/kernel/drivers/net.
These three files will be discussed in upcoming sections separately soon. If there are other missing files, check for lack of installation or differences in version, distribution or hardware. Adjust the list to match your system.
The next step is to make the image. A size of 4 Mbytes was enough to hold the files in our setup. You may increase this size if necessary.
Now the three files excluded by the egrep command. Copy the tftp program from /usr/bin to the image directory:
Identify the proper module for your network interface card. Use the output of the commands lspci and lsmod to identify the file, which resides in the directory /lib/modules/$(uname -r)/kernel/drivers/net.
Unmount, compress, and send the initrd image.
The next step is to make a boot floppy disk using Grub. GNU Grub is the GRand Unified Bootloader. It can handle BOOTP and TFTP, so it can boot from network.
In the working directory create a file named grub.conf with the following content:
In the last four lines are the Grub commands to boot from network:
To compile Grub, first download the source tarball from the Grub web site and unpack it. Run configure specifying the menu file you just created and the network interface card model. Run make as usual.
Again, where you see 3c90x put the model of your network interface card. First check if it is supported by Grub.
Once Grub is compiled, the image of the boot floppy disk is the concatenation of the files stage1/stage1 and stage2/stage2. To make the floppy disk run:
You should now have a boot floppy disk.
The clone script, shown in Appendix B, is not essential. You can make init=/bin/bash as a kernel parameter and end up with a shell from where you can run the available commands and programs.
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.
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.
If you don't want to save a whole disk image just the files within the file system, you can use a similar solution but with tar or cpio instead of dd. Also you need to mount the file system. More commands should be added to the clone script as shown below.
You have to put the mkdir and tar programs in the initrd image so that the script can use them.
In a situation where you used this setup to reorganize and resize your partitions, you may end up with a disk that doesn't boot. Running the setup command from Grub (including the grub program in the image) should resolve the situation. See the Grub documentation for details.
Depending on your kernel, additional modules may be necessary to access some block devices like SCSI devices. Just put the necessary modules in the /lib directory of the initrd image and the correspondent insmod commands in the clone script. The same applies for file systems. If, for instance, you want save the files instead of the image of a fat file system you will need the fat.o and vfat.o modules.
The Grub menu file grub.conf may be customized to present a few copy options or even execute a predefined operation such as repartitioning the disk and retrieving specified images from network. Again, you can use the concepts presented here to achieve a specific application.