Re-factor imgcreate/fs.py module
[livecd/EL-5.git] / tools / livecd-iso-to-pxeboot.sh
blobb1ccf5f733192cc1efbc094d122a1241e7af1b1f
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/lib/syslinux/pxelinux.0 ]; then
46 echo "Warning: /usr/lib/syslinux/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 -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/lib/syslinux/pxelinux.0 ]; then
99 cp /usr/lib/syslinux/pxelinux.0 tftpboot
100 else
101 echo "Warning: You need to add pxelinux.0 to tftpboot/ subdirectory"
104 # pxelinux configuration.
105 mkdir tftpboot/pxelinux.cfg
106 cat > tftpboot/pxelinux.cfg/default <<EOF
107 DEFAULT pxeboot
108 TIMEOUT 20
109 PROMPT 0
110 LABEL pxeboot
111 KERNEL vmlinuz0
112 APPEND initrd=initrd0.img root=/$ISOBASENAME rootfstype=iso9660 rootflags=loop
113 ONERROR LOCALBOOT 0
116 # All done, clean up.
117 umount $CDMNT
118 rmdir $CDMNT
120 echo "Your pxeboot image is complete."
121 echo
122 echo "Copy tftpboot/ subdirectory to /tftpboot or a subdirectory of /tftpboot."
123 echo "Set up your DHCP, TFTP and PXE server to serve /tftpboot/.../pxeboot.0"
124 echo
125 echo "Note: The initrd image contains the whole CD ISO and is consequently"
126 echo "very large. You will notice when pxebooting that initrd can take a"
127 echo "long time to download. This is normal behaviour."
129 exit 0