fix wrong behavior if no grml.sh is found
[grml-autoconfig.git] / autoconfig.functions
index d38aee4..4d13248 100755 (executable)
@@ -10,6 +10,7 @@
 export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin"
 DEBUG="/dev/null"
 KERNEL="$(uname -r)"
+ARCH="$(uname -m)"
 umask 022
 
 # old linuxrc version:
@@ -35,7 +36,7 @@ iszsh && setopt no_nomatch # || echo "Warning: not running under zsh!"
 # {{{ Read in boot parameters
 if [ -z "$CMDLINE" ]; then
   # if CMDLINE was set from the outside, we're debugging.
-  # Otherwise, take CMDLINE from Kernel and config files.
+  # otherwise, take CMDLINE from Kernel and config files.
   CMDLINE="$(cat /proc/cmdline)"
   [ -d /cdrom/bootparams/ ]      && CMDLINE="$CMDLINE $(cat /cdrom/bootparams/* | tr '\n' ' ')"
   [ -d /live/image/bootparams/ ] && CMDLINE="$CMDLINE $(cat /live/image/bootparams/* | tr '\n' ' ')"
@@ -44,49 +45,54 @@ fi
 
 ### {{{ Utility Functions
 
-# Simple shell grep
-stringinfile(){
-  case "$(cat $2)" in *$1*) return 0;; esac
-  return 1
-}
-
-# same for strings
-stringinstring(){
-  case "$2" in *$1*) return 0;; esac
-  return 1
-}
-
-# Reread boot command line; echo last parameter's argument or return false.
+# Get a bootoption's parameter: read boot command line and either
+# echo last parameter's argument or return false.
 getbootparam(){
-  stringinstring " $1=" "$CMDLINE" || return 1
-  result="${CMDLINE##* $1=}"
-  result="${result%%[  ]*}"
-  echo "$result"
-  return 0
+  local line
+  local ws
+  ws='  '
+  line=" $CMDLINE "
+  case "$line" in
+    *[${ws}]"$1="*)
+      result="${line##*[$ws]$1=}"
+      result="${result%%[$ws]*}"
+      echo "$result"
+      return 0 ;;
+    *) # no match?
+      return 1 ;;
+  esac
 }
 
 # Check boot commandline for specified option
 checkbootparam(){
-  stringinstring " $1" "$CMDLINE"
-  return "$?"
+  [ -n "$1" ] || ( echo "Error: missing argument to checkbootparam()" ; return 1 )
+  local line
+  local ws
+  ws='  '
+  line=" $CMDLINE "
+  case "$line" in
+    *[${ws}]"$1"=*|*[${ws}]"$1"[${ws}]*)
+      return 0 ;;
+    *)
+      return 1 ;;
+  esac
 }
 
+# Check wheter a configuration variable (like $CONFIG_TOHD) is
+# enabled or not
 checkvalue(){
-  if [ "$1" = "yes" ] ; then
-    return 0
-  else
-    return 1
-  fi
+  case "$1" in
+    [yY][eE][sS])     return 0 ;; # it's set to 'yes'
+    [tT][rR][uU][eE]) return 0 ;; # it's set to 'true'
+                   *) return 1 ;; # default
+  esac
 }
 
+# Are we using grml-small?
 checkgrmlsmall(){
   grep -q small /etc/grml_version 2>>$DEBUG && return 0 || return 1
 }
 
