Don't warn if SecureBoot is not enabled, instead only inform about current state
[grml-autoconfig.git] / autoconfig.functions
index 735ba00..ccf12d6 100755 (executable)
@@ -23,6 +23,28 @@ umask 022
 # Ignore these signals in non-interactive mode: INT, TERM, SEGV
 [ -z "$PS1" ] && trap "" 2 3 11
 
+if [ "$(cat /proc/1/comm 2>/dev/null)" = "systemd" ] ; then
+  SYSTEMD=true
+else
+  SYSTEMD=false
+fi
+
+service_wrapper() {
+  if [ "$#" -lt 2 ] ; then
+    echo "Usage: service_wrapper <service> <action>" >&2
+    return 1
+  fi
+
+  local service="$1"
+  local action="$2"
+
+  if $SYSTEMD ; then
+    systemctl "$action" "$service"
+  else
+    /etc/init.d/"$service" "$action"
+  fi
+}
+
 # zsh stuff
 iszsh(){
 if [ -n "$ZSH_VERSION" ] ; then
@@ -172,12 +194,10 @@ 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
 # }}}
 
@@ -454,11 +474,21 @@ config_time(){
  if [ -z "$INSTALLED" ]; then
     # The default hardware clock timezone is stated as representing local time.
     UTC="--localtime"
-    grep -q "^UTC=" /etc/default/rcS || echo "UTC=no" >> /etc/default/rcS
-    checkbootparam 'utc'       >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=yes|" /etc/default/rcS
-    checkbootparam 'gmt'       >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=yes|" /etc/default/rcS
-    checkbootparam 'localtime' >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=no|"  /etc/default/rcS
-    grep -q -i "^UTC=yes" /etc/default/rcS && UTC="-u"
+
+    if [ -f /etc/default/rcS ] ; then
+      grep -q "^UTC=" /etc/default/rcS || echo "UTC=no" >> /etc/default/rcS
+      checkbootparam 'utc'       >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=yes|" /etc/default/rcS
+      checkbootparam 'gmt'       >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=yes|" /etc/default/rcS
+      checkbootparam 'localtime' >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=no|"  /etc/default/rcS
+      grep -q -i "^UTC=yes" /etc/default/rcS && UTC="-u"
+    # recent initscripts package versions don't ship /etc/default/rcS anymore, instead rely on /etc/adjtime
+    elif [ -f /etc/adjtime ] ; then
+      checkbootparam 'utc'       >>$DEBUG 2>&1 && sed -i "s/^LOCAL/UTC/" /etc/adjtime
+      checkbootparam 'gmt'       >>$DEBUG 2>&1 && sed -i "s/^LOCAL/UTC/" /etc/adjtime
+      checkbootparam 'localtime' >>$DEBUG 2>&1 && sed -i "s/^UTC$/LOCAL/" /etc/adjtime
+      grep -q "^UTC$" /etc/adjtime && UTC="-u"
+    fi
+
     # hwclock uses the TZ variable
     KTZ="$(getbootparam 'tz' 2>>$DEBUG)"
     [ -z "$KTZ" ] && [ -r /etc/timezone ] && KTZ=$(cat /etc/timezone)
@@ -510,6 +540,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:
@@ -539,6 +590,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='  '
@@ -667,21 +723,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"
@@ -693,7 +754,8 @@ else
   fi
   if ! pgrep acpid >/dev/null ; then
     einfo "Starting acpi daemon."
-    /etc/init.d/acpid start >>$DEBUG 2>&1 ; eend $?
+    service_wrapper acpid.socket start >>$DEBUG 2>&1 ; eend $?
+    service_wrapper acpid start >>$DEBUG 2>&1 ; eend $?
   else
     ewarn "acpi daemon already running."
     eend 0
@@ -846,7 +908,11 @@ if $VIRTUALBOX ; then
    return 0
 fi
 
-if [ -x /etc/init.d/loadcpufreq ] ; then
+if ! [ -x /etc/init.d/loadcpufreq ] ; then
+  ewarn "loadcpufreq init script not available, ignoring cpu frequency scaling."
+  eend 0
+  return 0
+else
    einfo "Trying to set up cpu frequency scaling:"
    eindent
    SKIP_CPU_GOVERNOR=''
@@ -926,8 +992,8 @@ if checkbootparam 'ssh' ; then
    fi
 
    einfo "Starting secure shell server in background for root and user $localuser"
-   /etc/init.d/rmnologin start >>$DEBUG 2>>$DEBUG
-   /etc/init.d/ssh start >>$DEBUG 2>>$DEBUG &
+   service_wrapper rmnologin start >>$DEBUG 2>>$DEBUG
+   service_wrapper ssh start >>$DEBUG 2>>$DEBUG &
    eend $?
 
 fi
@@ -1071,83 +1137,83 @@ 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
 
-      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 '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}."
 
-         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}."
+      if checkbootparam 'micvol' ; then
+        MICVOL="$(getbootparam 'micvol' 2>>$DEBUG)"
+        einfo "Setting microphone to ${WHITE}${MICVOL}${NORMAL}."
+      else
+        MICVOL=0
+      fi
 
-            if checkbootparam 'micvol' ; then
-               MICVOL="$(getbootparam 'micvol' 2>>$DEBUG)"
-               einfo "Setting microphone to ${WHITE}${MICVOL}${NORMAL}."
-            else
-               MICVOL=0
-            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
 
-            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
 }
 # }}}
 
