Improve directory handling for $LOGFILE
[grml-live.git] / grml-live
index c39e5d5..766f588 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: Thu Oct 04 22:16:28 CEST 2007 [mika]
+# Latest change: Wed Oct 24 09:26:39 CEST 2007 [mika]
 ################################################################################
 
 # read configuration files, set some misc variables {{{
@@ -22,7 +22,7 @@ if [ "$(id -u 2>/dev/null)" != 0 ] ; then
 fi
 
 if [ -r /var/run/fai/FAI_INSTALLATION_IN_PROGRESS ] ; then
-   echo "/usr/sbin/fai already running or was aborted before."&>2
+   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
@@ -69,8 +69,15 @@ trap bailout 1 2 3 15
 [ -n "$RELEASENAME" ] || RELEASENAME="grml-live rocks"
 [ -n "$GRML_NAME" ] || GRML_NAME='grml'
 
-[ -n "$LOGDIR" ] || LOGDIR="/var/log/"
-LOGFILE="$LOGDIR/grml-live.log"
+[ -d /var/log/fai/dirinstall/"${HOSTNAME}" ] || mkdir -p /var/log/fai/dirinstall/${HOSTNAME} 
+if [ -d /var/log/fai/dirinstall/"${HOSTNAME}" ] ; then
+   LOGFILE=/var/log/fai/dirinstall/${HOSTNAME}/grml-live.log
+else
+   LOGFILE="$LOGDIR/grml-live.log"
+fi
+
+NFSROOT_CONF=/etc/grml/fai/make-fai-nfsroot.conf
+
 # }}}
 
 # some important functions {{{
@@ -128,9 +135,10 @@ usage()
   echo "
 $PN - build process script for generating a (grml based) Linux Live-ISO
 
-Usage: $PN [-c <classe[s]>] [-g <grml_name>] [-i <iso_name> ] \\
-                 [-o <output_directory>] [-s <suite>] [-t <template_directory>] \\
-                 [-s <suite>] [-v <version_number>] [-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>] [-FVh]
 
 Usage examples:
 
@@ -138,8 +146,8 @@ Usage examples:
     $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
-    $PN -c GRMLBASE,GRML_FULL,I386 -s sid -V
+    $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
@@ -152,19 +160,20 @@ http://grml.org/bugs/
 
 # command line parsing {{{
 
-while getopts "c:g:i:r:s:t:v:FhV" opt; do
+while getopts "a:c:g:i:o:r:s:t:v:FhV" opt; do
   case "$opt" in
+    a) ARCH="$OPTARG" ;;
     c) CLASSES="$OPTARG" ;;
     g) GRML_NAME="$OPTARG" ;;
     i) ISO_NAME="$OPTARG" ;;
-    r) RELEASENAME="$OPTARG" ;;
-    s) SUITE="$OPTARG" ;;
-    t) TEMPLATE_DIRECTORY="$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 ;;
@@ -174,13 +183,14 @@ while getopts "c:g:i:r:s:t:v:FhV" opt; do
 done
 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
 
+echo "Executing: $(basename $0) $*" >> $LOGFILE
 # }}}
 
 # some misc checks before executing FAI {{{
 [ -n "$CLASSES" ] || bailout 1 "Error: \$CLASSES unset, please set it in $LIVE_CONF or
-specify it on the command line using the -c|--classes option."
+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 -t|--target option."
+specify it on the command line using the -o option."
 # }}}
 
 # ask user whether the setup is ok {{{
@@ -197,7 +207,9 @@ if [ -z "$FORCE" ] ; then
    [ -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 "$VERBOSE" ]       && echo "  Using VERBOSE mode."
    echo
@@ -208,10 +220,12 @@ if [ -z "$FORCE" ] ; then
    fi
    echo
 
-   start_seconds=$(cut -d . -f 1 /proc/uptime)
-   log "------------------------------------------------------------------------------"
-   log "Starting grml-live run [$(date)]"
 fi
+
+start_seconds=$(cut -d . -f 1 /proc/uptime)
+log "------------------------------------------------------------------------------"
+log "Starting grml-live run [$(date)]"
+log "Executed command line: $0 $*"
 # }}}
 
 # on-the-fly configuration {{{
@@ -230,7 +244,7 @@ elif [ -n "$GRML_LIVE_SOURCES" ] ; then
 fi
 
 if [ -n "$FAI_DEBOOTSTRAP" ] ; then
-   sed -i "s#^FAI_DEBOOTSTRAP=.*#FAI_DEBOOTSTRAP=\"$FAI_DEBOOTSTRAP\"#" /etc/grml/fai/make-fai-nfsroot.conf
+   sed -i "s#^FAI_DEBOOTSTRAP=.*#FAI_DEBOOTSTRAP=\"$FAI_DEBOOTSTRAP\"#" $NFSROOT_CONF
 fi
 
 # does this suck? YES!
@@ -241,9 +255,16 @@ if [ -n "$SUITE" ] ; then
    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
 
-   DIST='\"etch\|=\"stable=\"lenny=\"testing=\"sid=\"unstable'
-   sed -i "s#FAI_DEBOOTSTRAP=$DIST#FAI_DEBOOTSTRAP=\"$SUITE#" $LIVE_CONF
-   sed -i "s#FAI_DEBOOTSTRAP=$DIST#FAI_DEBOOTSTRAP=\"$SUITE#" /etc/grml/fai/make-fai-nfsroot.conf
+   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
+
+# 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
 # }}}
 
