Switch wording from 'CD' to 'live mode' where possible; tab cleanups
[grml-autoconfig.git] / autoconfig.functions
index fd699fc..90f1779 100755 (executable)
@@ -32,6 +32,16 @@ fi
 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.
+  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' ' ')"
+fi
+# }}}
+
 ### {{{ Utility Functions
 
 # Simple shell grep
@@ -97,13 +107,7 @@ mount_sys(){
 }
 # }}}
 
-# {{{ Read in boot parameters
-[ -f /proc/version ] || mount_proc # make sure we can access /proc/cmdline when sourcing this file too
-CMDLINE="$(cat /proc/cmdline)"
-[ -d "${LIVECD_PATH}"/bootparams/ ] && CMDLINE="$CMDLINE $(cat ${LIVECD_PATH}/bootparams/* | tr '\n' ' ')"
-# }}}
-
-# {{{ Check if we are running from the grml-CD or HD
+# {{{ Check if we are running in live mode or from HD
 INSTALLED=""
 [ -e /etc/grml_cd ] || INSTALLED="yes"
 
@@ -541,8 +545,8 @@ eoutdent
 
 # skip startup of w3m {{{
 config_fast(){
-if checkbootparam "fast"; then
-  ewarn "Bootoption fast detected. Skipping startup of w3m."
+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 $?
 fi
@@ -739,13 +743,14 @@ if checkbootparam "blacklist" ; then
  if [ -z "$INSTALLED" ]; then
   einfo "Bootoption blacklist found."
   BLACK="$(getbootparam 'blacklist' 2>>$DEBUG)"
+  BLACKLIST_FILE='/etc/modprobe.d/grml.conf'
   if [ -n "$BLACK" ] ; then
     for module in $(echo ${BLACK//,/ }) ; do
-        einfo "Blacklisting module ${module} via /etc/modprobe.d/grml."
-        echo "# begin entry generated by config_blacklist of grml-autoconfig" >> /etc/modprobe.d/grml
-        echo "blacklist $module"     >> /etc/modprobe.d/grml
-        echo "alias     $module off" >> /etc/modprobe.d/grml
-        echo "# end   entry generated by config_blacklist of grml-autoconfig" >> /etc/modprobe.d/grml ; eend $?
+        einfo "Blacklisting module ${module} via ${BLACKLIST_FILE}."
+        echo "# begin entry generated by config_blacklist of grml-autoconfig" >> "$BLACKLIST_FILE"
+        echo "blacklist $module"     >> "$BLACKLIST_FILE"
+        echo "alias     $module off" >> "$BLACKLIST_FILE"
+        echo "# end   entry generated by config_blacklist of grml-autoconfig" >> "$BLACKLIST_FILE" ; eend $?
     done
   else
    eerror "No given module for blacklist found. Blacklisting will not work therefore."
@@ -881,19 +886,48 @@ config_swspeak(){
 
 # {{{ support hardware synthesizer via speakup
 config_hwspeak(){
-   if checkbootparam hwspeak ; then
+   if checkbootparam speakup.synth ; then
+      einfo "Bootoption speakup.synth found."
+      eindent
+
+      module="$(getbootparam speakup.synth 2>>$DEBUG)"
+      if [ -z "$module" ] ; then
+         eerror "Sorry, no speakup module specified for bootoption speakup.synth."
+         flitewrapper "Sorry, no speakup module specified for bootoption speakup.synth."
+      else
+         einfo "Trying to load $module"
+         modprobe "speakup_${module}"
+         eend $?
+      fi
+
+      if [ -d /proc/speakup/ ] || grep -q speakup /proc/modules ; then
+         einfo "Kernel should support speakup now." ; eend 0
+         flitewrapper "Kernel should support speakup now."
+      else
+         eerror "Kernel or hardware do not seem to support speakup. Skipping hwspeak." ; eend 1
+         flitewrapper "Kernel or hardware do not seem to support speakup. Sorry."
+      fi
+
+      eoutdent
+
+   # hwspeak:
+   elif checkbootparam hwspeak ; then
       einfo "Bootoption hwspeak found."
 
       if [ ! -d /proc/speakup/ ] && ! grep -q speakup /proc/modules ; then
-         ewarnKernel does not support hardware speakup - trying to load kernel modules:" ; eend 0
+         ewarn "Kernel does not support hardware speakup - trying to load kernel modules:" ; eend 0
          eindent
-         for module in $(find "/lib/modules/${KERNEL}/extra/speakup/" -name \*.ko | \
-                         sed 's#.*speakup/##g ; s#.ko$##g' | \
-                         grep -ve speakup_soft -ve speakup_dummy | sort -u) ; do
-            einfo "Trying to load $module"
-            modprobe $module
-            eend $?
-         done
+         if ! [ -d "/lib/modules/${KERNEL}/extra/speakup/" ] ; then
+            eerror "Kernel does not provide speakup modules, sorry." ; eend 1
+         else
+           for module in $(find "/lib/modules/${KERNEL}/extra/speakup/" -name \*.ko | \
+                           sed 's#.*speakup/##g ; s#.ko$##g' | \
+                           grep -ve speakup_soft -ve speakup_dummy | sort -u) ; do
+              einfo "Trying to load $module"
+              modprobe $module
+              eend $?
+           done
+         fi
          eoutdent
       fi
 
@@ -1435,7 +1469,12 @@ if checkbootparam ssh ; then
 
    # finally check if we have a password we can use:
    if [ -n "$SSH_PASSWD" ] ; then
-      echo "grml:$SSH_PASSWD" | chpasswd -m
+      # chpasswd sucks, seriously.
+      if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
+        echo "grml:$SSH_PASSWD" | chpasswd -m
+      else
+        echo "grml:$SSH_PASSWD" | chpasswd
+      fi
    fi
 
    einfo 'Starting secure shell server in background.'
@@ -1551,17 +1590,6 @@ fi # checkbootparam home
 }
 # }}}
 
-# {{{ Check for scripts on CD-ROM
-config_cdrom_scripts(){
-if checkbootparam "script"; then
-   for script in "${LIVECD_PATH}"/scripts/* ; do
-       einfo " grml script found on CD, executing ${WHITE}${script}${NORMAL}."
-       . $script
-   done
-fi
-}
-# }}}
-
 # {{{ Sound
 config_mixer(){
 if ! [ -x /usr/bin/aumix ] ; then
@@ -1822,7 +1850,9 @@ create_mnt_dirs(){
 
 # {{{ start X window system via grml-x
 config_x_startup(){
-if checkbootparam startx ; then
+# 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 [ -z "$INSTALLED" ] ; then
    WINDOWMANAGER="$(getbootparam 'startx' 2>>$DEBUG)"
@@ -1857,12 +1887,12 @@ EOF
    fi
 
   else
-    eerror "We are not running from CD - startx will not work, skipping it.
-     Please use something like xdm, gdm or kdm for starting X on a harddisk system!" ; eend 1
+    eerror "We are not running in live mode - startx will not work, skipping it."
+    eerror " -> Please use something like xdm, gdm or kdm for starting X on a harddisk system!" ; eend 1
   fi
  else
-   eerror "/usr/X11R6/bin/X is not present on this grml flavour.
-   Boot parameter startx does not work therefore." ; eend 1
+   eerror "/usr/X11R6/bin/X is not present on this grml flavour."
+   eerror "  -> Boot parameter startx does not work therefore." ; eend 1
  fi
 fi
 }
@@ -1876,94 +1906,66 @@ if checkbootparam extract ; then
 fi
 }
 
-config_unpack(){
-MOUNTDEVICE="$1"
-MESSAGE="$2"
-
-if [ ! -b "$MOUNTDEVICE" ] ; then
-   return;
-fi
-
-
-[ -d /mnt/grml ] || mkdir /mnt/grml
-umount /mnt/grml 1>>$DEBUG 2>&1 # make sure it is not mounted
-mount -o ro -t auto $MOUNTDEVICE /mnt/grml ; RC="$?"
-if [[ $RC == 0 ]]; then
-   einfo "Successfully mounted $MOUNTDEVICE $MESSAGE to /mnt/grml (readonly)." ; eend 0
-   eindent
-   CONFIG=''
-   CONFIG="$(/bin/ls -1d /mnt/grml/[Cc][Oo][Nn][Ff][Ii][Gg].[Tt][Bb][Zz] 2>>$DEBUG)"
-   if checkbootparam config ; then
-      FILENAME="$(getbootparam 'config' 2>>$DEBUG)"
-      if [ -e /mnt/grml/${FILENAME} ] ; then
-         einfo "Using /mnt/grml/$FILENAME instead of config.tbz"
-         CONFIG="/mnt/grml/$FILENAME"
-      fi
-   fi
-   if [ -n "$CONFIG" ]; then
-     einfo "Found file ${WHITE}${CONFIG}${NORMAL} - trying to extract it."
-     cd /
-     unp $CONFIG $EXTRACTOPTIONS ; eend $?
-   else
-     ewarn "Sorry, could not find file $FILENAME on device $MOUNTDEVICE $MESSAGE label GRMLCFG." ; eend 1
-   fi
-
-   SCRIPT=''
-   SCRIPT="$(/bin/ls -1d /mnt/grml/[Gg][Rr][Mm][Ll].[Ss][Hh] 2>>$DEBUG)"
-   if [ -n "$SCRIPT" ]; then
-     einfo "Found script ${WHITE}${SCRIPT}${NORMAL} - trying to execute it."
-     $SCRIPT ; eend $?
-   fi
-   eoutdent
-   else
-   einfo "Could not mount $MOUNTDEVICE to /mnt/grml - sorry." ; eend 1
-fi # mount $MOUNTDEVICE
-grep -q '/mnt/grml' /proc/mounts && umount /mnt/grml
-
-}
-
-config_automount(){
+config_finddcsdir() {
+#  - If no GRMLCFG partition is found and noautoconfig is _not_ given
+#    on the command line, nothing is changed and the dcs files are
+#    searched within the .iso, $dcs-dir is set to the root directory
+#    within the .iso
+#  - If a GRMLCFG partition is found, $dcs-dir is set to the root of
+#    the GRMLCFG partition unless noautoconfig is set. If noautoconfig is
+#    set, $dcs-dir is set to the root directory within the .iso.
+#  - If myconfig=foo is set on the command line, $dcs-dir is set to
+#    foo, even if a GRMLCFG partition is present.
+DCSDIR=""
 if checkbootparam noautoconfig || checkbootparam forensic ; then
   ewarn "Skipping running automount of device(s) labeled GRMLCFG as requested." ; eend 0
 else
- if [ -z "$INSTALLED" ] ; then
-     einfo "Searching for device(s) labeled with GRMLCFG. (Disable this via boot option: noautoconfig)" ; eend 0
-     eindent
-   # We do need the following fix so floppy disk is available to blkid in any case :-/
-     if [ -r /dev/fd0 ] ; then
+  if [ -z "$INSTALLED" ] ; then
+    if checkbootparam myconfig ; then
+      DCSDEVICE="$(getbootparam 'myconfig' 2>>$DEBUG)"
+      if [ -z "$DCSDEVICE" ]; then
+        einfo "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
+      eindent
+      # We do need the following fix so floppy disk is available to blkid in any case :-/
+      if [ -r /dev/fd0 ] ; then
         einfo "Floppy device detected. Trying to access floppy disk."
-   #     dd if=/dev/fd0 of=/dev/null bs=512 count=1 1>>$DEBUG 2>&1
         if timeout 4 dd if=/dev/fd0 of=/dev/null bs=512 count=1 1>>$DEBUG 2>&1 ; then
            blkid /dev/fd0 1>>$DEBUG 2>&1
         fi
-     fi
-     DEVICE=$(blkid -t LABEL=GRMLCFG | head -1 | awk -F: '{print $1}')
-     config_unpack "$DEVICE" "labeled GRMCFG"
+      fi
+      DCSDEVICE=$(blkid -t LABEL=GRMLCFG | head -1 | awk -F: '{print $1}')
+    fi
+    if [ -n "$DCSDEVICE" ]; then
+      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
+      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 $MOUNTDEVICE /mnt/grml ; RC="$?"
+        if [[ $RC == 0 ]]; then
+          einfo "Successfully mounted $DCSDEVICE to /mnt/grml (readonly)." ; eend 0
+          eindent
+        fi
+        DCSDIR="/mnt/grml"
+      fi
+    fi
   fi
 fi
-}
 
-config_myconfig(){
-
-if checkbootparam "config" ; then
-  CONFIG="$(getbootparam 'config' 2>>$DEBUG)"
-  [ -z "$CONFIG" ] && CONFIG='config.tbz'
-  einfo "Bootoption config found. config is set to: $CONFIG"
-  eindent
-    einfo "Trying to extract configuration file ${CONFIG}:"
-    cd / && unp "${LIVECD_PATH}"/config/$CONFIG $EXTRACTOPTIONS ; eend $?
-  eoutdent
+if [ -n "$DCSDIR" ]; then
+  einfo "Debs, config, scripts will be read from $DCSDIR." ; eend 0
+else
+  einfo "Debs, config, scripts will be read from the live image directly." ; eend 0
 fi
+}
 
-if checkbootparam myconfig ; then
- MOUNTDEVICE="$(getbootparam 'myconfig' 2>>$DEBUG)"
- if [ -n "$MOUNTDEVICE" ]; then
-     config_unpack "$MOUNTDEVICE"
- else
-   einfo "Sorry, no device for bootoption myconfig provided. Skipping." ; eend 1
- fi # [ -n "$MOUNTDEVICE" ]
-fi # checkbootparam myconfig
 
+config_partconf() {
 if checkbootparam "partconf" ; then
  MOUNTDEVICE="$(getbootparam 'partconf' 2>>$DEBUG)"
  if [ -n "$MOUNTDEVICE" ]; then
@@ -1991,18 +1993,61 @@ fi
 config_debs(){
 if checkbootparam "debs" ; then
    DEBS="$(getbootparam 'debs' 2>>$DEBUG)"
+   if ! echo $DEBS | grep -q '/'; then
+     # backwards compatibility: if no path is given get debs from debs/
+     DEBS="debs/$DEBS"
+   fi
    einfo "Tring to install debian package(s) ${DEBS}"
-   dpkg -i "${LIVECD_PATH}"/debs/$DEBS* ; eend $?
+   DEBS="$(eval echo ${DCSDIR}/$DEBS)"
+   dpkg -i $DEBS ; eend $?
 fi
 }
 
 config_scripts(){
 if checkbootparam "scripts" ; then
    SCRIPTS="$(getbootparam 'scripts' 2>>$DEBUG)"
-   [ -z "$SCRIPTS" ] && SCRIPTS='grml.sh'
-   einfo "Bootparameter scripts found. Trying to execute ${SCRIPTS}:"
-   sh -c "${LIVECD_PATH}"/scripts/$SCRIPTS ; eend $?
+   if [ -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
+   fi
+fi
+}
+
+config_config(){
+if checkbootparam config ; 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)"
+  fi
+  if [ -n "$CONFIG" ]; then
+    if [ -d "${DCSDIR}/${CONFIG}" ] ; then
+      einfo "Taking configuration from directory ${DCSDIR}/${CONFIG}"
+
+      cp -a ${DCSDIR}/${CONFIG}/* /
+    elif [ -f "${DCSDIR}/${CONFIG}" ]; then
+      einfo "Extracting configuration from file ${DCSDIR}/${CONFIG}"
+
+      cd /
+      unp ${DCSDIR}/${CONFIG} $EXTRACTOPTIONS ; eend $?
+    else
+      ewarn "Sorry, could not find configuration file or directory ${DCSDIR}/${FILENAME}." ; eend 1
+    fi
+  fi
 fi
+# umount /mnt/grml if it was mounted by finddcsdir
+# this doesn't really belong here
+grep -q '/mnt/grml' /proc/mounts && umount /mnt/grml
 }
 # }}}
 
@@ -2347,9 +2392,9 @@ config_tohd()
      fi
 
      if grep -q $TARGET /proc/mounts ; then
-       eerror "$TARGET already mounted, skipping execution of tohd therefore."
-       eend 1
-       return 1
+        eerror "$TARGET already mounted, skipping execution of tohd therefore."
+        eend 1
+        return 1
      fi
 
      local MOUNTDIR=$(mktemp -d)
@@ -2357,10 +2402,10 @@ config_tohd()
      if mount -o rw "$TARGET" "$MOUNTDIR" ; then
         einfo "Copyring live system to $TARGET - this might take a while"
         rsync -a --progress /live/image/live $MOUNTDIR
-       sync
-       umount "$MOUNTDIR"
+        sync
+        umount "$MOUNTDIR"
         eend $?
-       einfo "Booting with \"grml bootfrom=$TARGET\" should work now." ; eend 0
+        einfo "Booting with \"grml bootfrom=$TARGET\" should work now." ; eend 0
      else
         eerror "Error when trying to mount $TARGET, sorry."; eend 1
         return 1