#!/bin/sh # http://www.phys.ethz.ch/~franklin/Projects/dphys3/dphys3kernel # - build dphys3 replacement floppy image s w kernel 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 # base URL from which all Debian stuff can be downloaded CONF_DEBSERVER=http://ftp.debian.org/debian # use this to select an kernel source, to compile custom kernel # only needed if making custom kernel CONF_KERNEL_OWN="" # and take this kernel .config to use, for the self compile # if not set, extract .config File from Debian floppy image # only may be needed if making own kernel # your kernel config is NOT ALLOWED to have in it: # - devfs (CONFIG_DEVFS_FS), as old style /dev/* names are used and needed # your kernel config MUST HAVE COMPILED IN in it (= not as modules): # - floppy device (CONFIG_BLK_DEV_FD), for reading 2nd/root floppy # (only needed if you are going to make boot floppies) # - loopback devide (CONFIG_BLK_DEV_LOOP), for getting kernel from image # - RAM disk device (CONFIG_BLK_DEV_RAM), for root filesystem while install # - initrd support (CONFIG_BLK_DEV_INITRD), for loading root filesys # (only needed if you are going to make an boot CD) # - FAT filesystem (CONFIG_FAT_FS + CONFIG_MSDOS_FS), for extracting kernel CONF_KERNEL_CONFIG="" # server where we upload our compiled floppy images and driver archives to CONF_INST_SERVER=not-configured-server # user as which we login to above server CONF_USER=not-configured-user # make sure multiple users (group of them) can install files on package server # if this is set, chgrp directories and files to this group and chmod g+w CONF_GROUP="" # base directory on server where we put our stuff for download # this is the base directory for adding dists/.../*.deb and pool/.../*.deb # paralleling Debians use [dist|spool]/.../*.deb on their servers # and so should appear in the http:// space of the server # the -u options upload everything relative to this CONF_INST_BASE=/not/configured/directory # 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 # --- DEBUG_*, various debugging settings # these can be set to "yes" by -D option, followed by name without DEBUG_ # such as like this: dphys3kernel -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 -u #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 70MByte free space in its filesystem SYS_WORKDIR=/var/tmp/dphys3kernel-$$-work # where we want to put together stuff for upload # not ./upload as this may collide with something user already has SYS_UPLOADDIR=/var/tmp/dphys3kernel-$$-upload # 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, program and package name NAME=dphys3kernel 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 orignal Debian files from server DO_DOWN=yes COMMAND_OPTION=yes ;; g) # generate: generate our own files DO_GEN=yes COMMAND_OPTION=yes ;; u) # upload: upload generated files to local install server DO_UPLOAD=yes COMMAND_OPTION=yes ;; b) # base: upload generated package files to base directory CONF_INST_BASE=$1 shift 1 ;; r) # remove: remove Debian and own files, 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 orignal Debian files from distribution server -g generate: generate our own file -u upload: upload generated files to local install server for usage by -f before install -b baseaddr base: upload generated files relative to this base directory -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, see source for operation -h help: give out help how this script can be used If multiple of -d -g -u 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_UPLOAD: ${DO_UPLOAD}, DO_REMOVE: ${DO_REMOVE} ${CMD_DEBUG_WAIT} ${CMD_FATAL} making custom kernel images not yet supported - aborting ... >&2 exit 1 # --- 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 # --- no own kernel, no need to compile if [ x${CONF_KERNEL_OWN} = x ] ; then ${CMD_WARNING} no kernel source file, doing nothing >&2 ${CMD_WARNING} no own kernel, no making own images needed >&2 ${CMD_WARNING} using standard Debian floppies >&2 # and abort here, as nothing to do exit 0 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 Debian boot disk image to work on # determine the correct boot floppy image file # used by -d and -g and others, so once here outside of -d BOOT_URL=${CONF_DEBSERVER}/${DEBINST}/${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 Debian 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 # downloaded and to-upload files should end in current directory # this is also most likely where config and kernel archive will be # everything else in work directory and then deleted at the end /usr/bin/wget -q -N ${BOOT_URL} -O ${BOOT} ${CMD_DEBUG_PRINT} `/bin/ls -al ${BOOT}` ${CMD_DEBUG_WAIT} fi # --- download Debian root disk image to work on # determine the correct root floppy image file # used by -d and -g and others, so once here outside of -d ROOT_URL=${CONF_DEBSERVER}/${DEBINST}/${ROOTIMG} ROOT=`/usr/bin/basename ${ROOT_URL}` if [ x${DO_DOWN} != x ] ; then ${CMD_INFO_PRINT} downloading Debian 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 # --- download Debian drivers disk image to work on # determine the correct full drivers archive DRV_URL=${CONF_DEBSERVER}/${DEBINST}/${DRIVIMG} DRV=`/usr/bin/basename ${DRV_URL}` if [ x${DO_DOWN} != x ] ; then ${CMD_INFO_PRINT} downloading Debian drivers 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 ${DRV_URL} -O ${DRV} ${CMD_DEBUG_PRINT} `/bin/ls -al ${DRV}` ${CMD_DEBUG_WAIT} fi # --- space to work in if [ x${DO_GEN} != x ] ; then /bin/mkdir -p ${SYS_WORKDIR} fi # --- unpack users kernel if [ x${DO_GEN} != x ] ; then ${CMD_INFO_PRINT} unpacking kernel from ${CONF_KERNEL_OWN} ... ${CMD_DEBUG_SLEEP} if [ ! -f ${CONF_KERNEL_OWN} ] ; then ${CMD_ERROR} kernel archive ${CONF_KERNEL_OWN} not found >&2 exit 1 fi # remove any existing kernel, to avoid mess on overwriting # directory may be linux for < 2.4 or linux- for >= 2.4 if [ -d ${SYS_WORKDIR}/linux* ] ; then /bin/rm -rf ${SYS_WORKDIR}/linux* fi # allow either .tar.bz or .tar.gz archives if [ x`/bin/echo ${CONF_KERNEL_OWN} | /bin/grep '\.tar\.bz2$'` != x ] ; then /bin/tar -jxpf ${CONF_KERNEL_OWN} -C ${SYS_WORKDIR} elif [ x`/bin/echo ${CONF_KERNEL_OWN} | /bin/grep '\.tar\.gz$'` != x ] ; then /bin/tar -zxpf ${CONF_KERNEL_OWN} -C ${SYS_WORKDIR} else /bin/echo "kernel archive ${CONF_KERNEL_OWN} is not a .tar.bz or .tar.gz" exit 1 fi if [ -d ${SYS_WORKDIR}/linux* ] ; then # not LINUX=${SYS_WORKDIR}/linux* as then cp -p ${LINUX}/.config # becomes cp -p ${SYS_WORKDIR}/linux*/.config and doesnt expand LINUX=`/bin/echo ${SYS_WORKDIR}/linux*` else ${CMD_ERROR} kernel archive ${CONF_KERNEL_OWN} did not unpack >&2 exit 1 fi ${CMD_DEBUG_PRINT} `/bin/ls -ald ${LINUX}` ${CMD_DEBUG_WAIT} fi # --- extract Debians .config file to kernel directory if [ x${DO_GEN} != x ] ; then # no .config file given so extract one from Debian boot disk if [ x${CONF_KERNEL_CONFIG} != x ] ; then ${CMD_INFO_PRINT} copying in .config from ${CONF_KERNEL_CONFIG} ... ${CMD_DEBUG_SLEEP} if [ ! -f ${CONF_KERNEL_CONFIG} ] ; then ${CMD_ERROR} kernel config ${CONF_KERNEL_CONFIG} not found >&2 exit 1 fi /bin/cp -p ${CONF_KERNEL_CONFIG} ${LINUX}/.config else ${CMD_INFO_PRINT} extracting .config from Debian boot disk ... ${CMD_DEBUG_SLEEP} # get boot file system from floppy image BOOTFLOPPY=${SYS_WORKDIR}/boot /bin/mkdir -p ${BOOTFLOPPY} /bin/mount -o loop ${BOOT} ${BOOTFLOPPY} /bin/gunzip -c ${BOOTFLOPPY}/config.gz > ${LINUX}/.config /bin/umount ${BOOTFLOPPY} /bin/rm -rf ${BOOTFLOPPY} fi ${CMD_DEBUG_PRINT} `/bin/ls -al ${LINUX}/.config` ${CMD_DEBUG_WAIT} fi # --- make oldconfig if [ x${DO_GEN} != x ] ; then ${CMD_INFO_PRINT} making oldconfig in ${LINUX} ... ${CMD_DEBUG_SLEEP} /bin/echo /bin/echo "*** --- oldconfig... --- ***" /bin/echo "(if you are using an debian .config this may ask you questions)" /bin/echo (cd ${LINUX}; /usr/bin/make oldconfig) ${CMD_DEBUG_PRINT} `/bin/ls -al ${LINUX}/.config` ${CMD_DEBUG_WAIT} fi fi # --- build the kernel, make dep bzImage modules if [ x${DO_GEN} != x ] ; then ${CMD_INFO_PRINT} building the kernel in ${LINUX} ... ${CMD_DEBUG_SLEEP} /bin/echo /bin/echo "*** --- dep... --- ***" /bin/echo (cd ${LINUX}; /usr/bin/make dep) /bin/echo /bin/echo "*** --- bzImage... --- ***" /bin/echo # this needs to be bzImage because syslinux bootloader only knows that (cd ${LINUX}; /usr/bin/make -j 4 bzImage) /bin/echo /bin/echo "*** --- modules... --- ***" /bin/echo (cd ${LINUX}; /usr/bin/make -j 4 modules) ${CMD_DEBUG_PRINT} `/bin/ls -al ${LINUX}/.config` ${CMD_DEBUG_PRINT} `/bin/ls -al ${LINUX}/vmlinux` ${CMD_DEBUG_PRINT} `/bin/ls -al ${LINUX}/System.map` ${CMD_DEBUG_PRINT} `/bin/ls -al ${LINUX}/arch/${SYS_ARCH}/boot/bzImage` ${CMD_DEBUG_WAIT} fi # --- pack stuff into dphys2 boot floppy image # name for new floppy image # used also in other places BOOT_DPHYS2=`/usr/bin/basename ${OWNBOOT}` if [ x${DO_GEN} != x ] ; then ${CMD_INFO_PRINT} packing kernel onto dphys2 boot disk ... ${CMD_DEBUG_SLEEP} # copy of Debians image (for Syslinux and help stuff) to pack kernel into /bin/cp -p ${BOOT} ${BOOT_DPHYS2} # put our kernel stuff onto it BOOTFLOPPY=${SYS_WORKDIR}/boot /bin/mkdir -p ${BOOTFLOPPY} /bin/mount -o loop ${BOOT_DPHYS2} ${BOOTFLOPPY} /bin/cp -p ${LINUX}/arch/${SYS_ARCH}/boot/bzImage ${BOOTFLOPPY}/linux.bin /bin/gzip -c ${LINUX}/System.map > ${BOOTFLOPPY}/sys_map.gz /bin/gzip -c ${LINUX}/.config > ${BOOTFLOPPY}/config.gz # change name of kernel in floppy images boot files KERNVERS=`/bin/echo ${CONF_KERNEL_OWN} | \ /usr/bin/cut -f 2 -d "-" | /usr/bin/cut -f1-3 -d "."` # announce what version the user has inserted, for checking if right one # use name.tmp naming instead of name.ext.1 because of FAT 8.3 limit /bin/sed -e "/^This disk uses/s/2.*$/${KERNVERS}/" \ ${BOOTFLOPPY}/debian.txt > ${BOOTFLOPPY}/debian.tmp /bin/mv ${BOOTFLOPPY}/debian.tmp ${BOOTFLOPPY}/debian.txt # also change ending for kernel/config/system.map filenames in /boot # no need to set chmod +x, as on FAT all files have this allways # also use name.tmp naming instead of name.ext.1 because of FAT 8.3 limit /bin/sed -e "/^VERSION=/s/2.*$/${KERNVERS}/" \ ${BOOTFLOPPY}/install.sh > ${BOOTFLOPPY}/install.tmp /bin/mv ${BOOTFLOPPY}/install.tmp ${BOOTFLOPPY}/install.sh ${CMD_DEBUG_PRINT} +++ begin bootfloppy listing +++ if [ x${DEBUG_PRINT_STEP} = xyes ] ; then /bin/ls -al ${BOOTFLOPPY} fi ${CMD_DEBUG_PRINT} +++ end bootfloppy listing +++ ${CMD_DEBUG_WAIT} /bin/umount ${BOOTFLOPPY} /bin/rm -rf ${BOOTFLOPPY} # writing to loop mounted image file does not seem to update its access time /bin/touch ${BOOT_DPHYS2} ${CMD_DEBUG_PRINT} `/bin/ls -al ${BOOT_DPHYS2}` ${CMD_DEBUG_WAIT} fi # --- pack stuff into dphys2 drivers archive # name for new floppy image # used also in other places DRV_DPHYS2=`/usr/bin/basename ${OWNDRIVERS}` if [ x${DO_GEN} != x ] ; then ${CMD_INFO_PRINT} packing modules into dphys2 drivers archive ... ${CMD_DEBUG_SLEEP} # create our /lib/modules//* stuff /bin/rm -rf ${SYS_WORKDIR}/modules /bin/mkdir -p ${SYS_WORKDIR}/modules (cd ${LINUX}; INSTALL_MOD_PATH=`/bin/pwd`/../modules make modules_install) # unpack drivers archive to insert our modules /bin/rm -rf ${SYS_WORKDIR}/drivers /bin/mkdir -p ${SYS_WORKDIR}/drivers /bin/tar zxpf ${DRV} -C ${SYS_WORKDIR}/drivers # insert our modules into driver archive /bin/tar zcf ${SYS_WORKDIR}/drivers/modules.tgz -C ${SYS_WORKDIR}/modules . # and repack our new drivers archive /bin/tar zcf ${DRV_DPHYS2} -C ${SYS_WORKDIR}/drivers . ${CMD_DEBUG_PRINT} +++ begin drivers listing +++ if [ x${DEBUG_PRINT_STEP} = xyes ] ; then /bin/ls -al ${SYS_WORKDIR}/drivers ${DRV_DPHYS2} fi ${CMD_DEBUG_PRINT} +++ end drivers listing +++ ${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 temporary directory and its contents, to save space # must be after debug printout if [ x${DEBUG_LEAVE_TEMPFILES} != xyes ] ; then # but offer to leave this to investigate bugs /bin/rm -rf ${SYS_WORKDIR} fi fi # --- upload generated floppy images if [ x${DO_UPLOAD} != x ] ; then ${CMD_INFO_PRINT} uploading images to ${CONF_INST_SERVER} ... ${CMD_DEBUG_SLEEP} # put together stuff to upload, needs an temp dir BOOT_DIR=${SYS_UPLOADDIR}/`/usr/bin/dirname ${OWNINST}/${BOOTIMG}` ROOT_DIR=${SYS_UPLOADDIR}/`/usr/bin/dirname ${OWNINST}/${ROOTIMG}` /bin/mkdir -p ${BOOT_DIR} ${ROOT_DIR} if [ x${CONF_GROUP} != x ] ; then /bin/chgrp -R ${CONF_GROUP} ${SYS_UPLOADDIR} /bin/chmod -R 2775 ${SYS_UPLOADDIR} fi if [ -f ${BOOT} ] ; then /bin/cp -p ${BOOT} ${BOOT_DIR} if [ x${CONF_GROUP} != x ] ; then /bin/chgrp ${CONF_GROUP} ${BOOT_DIR}/* /bin/chmod 664 ${BOOT_DIR}/* fi fi if [ -f ${ROOT} ] ; then /bin/cp -p ${ROOT} ${ROOT_DIR} if [ x${CONF_GROUP} != x ] ; then /bin/chgrp ${CONF_GROUP} ${ROOT_DIR}/* /bin/chmod 664 ${ROOT_DIR}/* fi fi # include directory tree, duplicating tree to server, in case server has none # may be none if new package is in an previously unused section # this is a bit site dependant, you need ssh on your local package server # 2> needed to get rid of error message, because scp can not change dir modes # this loses us the progress bar, because scp tests also 2> for tty or not # if user has given us somewhere to upload to, else dphys2root gets screwed if [ x${CONF_INST_SERVER} != x ] ; then /usr/bin/scp -pr ${SYS_UPLOADDIR}/* \ ${CONF_USER}@${CONF_INST_SERVER}:${CONF_INST_BASE} 2> /dev/null else ${CMD_FATAL} no server to upload boot floppy image to >&2 exit 1 fi ${CMD_DEBUG_PRINT} `/bin/ls -al ${SYS_UPLOADDIR}/${OWNINST}/${BOOTIMG}` ${CMD_DEBUG_PRINT} `/bin/ls -al ${SYS_UPLOADDIR}/${OWNINST}/${ROOTIMG}` ${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_UPLOADDIR} fi fi # --- remove disk images if [ x${DO_REMOVE} != x ] ; then ${CMD_INFO_PRINT} removing boot disk images and drivers tar archives ... ${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 ${DRV} 2> /dev/null` ${CMD_DEBUG_WAIT} /bin/rm -f ${BOOT} ${ROOT} ${DRV} fi