Adjust logfile handling for new FAI; support grml2ram bootoption
[grml-live.git] / grml-live
index 16d4a6f..ca826c6 100755 (executable)
--- a/grml-live
+++ b/grml-live
@@ -4,7 +4,7 @@
 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
 # Bug-Reports:   see http://grml.org/bugs/
 # License:       This file is licensed under the GPL v2 or any later version.
-# Latest change: Mon Sep 17 00:21:49 CEST 2007 [mika]
+# Latest change: Sun Nov 25 16:35:35 CET 2007 [mika]
 ################################################################################
 
 # read configuration files, set some misc variables {{{
@@ -15,28 +15,56 @@ export LC_ALL=C
 # exit on any error:
 set -e
 
+GRML_LIVE_VERSION='0.0.10'
+CMDLINE="$0 $@"
+ISO_DATE="$(date +%Y-%m-%d)"
+
 # we need root permissions for the build-process:
 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
    echo "Error: please run this script with uid 0 (root)." >&2
    exit 1
 fi
 
+if [ -r /var/run/fai/FAI_INSTALLATION_IN_PROGRESS ] ; then
+   echo "/usr/sbin/fai already running or was aborted before.">&2
+   echo "You may remove /var/run/fai/FAI_INSTALLATION_IN_PROGRESS and try again.">&2
+   exit 1
+fi
+
+if [ -r /var/run/fai/fai_softupdate_is_running ] ; then
+   echo "/usr/sbin/fai softupdate already running or was aborted before.">&2
+   echo "You may remove /var/run/fai/fai_softupdate_is_running and try again.">&2
+   exit 1
+fi
+
 # make sure they are not set by default
 VERBOSE=''
 FORCE=''
+UPDATE=''
+
+if [ -r /etc/grml/lsb-functions ] ; then
+   . /etc/grml/lsb-functions
+else
+   einfo()  { echo "  [*] $*" ;}
+   eerror() { echo "  [!] $*">&2 ;}
+   ewarn()  { echo "  [x] $*" ;}
+   eend()   { return 0 ;}
+fi
 
 # source main configuration file:
-. /etc/grml/grml-live.conf
+LIVE_CONF=/etc/grml/grml-live.conf
+. $LIVE_CONF
 
 PN=$(basename $0)
-# TMPFILE=$(mktemp)
 # }}}
 
 # clean exit {{{
 bailout() {
-  # rm -f "$TMPFILE"
+  [ -n "$MIRROR_DIRECTORY" ] && umount "${CHROOT_OUTPUT}/${MIRROR_DIRECTORY}"
+  rm -f /var/run/fai/fai_softupdate_is_running /var/run/fai/FAI_INSTALLATION_IN_PROGRESS
   [ -n "$1" ] && EXIT="$1" || EXIT="1"
-  [ -n "$2" ] && echo "$2">&2
+  [ -n "$2" ] && eerror "$2">&2
+  log "------------------------------------------------------------------------------"
   exit "$EXIT"
 }
 trap bailout 1 2 3 15
@@ -47,7 +75,76 @@ trap bailout 1 2 3 15
 [ -n "$HOSTNAME" ] || HOSTNAME=grml
 [ -n "$USERNAME" ] || USERNAME=grml
 [ -n "$CLASSES" ]  || CLASSES="GRML,I386"
-[ -n "$TARGET" ] || bailout 1 "${PN}: \$TARGET not specified. Please adjust /etc/grml/grml-live.conf. Exiting."
+[ -n "$BOOT_METHOD" ] || BOOT_METHOD='isolinux'
+[ -n "$OUTPUT" ] || bailout 1 "${PN}: \$OUTPUT not specified. Please adjust $LIVE_CONF. Exiting."
+
+[ -n "$VERSION" ]  || VERSION="0.0.1"
+[ -n "$RELEASENAME" ] || RELEASENAME="grml-live rocks"
+[ -n "$GRML_NAME" ] || GRML_NAME='grml'
+
+# logfile:
+if [ -z "$LOGFILE" ] ; then
+   LOGFILE=/var/log/grml-live.log
+fi
+touch $LOGFILE
+chown root:adm $LOGFILE
+chmod 640 $LOGFILE
+# clean/zero logfile:
+if [ -n "$ZERO_LOGFILE" ] ; then
+   echo -n > $LOGFILE
+fi
+
+NFSROOT_CONF=/etc/grml/fai/make-fai-nfsroot.conf
+
+# }}}
+
+# some important functions {{{
+
+# log output:
+# usage: log "string to log"
+log() { echo "$*" >> $LOGFILE ; }
+
+# cut string at character number int = $1
+# usage: cut_string 5 "1234567890" will output "12345"
+cut_string() {
+  [ -n "$2" ] || return 1
+  echo "$2" | head -c "$1"; echo -ne "\n"
+}
+
+# prepend int = $1 spaces before string = $2
+# usage: extend_string_begin 5 "123" will output "  123"
+extend_string_begin() {
+  [ -n "$2" ] || return 1
+  local COUNT="$(echo $2 | wc -c)"
+  local FILL="$(expr $COUNT - $1)"
+  while [ "$FILL" -gt 1 ] ; do
+    echo -n " "
+    local FILL=$(expr $FILL - 1)
+  done
+  while [ "$FILL" -lt 1 ] ; do
+    echo -n " "
+    local FILL=$(expr $FILL + 1)
+  done
+  echo "$2" | head -c "$1"; echo -ne "\n"
+}
+
+# append int = $1 spaces to string = $2
+# usage: extend_string_begin 5 "123" will output "123  "
+extend_string_end() {
+  [ -n "$2" ] || return 1
+  echo -n "$2" | head -c "$1"
+  local COUNT="$(echo $2 | wc -c)"
+  local FILL="$(expr $COUNT - $1)"
+  while [ "$FILL" -gt 1 ] ; do
+    echo -n " "
+    local FILL=$(expr $FILL - 1)
+  done
+  while [ "$FILL" -lt 1 ] ; do
+    echo -n " "
+    local FILL=$(expr $FILL + 1)
+  done
+  echo -ne "\n"
+}
 # }}}
 
 # usage information {{{