-checkgrmlusb(){
-  grep -q usb /etc/grml_version 2>>$DEBUG && return 0 || return 1
-}
-
 # execute flite only if it's present
 flitewrapper() {
    [ -x /usr/bin/flite ] && flite -o play -t "$*"
@@ -99,7 +105,7 @@ mount_proc(){
 }
 
 mount_pts(){
-  stringinfile "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
+  grep -q "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
 }
 
 mount_sys(){
@@ -129,8 +135,8 @@ fi
 
 # {{{ debug
 config_debug(){
- checkbootparam 'debug'                        && BOOTDEBUG="yes"
stringinstring "BOOT_IMAGE=debug " "$CMDLINE" && BOOTDEBUG="yes"
+ checkbootparam 'debug'            && BOOTDEBUG="yes"
checkbootparam "BOOT_IMAGE=debug" && BOOTDEBUG="yes"
 
  rundebugshell(){
   if [ -n "$BOOTDEBUG" ]; then
@@ -424,7 +430,7 @@ if checkbootparam 'novmware' ; then
    ewarn "Skipping running vmware specific stuff as requested on boot commandline." ; eend 0
 else
    if [ -z "$INSTALLED" ] ; then
-      if vmware-detect || stringinstring "BOOT_IMAGE=vmware " "$CMDLINE" ; then
+      if vmware-detect || checkbootparam "BOOT_IMAGE=vmware" ; then
          if ! checkbootparam 'qemu' ; then
             if [ -r /etc/X11/xorg.conf.vmware ] ; then
                einfo "VMware: Copying /etc/X11/xorg.conf.vmware to /etc/X11/xorg.conf"
@@ -545,7 +551,7 @@ eoutdent
 
 # skip startup of w3m {{{
 config_fast(){
-if checkbootparam 'fast '; then
+if checkbootparam 'fast'; then
   ewarn "Bootoption fast detected. Skipping startup of grml-quickconfig."
     sed -i 's#^1:.*#1:12345:respawn:/usr/bin/openvt -f -c 1 -w -- /bin/zsh#' /etc/inittab
   /sbin/telinit q ; eend $?
@@ -995,7 +1001,7 @@ config_interactive(){
 config_agp(){
 if checkbootparam 'forceagp' ; then
 # Probe for AGP. Hope this can fail safely
-  stringinfile "AGP" "/proc/pci" 2>>$DEBUG && { modprobe agpgart || modprobe agpgart agp_try_unsupported=1; } >>$DEBUG 2>&1 && einfo "AGP bridge detected." ; eend 0
+  grep -q "AGP" "/proc/pci" 2>>$DEBUG && { modprobe agpgart || modprobe agpgart agp_try_unsupported=1; } >>$DEBUG 2>&1 && einfo "AGP bridge detected." ; eend 0
 fi
 }
 # }}}
@@ -1015,14 +1021,14 @@ addautomount(){
     [ -d "/mnt/auto/$d" ] || mkdir -p "/mnt/auto/$d"
     [ -L "/mnt/$d" ]      || ln -s "/mnt/auto/$d" "/mnt/$d"
     anew="$d        -fstype=auto,$2 :$i"
-    stringinfile "$anew" "/etc/auto.mnt" || echo "$anew" >> /etc/auto.mnt
+    grep -q "$anew" "/etc/auto.mnt" || echo "$anew" >> /etc/auto.mnt
     AUTOMOUNTS="$AUTOMOUNTS $d"
     new="$1 /mnt/auto/$d  auto   users,noauto,exec,$2 0 0"
   else
     [ -d /mnt/$d ] && mkdir -p /mnt/$d
     new="$1 /mnt/$d  auto   users,noauto,exec,$2 0 0"
   fi
-  stringinfile "$new" "/etc/fstab" || echo "$new" >> /etc/fstab
+  grep -q "$new" "/etc/fstab" || echo "$new" >> /etc/fstab
 }
 
   AUTOMOUNTS="floppy cdrom"
@@ -1187,7 +1193,7 @@ if [ -z "$INSTALLED" ] ; then
                 einfo "Using GRML swapfile ${WHITE}${SWAPFILE}${NORMAL}."
               eoutdent
               fnew="$SWAPFILE swap swap defaults 0 0"
-              stringinfile "$fnew" "/etc/fstab" || echo "$fnew" >> /etc/fstab
+              grep -q "$fnew" "/etc/fstab" || echo "$fnew" >> /etc/fstab
               GRML_SWP="$GRML_SWP $SWAPFILE"
               eend 0
            fi
@@ -1268,7 +1274,7 @@ else
     einfo "Network device ${WHITE}${DEVICE}${NORMAL} detected, DHCP broadcasting for IP. (Backgrounding)"
     trap 2 3 11
     ifconfig $DEVICE up >>$DEBUG 2>&1
-    ( pump -i $DEVICE >>$DEBUG 2>&1 && echo finished_running_pump > /etc/network/status/$DEVICE ) &
+    ( pump -i $DEVICE --script=/usr/lib/grml-autoconfig/pump-runparts >>$DEBUG 2>&1 && echo finished_running_pump > /etc/network/status/$DEVICE ) &
     trap "" 2 3 11
     sleep 1
     eend 0
@@ -1591,9 +1597,10 @@ fi # checkbootparam home
 # }}}
 
 # {{{ Sound
+
 config_mixer(){
-if ! [ -x /usr/bin/aumix ] ; then
-  eerror "aumix binary not available. Can not set sound volumes therefore." ; eend 1
+if ! [ -x /usr/bin/amixer ] ; then
+  eerror "amixer binary not available. Can not set sound volumes therefore." ; eend 1
 else
 
   if ! [ -r /proc/asound/cards ] ; then
@@ -1613,14 +1620,12 @@ else
 
   if checkbootparam 'nosound' ; then
     einfo "Muting sound devices on request."
-    # some IBM notebooks require the following stuff:
-    if [ -x /usr/bin/amixer ] ; then
-       if amixer get Front 1>/dev/null 2>>$DEBUG ; then
-          amixer set Front unmute 1>/dev/null
-          amixer set Front 0% 1>/dev/null
-       fi
-    fi
-    ERROR=$(aumix -w 0 -v 0 -p 0 -m 0 2>&1) ; RC=$?
+
+    fix_ibm_sound 0
+    # mute the master, this should be sufficient
+    ERROR=$(amixer -q set Master mute)
+    RC=$?
+
     if [ -n "$ERROR" ] ; then
        eindent
        eerror "Problem muting sound devices: $ERROR"
@@ -1629,13 +1634,9 @@ else
     eend $RC
   elif [ -z "$INSTALLED" ]; then
       einfo "Setting mixer volumes to level ${WHITE}${VOL}${NORMAL}."
-      # some IBM notebooks require the following stuff:
-      if [ -x /usr/bin/amixer ] ; then
-         if amixer get Front 1>/dev/null 2>>$DEBUG ; then
-            amixer set Front unmute 1>/dev/null
-            amixer set Front ${VOL}% 1>/dev/null
-         fi
-      fi
+
+      fix_ibm_sound ${VOL}
+
       # by default assume '0' as volume for microphone:
       if checkbootparam 'micvol' ; then
          MICVOL="$(getbootparam 'micvol' 2>>$DEBUG)"
@@ -1644,17 +1645,39 @@ else
       fi
 
       # finally set the volumes:
-      ERROR=$(aumix -w $VOL -v $VOL -p $VOL -m $MICVOL 2>&1) ; RC=$?
-      if [ -n "$ERROR" ] ; then
-         eindent
-         eerror "Problem setting mixer volumes: $ERROR (no soundcard?)"
-         eoutdent
+      RC=0
+      for CONTROL in Master PCM ; do
+         amixer -q set ${CONTROL} ${VOL}%
+         if [ $? -ne 0 ] ; then RC=$? ; fi
+      done
+      # dont know how to set microphone volume for all soundcards with amixer,
+      # so use aumix instead :/
+      if [ ${MICVOL} -ne 0 -a -x /usr/bin/aumix ] ; then
+         aumix -m $MICVOL &>/dev/null
+         if [ $? -ne 0 ] ; then RC=$? ; fi
       fi
+
       eend $RC
   fi
 
 fi
 }
+
+# on some IBM notebooks the front control has to be toggled
+fix_ibm_sound() {
+ if [ -z $1 ] ; then
+    return
+ fi
+
+ VOL=${1}
+
+ if [ -x /usr/bin/amixer ] ; then
+    if amixer -q get Front 1>/dev/null 2>>$DEBUG ; then
+       amixer -q set Front unmute &>/dev/null
+       amixer -q set Front ${VOL}% &>/dev/null
+    fi
+ fi
+}
 # }}}
 
 # {{{ modem detection
@@ -1787,31 +1810,59 @@ config_services(){
 }
 # }}}
 
-# {{{ config files
-config_netconfig(){
- if checkbootparam 'netconfig' ; then
-  CONFIG="$(getbootparam 'netconfig' 2>>$DEBUG)"
-  CONFIGFILE='/tmp/netconfig.grml'
-
+# {{{ remote files
+get_remote_file() {
+  [ "$#" -eq 2 ] || ( echo "Error: wrong parameter for get_remote_file()" ; return 1 )
+  SOURCE=$(eval echo "$1")
+  TARGET="$2"
   getconfig() {
-  wget --timeout=10 --dns-timeout=10  --connect-timeout=10 \
-       --read-timeout=10 $CONFIG -O $CONFIGFILE && return 0 || return 1
+  wget --timeout=10 --dns-timeout=10  --connect-timeout=10 --tries=1 \
+       --read-timeout=10 ${SOURCE} -O ${TARGET} && return 0 || return 1
   }
-  einfo "Trying to get ${WHITE}${CONFIG}${NORMAL}"
+  einfo "Trying to get ${WHITE}${TARGET}${NORMAL}"
   counter=10
   while ! getconfig && [[ "$counter" != 0 ]] ; do
-    echo -n "Sleeping for 5 seconds and trying to get config again... "
+    echo -n "Sleeping for 1 second and trying to get config again... "
     counter=$(( counter-1 ))
     echo "$counter tries left" ; sleep 1
   done
-  if [ -r "$CONFIGFILE" ] ; then
+  if [ -s "$TARGET" ] ; then
     einfo "Downloading was successfull." ; eend 0
-    einfo "md5sum of ${WHITE}${CONFIG}${NORMAL}: "
-    md5sum $CONFIGFILE ; eend 0
-    cd / && einfo "Unpacking ${WHITE}${CONFIGFILE}${NORMAL}:" && /usr/bin/unp $CONFIGFILE $EXTRACTOPTIONS ; eend $?
+    einfo "md5sum of ${WHITE}${TARGET}${NORMAL}: "
+    md5sum ${TARGET} ; eend 0
+    return 0;
   else
-    einfo "Sorry, could not fetch $CONFIG" ; eend 1
+    einfo "Sorry, could not fetch ${SOURCE}" ; eend 1
+    return 1;
+ fi
+}
+# }}}
+
+# {{{ config files
+config_netconfig(){
+ if checkbootparam 'netconfig' ; then
+  CONFIG="$(getbootparam 'netconfig' 2>>$DEBUG)"
+  CONFIGFILE='/tmp/netconfig.grml'
+
+  if get_remote_file ${CONFIG} ${CONFIGFILE} ; then
+    cd / && einfo "Unpacking ${WHITE}${CONFIGFILE}${NORMAL}:" && /usr/bin/unp $CONFIGFILE $EXTRACTOPTIONS ; eend $?
   fi
+
+ fi
+}
+# }}}
+
+# {{{ remote scripts
+config_netscript() {
+ if checkbootparam 'netscript' ; then
+  CONFIG="$(getbootparam 'netscript' 2>>$DEBUG)"
+  SCRIPTFILE='/tmp/netscript.grml'
+
+  if get_remote_file ${CONFIG} ${SCRIPTFILE} ; then
+    chmod +x ${SCRIPTFILE}
+    einfo "Running ${WHITE}${SCRIPTFILE}${NORMAL}:" && ${SCRIPTFILE} ; eend $?
+  fi
+
  fi
 }
 # }}}
@@ -1852,8 +1903,8 @@ create_mnt_dirs(){
 config_x_startup(){
 # make sure we start X only if startx is used *before* a nostartx option
 # so it's possible to disable automatic X startup using nostart
-if checkbootparam 'startx' && ! grep -q 'startx.*nostartx' "$CMDLINE" ; then
- if [ -x /usr/X11R6/bin/X ] ; then
+if checkbootparam 'startx' && ! echo "$CMDLINE" | grep -q 'startx.*nostartx' ; then
+ if [ -x $(which X) ] ; then
   if [ -z "$INSTALLED" ] ; then
    WINDOWMANAGER="$(getbootparam 'startx' 2>>$DEBUG)"
    if [ -z "$WINDOWMANAGER" ] ; then
@@ -1917,6 +1968,7 @@ config_finddcsdir() {
 #  - If myconfig=foo is set on the command line, $dcs-dir is set to
 #    foo, even if a GRMLCFG partition is present.
 DCSDIR=""
+DCSMP="/mnt/grml"
 if checkbootparam 'noautoconfig' || checkbootparam 'forensic' ; then
   ewarn "Skipping running automount of device(s) labeled GRMLCFG as requested." ; eend 0
 else
@@ -1924,7 +1976,7 @@ else
     if checkbootparam 'myconfig' ; then
       DCSDEVICE="$(getbootparam 'myconfig' 2>>$DEBUG)"
       if [ -z "$DCSDEVICE" ]; then
-        einfo "No device for bootoption myconfig provided." ; eend 1
+        eerror "Error: No device for bootoption myconfig provided." ; eend 1
       fi # [ -z "$DCSDEVICE" ]
     elif checkvalue $CONFIG_MYCONFIG; then # checkbootparam myconfig
       einfo "Searching for device(s) labeled with GRMLCFG. (Disable this via boot option: noautoconfig)" ; eend 0
@@ -1937,28 +1989,37 @@ else
         fi
       fi
       DCSDEVICE=$(blkid -t LABEL=GRMLCFG | head -1 | awk -F: '{print $1}')
+      DCSMP="/mnt/grmlcfg"
+      eoutdent
     fi
-    if [ -n "$DCSDEVICE" ]; then
+
+    # if not specified/present then assume default:
+    if [ -z "$DCSDEVICE" ]; then
+      DCSDIR="/live/image"
+    else
+      eindent
       einfo "debs, config, scripts are read from $DCSDEVICE." ; eend 0
       DCSDIR="$(< /proc/mounts awk -v DCSDEV=$DCSDEVICE '{if ($1 == DCSDEV) { print $2 }}')"
       if [ -n "$DCSDIR" ]; then
-        einfo "$DCSDEVICE already mounted on $DCSDIR"; eend 0
+        ewarn "$DCSDEVICE already mounted on $DCSDIR"; eend 0
       else
-        [ -d /mnt/grml ] || mkdir /mnt/grml
-        umount /mnt/grml 1>>$DEBUG 2>&1 # make sure it is not mounted
-        mount -o ro -t auto $DCSDEVICE  /mnt/grml ; RC="$?"
+        [ -d $DCSMP ] || mkdir $DCSMP
+        umount $DCSMP 1>>$DEBUG 2>&1 # make sure it is not mounted
+        mount -o ro -t auto $DCSDEVICE  $DCSMP ; RC="$?"
         if [[ $RC == 0 ]]; then
-          einfo "Successfully mounted $DCSDEVICE to /mnt/grml (readonly)." ; eend 0
-          eindent
+          einfo "Successfully mounted $DCSDEVICE to $DCSMP (readonly)." ; eend 0
+        else
+          eerror "Error: mounting $DCSDEVICE to $DCSMP (readonly) failed." ; eend 1
         fi
-        DCSDIR="/mnt/grml"
+        DCSDIR="$DCSMP"
       fi
+      eoutdent
     fi
   fi
 fi
 
-if [ -n "$DCSDIR" ]; then
-  einfo "Debs, config, scripts will be read from $DCSDIR." ; eend 0
+if [ -n "$DCSDIR" -a "$DCSDIR" != "/live/image" ] ; then
+  einfo "Debs, config, scripts (if present) will be read from $DCSDIR." ; eend 0
 elif checkbootparam 'debs' || checkbootparam 'config' || checkbootparam 'scripts'; then
   einfo "Debs, config, scripts will be read from the live image directly." ; eend 0
 fi
@@ -2004,28 +2065,38 @@ fi
 }
 
 config_scripts(){
-if checkbootparam 'scripts' ; then
+if checkbootparam 'scripts' || [ "$DCSMP" = "/mnt/grmlcfg" ]; then
    SCRIPTS="$(getbootparam 'scripts' 2>>$DEBUG)"
-   if [ -z "$SCRIPTS" ]; then
+   if [ -d ${DCSDIR}/scripts ] && [ -z "$SCRIPTS" ]; then
      SCRIPTS="$(cd ${DCSDIR}/scripts; /bin/ls -1d [Gg][Rr][Mm][Ll].[Ss][Hh] 2>>$DEBUG)"
    fi
    if ! echo $SCRIPTS | grep -q '/'; then
      # backwards compatibility: if no path is given get scripts from scripts/
      SCRIPTS="scripts/$SCRIPTS"
    fi
-   SCRIPTS="${DCSDIR}/$SCRIPTS"
-   if [ -d "$SCRIPTS" ]; then
-     einfo "Bootparameter scripts found. Trying to execute from directory ${SCRIPTS}:"
-     run-parts $SCRIPTS
-   else
-     einfo "Bootparameter scripts found. Trying to execute ${SCRIPTS}:"
-     sh -c $SCRIPTS
+   if [ "$DCSMP" = "/mnt/grmlcfg" ]; then
+     # we are executing from a GRMLCFG labeled fs
+     # kick everything we have done before and start over
+     SCRIPTS="$(cd ${DCSDIR}; /bin/ls -1d [Gg][Rr][Mm][Ll].[Ss][Hh] 2>>$DEBUG)"
+   fi
+   if [ -n "$SCRIPTS" ]; then
+     SCRIPTS="${DCSDIR}/$SCRIPTS"
+     if [ "$DCSMP" = "/mnt/grmlcfg" ]; then
+       einfo "Trying to execute ${SCRIPTS}"
+       sh -c $SCRIPTS
+     elif [ -d "$SCRIPTS" ]; then
+       einfo "Bootparameter scripts found. Trying to execute from directory ${SCRIPTS}:"
+       run-parts $SCRIPTS
+     else
+       einfo "Bootparameter scripts found. Trying to execute ${SCRIPTS}:"
+       sh -c $SCRIPTS
+     fi
    fi
 fi
 }
 
 config_config(){
-if checkbootparam 'config' ; then
+if checkbootparam 'config' || [ "$DCSMP" = "/mnt/grmlcfg" ]; then
   CONFIG="$(getbootparam 'config' 2>>$DEBUG)"
   if [ -z "$CONFIG" ]; then
     CONFIG="$(cd ${DCSDIR}; ls -1d [Cc][Oo][Nn][Ff][Ii][Gg].[Tt][Bb][Zz] 2>>$DEBUG)"
@@ -2045,9 +2116,9 @@ if checkbootparam 'config' ; then
     fi
   fi
 fi
-# umount /mnt/grml if it was mounted by finddcsdir
+# umount $DCSMP if it was mounted by finddcsdir
 # this doesn't really belong here
-grep -q '/mnt/grml' /proc/mounts && umount /mnt/grml
+grep -q '$DCSMP' /proc/mounts && umount $DCSMP
 }
 # }}}
 
@@ -2419,7 +2490,7 @@ config_tohd()
 # {{{ grml2hd: automatic installation
 config_grml2hd(){
 
-if stringinstring "BOOT_IMAGE=grml2hd " "$CMDLINE" ; then
+if checkbootparam "BOOT_IMAGE=grml2hd" ; then
 
 if checkbootparam 'user' ; then
    NEWUSER=''
@@ -2464,14 +2535,14 @@ else
    ewarn "There was an error adjusting /etc/grml2hd/config. Skipping execution of grml2hd for security reasons." ; eend 1
 fi
 
-fi # if stringinstring "BOOT_IMAGE=grml2hd ...
+fi # if checkbootparam "BOOT_IMAGE=grml2hd ...
 }
 # }}}
 
 # {{{ debootstrap: automatic installation
 config_debootstrap(){
 
-if stringinstring "BOOT_IMAGE=debian2hd " "$CMDLINE" ; then
+if checkbootparam "BOOT_IMAGE=debian2hd" ; then
 
 einfo "Bootoption debian2hd found. Setting up environment for automatic installation via grml-debootstrap." ; eend 0
 
@@ -2556,7 +2627,7 @@ screen /usr/bin/grml-debootstrap_noninteractive
 einfo "Invoking a shell, just exit to continue booting..."
 /bin/zsh
 
-fi # stringinstring "BOOT_IMAGE=debian2hd
+fi # checkbootparam "BOOT_IMAGE=debian2hd
 }
 # }}}