Add nocleanup option to retain temp files
[livecd.git] / tools / livecd-iso-to-pxeboot.sh
blob83881d46412892612652229496065d2a8080a0e8
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 # Mount the ISO.
70 # FIXME: would be better if we had better mountpoints
71 CDMNT=$(mktemp -d /media/cdtmp.XXXXXX)
72 mount -o loop "$ISO" $CDMNT || exitclean
74 trap exitclean SIGINT SIGTERM
76 # Does it look like an ISO?
77 if [[ ( ! -d $CDMNT/isolinux ) || ( ! -f $CDMNT/isolinux/initrd0.img && ! -f $CDMNT/isolinux/initrd.img ) ]]; then
78 echo "The ISO image doesn't look like a LiveCD ISO image to me."
79 exitclean
82 if [[ -f $CDMNT/isolinux/initrd0.img ]]; then
83 INITRD=initrd0.img
84 VMLINUZ=vmlinuz0
85 else
86 INITRD=initrd.img
87 VMLINUZ=vmlinuz
90 mkdir tftpboot
92 # Create a cpio archive of just the ISO and append it to the
93 # initrd image. The Linux kernel will do the right thing,
94 # aggregating both cpio archives (initrd + ISO) into a single
95 # filesystem.
96 ISOBASENAME=`basename "$ISO"`
97 ISODIRNAME=`dirname "$ISO"`
98 ( cd "$ISODIRNAME" && echo "$ISOBASENAME" | cpio -H newc --quiet -L -o ) |
99 gzip -9 |
100 cat $CDMNT/isolinux/$INITRD - > tftpboot/$INITRD
102 # Kernel image.
103 cp $CDMNT/isolinux/$VMLINUZ tftpboot/$VMLINUZ
105 # pxelinux bootloader.
106 if [ -f /usr/share/syslinux/pxelinux.0 ]; then
107 cp /usr/share/syslinux/pxelinux.0 tftpboot
108 elif [ -f /usr/lib/syslinux/pxelinux.0 ]; then
109 cp /usr/lib/syslinux/pxelinux.0 tftpboot
110 else
111 echo "Warning: You need to add pxelinux.0 to tftpboot/ subdirectory"
114 # Get boot append line from original cd image.
115 if [ -f $CDMNT/isolinux/isolinux.cfg ]; then
116 APPEND=$(grep -m1 append $CDMNT/isolinux/isolinux.cfg | sed -e "s#CDLABEL=[^ ]*#/$ISOBASENAME#" -e "s/ *append *//")
119 # pxelinux configuration.
120 mkdir tftpboot/pxelinux.cfg
121 cat > tftpboot/pxelinux.cfg/default <<EOF
122 DEFAULT pxeboot
123 TIMEOUT 20
124 PROMPT 0
125 LABEL pxeboot
126 KERNEL $VMLINUZ
127 APPEND rootflags=loop $APPEND
128 ONERROR LOCALBOOT 0
131 # All done, clean up.
132 umount $CDMNT
133 rmdir $CDMNT
135 echo "Your pxeboot image is complete."
136 echo
137 echo "Copy tftpboot/ subdirectory to /tftpboot or a subdirectory of /tftpboot."
138 echo "Set up your DHCP, TFTP and PXE server to serve /tftpboot/.../pxeboot.0"
139 echo
140 echo "Note: The initrd image contains the whole CD ISO and is consequently"
141 echo "very large. You will notice when pxebooting that initrd can take a"
142 echo "long time to download. This is normal behaviour."
144 exit 0