Version 13.4.5
[livecd.git] / tools / livecd-iso-to-pxeboot.sh
blob45a1ffe939b2f7340126cfe156bf536cc53da6a2
1 #!/bin/bash
2 # Convert a live CD iso so that it can be booted over the network
3 # using PXELINUX.
4 # Copyright 2008 Red Hat, Inc.
5 # Written by Richard W.M. Jones <rjones@redhat.com>
6 # Based on a script by Jeremy Katz <katzj@redhat.com>
7 # Based on original work by Chris Lalancette <clalance@redhat.com>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; version 2 of the License.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Library General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 export PATH=/sbin:/usr/sbin:$PATH
24 usage() {
25 echo "Usage: livecd-iso-to-pxeboot <isopath>"
26 exit 1
29 cleanup() {
30 [ -d "$CDMNT" ] && umount $CDMNT && rmdir $CDMNT
33 exitclean() {
34 echo "Cleaning up to exit..."
35 cleanup
36 exit 1
39 if [ $(id -u) != 0 ]; then
40 echo "You need to be root to run this script."
41 exit 1
44 # Check pxelinux.0 exists.
45 if [ ! -f /usr/share/syslinux/pxelinux.0 -a ! -f /usr/lib/syslinux/pxelinux.0 ]; then
46 echo "Warning: pxelinux.0 not found."
47 echo "Make sure syslinux or pxelinux is installed on this system."
50 while [ $# -gt 1 ]; do
51 case "$1" in
52 *) usage ;;
53 esac
54 shift
55 done
57 ISO="$1"
59 if [ -z "$ISO" -o ! -e "$ISO" ]; then
60 usage
63 if [ -d tftpboot ]; then
64 echo "Subdirectory tftpboot exists already. I won't overwrite it."
65 echo "Delete the subdirectory before running."
66 exit 1
69 mkdir tftpboot
71 # Mount the ISO.
72 # FIXME: would be better if we had better mountpoints
73 CDMNT=$(mktemp -d /media/cdtmp.XXXXXX)
74 mount -o loop "$ISO" $CDMNT || exitclean
76 trap exitclean SIGINT SIGTERM
78 # Does it look like an ISO?
79 if [ ! -d $CDMNT/isolinux -o ! -f $CDMNT/isolinux/initrd0.img ]; then
80 echo "The ISO image doesn't look like a LiveCD ISO image to me."
81 exitclean
84 # Create a cpio archive of just the ISO and append it to the
85 # initrd image. The Linux kernel will do the right thing,
86 # aggregating both cpio archives (initrd + ISO) into a single
87 # filesystem.
88 ISOBASENAME=`basename "$ISO"`
89 ISODIRNAME=`dirname "$ISO"`
90 ( cd "$ISODIRNAME" && echo "$ISOBASENAME" | cpio -H newc --quiet -L -o ) |
91 gzip -9 |
92 cat $CDMNT/isolinux/initrd0.img - > tftpboot/initrd0.img
94 # Kernel image.
95 cp $CDMNT/isolinux/vmlinuz0 tftpboot/vmlinuz0
97 # pxelinux bootloader.
98 if [ -f /usr/share/syslinux/pxelinux.0 ]; then
99 cp /usr/share/syslinux/pxelinux.0 tftpboot
100 elif [ -f /usr/lib/syslinux/pxelinux.0 ]; then
101 cp /usr/lib/syslinux/pxelinux.0 tftpboot
102 else
103 echo "Warning: You need to add pxelinux.0 to tftpboot/ subdirectory"
106 # Get boot append line from original cd image.
107 if [ -f $CDMNT/isolinux/isolinux.cfg ]; then
108 APPEND=$(grep -m1 append $CDMNT/isolinux/isolinux.cfg | sed -e "s#CDLABEL=[^ ]*#/$ISOBASENAME#" -e "s/ *append *//")
111 # pxelinux configuration.
112 mkdir tftpboot/pxelinux.cfg
113 cat > tftpboot/pxelinux.cfg/default <<EOF
114 DEFAULT pxeboot
115 TIMEOUT 20
116 PROMPT 0
117 LABEL pxeboot
118 KERNEL vmlinuz0
119 APPEND rootflags=loop $APPEND
120 ONERROR LOCALBOOT 0
123 # All done, clean up.
124 umount $CDMNT
125 rmdir $CDMNT
127 echo "Your pxeboot image is complete."
128 echo
129 echo "Copy tftpboot/ subdirectory to /tftpboot or a subdirectory of /tftpboot."
130 echo "Set up your DHCP, TFTP and PXE server to serve /tftpboot/.../pxeboot.0"
131 echo
132 echo "Note: The initrd image contains the whole CD ISO and is consequently"
133 echo "very large. You will notice when pxebooting that initrd can take a"
134 echo "long time to download. This is normal behaviour."
136 exit 0