@@ -56,14 +153,19 @@ usage()
   echo "
 $PN - build process script for generating a (grml based) Linux Live-ISO
 
-Usage: $PN [-c <classe[s]>] [-t <target_directory>] [-s <suite> [-Fvh]
+Usage: $PN [-a <architecture>] [-c <classe[s]>] [-g <grml_name>] \\
+                 [-i <iso_name> ] [-o <output_directory>] [-s <suite>] \\
+                 [-t <template_directory>] [-s <suite>] \\
+                 [-v <version_number>] [-FVhu]
 
 Usage examples:
 
     $PN
-    $PN -c GRML,I386 -t /dev/shm/grml
-    $PN -c GRML,GRML_X,I386 -t /grml/
-    $PN -s sid -c GRML,I386
+    $PN -c GRMLBASE,GRML_SMALL,I386 -o /grml/
+    $PN -c GRMLBASE,GRML_MEDIUM,I386 -o /dev/shm/grml
+    $PN -c GRMLBASE,GRML_SMALL,I386 -g grml-small -v 1.0
+    $PN -c GRMLBASE,GRML_FULL,I386 -i grml_0.0-1.iso -v 0.0-1
+    $PN -c GRMLBASE,GRML_FULL,I386 -s sid -V -r 'grml-live rocks'
 
 More details: man grml-live
               /usr/share/doc/grml-live/grml-live.html
@@ -76,46 +178,59 @@ http://grml.org/bugs/
 
 # command line parsing {{{
 
-while getopts "c:s:t:Fhv" opt; do
+while getopts "a:c:g:i:o:r:s:t:v:FhuV" opt; do
   case "$opt" in
+    a) ARCH="$OPTARG" ;;
     c) CLASSES="$OPTARG" ;;
-    # f) FLAVOUR="$OPTARG" ;; # TODO
+    g) GRML_NAME="$OPTARG" ;;
+    i) ISO_NAME="$OPTARG" ;;
+    o) OUTPUT="$OPTARG"
+       CHROOT_OUTPUT="$OUTPUT/grml_chroot"
+       BUILD_OUTPUT="$OUTPUT/grml_cd"
+       ISO_OUTPUT="$OUTPUT/grml_isos"
+       ;;
+    r) RELEASENAME="$OPTARG" ;;
+    s) SUITE="$OPTARG" ;;
+    t) TEMPLATE_DIRECTORY="$OPTARG";;
+    v) VERSION="$OPTARG" ;;
     F) FORCE=1 ;;
     h) usage ; bailout 0 ;;
-    s) SUITE="$OPTARG" ;;
-    t) TARGET="$OPTARG"
-       CHROOT_TARGET="$TARGET/grml_chroot"
-       BUILD_TARGET="$TARGET/grml_cd"
-       ISO_TARGET="$TARGET/grml_isos"
-       ;;
-    v) VERBOSE="-v" ;;
+    u) UPDATE=1 ;;
+    V) VERBOSE="-v" ;;
     ?) echo "invalid option -$OPTARG" >&2; bailout 1 ;;
   esac
 done
 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
-
 # }}}
 
 # some misc checks before executing FAI {{{
-[ -n "$CLASSES" ] || bailout 1 "Error: \$CLASSES unset, please set it in /etc/grml/grml-live.conf or
-specify it on the command line using the -c|--classes option."
-[ -n "$TARGET" ] || bailout 1 "Error: \$TARGET unset, please set it in /etc/grml/grml-live.conf or
-specify it on the command line using the -t|--target option."
+[ -n "$CLASSES" ] || bailout 1 "Error: \$CLASSES unset, please set it in $LIVE_CONF or
+specify it on the command line using the -c option."
+[ -n "$OUTPUT" ] || bailout 1 "Error: \$OUTPUT unset, please set it in $LIVE_CONF or
+specify it on the command line using the -o option."
 # }}}
 
 # ask user whether the setup is ok {{{
 if [ -z "$FORCE" ] ; then
    echo