@@ -1156,38 +1222,27 @@ config_syslog(){
  if checkbootparam 'nosyslog'; then
     ewarn "Not starting syslog daemon as requested on boot commandline." ; eend 0
  else
-    SYSLOGD=''
-    [ -x /etc/init.d/syslog-ng ] && SYSLOGD='syslog-ng'
-    [ -x /etc/init.d/rsyslog   ] && SYSLOGD='rsyslog'
-    [ -x /etc/init.d/dsyslog   ] && SYSLOGD='dsyslog'
-    [ -x /etc/init.d/sysklogd  ] && SYSLOGD='sysklogd'
-    [ -x /etc/init.d/inetutils-syslogd ] && SYSLOGD='inetutils-syslogd'
-
-    if [ -z "$SYSLOGD" ] ; then
-       eerror "No syslog daemon found." ; eend 1
-    else
-       einfo "Starting $SYSLOGD in background."
-       /etc/init.d/$SYSLOGD start >>$DEBUG &
-       eend 0
-    fi
+    einfo "Starting rsyslog in background."
+    service_wrapper rsyslog start >>$DEBUG &
+    eend 0
  fi
 }
 # }}}
 
 # {{{ 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."
-      /etc/init.d/gpm start >>$DEBUG &
+      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
 }
 # }}}
 
@@ -1202,10 +1257,10 @@ config_services(){
       # if they contain the keyword "DO_NO_RUN_IN_BACKGROUND".
       if grep -q 'DO_NO_RUN_IN_BACKGROUND' "/etc/init.d/${service}" 2>>$DEBUG ; then
         einfo "Starting service ${service}."
-        /etc/init.d/${service} start >>$DEBUG
+        service_wrapper "${service}" start >>$DEBUG
       else
         einfo "Starting service ${service} in background."
-        /etc/init.d/${service} start >>$DEBUG &
+        service_wrapper "${service}" start >>$DEBUG &
       fi
     done
     eend $?
@@ -1307,6 +1362,8 @@ config_stats() {
 
 # {{{ start X window system via grml-x
 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' && ! echo "$CMDLINE" | grep -q 'startx.*nostartx' ; then
@@ -1320,17 +1377,28 @@ 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
+     cat>|/etc/init.d/startx<<EOF
 #!/bin/sh
-su $localuser -c "/usr/bin/grml-x ${WINDOWMANAGER}"
+chgrp tty \${TTY}
+chmod g=rw \${TTY}
+sudo -u "${localuser}" /usr/bin/grml-x ${WINDOWMANAGER}
 EOF
-   chmod 755 /etc/init.d/xstartup
+     chmod 755 /etc/init.d/startx
+     chvt 6
+     return
+   fi
+   cat>|/etc/init.d/startx<<EOF
+#!/bin/sh
+su "${localuser}" -c "/usr/bin/grml-x ${WINDOWMANAGER}"
+EOF
+   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 $?
@@ -1588,7 +1656,11 @@ 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 'Start mdmonitor' 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:"
@@ -1709,18 +1781,25 @@ config_lvm(){
   if checkbootparam 'nolvm' ; then
      ewarn "Skipping LVM code as requested on boot commandline." ; eend 0
   else
-    # Debian etch provides /etc/init.d/lvm only, newer suites provide /etc/init.d/lvm2
-    if ! [ -x /sbin/lvm -a -x /sbin/lvdisplay ] || ! [ -x /etc/init.d/lvm2 -o -x /etc/init.d/lvm ] ; then
+    if ! [ -x /sbin/lvm ] ; then
        eerror "LVM not available, can not execute it." ; eend 1
     else
        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-lvmetad' to activate them 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:"
-             /etc/init.d/lvm2 start ; eend $?
+             if $SYSTEMD ; then
+               service_wrapper lvm2-lvmetad start ; eend $?
+             else
+               service_wrapper lvm2 start ; eend $?
+             fi
           fi
           eoutdent
        fi