#!/bin/sh # http://www.phys.ethz.ch/~franklin/Projects/dphys3/dphys3pxe # - extract dphys3 PXE/TFTP boot server files for Debian 3.1 (sarge) # copyright ETH Zuerich Physics Departement, # use under either BSD or GPL license # author Neil Franklin, last modification 2005.04.15 ### ------ configuration for this site # first CONF_* various site or subnet dependant user config variables # then DEBUG_* various debugging settings # last SYS_* various system internal values # some of these are overridable by command line options # --- CONF_* various site or subnet dependant user config variables # this is the directory to use, within the dists/.../disks-i386 directory # vary this for multiple sets of files (say multiple kernels or configs) # naming schemes maybe by subnets, or by kernel options, or by compiling user CONF_DISKSVERSION=custom # base URL from which our generated stuff can be downloaded # used for generating the URLs. to access our packages web server # the end result should parallel the scp login@server:/basedir above CONF_OWNSERVER=http://not-configured-server/not/configured/directory # install directory for PXE, where our generated files go # we install via ssh/scp, as this is most likely available on boot servers # so this line needs to be format for an scp login@server:/basedir CONF_INST_PXE=not-configured-user@not-configured-server:/not/configured/dir # make sure multiple users (group of them) can install files on boot server # if this is set, chgrp files to this group and chmod g+w CONF_INST_GROUP="" # --- DEBUG_*, various debugging settings # these can be set to "yes" by -D option, followed by name without DEBUG_ # such as like this: dphys3pxe -D PRINT_STEP -D LEAVE_TEMPFILES -g # set this to sleep after displaying each steps header, number is in seconds #DEBUG_SLEEP=2 # set this to output debug state info after each step #DEBUG_PRINT_STEP=yes # set this to wait after each debug state info #DEBUG_WAIT_STEP=yes # set this to not delete temporary files (workdir) after -g #DEBUG_LEAVE_TEMPFILES=yes # set this to leave temporary directories undeleted after -i #DEBUG_LEAVE_TEMPDIRS=yes # --- SYS_*, various system internal values # where we want to do all the unpack/modify/pack stuff # this must be a directory that root can access (not a root-squash NFS mount) # and there should be about 10MByte free space in its filesystem SYS_WORKDIR=/var/tmp/dphys3pxe-$$-work # where we want to put together stuff for install # not ./install as this may collide with something user already has SYS_INSTALLDIR=/var/tmp/dphys3pxe-$$-install # note: this program has only been designed for and tested with i386 # others at own risk, I expect them to fail badly SYS_ARCH=i386 ### ------ actual implementation from here on # no user settings any more below this point # --- tidy up some commands, make systematic # stuff that goes wrong, not expected by user, not in data output, use >&2 # so also with $0 in case this script was called by an other script # something within the system that user does not expect, give up CMD_FATAL="/bin/echo $0: FATAL:" # something from users input, user will correct this and continue CMD_ERROR="/bin/echo $0: ERROR:" # something we can continue with, but may be wrong, and user may not suspect it CMD_WARNING="/bin/echo $0: WARNING:" # something most likely not wrong, but tell user for the odd case it is wrong CMD_NOTE="/bin/echo $0: NOTE:" # normal stuff users expect, so to stdout as normal output, no $0, no marking CMD_INFO="/bin/echo" # stuff users asked for, so add to stdout as normal output, no $0, but mark it CMD_DEBUG="/bin/echo DEBUG:" # control output CMD_INFO_PRINT=${CMD_INFO} CMD_VERBOSE_PRINT=/bin/true # --- config file stuff # what we are NAME=dphys3pxe PNAME=dphys3 # check user config file(s), let user override settings # same config files used in dphys3[kernel|preseed|boot|root|cd|pxe] if [ -f /etc/${PNAME} ] ; then . /etc/${PNAME} fi if [ -f ~/.${PNAME} ] ; then . ~/.${PNAME} fi if [ -f ./${PNAME} ] ; then . ./${PNAME} fi # --- parse command line # so long next parameter is a set of options (= begins with an - character) while [ x`/bin/echo $1 | /usr/bin/cut -c 1` = x- ] ; do # extract options from parameter (cut off the "-") OPTS=`/bin/echo $1 | /usr/bin/cut -c 2-` shift 1 # so long still unprocessed option characters while [ x${OPTS} != x ] ; do # first option to process OPT=`/bin/echo ${OPTS} | /usr/bin/cut -c 1` # and rest of options for later OPTS=`/bin/echo ${OPTS} | /usr/bin/cut -c 2-` case ${OPT} in d) # download: download file from server DO_DOWN=yes COMMAND_OPTION=yes ;; g) # generate: generate extracted files DO_GEN=yes COMMAND_OPTION=yes ;; i) # install: install files onto boot server DO_INST=yes COMMAND_OPTION=yes ;; r) # remove: remove file, after installing DO_REMOVE=yes COMMAND_OPTION=yes ;; q) # quiet: don't inform user what we are doing CMD_INFO_PRINT=/bin/true ;; v) # verbose: detailed inform user what we are doing CMD_VERBOSE_PRINT=${CMD_INFO} ;; D) # Debug: set an debug option to yes eval DEBUG_$1=yes shift 1 ;; h) # help: give out help how this script can be used /bin/cat << END-HELP-TEXT Usage is: $0 [options] options: -d download: download original Debian or own files from distribution server -g generate: generate extracted PXE boot server files -i install: install extracted files onto boot server -r remove: remove Debian and own files, after installing -q quiet: don't give user an running report of what we are doing -v verbose: give user detailed running report -D Debug: activate an debug option -h help: give out help how this script can be used If multiple of -d -g -i or -r are used, they are processed in the order they appear here in the help section, not the order they are typed on the command line. Other options modify behaviour. END-HELP-TEXT exit 0 ;; *) # not one of our recognized options ${CMD_ERROR} unknown option: ${OPT} >&2 ${CMD_INFO} >&2 # call self with -h to display help $0 -h exit 1 ;; esac done done if [ x${COMMAND_OPTION} = x ] ; then ${CMD_WARNING} no command option selected, doing nothing ... >&2 fi # control debug output CMD_DEBUG_SLEEP=/bin/true if [ x${DEBUG_SLEEP} != x ] ; then CMD_DEBUG_SLEEP="/bin/sleep ${DEBUG_SLEEP}" fi CMD_DEBUG_PRINT=/bin/true if [ x${DEBUG_PRINT_STEP} = xyes ] ; then CMD_DEBUG_PRINT=${CMD_DEBUG} fi CMD_DEBUG_WAIT=/bin/true if [ x${DEBUG_WAIT_STEP} = xyes ] ; then CMD_DEBUG_WAIT="read -p ---DEBUG-wait-after-step--- dummy" fi ${CMD_DEBUG_PRINT} options are: \ DO_DOWN: ${DO_DOWN}, DO_GEN: ${DO_GEN}, \ DO_INST: ${DO_INST}, DO_REMOVE: ${DO_REMOVE} ${CMD_DEBUG_WAIT} # --- test if we can work # this script is intended to be run as root, it will fail if run as user! if [ ! `/usr/bin/whoami` = root ] ; then ${CMD_ERROR} sorry, you need to run this script as root - aborting ... >&2 exit 1 fi # --- compute paths on disk image server # path for Debian directory we download from server DEBINST=dists/sarge/main/installer-${SYS_ARCH}/current # path for our directory we upload to and download from server OWNINST=dists/sarge/local/main/installer-${SYS_ARCH}/${CONF_DISKSVERSION} # actual files inside all of these BOOTIMG=images/floppy/boot.img ROOTIMG=images/floppy/root.img DRIVIMG=images/floppy/net-drivers.img # --- download own boot disk image to work on # this allways uses our boot floppy image, because we allways want auto install BOOT_URL=${CONF_OWNSERVER}/${OWNINST}/${BOOTIMG} # in current dir, not workdir, as user wants to see this BOOT=`/usr/bin/basename ${BOOT_URL}` if [ x${DO_DOWN} != x ] ; then ${CMD_INFO_PRINT} downloading boot disk image ... ${CMD_DEBUG_SLEEP} # -N so only newer is got, save waisting bandwidth if already here # if user wants to force download, use an make clean before /usr/bin/wget -q -N ${BOOT_URL} -O ${BOOT} ${CMD_DEBUG_PRINT} `/bin/ls -al ${BOOT}` ${CMD_DEBUG_WAIT} fi # --- download own root disk image to work on # this allways uses our root floppy image, because we allways want auto install ROOT_URL=${CONF_OWNSERVER}/${OWNINST}/${ROOTIMG} ROOT=`/usr/bin/basename ${ROOT_URL}` if [ x${DO_DOWN} != x ] ; then ${CMD_INFO_PRINT} downloading root disk image ... ${CMD_DEBUG_SLEEP} # -N so only newer is got, save waisting bandwidth if already here # if user wants to force download, use an make clean before /usr/bin/wget -q -N ${ROOT_URL} -O ${ROOT} ${CMD_DEBUG_PRINT} `/bin/ls -al ${ROOT}` ${CMD_DEBUG_WAIT} fi # --- space to work in if [ x${DO_GEN} != x ] ; then /bin/mkdir -p ${SYS_WORKDIR} fi # --- extract kernel from boot floppy image # name for kernel to be extracted # also used in other places KERNEL=linux if [ x${DO_GEN} != x ] ; then ${CMD_INFO_PRINT} extracting kernel from boot floppy ... ${CMD_DEBUG_SLEEP} # open boot floppy image BOOTFLOPPY=${SYS_WORKDIR}/boot /bin/mkdir -p ${BOOTFLOPPY} /bin/mount -o loop ${BOOT} ${BOOTFLOPPY} /bin/cp -p ${BOOTFLOPPY}/${KERNEL} . /bin/umount ${BOOTFLOPPY} /bin/rm -rf ${BOOTFLOPPY} ${CMD_DEBUG_PRINT} `/bin/ls -al ${KERNEL}` ${CMD_DEBUG_WAIT} fi # --- extract kernel from boot floppy image # name for initrd filesystem to be extracted # also used in other places INITRD=initrd.gz if [ x${DO_GEN} != x ] ; then ${CMD_INFO_PRINT} extracting initrd from root floppy ... ${CMD_DEBUG_SLEEP} # open root floppy image ROOTFLOPPY=${SYS_WORKDIR}/boot /bin/mkdir -p ${ROOTFLOPPY} /bin/mount -o loop ${ROOT} ${ROOTFLOPPY} /bin/cp -p ${ROOTFLOPPY}/${INITRD} . /bin/umount ${ROOTFLOPPY} /bin/rm -rf ${ROOTFLOPPY} ${CMD_DEBUG_PRINT} `/bin/ls -al ${INITRD}` ${CMD_DEBUG_WAIT} fi # --- tidy up after working if [ x${DO_GEN} != x ] ; then ${CMD_INFO_PRINT} tidying up from working ... ${CMD_DEBUG_SLEEP} ${CMD_DEBUG_PRINT} +++ begin workdirectory listing +++ if [ x${DEBUG_PRINT_STEP} = xyes ] ; then /bin/ls -al ${SYS_WORKDIR} fi ${CMD_DEBUG_PRINT} +++ end workdirectory listing +++ ${CMD_DEBUG_WAIT} # remove all temporary directories and their contents, to save space # must be after debug printout if [ x${DEBUG_LEAVE_TEMPFILES} != xyes ] ; then # but offer to leave them to investigate bugs /bin/rm -rf ${SYS_WORKDIR} fi fi # --- install PXE files onto an boot server if [ x${DO_INST} != x ] ; then ${CMD_INFO_PRINT} installing PXE files onto boot server ... ${CMD_DEBUG_SLEEP} # put together stuff to install, needs an temp dir /bin/mkdir -p ${SYS_INSTALLDIR} if [ -f ${KERNEL} -a -f ${INITRD} ] ; then /bin/cp -p ${KERNEL} ${INITRD} ${SYS_INSTALLDIR} if [ x${CONF_INST_GROUP} != x ] ; then /bin/chgrp ${CONF_INST_GROUP} ${SYS_INSTALLDIR}/* /bin/chmod 664 ${SYS_INSTALLDIR}/* fi else ${CMD_ERROR} extracted files for uploading are missing, not generated? >&2 exit 1 fi # ${CONF_INST_PXE} is install directory for PXE, where our generated files go # this line needs to be the first part for an scp login@server:/basedir # if user has given us somewhere to install to, else boot server gets screwed if [ x${CONF_INST_PXE} != x ] ; then /usr/bin/scp -p ${SYS_INSTALLDIR}/* ${CONF_INST_PXE} else ${CMD_FATAL} no server to install PXE boot files on >&2 exit 1 fi ${CMD_DEBUG_PRINT} `/bin/ls -al ${SYS_INSTALLDIR}/${KERNEL}` ${CMD_DEBUG_PRINT} `/bin/ls -al ${SYS_INSTALLDIR}/${INITRD}` ${CMD_DEBUG_WAIT} # remove temporary directory and its contents, to save space # must be after debug printout if [ x${DEBUG_LEAVE_TEMPDIRS} != xyes ] ; then # but offer to leave this to investigate bugs /bin/rm -rf ${SYS_INSTALLDIR} fi fi # --- remove PXE boot files if [ x${DO_REMOVE} != x ] ; then ${CMD_INFO_PRINT} removing PXE files ... ${CMD_DEBUG_PRINT} `/bin/ls -al ${BOOT} 2> /dev/null` ${CMD_DEBUG_PRINT} `/bin/ls -al ${ROOT} 2> /dev/null` ${CMD_DEBUG_PRINT} `/bin/ls -al ${KERNEL} 2> /dev/null` ${CMD_DEBUG_PRINT} `/bin/ls -al ${INITRD} 2> /dev/null` ${CMD_DEBUG_WAIT} /bin/rm -f ${BOOT} ${ROOT} ${KERNEL} ${INITRD} fi