-   echo "$PN - check your configuration (or invoke using -F to force execution without prompting)"
+   echo "${PN} [${GRML_LIVE_VERSION}]: check your configuration (or use -F to force execution):"
    echo
    echo "  FAI classes:       $CLASSES"
-   echo "  output directory:  $TARGET"
-   [ -n "$CHROOT_TARGET" ] && echo "  chroot target:     $CHROOT_TARGET"
-   [ -n "$BUILD_TARGET" ]  && echo "  build target:      $BUILD_TARGET"
-   [ -n "$ISO_TARGET" ]    && echo "  ISO target:        $ISO_TARGET"
+   echo "  main directory:    $OUTPUT"
+   [ -n "$CHROOT_OUTPUT" ] && echo "  chroot target:     $CHROOT_OUTPUT"
+   [ -n "$BUILD_OUTPUT" ]  && echo "  build target:      $BUILD_OUTPUT"
+   [ -n "$ISO_OUTPUT" ]    && echo "  ISO target:        $ISO_OUTPUT"
+   [ -n "$GRML_NAME" ]     && echo "  grml name:         $GRML_NAME"
+   [ -n "$RELEASENAME" ]   && echo "  release name:      $RELEASENAME"
+   [ -n "$VERSION" ]       && echo "  grml version:      $VERSION"
    [ -n "$SUITE" ]         && echo "  Debian suite:      $SUITE"
+   [ -n "$ARCH" ]          && echo "  Architecture:      $ARCH"
+   [ -n "$BOOT_METHOD" ]   && echo "  Boot method:       $BOOT_METHOD"
+   [ -n "$TEMPLATE_DIRECTORY" ] && echo "  Template files:    $TEMPLATE_DIRECTORY"
    [ -n "$FAI_ARGS" ]      && echo "  additional arguments for FAI: $FAI_ARGS"
+   [ -n "$LOGFILE" ]       && echo "  Logging to file:   $LOGFILE"
    [ -n "$VERBOSE" ]       && echo "  Using VERBOSE mode."
+   [ -n "$UPDATE" ]        && echo "  Executing UPDATE instead of fresh installation."
    echo
    echo -n "Is this ok for you? [y/N] "
    read a
@@ -124,131 +239,302 @@ if [ -z "$FORCE" ] ; then
    fi
    echo
 fi
+
+start_seconds=$(cut -d . -f 1 /proc/uptime)
+log "------------------------------------------------------------------------------"
+log "Starting grml-live [${GRML_LIVE_VERSION}] run on $(date)"
+log "Executed command line: $CMDLINE"
+
+einfo "Logging actions to logfile $LOGFILE"
 # }}}
 
 # on-the-fly configuration {{{
-if [ -n "$GRML_LIVE_SOURCES" ] ; then
+if [ -n "$MIRROR_DIRECTORY" ] ; then
+   if ! [ -d "$MIRROR_DIRECTORY/debian" ] ; then
+      log "Sorry, $MIRROR_DIRECTORY/debian does not seem to exist. Exiting. [$(date)]"
+      eerror "Sorry, $MIRROR_DIRECTORY/debian does not seem to exist. Exiting."
+      bailout 1
+   fi
+   echo "$MIRROR_SOURCES" > /etc/grml/fai/apt/sources.list
+   if [ -n "$GRML_LIVE_SOURCES" ] ; then
+      echo "$GRML_LIVE_SOURCES" >> /etc/grml/fai/apt/sources.list
+   fi
+elif [ -n "$GRML_LIVE_SOURCES" ] ; then
    echo "$GRML_LIVE_SOURCES" > /etc/grml/fai/apt/sources.list
 fi
 
-# this heuristic is broken as it works exactly *once* by default :-/
-# TODO / FIXME
+if [ -n "$FAI_DEBOOTSTRAP" ] ; then
+   sed -i "s#^FAI_DEBOOTSTRAP=.*#FAI_DEBOOTSTRAP=\"$FAI_DEBOOTSTRAP\"#" $NFSROOT_CONF
+fi
+
+# does this suck? YES!
 if [ -n "$SUITE" ] ; then
