Clone Howto
Clone Howto
a Network
Guilherme Tupynambá
gtupy (at) uol.com.br
2002−09−09
Revision History
Revision 0.3 2002−09−24 Revised by: gct
Review suggestions incorporated
Revision 0.2 2002−09−23 Revised by: jyg
Minor revisions
Revision 0.1 2002−09−09 Revised by: gct
First draft.
This document describes a setup that allows a machine to boot Linux from BOOTP/TFTP, using the Grub
boot loader, and save and restore disk and partition images to and from a TFTP server.
HOWTO Clone Disk Images on Linux Booted from a Network
Table of Contents
1. Legal Notices....................................................................................................................................................1
1.1. Disclaimer.........................................................................................................................................1
1.2. Copyright..........................................................................................................................................1
1.3. Feedback...........................................................................................................................................1
2. Introduction.....................................................................................................................................................2
2.1. Why clone disk images.....................................................................................................................2
2.2. Why boot from a network.................................................................................................................2
2.3. Network boot process overview........................................................................................................2
B. Clone script...................................................................................................................................................13
References..........................................................................................................................................................16
i
1. Legal Notices
1.1. Disclaimer
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.
1.2. Copyright
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).
1.3. Feedback
Comments on this article will be highly appreciated. Direct your comments to <gtupy (at)
uol.com.br>.
1. Legal Notices 1
2. Introduction
2.1. Why clone disk images
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.
2. Introduction 2
3. Setting up DHCP and TFTP servers
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.
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:
#/etc/dhcpd.conf
server−identifier dhcp.clonedomain.com;
default−lease−time 172800;
max−lease−time 604800;
option domain−name "clonedomain.com";
subnet 10.0.0.0 netmask 255.255.255.0 {
range dynamic−bootp 10.0.0.2 10.0.0.254;
}
/etc/rc.d/init.d/dhcpd start.
# mkdir /tftpboot
# chown nobody:nobody /tftpboot
The directory /tftpboot is owned by user nobody, because this is the default user id set up by tftpd to
access the files.
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = −c −s /tftpboot
disable = no
per_source = 11
cps = 100 2
}
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.
/etc/rc.d/init.d/xinetd reload
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.
4.1. Kernel
Identify the compressed kernel file:
# cd /boot
# ls vmlinuz−$(uname −r)
vmlinuz−2.4.18−3
The version may vary, according to your system. Upload this file to the TFTP server, renaming it to
vmlinuz:
# tftp 10.0.0.1
tftp> binary
tftp> put vmlinuz−2.4.18−3 vmlinuz
Sent 1030147 bytes in 2.3 seconds
tftp> quit
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.
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:
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.
Now the three files excluded by the egrep command. Copy the tftp program from /usr/bin to the image
directory:
# cp −p /usr/bin/tftp mnt/bin/
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.
Whenever you see a reference to 3c59x, use the name of the module suited for your case.
Edit the clone script found in Appendix B, changing the variables as explained in Section 6. Make it
executable and copy it to the image directory:
# chmod +x clone
# cp −p clone mnt/bin/
default=0
timeout=1
title Clone
bootp
root (nd)
kernel /vmlinuz rw root=/dev/ram ramdisk_size=4096 init=/bin/clone
initrd /initrd.gz
In the last four lines are the Grub commands to boot from network:
Again, where you see 3c90x put the model of your network interface card. First check if it is supported by
Grub.
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.
# mkdir /mnt
# mount ${device_name} /mnt
# mknod ${image} p
# tftp ${tftp_server} <<−EOT &
binary
put ${image}
EOT
tar czf ${image}
or
You have to put the mkdir and tar programs in the initrd image so that the script can use them.
set −e
export PATH=/sbin:/bin
tftp_server=10.0.0.1
nic_module=3c59x.o
major_a=(2 3 22)
family_a=(fd hda hdc)
image_a=(img0001 img0002 img0003 img0004)
while true; do \
[ ! −z "${image}" ] && unset image
echo
echo "Clone Menu"
echo
echo "Operation"
echo
PS3="Choose operation (1−${#operation_a[*]}): "
select operation in "${operation_a[@]}"; do \
[ −z "${operation}" ] && continue
echo
echo $REPLY − $operation
echo
break
done
major_i=$[REPLY−1]
major=${major_a[$major_i]}
B. Clone script 13
HOWTO Clone Disk Images on Linux Booted from a Network
echo
echo −e "Operation:\t$operation"
device_name=/dev/${family_a[${major_i}]}${minor}
echo −e "Device:\t\t${device_name} ($major, $minor)"
[ ! −z "${image}" ] && echo −e "Image:\t\t${image}"
echo
echo "Confirmation"
echo
PS3="Ok/Cancel (1−2): "
select ok in Ok Cancel; do \
[ −z "${ok}" ] && continue
echo
echo $REPLY − $ok
echo
break
done
if [ "${ok}" = "Ok" ]; then \
if [ ! −b ${device_name} ]; then \
echo "Creating ${device_name}"
mknod ${device_name} b ${major} ${minor}
fi
if [ ! −z "${image}" ]; then \
if [ ! −p ${image} ]; then \
echo "Creating pipe"
mknod ${image} p
fi
fi
if [ "${operation}" = "${operation_a[0]}" ]; then \
tftp ${tftp_server} <<−EOT &
binary
get ${image}
EOT
gzip −c −d < ${image} | dd of=${device_name}
elif [ "${operation}" = "${operation_a[1]}" ]; then \
tftp ${tftp_server} <<−EOT &
binary
put ${image}
EOT
dd if=${device_name} | gzip −c > ${image}
elif [ "${operation}" = "${operation_a[2]}" ]; then \
fdisk ${device_name}
fi
echo
fi
B. Clone script 14
HOWTO Clone Disk Images on Linux Booted from a Network
echo "Continuation"
echo
PS3="Continue/Exit (1−2): "
select new in Continue Exit; do \
[ −z "${new}" ] && continue
echo
echo $REPLY − $new
echo
break
done
[ "${new}" = "Exit" ] && break
done
exit 0
B. Clone script 15
References
[grub] GRUB.
http://www.gnu.org/software/grub/
References 16