startx: share selected window manager via /var/run/grml-x/window-manager
[grml-autoconfig.git] / autoconfig.functions
index 3fc0232..8ae71d1 100755 (executable)
@@ -194,45 +194,13 @@ fi
 
 # {{{ source lsb-functions , color handling
 if checkbootparam 'nocolor'; then
-  RC_NOCOLOR=yes
   . /etc/grml/lsb-functions
   einfo "Disabling colors in bootsequence as requested on commandline." ; eend 0
 else
   . /etc/grml/lsb-functions
-  . /etc/grml_colors
 fi
 # }}}
 
-# {{{ debug
-config_debug(){
- checkbootparam 'debug'            && BOOTDEBUG="yes"
- checkbootparam "BOOT_IMAGE=debug" && BOOTDEBUG="yes"
-
- rundebugshell(){
-  if [ -n "$BOOTDEBUG" ]; then
-     einfo "Starting intermediate shell stage $stage as requested by \"debug\" option."
-     if [ grep -q "debug=noscreen" "$CMDLINE" ] ; then
-        einfo "Notice that the shell does not provide job handling: ctrl-z, bg and fg won't work!"
-        einfo "Just exit the shell to continue boot process..."
-        /bin/zsh
-     else
-        eindent
-        if [ -r /etc/grml/screenrc ] ; then
-           einfo "Starting GNU screen to be able to use a full featured shell environment."
-           einfo "Just exit the shells (and therefore screen) to continue boot process..."
-           /bin/zsh -c "screen -c /etc/grml/screenrc"
-        else
-           einfo "Notice that the shell does not provide job handling: ctrl-z, bg and fg won't work!"
-           einfo "Just exit the shell to continue boot process..."
-           /bin/zsh
-        fi
-        eoutdent
-     fi
-  fi
- }
-}
-# }}}
-
 # {{{ log
 config_log(){
 if checkbootparam 'log' || checkbootparam 'debug' ; then
@@ -542,6 +510,27 @@ config_kernel(){
 }
 # }}}
 
+# {{{ secure boot
+config_secureboot(){
+  if [ -x /usr/bin/mokutil ] ; then
+    local secstate=$(mokutil --sb-state 2>/dev/null) # "SecureBoot enabled"
+    if [ -n "$secstate" ] ; then
+      einfo "SecureBoot is enabled" ; eend 0
+    else
+      einfo "SecureBoot not detected" ; eend 0
+    fi
+  else
+    if modprobe efivars &>/dev/null ; then
+      if od -An -t u1 /sys/firmware/efi/vars/SecureBoot-*/data 2>/dev/null | grep -q 1 ; then
+        einfo "SecureBoot is enabled" ; eend 0
+      else
+        einfo "SecureBoot not detected" ; eend 0
+      fi
+    fi
+  fi
+}
+# }}}
+
 # {{{ timezone
 config_timezone(){
  # don't touch the files if running from harddisk:
@@ -571,6 +560,11 @@ config_timezone(){
 # activate serial console {{{
 config_console(){
 if checkbootparam 'console'; then
+  # this hack is no longer necessary with systemd
+  if $SYSTEMD ; then
+    return
+  fi
+
   local line
   local ws
   ws='  '
@@ -699,21 +693,26 @@ fi
 
 # {{{ ACPI
 config_acpi(){
-if checkbootparam 'noacpi'; then
-  ewarn "ACPI: Not loading modules as requested by boot option \"noacpi\"." ; eend 0
-elif checkbootparam 'nogrmlacpi' ; then
-  ewarn "ACPI: Not loading modules as requested by boot option \"nogrmlacpi\"." ; eend 0
-elif [ ! -d /proc/acpi ] ; then
-  ewarn "ACPI: Kernel support not present." ; eend 0
-else
-  einfo "ACPI: Loading modules (disable with boot option noacpi / nogrmlacpi): "
-  eindent
-  found=""
-  for a in /lib/modules/$KERNEL/kernel/drivers/acpi/*; do
-    basename="${a##*/}"
-    basename="${basename%%.*}"
-    case "$basename" in *_acpi)
-     egrep -qi "${basename%%_acpi}" /proc/acpi/dsdt 2>>$DEBUG || continue ;;
+  if $SYSTEMD ; then
+    echo "systemd detected, no acpi(d) stuff needed." >>"$DEBUG"
+    return
+  fi
+
+  if checkbootparam 'noacpi'; then
+    ewarn "ACPI: Not loading modules as requested by boot option \"noacpi\"." ; eend 0
+  elif checkbootparam 'nogrmlacpi' ; then
+    ewarn "ACPI: Not loading modules as requested by boot option \"nogrmlacpi\"." ; eend 0
+  elif [ ! -d /proc/acpi ] ; then
+    ewarn "ACPI: Kernel support not present." ; eend 0
+  else
+    einfo "ACPI: Loading modules (disable with boot option noacpi / nogrmlacpi): "
+    eindent
+    found=""
+    for a in /lib/modules/$KERNEL/kernel/drivers/acpi/*; do
+      basename="${a##*/}"
+      basename="${basename%%.*}"
+      case "$basename" in *_acpi)
+        egrep -qi "${basename%%_acpi}" /proc/acpi/dsdt 2>>$DEBUG || continue ;;
     esac
     modprobe $basename >>$DEBUG 2>&1 && found="yes"
     local BASE="$BASE $basename"
@@ -893,8 +892,7 @@ else
       eindent
         SKIP_CPU_GOVERNOR=1
         oldIFS="$IFS"
-        IFS="
-"
+        IFS=$'\n'
          for line in $(grep FATAL "$LOADCPUFREQ" | sed 's/.*FATAL: //; s/ (.*)//') ; do
              eerror "$line" ; eend $RC
          done
@@ -1108,83 +1106,82 @@ fi
 
 # {{{ Sound
 config_mixer () {
-   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
-         ewarn "No soundcard present, skipping mixer settings therefore."
-         eend 0
-         return
+  if ! [ -x /usr/bin/amixer ] ; then
+    logger -t grml-autoconfig "amixer binary not available"
+    return
+  fi
+
+  if ! [ -r /proc/asound/cards ] ; then
+    ewarn "No soundcard present, skipping mixer settings therefore."
+    eend 0
+    return
+  fi
+
+  for card in $(cat /proc/asound/cards| grep -e '^\s*[0-9]' | awk '{print $1}') ; do
+    einfo "Configuring soundcard \"$(awk -F\[ '/^ *'$card' \[/{ FS=" "; $0=$2; print $1}' < /proc/asound/cards)\""
+    eindent
+
+    if checkbootparam 'vol' ; then
+      VOL="$(getbootparam 'vol' 2>>$DEBUG)"
+      if [ -z "$VOL" ] ; then
+        eerror "Bootoption vol found but no volume level/parameter given. Using defaults (75%)."
+        VOL='75'
+        eend 1
+      fi
+    else
+      VOL='75'
+    fi
+
+    if checkbootparam 'nosound' ; then
+      einfo "Muting sound devices on request."
+      ERROR=$(amixer -q set Master mute)
+      RC=$?
+      if [ -n "$ERROR" ] ; then
+        eindent
+        eerror "Problem muting sound devices: $ERROR"
+        eoutdent
       fi
+      eend $RC
+    elif [ -z "$INSTALLED" ] ; then
+      einfo "Setting mixer volumes to level ${WHITE}${VOL}${NORMAL}."
 
-      for card in $(cat /proc/asound/cards| grep -e '^\s*[0-9]' | awk '{print $1}') ; do
-         einfo "Configuring soundcard \"$(awk -F\[ '/^ *'$card' \[/{ FS=" "; $0=$2; print $1}' < /proc/asound/cards)\""
-         eindent
+      if checkbootparam 'micvol' ; then
+        MICVOL="$(getbootparam 'micvol' 2>>$DEBUG)"
+        einfo "Setting microphone to ${WHITE}${MICVOL}${NORMAL}."
+      else
+        MICVOL=0
+      fi
 
-         if checkbootparam 'vol' ; then
-            VOL="$(getbootparam 'vol' 2>>$DEBUG)"
-            if [ -z "$VOL" ] ; then
-               eerror "Bootoption vol found but no volume level/parameter given. Using defaults (75%)."
-               VOL='75'
-               eend 1
-            fi
-         else
-            VOL='75'
-         fi
-
-         if checkbootparam 'nosound' ; then
-            einfo "Muting sound devices on request."
-            ERROR=$(amixer -q set Master mute)
-            RC=$?
-            if [ -n "$ERROR" ] ; then
-               eindent
-               eerror "Problem muting sound devices: $ERROR"
-               eoutdent
-            fi
-            eend $RC
-         elif [ -z "$INSTALLED" ] ; then
-            einfo "Setting mixer volumes to level ${WHITE}${VOL}${NORMAL}."
+      CONTROLS=$(amixer -c $card scontrols | awk -F"Simple mixer control " '{print $2}')
+      IFSOLD=${IFS:-}
+      IFS=$'\n'
+      for CONTROL in ${=CONTROLS} ; do
+        # such devices can not be controlled with amixer ... unmute
+        [[ "$CONTROL" == *Console* ]] && continue
 
-            if checkbootparam 'micvol' ; then
-               MICVOL="$(getbootparam 'micvol' 2>>$DEBUG)"
-               einfo "Setting microphone to ${WHITE}${MICVOL}${NORMAL}."
-            else
-               MICVOL=0
-            fi
+        if ! echo "${CONTROL}" | grep -q -i "mic" ; then
+          if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*pswitch' ; then
+            amixer -c $card -q set "${CONTROL}" unmute
+          fi
+          if amixer -c $card sget "${CONTROL}" | grep -q -P 'Capabilities:.*(pvolume| volume)' ; then
+            amixer -c $card -q set "${CONTROL}" "${VOL}"%
+          fi
+        fi
 
-            CONTROLS=$(amixer -c $card scontrols | awk -F"Simple mixer control " '{print $2}')
-            IFSOLD=${IFS:-}
-            IFS='
-'
-            for CONTROL in ${=CONTROLS} ; do
-               # such devices can not be controlled with amixer ... unmute
-               [[ "$CONTROL" == *Console* ]] && continue
-
-               if ! echo "${CONTROL}" | grep -q -i "mic" ; then
-                   if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*pswitch' ; then
-                      amixer -c $card -q set "${CONTROL}" unmute
-                   fi
-                   if amixer -c $card sget "${CONTROL}" | grep -q -P 'Capabilities:.*(pvolume| volume)' ; then
-                      amixer -c $card -q set "${CONTROL}" "${VOL}"%
-                   fi
-               fi
-
-               if [ ${MICVOL} -ne 0 ] ; then
-                  if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*cswitch' ; then
-                     amixer -c $card -q set "${CONTROL}" unmute
-                  fi
-                  if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*cvolume' ; then
-                     amixer -c $card -q set "${CONTROL}" $MICVOL%
-                  fi
-                  eend $?
-               fi
-            done
-            IFS=$IFSOLD
-         fi # checkbootparam 'nosound'
-         eoutdent
+        if [ ${MICVOL} -ne 0 ] ; then
+          if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*cswitch' ; then
+            amixer -c $card -q set "${CONTROL}" unmute
+          fi
+          if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*cvolume' ; then
+            amixer -c $card -q set "${CONTROL}" $MICVOL%
+          fi
+          eend $?
+        fi
       done
-   fi
+      IFS=$IFSOLD
+    fi # checkbootparam 'nosound'
+    eoutdent
+  done
 }
 # }}}
 
@@ -1202,18 +1199,18 @@ config_syslog(){
 
 # {{{ gpm
 config_gpm(){
- if checkbootparam 'nogpm'; then
-  ewarn "Not starting GPM as requested on boot commandline." ; eend 0
- else
-   if ! [ -r /dev/input/mice ] ; then
 if checkbootparam 'nogpm'; then
+    ewarn "Not starting GPM as requested on boot commandline." ; eend 0
 else
+    if ! [ -r /dev/input/mice ] ; then
       eerror "No mouse found - not starting GPM." ; eend 1
-   else
+    else
       einfo "Starting gpm in background."
       service_wrapper gpm start >>$DEBUG &
       # ( while [ ! -e /dev/psaux ]; do sleep 5; done; /etc/init.d/gpm start >>$DEBUG ) &
       eend 0
-   fi
- fi
+    fi
 fi
 }
 # }}}
 
@@ -1302,42 +1299,9 @@ config_netscript() {
 }
 # }}}
 
-# {{{ stats
-config_stats() {
- if ! checkbootparam 'nostats' ; then
-   BASE_URL="http://stats.grml.org/report/"
-   ACTION_NAME=Boot
-
-   HOST_ID=$(cat /proc/sys/kernel/random/boot_id)
-
-   grep -q " lm " /proc/cpuinfo && HAS_64BIT="1" || HAS_64BIT="0"
-   DATE_STRING=$(date +'h=%H&m=%M&s=%S')
-   [ -e /etc/grml_version ] && VERSION=$(cat /etc/grml_version) || \
-     VERSION=$(lsb_release -d | awk -F: '{gsub(/^[ \t]+/, "", $2); print $2}')
-
-   PARAMS="$( echo "$CMDLINE" | sed -e 's/=[^ ]*/=x/g' | tr " " "\n"|sort|tr "\n" " " )"
-
-   echo "$CMDLINE" | grep -q -e "fetch" -e "nfsroot" && BOOT="remote"
-   [ -z "$BOOT" ] && BOOT="local"
-
-   ADDITIONAL_PARAMS=""
-   ( [ -n "$COLUMNS" ] && [ -n "$LINES" ] ) && \
-     ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS&res=$((COLUMNS * 8))x$((LINES * 16))"
-
-   URI='$BASE_URL?action=${ACTION_NAME}\&$DATE_STRING\&unique_id=${HOST_ID}\&support_64bit=$HAS_64BIT\&version=$VERSION\&bootup=$BOOT\&params=$PARAMS$ADDITIONAL_PARAMS'
-
-   get_remote_file "$URI" "/dev/null"  >/dev/null 2>&1 &!
- fi
-}
-# }}}
-
 # {{{ start X window system via grml-x
 config_x_startup(){
 
-  if $SYSTEMD ; then
-    ewarn "The startx boot option isn't yet supported via systemd, sorry." ; eend 0
-    return
-  fi
 
 # 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
@@ -1352,17 +1316,25 @@ if checkbootparam 'startx' && ! echo "$CMDLINE" | grep -q 'startx.*nostartx' ; t
    fi
    einfo "Setting up and invoking grml-x ${WINDOWMANAGER}. Just exit X windows system to get full featured consoles."
    config_userlocal
- cat>|/etc/init.d/xstartup<<EOF
+   if $SYSTEMD ; then
+     if [ -n "$WINDOWMANAGER" ] ; then
+       mkdir -p /var/run/grml-x/
+       echo "$WINDOWMANAGER" > /var/run/grml-x/window-manager
+     fi
+     chvt 7
+     return
+   fi
+   cat>|/etc/init.d/startx<<EOF
 #!/bin/sh
-su $localuser -c "/usr/bin/grml-x ${WINDOWMANAGER}"
+su "${localuser}" -c "/usr/bin/grml-x ${WINDOWMANAGER}"
 EOF
-   chmod 755 /etc/init.d/xstartup
+   chmod 755 /etc/init.d/startx
 
-   # adjust inittab for xstartup
+   # adjust inittab for startx
    if grep -q '^6:' /etc/inittab ; then
-      sed -i 's|^6:.*|6:2345:respawn:/bin/zsh --login -c "/etc/init.d/xstartup ; /usr/share/grml-scripts/run-welcome" >/dev/tty6 2>\&1 </dev/tty6|' /etc/inittab
+      sed -i 's|^6:.*|6:2345:respawn:/bin/zsh --login -c "/etc/init.d/startx ; /usr/share/grml-scripts/run-welcome" >/dev/tty6 2>\&1 </dev/tty6|' /etc/inittab
    else # just append tty6 to inittab if no definition is present:
-      echo '6:2345:respawn:/bin/zsh --login -c "/etc/init.d/xstartup ; /usr/share/grml-scripts/run-welcome" >/dev/tty6 2>&1 < /dev/tty6' >> /etc/inittab
+      echo '6:2345:respawn:/bin/zsh --login -c "/etc/init.d/startx ; /usr/share/grml-scripts/run-welcome" >/dev/tty6 2>&1 < /dev/tty6' >> /etc/inittab
    fi
 
    /sbin/telinit q ; eend $?
@@ -1620,15 +1592,18 @@ config_swraid(){
 
        if ! checkbootparam 'swraid' ; then
           eindent
-          einfo "Just run 'Start mdadm-raid' to assemble md arrays or boot using 'swraid' as bootoption for autostart."
+          if $SYSTEMD ; then
+            einfo "Just run 'mdadm --assemble --scan' to assemble md arrays or boot using 'swraid' as bootoption for autostart."
+          else
+            einfo "Just run 'Start mdadm-raid' to assemble md arrays or boot using 'swraid' as bootoption for autostart."
+          fi
           eoutdent
        else
           einfo "Bootoption swraid found. Searching for software RAID arrays:"
           eindent
            IFSOLD=${IFS:-}
-           IFS='
-'
-           for line in $(mdadm --assemble --scan --auto=yes --symlink=no 2>&1) ; do
+           IFS=$'\n'
+           for line in $(mdadm --assemble --scan 2>&1) ; do
                case $line in
                  *'No arrays found'*)
                    ewarn "$line" ; eend 0
@@ -1648,8 +1623,7 @@ config_swraid(){
                ewarn "No active arrays found" ; eend 0
             else
                IFSOLD=${IFS:-}
-               IFS='
-'
+               IFS=$'\n'
                for line in $(grep '^md[0-9]' /proc/mdstat) ; do
                    einfo "active arrays: $line" ; eend 0
                done
@@ -1683,8 +1657,7 @@ config_dmraid(){
     [ -n "$1" ] || return 1
 
     IFSOLD=${IFS:-}
-    IFS='
-'
+    IFS=$'\n'
     eindent
 
     for line in $(dmraid $1 ; echo errcode:$?); do
@@ -1747,11 +1720,21 @@ config_lvm(){
        if lvdisplay 2>&1 | grep -v 'No volume groups found' >/dev/null 2>&1 ; then
           einfo "You seem to have logical volumes (LVM) on your system."
           eindent
-          einfo "Just run 'Start lvm2' to activate them or boot using 'lvm' as bootoption for autostart."
+          if $SYSTEMD ; then
+            einfo "Just run 'Start lvm2-pvscan@name' to activate LV or VG 'name' or boot using 'lvm' as bootoption for autostart."
+          else
+            einfo "Just run 'Start lvm2' to activate them or boot using 'lvm' as bootoption for autostart."
+          fi
           eend 0
           if checkbootparam 'lvm' ; then
-             einfo "Bootoption LVM found. Searching for logical volumes:"
-             service_wrapper lvm2 start ; eend $?
+             einfo "Bootoption LVM found. Searching for logical volumes and enabling them:"
+             if $SYSTEMD ; then
+               service_wrapper lvm2-lvmetad start
+               vgchange -ay
+               eend $?
+             else
+               service_wrapper lvm2 start ; eend $?
+             fi
           fi
           eoutdent
        fi
@@ -1967,7 +1950,6 @@ config_distri(){
 if checkbootparam 'distri'; then
   DISTRI="$(getbootparam 'distri' 2>>$DEBUG)"
   if [ -r "${LIVECD_PATH}"/desktop/"$DISTRI".jpg ] ; then
-     [ -n "$BOOTDEBUG" ] && einfo "Debug: bootoption distri found and file ${LIVECD_PATH}/desktop/${DISTRI} present" && eend 0
      # make sure the desktop.jpg file is not a symlink, so copying does not file then
      [ -L /usr/share/grml/desktop.jpg ] && rm /usr/share/grml/desktop.jpg
      cp "${LIVECD_PATH}"/desktop/"$DISTRI".jpg /usr/share/grml/desktop.jpg