-   sed -i "s#etch #$SUITE #"   /etc/grml/fai/make-fai-nfsroot.conf
-   sed -i "s#etch #$SUITE #"   /etc/grml/grml-live.conf
-   sed -i "s# etch # $SUITE #" /etc/grml/grml-live.conf
-   sed -i "s# etch # $SUITE #" /etc/grml/fai/apt/sources.list
+   sed -i "s/SUITE=.*/SUITE=\"$SUITE\"/" $LIVE_CONF
+
+   DIST="\|\ etch\ \|\ stable\ \|\ lenny\ \|\ testing\ \|\ sid\ \|\ unstable\ "
+   sed -i "s/\(deb .\+\)\([ \t]+\)$DIST\([ \t]+\)\(main \)/\1\2 $SUITE \3\4/" $LIVE_CONF
+   sed -i "s/\(deb .\+\)\([ \t]+\)$DIST\([ \t]+\)\(main \)/\1\2 $SUITE \3\4/" /etc/grml/fai/apt/sources.list
+   # notice: activate grml-live pool only if we are building against unstable:
+   if grep -qe unstable -qe sid /etc/grml/fai/apt/sources.list ; then
+      grep -q 'grml-live.*main' /etc/grml/fai/apt/sources.list || \
+      grep grml-stable /etc/grml/fai/apt/sources.list | \
+           sed 's/grml-stable/grml-live/' >> /etc/grml/fai/apt/sources.list
+   else
+      grep -q 'grml-live.*main' /etc/grml/fai/apt/sources.list && \
+      sed -i 's/.*grml-live.*/# removed grml-live repository/' /etc/grml/fai/apt/sources.list
+   fi
+
+   sed -i "s|FAI_DEBOOTSTRAP=\"[a-z]* |FAI_DEBOOTSTRAP=\"$SUITE |" $LIVE_CONF
+   sed -i "s|FAI_DEBOOTSTRAP=\"[a-z]* |FAI_DEBOOTSTRAP=\"$SUITE |" $NFSROOT_CONF
 fi
 
-if [ -n "$FAI_DEBOOTSTRAP" ] ; then
-   sed -i "s#^FAI_DEBOOTSTRAP#FAI_DEBOOTSTRAP=$FAI_DEBOOTSTRAP#" /etc/grml/fai/make-fai-nfsroot.conf
+# set $ARCH
+[ -n "$ARCH" ] || ARCH="$(dpkg --print-architecture)"
+if grep -q -- 'FAI_DEBOOTSTRAP_OPTS.*--arch' $NFSROOT_CONF ; then
+   sed -i "s/--arch [a-z0-9]* /--arch $ARCH /" $NFSROOT_CONF
+else
+   sed -i "s|FAI_DEBOOTSTRAP_OPTS=\"\(.*\)|FAI_DEBOOTSTRAP_OPTS=\"--arch $ARCH \1|" $NFSROOT_CONF
 fi
 # }}}
 
-# CHROOT_TARGET - execute FAI {{{
-[ -n "$CHROOT_TARGET" ] || CHROOT_TARGET="$TARGET/grml_chroot"
+# CHROOT_OUTPUT - execute FAI {{{
+[ -n "$CHROOT_OUTPUT" ] || CHROOT_OUTPUT="$OUTPUT/grml_chroot"
+
+if [ -n "$UPDATE" ] ; then
+   FAI_ACTION=softupdate
+else
+   FAI_ACTION=dirinstall
+fi
+
+if [ -n "$UPDATE" ] ; then
+   if ! [ -r "$CHROOT_OUTPUT/etc/grml_version" ] ; then
+      log "Error: It does not look like you have a working chroot system. Updating not possible."
+      eerror "Error: It does not look like you have a working chroot system. Updating not possible."
+      eend 1
+      bailout 20
+   fi
+fi
 
-if [ -d "$CHROOT_TARGET" ] ; then
-   echo "  [x] $CHROOT_TARGET exists already, skipping the stage 'fai dirnstall'"
+if [ -d "$CHROOT_OUTPUT/bin" -a -z "$UPDATE" ] ; then
+   log "$CHROOT_OUTPUT exists already, skipping stage 'fai dirinstall'"
+   ewarn "$CHROOT_OUTPUT exists already, skipping stage 'fai dirinstall'" ; eend 0
 else
