Linux/Ubuntu/NetbootPxeLiveCDMultipleReleases: pxe-prepare

File pxe-prepare, 6.2 KB (added by tj, 8 years ago)

Start/Stop? TFTP and NFS for PXE booting

Line 
1#!/bin/bash
2# prepare PXE boot images and NFS exports for network boot of live CD images
3# Copyright 2009-2010 TJ <ubuntu@tjworld.net>
4# Licensed on the terms of the GNU GPL version 3
5# see /usr/share/common-licenses/GPL-3
6
7# Usage: pxe-prepare [ start | stop ]
8
9# All files and directories in CD_IMAGE_ROOT are expected to be bootable
10# file-system images. If a file is an ISO it is loop-mounted. If a directory
11# it is expected to be an already-extracted ISO or other bootable
12# file-system image.
13# The names of the entries are used to create the TFTP and NFS mount-points so it
14# is useful to maintain a consistent naming pattern.
15
16set +x
17
18# If these variables aren't set in the environment use these default values
19TFTP_SERVER=${TFTP_SERVER:-tftpd-hpa}
20TFTP_ROOT=${TFTP_ROOT:-/var/lib/tftpboot}
21CD_IMAGE_ROOT=${CD_IMAGE_ROOT:-/home/all/iso-image}
22NFS_ROOT=${NFS_ROOT:-/srv/boot}
23NFS_SERVER=${NFS_SERVER:-10.254.251.2}
24PXE_MENU=${PXE_MENU:-/usr/local/share/tftpboot/default.pxemenu}
25NETCONSOLE_IP=${NETCONSOLE_IP:-10.254.251.2}
26BOOT_IP=${BOOT_IP:-10.254.251.95}
27
28add_nfs_mount()
29{
30  if [ ! -z $1 ]; then
31    exportfs | grep "${1}$" >/dev/null
32    if [ $? -ne 0 ]; then
33      # basename not found in current NFS exports so add it
34      # link the NFS boot path to the TFTP chroot jail
35      mkdir ${NFS_ROOT}/iso-image/$1
36      mount --rbind ${TFTP_ROOT}/iso-image/$1 ${NFS_ROOT}/iso-image/$1
37      exportfs -i -o async,no_root_squash,no_subtree_check,ro 0.0.0.0/0.0.0.0:${NFS_ROOT}/iso-image/$1
38    fi
39  fi
40}
41
42remove_nfs_mount()
43{
44 if [ ! -z $1 ]; then
45    exportfs | grep "${1}$" >/dev/null
46    if [ $? -eq 0 ]; then
47      # basename found in current NFS exports so remove it
48      exportfs -u 0.0.0.0/0.0.0.0:${NFS_ROOT}/iso-image/$1
49      umount ${NFS_ROOT}/iso-image/$1
50      rmdir ${NFS_ROOT}/iso-image/$1
51    fi
52  fi
53}
54
55# entry point
56
57# ensure required packages are installed
58if [ ! -z "$1" ]; then
59  if [ ! -x /usr/sbin/in.tftpd ]; then
60   MISSING="tftpd-hpa"
61  fi
62  if [ ! -x /usr/sbin/exportfs ]; then
63    MISSING="$MISSING nfs-kernel-server"
64  fi
65  if [ ! -e /usr/lib/syslinux/pxelinux.0 ]; then
66    MISSING="$MISSING syslinux"
67  fi
68  if [ ! -z "$MISSING" ]; then
69    echo "Required packages not installed: $MISSING"
70    exit 1
71  fi
72
73  case $1 in
74   (start)
75     # stop any existing tftp server instance
76     stop $TFTP_SERVER
77     # ensure mount directories exist
78     mkdir -p "${TFTP_ROOT}/iso-image"
79     mkdir -p "${NFS_ROOT}/iso-image"
80
81     # copy in (don't symlink) the syslinux executables
82     cp /usr/lib/syslinux/pxelinux.0 "${TFTP_ROOT}/"
83     cp /usr/lib/syslinux/menu.c32 "${TFTP_ROOT}/"
84
85     # create the default PXE boot menu
86     mkdir -p "${TFTP_ROOT}/pxelinux.cfg"
87     cat "${PXE_MENU}" > "${TFTP_ROOT}/pxelinux.cfg/default"
88
89     # mount each directory or ISO file from the CD-images directory
90     # (that has an associated .pxelabel text file containing a PXE label statement)
91     for dirent in ${CD_IMAGE_ROOT}/*; do
92       BASENAME="$(basename $dirent .iso)"
93       if [ -e "${CD_IMAGE_ROOT}/${BASENAME}.pxelabel" ]; then
94         if [ -d $dirent ]; then
95           echo "mounting directory $BASENAME"
96           mkdir -p "${TFTP_ROOT}/iso-image/$BASENAME"
97           mount --bind $dirent "${TFTP_ROOT}/iso-image/$BASENAME"
98           add_nfs_mount $BASENAME
99         elif [ -f $dirent ]; then
100           # convert any symbolic links
101           dirent=$(readlink $dirent)
102           # get a (brief) mime type report
103           FILE_TYPE="$(file -b --mime-type $dirent)"
104
105           if [ ! -z "$FILE_TYPE" ] && [ "$FILE_TYPE" = "application/x-iso9660-image" ]; then
106             # mount the ISO image directly via a loop device
107             echo "mounting ISO $BASENAME"
108             mkdir -p "${TFTP_ROOT}/iso-image/$BASENAME"
109             mount -t iso9660 -o loop $dirent "${TFTP_ROOT}/iso-image/$BASENAME"
110            add_nfs_mount $BASENAME
111           else
112            echo "Problem with $BASENAME (mime-type: $FILE_TYPE, name: $dirent)"
113            break
114           fi
115         fi
116         echo " TFTP: iso-image/${BASENAME}"
117         echo " NFS:  ${NFS_SERVER}/srv/boot/iso-image/${BASENAME}"
118         # add entry to PXE menu for this image
119         cat "${CD_IMAGE_ROOT}/${BASENAME}.pxelabel" >> "${TFTP_ROOT}/pxelinux.cfg/default"
120         # build PXE menu entry for this ISO + .pxelabel + .kopts
121         if [ -d "${TFTP_ROOT}/iso-image/${BASENAME}/install/netboot/ubuntu-installer" ]; then
122           echo "    kernel iso-image/${BASENAME}/install/netboot/ubuntu-installer/i386/linux" >> "${TFTP_ROOT}/pxelinux.cfg/default"
123           INITRD=$(basename $(find ${TFTP_ROOT}/iso-image/${BASENAME}/install/netboot/ubuntu-installer/i386/ -name 'initrd.*'))
124           echo -n "    append netboot=nfs nfsroot=${NFS_SERVER}:/srv/boot/iso-image/${BASENAME} initrd=iso-image/${BASENAME}/install/netboot/ubuntu-installer/i386/${INITRD} " >> "${TFTP_ROOT}/pxelinux.cfg/default"
125         elif [ -d "${TFTP_ROOT}/iso-image/${BASENAME}/casper" ]; then
126           echo "    kernel iso-image/${BASENAME}/casper/vmlinuz" >> "${TFTP_ROOT}/pxelinux.cfg/default"
127           INITRD=$(basename $(find ${TFTP_ROOT}/iso-image/${BASENAME}/casper/ -name 'initrd.*'))
128           echo -n "    append boot=casper netboot=nfs nfsroot=${NFS_SERVER}:/srv/boot/iso-image/${BASENAME} initrd=iso-image/${BASENAME}/casper/${INITRD} " >> "${TFTP_ROOT}/pxelinux.cfg/default"
129         fi
130         if [ -e "${CD_IMAGE_ROOT}/${BASENAME}.kopts" ]; then
131           cat "${CD_IMAGE_ROOT}/${BASENAME}.kopts" | sed -e "s/\${BOOT_IP}/${BOOT_IP}/g" -e "s/\${NETCONSOLE_IP}/${NETCONSOLE_IP}/g" >> "${TFTP_ROOT}/pxelinux.cfg/default"
132         fi
133         echo "" >> "${TFTP_ROOT}/pxelinux.cfg/default"
134       fi
135     done
136    # start the tftp daemon
137    start $TFTP_SERVER
138     ;;
139
140   (stop)
141     rm -rf "${TFTP_ROOT}/pxelinux.cfg"
142     for dirent in ${TFTP_ROOT}/iso-image/*; do
143       BASENAME="$(basename $dirent)"
144       if [ -d $dirent ]; then
145         echo "unmounting $BASENAME"
146         remove_nfs_mount $BASENAME
147         umount $dirent
148         rmdir $dirent
149       fi
150     done
151     rmdir "${NFS_ROOT}/iso-image"
152     rmdir "${TFTP_ROOT}/iso-image"
153     rm "${TFTP_ROOT}/menu.c32"
154     rm "${TFTP_ROOT}/pxelinux.0"
155     # stop the tftp daemon
156     stop $TFTP_SERVER
157     ;;
158  esac
159
160else
161  echo "Usage: $(basename $0) [start|stop]"
162fi