@@ -267,7 +288,10 @@ else
    # notice: 'fai dirinstall' does not seem to exit appropriate, so:
    ERROR=''
    if [ -r "/var/log/fai/dirinstall/$HOSTNAME/software.log" ] ; then
+      # 1 errors during executing of commands
       grep 'dpkg: error processing' /var/log/fai/dirinstall/$HOSTNAME/software.log >> $LOGFILE && ERROR=1
+      grep 'E: Method http has died unexpectedly!' /var/log/fai/dirinstall/$HOSTNAME/software.log >> $LOGFILE && ERROR=2
+      grep 'ERROR: chroot' /var/log/fai/dirinstall/$HOSTNAME/software.log >> $LOGFILE && ERROR=3
    fi
 
    if [ -r "/var/log/fai/dirinstall/$HOSTNAME/shell.log" ] ; then
@@ -275,10 +299,11 @@ else
    fi
 
    if [ -n "$ERROR" ] ; then
-      log "There was an error during execution of stage 'fai dirinstall' [$(date)]"
+      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 /var/log/fai/dirinstall/$HOSTNAME/ for details. [exit ${ERROR}]"
-      eend 1 ; exit 1
+      eend 1
+      bailout 1
    else
       log "Finished execution of stage 'fai dirinstall' [$(date)]"
       einfo "Finished execution of stage 'fai dirinstall'"
@@ -290,8 +315,6 @@ fi
 [ -n "$BUILD_OUTPUT" ] || BUILD_OUTPUT="$OUTPUT/grml_cd"
 mkdir -p "$BUILD_OUTPUT" || bailout 6 "Problem with creating $BUILD_OUTPUT for stage ARCH"
 
-[ -n "$ARCH" ] || ARCH="$(dpkg --print-architecture)"
-
 # i386:
 if [ "$ARCH" = i386 ] || [ "$ARCH" = amd64 ] ; then
    if [ -d "$BUILD_OUTPUT"/boot ] ; then
@@ -300,20 +323,41 @@ if [ "$ARCH" = i386 ] || [ "$ARCH" = amd64 ] ; then
    else
       # booting stuff:
       mkdir -p "$BUILD_OUTPUT"/boot/isolinux
-      cp /boot/memtest86+.bin                              "$BUILD_OUTPUT"/boot/isolinux/memtest
-      # do not keep the initrd inside the chroot, let's save space instead:
-      mv "$CHROOT_OUTPUT"/boot/initrd*                     "$BUILD_OUTPUT"/boot/isolinux/initrd.gz
+      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:
+      if [ -f "$CHROOT_OUTPUT"/boot/initrd* ] ; then
+         cp "$CHROOT_OUTPUT"/boot/initrd*  "$BUILD_OUTPUT"/boot/isolinux/initrd.gz
+      else
+         log "No initrd found inside $CHROOT_OUTPUT/boot/ - Exiting"
+         eerror "No initrd found inside $CHROOT_OUTPUT/boot/ - Exiting" ; eend 1
+         bailout 10
+      fi
+
       cp "$CHROOT_OUTPUT"/boot/vmlinuz*                    "$BUILD_OUTPUT"/boot/isolinux/linux26
       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/
-      cp /usr/share/grml-live/templates/boot/isolinux/*    "$BUILD_OUTPUT"/boot/isolinux/
-      cp /usr/share/grml-live/templates/boot/isolinux/*    "$BUILD_OUTPUT"/boot/isolinux/
-      cp -a /usr/share/grml-live/templates/boot/grub       "$BUILD_OUTPUT"/boot/
 
+      [ -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/
+      cp -a ${TEMPLATE_DIRECTORY}/boot/grub                  "$BUILD_OUTPUT"/boot/
+
+      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 /usr/share/grml-live/templates/GRML/* "$BUILD_OUTPUT"/GRML/
+      cp -a ${TEMPLATE_DIRECTORY}/GRML/* "$BUILD_OUTPUT"/GRML/
 
       # adjust boot splash information:
       ISO_DATE="$(date +%Y-%m-%d)"
@@ -334,7 +378,9 @@ if [ "$ARCH" = i386 ] || [ "$ARCH" = amd64 ] ; then
       sed -i "s/%GRML_NAME%/$GRML_NAME/"       "$BUILD_OUTPUT"/boot/grub/menu.lst
 
       # autostart for Windows:
-      cp /usr/share/grml-live/templates/windows/autostart/* "$BUILD_OUTPUT"/
+      if [ -d "${TEMPLATE_DIRECTORY}/windows/autostart/" ] ; then
+         cp ${TEMPLATE_DIRECTORY}/windows/autostart/* "$BUILD_OUTPUT"/
+      fi
       # windows-binaries:
       if [ -n "$WINDOWS_BINARIES" ] ; then
          if [ -f "$BUILD_OUTPUT"/windows/putty.exe ] ; then
@@ -409,7 +455,7 @@ fi
 # }}}
 
 # finalize {{{
-[ -n "$start_seconds" ] && SECONDS="$[$(cut -d . -f 1 /proc/uptime)-$start_seconds]"
+[ -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]"
 log "------------------------------------------------------------------------------"