-   mkdir "$CHROOT_TARGET" || bailout 5 "Problem with creating $CHROOT_TARGET for FAI"
-   fai $VERBOSE -C "$GRML_FAI_CONFIG" -c"$CLASSES" dirinstall "$CHROOT_TARGET" $FAI_ARGS
-   umount $CHROOT_TARGET/proc 2>/dev/null || /bin/true
-   umount $CHROOT_TARGET/sys  2>/dev/null || /bin/true
-   echo "  [*] Finished execution of stage 'fai dirinstall'"
+   mkdir -p "$CHROOT_OUTPUT" || bailout 5 "Problem with creating $CHROOT_OUTPUT for FAI"
+   if [ -n "${MIRROR_DIRECTORY}" ] ; then
+      mkdir -p "${CHROOT_OUTPUT}/${MIRROR_DIRECTORY}"
+      mount --bind "${MIRROR_DIRECTORY}" "${CHROOT_OUTPUT}/${MIRROR_DIRECTORY}"
+   fi
+   fai $VERBOSE -C "$GRML_FAI_CONFIG" -c"$CLASSES" -u "$HOSTNAME" $FAI_ACTION "$CHROOT_OUTPUT" $FAI_ARGS | tee -a $LOGFILE
+
+   log "Setting /etc/grml_version to $GRML_NAME $VERSION Release Codename $RELEASENAME [$ISO_DATE]"
+   echo "$GRML_NAME $VERSION Release Codename $RELEASENAME [$ISO_DATE]" > $CHROOT_OUTPUT/etc/grml_version
+   chmod 644 $CHROOT_OUTPUT/etc/grml_version
+
+   # Remove all FAI logs from chroot if class RELEASE is used:
+   if [ -f $CHROOT_OUTPUT/etc/grml_fai_release ] ; then
+      rm -rf $CHROOT_OUTPUT/var/log/fai/*
+   fi
+
+   umount $CHROOT_OUTPUT/proc 2>/dev/null || /bin/true
+   umount $CHROOT_OUTPUT/sys  2>/dev/null || /bin/true
+   [ -n "$MIRROR_DIRECTORY" ] && umount "${CHROOT_OUTPUT}/${MIRROR_DIRECTORY}"
+
+   # notice: 'fai dirinstall' does not seem to exit appropriate, so:
+   ERROR=''
+   CHECKLOG=/var/log/fai/$HOSTNAME/last
+   if [ -r "$CHECKLOG/software.log" ] ; then
+      # 1 errors during executing of commands
+      # Unable to write mmap - msync (28 No space left on device)
+      # 'No candidate version found for' [/var/log/fai/current/software.log]
+      grep 'dpkg: error processing' $CHECKLOG/software.log >> $LOGFILE && ERROR=1
+      grep 'E: Method http has died unexpectedly!' $CHECKLOG/software.log >> $LOGFILE && ERROR=2
+      grep 'ERROR: chroot' $CHECKLOG/software.log >> $LOGFILE && ERROR=3
+   fi
+
+   if [ -r "$CHECKLOG/shell.log" ] ; then
+      grep 'FAILED with exit code' $CHECKLOG/shell.log >> $LOGFILE && ERROR=2
+   fi
+
+   if [ -n "$ERROR" ] ; then
+      log "There was an error [${ERROR}] during execution of stage 'fai dirinstall' [$(date)]"
+      eerror "There was an error during execution of stage 'fai dirinstall'"
+      echo "   Check out $CHECKLOG for details. [exit ${ERROR}]"
+      eend 1
+      bailout 1
+   else
+      log "Finished execution of stage 'fai dirinstall' [$(date)]"
+      einfo "Finished execution of stage 'fai dirinstall'"
+   fi
 fi
 # }}}
 
-# BUILD_TARGET - execute arch specific stuff and squashfs {{{
-[ -n "$BUILD_TARGET" ] || BUILD_TARGET="$TARGET/grml_cd"
-mkdir -p "$BUILD_TARGET" || bailout 6 "Problem with creating $BUILD_TARGET for stage ARCH"
+# BUILD_OUTPUT - execute arch specific stuff and squashfs {{{
+[ -n "$BUILD_OUTPUT" ] || BUILD_OUTPUT="$OUTPUT/grml_cd"
+mkdir -p "$BUILD_OUTPUT" || bailout 6 "Problem with creating $BUILD_OUTPUT for stage ARCH"
 
 # i386:
-[ -n "$ARCH" ] || ARCH="$(dpkg --print-architecture)"
-if [ "$ARCH" = i386 ] ; then
-   if [ -d "$BUILD_TARGET"/boot/isolinux ] ; then
-      echo "  [x] $BUILD_TARGET/boot/isolinux exists already - skipping stage 'boot/isolinux'"
-      continue
+if [ "$ARCH" = i386 ] || [ "$ARCH" = amd64 ] ; then
+   if [ -d "$BUILD_OUTPUT"/boot -a -z "$UPDATE" ] ; then
+      log "$BUILD_OUTPUT/boot exists already, skipping stage 'boot'"
+      ewarn "$BUILD_OUTPUT/boot exists already, skipping stage 'boot'" ; eend 0
    else
       # booting stuff:
-      mkdir -p "$BUILD_TARGET"/boot/isolinux
-      mkdir "$BUILD_TARGET"/GRML
-      cp /boot/memtest86+.bin                                        "$BUILD_TARGET"/boot/isolinux/memtest
-      cp "$CHROOT_TARGET"/boot/initrd*                               "$BUILD_TARGET"/boot/isolinux/initrd.gz
-      cp "$CHROOT_TARGET"/boot/vmlinuz*                              "$BUILD_TARGET"/boot/isolinux/linux26
-      cp /usr/lib/syslinux/chain.c32                                 "$BUILD_TARGET"/boot/isolinux/
-      cp /usr/lib/syslinux/isolinux.bin                              "$BUILD_TARGET"/boot/isolinux/
-      cp /usr/lib/syslinux/memdisk                                   "$BUILD_TARGET"/boot/isolinux/
-      cp /usr/lib/syslinux/menu.c32                                  "$BUILD_TARGET"/boot/isolinux/
-      cp /usr/share/grml-live/i386_files/boot/isolinux/allinone.img  "$BUILD_TARGET"/boot/isolinux/
-      cp /usr/share/grml-live/i386_files/boot/isolinux/balder10.imz  "$BUILD_TARGET"/boot/isolinux/
-      cp /usr/share/grml-live/i386_files/boot/isolinux/boot-beep.msg "$BUILD_TARGET"/boot/isolinux/
-      cp /usr/share/grml-live/i386_files/boot/isolinux/boot.msg      "$BUILD_TARGET"/boot/isolinux/
-      cp /usr/share/grml-live/i386_files/boot/isolinux/f*            "$BUILD_TARGET"/boot/isolinux/
-      cp /usr/share/grml-live/i386_files/boot/isolinux/isolinux.cfg  "$BUILD_TARGET"/boot/isolinux/
-      cp /usr/share/grml-live/i386_files/boot/isolinux/logo.16       "$BUILD_TARGET"/boot/isolinux/
-      cp /usr/share/grml-live/i386_files/boot/isolinux/syslinux.cfg  "$BUILD_TARGET"/boot/isolinux/
+      [ -d "$BUILD_OUTPUT"/boot/isolinux ] || mkdir -p "$BUILD_OUTPUT"/boot/isolinux
+      cp /boot/memtest86+.bin "$BUILD_OUTPUT"/boot/isolinux/memtest
+
+      # if we don't have an initrd we a) can't boot and b) there was an error
+      # during build, so check for the file:
+      INITRD="$(ls $CHROOT_OUTPUT/boot/initrd* 2>/dev/null| grep -v '.bak$' | sort -r | head -1)"
+      if [ -n "$INITRD" ] ; then
+         cp $INITRD "$BUILD_OUTPUT"/boot/isolinux/initrd.gz
+         find $CHROOT_OUTPUT/boot/ -name initrd\*.bak -exec rm {} \;
+      else
+         log "No initrd found inside $CHROOT_OUTPUT/boot/ - Exiting"
+         eerror "No initrd found inside $CHROOT_OUTPUT/boot/ - Exiting" ; eend 1
+         bailout 10
+      fi
+
+      KERNEL_IMAGE="$(ls $CHROOT_OUTPUT/boot/vmlinuz* 2>/dev/null | sort -r | head -1)"
+      if [ -n "$KERNEL_IMAGE" ] ; then
+         cp "$KERNEL_IMAGE" "$BUILD_OUTPUT"/boot/isolinux/linux26
+      else
+         log "No kernel found inside $CHROOT_OUTPUT/boot/ - Exiting"
+         eerror "No kernel found inside $CHROOT_OUTPUT/boot/ - Exiting" ; eend 1
+         bailout 11
+      fi
+
+      cp /usr/lib/syslinux/chain.c32     "$BUILD_OUTPUT"/boot/isolinux/
+      cp /usr/lib/syslinux/isolinux.bin  "$BUILD_OUTPUT"/boot/isolinux/
+      cp /usr/lib/syslinux/memdisk       "$BUILD_OUTPUT"/boot/isolinux/
+      cp /usr/lib/syslinux/menu.c32      "$BUILD_OUTPUT"/boot/isolinux/
+
+      [ -n "$TEMPLATE_DIRECTORY" ] || TEMPLATE_DIRECTORY='/usr/share/grml-live/templates'
+      if ! [ -d "${TEMPLATE_DIRECTORY}"/boot ] ; then
+         log "${TEMPLATE_DIRECTORY}/boot does not exist. Exiting."
+         eerror "${TEMPLATE_DIRECTORY}/boot does not exist. Exiting." ; eend 1
+         bailout 8
+      fi
+
+      cp ${TEMPLATE_DIRECTORY}/boot/isolinux/*  "$BUILD_OUTPUT"/boot/isolinux/
+      cp ${TEMPLATE_DIRECTORY}/boot/isolinux/*  "$BUILD_OUTPUT"/boot/isolinux/
+
+      if ! [ -d "${BUILD_OUTPUT}/boot/grub" ] ; then
+         cp -a ${TEMPLATE_DIRECTORY}/boot/grub  "$BUILD_OUTPUT"/boot/
+      fi
+
+      if ! [ -d "${TEMPLATE_DIRECTORY}"/GRML ] ; then
+         log "${TEMPLATE_DIRECTORY}/GRML does not exist. Exiting."
+         eerror "${TEMPLATE_DIRECTORY}/GRML does not exist. Exiting." ; eend 1
+         bailout 9
+      fi
+
+      [ -d "$BUILD_OUTPUT"/GRML ] || mkdir "$BUILD_OUTPUT"/GRML
+      cp -a ${TEMPLATE_DIRECTORY}/GRML/* "$BUILD_OUTPUT"/GRML/
+
+      # adjust boot splash information:
+      RELEASE_INFO="$GRML_NAME $VERSION - Release Codename $RELEASENAME"
+      RELEASE_INFO="$(cut_string 68 "$RELEASE_INFO")"
+      RELEASE_INFO="$(extend_string_end 68 "$RELEASE_INFO")"
+
+      sed -i "s/%RELEASE_INFO%/$GRML_NAME $VERSION - $RELEASENAME/" "$BUILD_OUTPUT"/GRML/grml-version
+      sed -i "s/%DATE%/$ISO_DATE/"             "$BUILD_OUTPUT"/GRML/grml-version
+
+      sed -i "s/%RELEASE_INFO%/$RELEASE_INFO/" "$BUILD_OUTPUT"/boot/isolinux/boot.msg
+      sed -i "s/%DATE%/$ISO_DATE/"             "$BUILD_OUTPUT"/boot/isolinux/boot.msg
+
+      sed -i "s/%RELEASE_INFO%/$RELEASE_INFO/" "$BUILD_OUTPUT"/boot/isolinux/boot-beep.msg
+      sed -i "s/%DATE%/$ISO_DATE/"             "$BUILD_OUTPUT"/boot/isolinux/boot-beep.msg
+
+      sed -i "s/%VERSION%/$VERSION/"           "$BUILD_OUTPUT"/boot/grub/menu.lst
+      sed -i "s/%GRML_NAME%/$GRML_NAME/"       "$BUILD_OUTPUT"/boot/grub/menu.lst
+
       # autostart for Windows:
-      cp /usr/share/grml-live/windows/autostart/autorun.bat          "$BUILD_TARGET"/
-      cp /usr/share/grml-live/windows/autostart/autorun.inf          "$BUILD_TARGET"/
-      cp /usr/share/grml-live/windows/autostart/autorun.pif          "$BUILD_TARGET"/
-      cp /usr/share/grml-live/windows/autostart/cdrom.ico            "$BUILD_TARGET"/
+      if [ -d "${TEMPLATE_DIRECTORY}/windows/autostart/" ] ; then
+         cp ${TEMPLATE_DIRECTORY}/windows/autostart/* "$BUILD_OUTPUT"/
+      fi
+
       # windows-binaries:
       if [ -n "$WINDOWS_BINARIES" ] ; then
-         if [ -d "$BUILD_TARGET"/windows ] ; then
-            echo "  [x] $BUILD_TARGET/windows exists already - skipping stage 'WINDOWS_BINARIES'"
-            return 0
+         if [ -f "$BUILD_OUTPUT"/windows/putty.exe ] ; then
+            log "$BUILD_OUTPUT/windows exists already, skipping stage 'WINDOWS_BINARIES'"
+            ewarn "$BUILD_OUTPUT/windows exists already, skipping stage 'WINDOWS_BINARIES'" ; eend 0
          else
-            mkdir "$BUILD_TARGET"/windows
-            ( cd "$BUILD_TARGET"/windows
-              for file in pageant plink pscp psftp putty puttygen ; do
-                  wget ${WINDOWS_BINARIES}/${file}.exe
-              done )
+            if ! [ -d "$BUILD_OUTPUT"/windows ] ; then
+               mkdir "$BUILD_OUTPUT"/windows
+               ( cd "$BUILD_OUTPUT"/windows
+                 for file in pageant plink pscp psftp putty puttygen ; do
+                    wget -O ${file}.exe ${WINDOWS_BINARIES}/${file}.exe
+                    md5sum ${file}.exe > ${file}.exe.md5
+                 done )
+            fi
          fi
-      echo "  [*] Finished execution of stage 'WINDOWS_BINARIES'"
+      log "Finished execution of stage 'WINDOWS_BINARIES' [$(date)]"
+      einfo "Finished execution of stage 'WINDOWS_BINARIES'" ; eend 0
       fi
-   echo "  [*] Finished execution of stage 'boot/isolinux'"
+   einfo "Finished execution of stage 'boot'" ; eend 0
    fi
-# amd64:
-elif [ "$ARCH" = amd64 ] ; then
-    echo 'Warning: gebi, it is your turn. :)'>&2
 # ppc:
 elif [ "$ARCH" = powerpc ] ; then
-    echo 'Warning: formorer, it is your turn. :)'>&2
+    ewarn 'Warning: formorer, it is your turn. :)'>&2
 # unsuported:
 else
-   echo 'Warning: Unsupported ARCH, sorry. Want to support it? Contribute!'>&2
+   eerror 'Error: Unsupported ARCH, sorry. Want to support it? Contribute!' ; eend 1
 fi
 
-if [ -d "$BUILD_TARGET"/live ] ; then
-   echo "  [x] $BUILD_TARGET/live exists already, skipping stage 'squashfs'"
+if [ -f "$BUILD_OUTPUT"/live/grml.squashfs -a -z "$UPDATE" ] ; then
+   log "$BUILD_OUTPUT/live exists already, skipping stage 'squashfs'"
+   ewarn "$BUILD_OUTPUT/live exists already, skipping stage 'squashfs'" ; eend 0
 else
-   mkdir "$BUILD_TARGET"/live
-   mksquashfs $CHROOT_TARGET/* $BUILD_TARGET/live/grml.squashfs -noappend
+   [ -d "$BUILD_OUTPUT"/live ] || mkdir "$BUILD_OUTPUT"/live
+   mksquashfs $CHROOT_OUTPUT/* $BUILD_OUTPUT/live/grml.squashfs -noappend
+   log "Finished execution of stage 'squashfs' [$(date)]"
+   einfo "Finished execution of stage 'squashfs'" ; eend 0
 fi
-echo "  [*] Finished execution of stage 'squashfs'"
+
+# create md5sum file:
+( cd $BUILD_OUTPUT/GRML &&
+find .. -type f -not -name md5sums -not -name isolinux.bin -exec md5sum {} \; > md5sums )
 # }}}
 
-# ISO_TARGET - mkisofs {{{
-[ -n "$ISO_TARGET" ] || ISO_TARGET="$TARGET/grml_isos"
-if [ -d "$ISO_TARGET" ] ; then
-   echo "  [x] $ISO_TARGET exists already, skipping the stage 'iso build'"
+# ISO_OUTPUT - mkisofs {{{
+[ -n "$ISO_OUTPUT" ] || ISO_OUTPUT="$OUTPUT/grml_isos"
+[ -n "$ISO_NAME" ] || ISO_NAME="${GRML_NAME}_${VERSION}.iso"
+
+if [ "$BOOT_METHOD" = "isolinux" ] ; then
+   BOOT_FILE="boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat"
+elif [ "$BOOT_METHOD" = "grub" ] ; then
+   BOOT_FILE="boot/grub/stage2_eltorito"
+fi
+
+if [ -f "${ISO_OUTPUT}/${ISO_NAME}" -a -z "$UPDATE" ] ; then
+   log "$ISO_OUTPUT exists already, skipping stage 'iso build'"
+   ewarn "$ISO_OUTPUT exists already, skipping stage 'iso build'" ; eend 0
 else
-   mkdir -p "$ISO_TARGET" || bailout 6 "Problem with creating $ISO_TARGET for stage 'iso build'"
-   ( cd "$BUILD_TARGET" &&
-     mkisofs -V "Debian/etch grml" -publisher 'Michael Prokop <mika@grml.org>' \
-             -l -r -J -no-emul-boot -boot-load-size 4 -boot-info-table    \
-             -c boot/isolinux/boot.cat -b boot/isolinux/isolinux.bin      \
-             -o "$ISO_TARGET"/grml_0.0-1.iso .
-   )
-   echo "  [*] Finished execution of stage 'iso build'"
+   mkdir -p "$ISO_OUTPUT" || bailout 6 "Problem with creating $ISO_OUTPUT for stage 'iso build'"
+   CURRENT_DIR=$(pwd)
+   cd "$BUILD_OUTPUT" &&
+   mkisofs -V "grml $VERSION" -publisher 'grml-live | grml.org' \
+           -l -r -J -no-emul-boot -boot-load-size 4 -boot-info-table    \
+           -b $BOOT_FILE \
+           -o "${ISO_OUTPUT}/${ISO_NAME}" . ; RC=$?
+   cd $CURRENT_DIR
+   if [ "$RC" = 0 ] ; then
+      log "Finished execution of stage 'iso build' [$(date)]"
+      einfo "Finished execution of stage 'iso build'" ; eend 0
+   else
+      log "There was an error ($RC) executing stage 'iso build' [$(date)]"
+      eerror "There was an error executing stage 'iso build'" ; eend 1
+      bailout $RC
+   fi
 fi
 # }}}
 
 # finalize {{{
-echo "  [*] Sucessfully finished execution of $PN"
+[ -n "$start_seconds" ] && SECONDS="$[$(cut -d . -f 1 /proc/uptime)-$start_seconds]" || SECONDS="unknown"
+einfo "Sucessfully finished execution of $PN [running ${SECONDS} seconds]" ; eend 0
+log "Sucessfully finished execution of $PN [running ${SECONDS} seconds]"
 bailout 0
 # }}}
 
 ## END OF FILE #################################################################
-# vim:foldmethod=marker ts=2 ft=sh ai expandtab tw=80 sw=2
+# vim:foldmethod=marker ts=2 ft=sh ai expandtab tw=80 sw=3