config_finddcsdir: some minor message display improvements
[grml-autoconfig.git] / autoconfig.functions
index b8579b7..acf2def 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:
@@ -47,10 +48,14 @@ fi
 # Get a bootoption's parameter: read boot command line and either
 # echo last parameter's argument or return false.
 getbootparam(){
-  case "$CMDLINE" in
-    *$1=*)
-      result="${CMDLINE##* $1=}"
-      result="${result%%[      ]*}"
+  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?
@@ -61,13 +66,16 @@ getbootparam(){
 # Check boot commandline for specified option
 checkbootparam(){
   [ -n "$1" ] || ( echo "Error: missing argument to checkbootparam()" ; return 1 )
-  # make sure we match beginning and end of line as well
-  # but don't match 'vg' if we want to check for 'vga'
-  if echo "$CMDLINE" | grep -qw "$1" ; then
-     return 0
-  else
-     return 1
-  fi
+  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
@@ -1785,31 +1793,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
 }
 # }}}
@@ -1850,8 +1886,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
@@ -1922,7 +1958,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
@@ -1935,28 +1971,36 @@ else
         fi
       fi
       DCSDEVICE=$(blkid -t LABEL=GRMLCFG | head -1 | awk -F: '{print $1}')
+      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="$?"
         if [[ $RC == 0 ]]; then
           einfo "Successfully mounted $DCSDEVICE to /mnt/grml (readonly)." ; eend 0
-          eindent
+        else
+          eerror "Error: mounting $DCSDEVICE to /mnt/grml (readonly) failed." ; eend 1
         fi
         DCSDIR="/mnt/grml"
       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