Release new version 0.22.0
[grml-autoconfig.git] / autoconfig.functions
index 883231b..3bab753 100755 (executable)
@@ -184,7 +184,8 @@ KVM=false
 VIRTUALBOX=false
 VMWARE=false
 
-if vmware-detect &>/dev/null; then
+if virt-what 2>/dev/null | grep -q 'vmware' || \
+  imvirt 2>/dev/null | grep -iq "vmware" ; then
   VIRTUAL=true; VMWARE=true; VIRTUAL_ENV='VMware'
 fi
 
@@ -212,12 +213,8 @@ fi
 config_log(){
 if checkbootparam 'log' || checkbootparam 'debug' ; then
    export DEBUG="/tmp/grml.log.`date +%Y%m%d`"
-   touch $DEBUG
-   einfo "Bootparameter log found. Log files: ${DEBUG} and /var/log/boot"
-   eindent
-     einfo "Starting bootlogd." # known to be *very* unreliable :(
-     bootlogd -r -c >>$DEBUG 2>&1 ; eend $?
-   eoutdent
+   touch "${DEBUG}"
+   einfo "Bootparameter log found, debug log file from grml-autoconfig available at '${DEBUG}'"
 else
    DEBUG="/dev/null"
 fi
@@ -259,7 +256,7 @@ config_language(){
         if [ -r /usr/share/consolefonts/Uni3-Terminus16.psf.gz ] ; then
           CONSOLEFONT='Uni3-Terminus16'
         else
-          ewarn "/usr/share/consolefonts/Uni3-Terminus16.psf.gz not available. Please upgrade package console-terminus." ; eend 1
+          ewarn "/usr/share/consolefonts/Uni3-Terminus16.psf.gz not available. Please upgrade package console-setup-linux." ; eend 1
         fi
         if ! hasfb ; then
           CONSOLEFONT='Lat15-Terminus16'
@@ -543,33 +540,41 @@ config_kernel(){
 # }}}
 
 # {{{ secure boot
-config_secureboot(){
+# helper function to check whether we're running under (enabled) Secure Boot
+running_under_secureboot() {
   # systemd does this for us, but if we are not running under systemd then mokutil
   # doesn't work as needed as it relies on /sys/firmware/efi/efivars (while
   # /sys/firmware/efi/vars would exist)
   if ! $SYSTEMD ; then
-    if modprobe efivars &>/dev/null ; then
+    if modprobe efivarfs &>/dev/null ; then
       mount -t efivarfs efivarfs /sys/firmware/efi/efivars
     fi
   fi
 
-  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
+  if [[ -x "$(command -v mokutil)" ]] ; then
+    if mokutil --sb-state 2>/dev/null | grep -q 'SecureBoot enabled' ; then
+      return 0
     else
-      einfo "SecureBoot not detected" ; eend 0
+      return 1
     fi
   else
-    if modprobe efivars &>/dev/null ; then
+    if modprobe efivarfs &>/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
+        return 0
       else
-        einfo "SecureBoot not detected" ; eend 0
+        return 1
       fi
     fi
   fi
 }
+
+config_secureboot(){
+  if running_under_secureboot ; then
+      einfo "SecureBoot is enabled" ; eend 0
+  else
+      einfo "SecureBoot not detected" ; eend 0
+  fi
+}
 # }}}
 
 # {{{ timezone
@@ -900,19 +905,23 @@ fi # -z $INSTALLED
 
 # {{{ CPU-detection
 config_cpu(){
-if checkbootparam 'nocpu'; then
-  ewarn "Skipping CPU detection as requested on boot commandline." ; eend 0
-  return 0
-fi
+  if checkbootparam 'nocpu'; then
+    ewarn "Skipping CPU detection as requested on boot commandline." ; eend 0
+    return 0
+  fi
 
-if [[ $(grep -c processor /proc/cpuinfo) -gt 1 ]] ; then
-   einfo "Found CPU:"
-   CPU=$(awk -F: '/^processor/{printf "        Processor"$2" is"};/^model name/{printf $2};/^vendor_id/{printf vendor};/^cpu MHz/{printf " %dMHz",int($2)};/^cache size/{printf ","$2" Cache"};/^$/{print ""}' /proc/cpuinfo 2>>$DEBUG)
-   echo $CPU | sed 's/ \{1,\}/ /g'
-   eend 0
-else
-   einfo "Found CPU: `awk -F: '/^processor/{printf " Processor"$2" is"};/^model name/{printf $2};/^vendor_id/{printf vendor};/^cpu MHz/{printf " %dMHz",int($2)};/^cache size/{printf ","$2" Cache"};/^$/{print ""}' /proc/cpuinfo 2>>$DEBUG` " ; eend 0
-fi
+  if ! [ -x "$(which lscpu)" ] ; then
+    ewarn "Skipping CPU detection due to lack of lscpu."; eend 0
+    return 0
+  fi
+
+  local cpu_info num_cpus
+
+  cpu_info="$(lscpu | sed -n '/^Model name:/s/[^:]*:\s*//p')"
+  num_cpus=$(grep -c processor /proc/cpuinfo)
+
+  einfo "Found ${num_cpus} CPU(s): ${cpu_info}"
+  eend 0
 }
 # }}}
 
@@ -1403,7 +1412,7 @@ else
     else
       eindent
       einfo "debs, config, scripts are read from $DCSDEVICE." ; eend 0
-      DCSDIR="$(< /proc/mounts awk -v DCSDEV=$DCSDEVICE '{if ($1 == DCSDEV) { print $2 }}')"
+      DCSDIR="$(< /proc/mounts awk -v DCSDEV=$DCSDEVICE '{if ($1 == DCSDEV) { print $2; exit }}')"
       if [ -n "$DCSDIR" ]; then
         ewarn "$DCSDEVICE already mounted on $DCSDIR"; eend 0
       else
@@ -1624,75 +1633,6 @@ config_swraid(){
 }
 # }}}
 
-# {{{ dmraid
-config_dmraid(){
-  [ -n "$INSTALLED" ] && return 0
-
-  if checkbootparam 'nodmraid' ; then
-    ewarn "Skipping dmraid code as requested on boot commandline." ; eend 0
-    return 0
-  fi
-
-  if ! [ -x /sbin/dmraid ] ; then
-    eerror "dmraid not available, can not execute it." ; eend 1
-    return
-  fi
-
-  dmraid_wrapper() {
-    # usage: dmraid_wrapper <dmraid_option>
-    [ -n "$1" ] || return 1
-
-    IFSOLD=${IFS:-}
-    IFS=$'\n'
-    eindent
-
-    for line in $(dmraid $1 ; echo errcode:$?); do
-      case $line in
-        *'no block devices found'*)
-          einfo "No block devices found" ; eend 0
-          break
-          ;;
-        *'no raid disks'*)
-          einfo "No active dmraid devices found" ; eend 0
-          break
-          ;;
-        errcode:0)
-          eend 0;
-          ;;
-        errcode:1)
-          eend 1
-          ;;
-        *)
-          einfo "$line"
-          ;;
-      esac
-    done
-
-    eoutdent
-    IFS=$IFSOLD
-  }
-
-  if checkbootparam 'dmraid' ; then
-    local ACTION="$(getbootparam 'dmraid' 2>>$DEBUG)"
-    if [ "$ACTION" = "off" ] ; then
-      # Deactivates all active software RAID sets:
-      einfo "Deactivating present dmraid sets (as requested via dmraid=off):"
-      dmraid_wrapper -an
-    else
-      # Activate all software RAID sets discovered:
-      einfo "Activating present dmraid sets (as requested via dmraid):"
-      dmraid_wrapper -ay
-    fi
-
-    return
-  fi
-
-  # by default (no special bootoptions) discover all software RAID devices:
-  einfo "Searching for any present dmraid sets:"
-  dmraid_wrapper -r
-}
-# }}}
-
 # {{{ LVM (Logical Volumes)
 config_lvm(){
   [ -n "$INSTALLED" ] && return 0
@@ -1847,16 +1787,22 @@ fi # checkbootparam "BOOT_IMAGE=debian2hd
 
 # {{{ virtualbox shared folders
 config_virtualbox_shared_folders() {
-if $VIRTUALBOX ; then
-  einfo "VirtualBox detected, trying to set up Shared Folders."
-  if ! modinfo vboxsf &>/dev/null ; then
-    ewarn "vboxsf driver not present, not setting up VirtualBox Shared Folders."
-    eend 0
-  elif ! [ -x /usr/sbin/VBoxService ] ; then
-    ewarn "virtualbox-guest-utils not installed, not setting up VirtualBox Shared Folders."
-    eend 0
+  if ! $VIRTUALBOX ; then
+    return
+  fi
+
+  if checkbootparam 'novboxsf' ; then
+    ewarn "Skipping VirtualBox Shared Folders setup as requested on boot commandline." ; eend 0
   else
-    eindent
+    einfo "VirtualBox detected, trying to set up Shared Folders."
+    if ! modinfo vboxsf &>/dev/null ; then
+      ewarn "vboxsf driver not present, not setting up VirtualBox Shared Folders."
+      eend 0
+    elif ! [ -x /usr/sbin/VBoxService ] ; then
+      ewarn "virtualbox-guest-utils not installed, not setting up VirtualBox Shared Folders."
+      eend 0
+    else
+      eindent
 
       einfo "Loading vboxsf driver."
       lsmod | grep -q vboxsf || modprobe vboxsf
@@ -1869,8 +1815,8 @@ if $VIRTUALBOX ; then
 
       config_userfstab
 
-      einfo "Adding $fstabuser to group vboxsf."
-      adduser grml vboxsf &>/dev/null
+      einfo "Adding user ${fstabuser:-grml} to group vboxsf."
+      adduser "${fstabuser:-grml}" vboxsf >>"${DEBUG}" 2>&1
       eend $?
 
       einfo "Starting VBoxService."
@@ -1882,7 +1828,7 @@ if $VIRTUALBOX ; then
         vbautomation="$(getbootparam 'vbautomation' 2>>$DEBUG)"
       fi
 
-      if ! VBoxControl sharedfolder list | egrep -q "^[0-9]+ - ${vbautomation}$" ; then
+      if ! VBoxControl sharedfolder list | egrep -q "^[0-9]+ - ${vbautomation}(\s+|$)" ; then
         ewarn "No automount shared folder '$vbautomation' available"
         eend 0
       else
@@ -1931,9 +1877,36 @@ if $VIRTUALBOX ; then
         fi
       fi
 
-    eoutdent
+      eoutdent
+    fi
   fi
-fi
+}
+# }}}
+
+# {{{ VirtualBox application
+config_virtualbox_setup() {
+  if checkbootparam 'novbox' ; then
+    ewarn "Skipping VirtualBox setup as requested on boot commandline." ; eend 0
+    return
+  fi
+
+  if ! [ -x /usr/bin/VBox ] ; then
+    return
+  fi
+
+  if running_under_secureboot ; then
+    ewarn "VirtualBox service can not be started as running under enabled Secure Boot." ; eend 0
+    return
+  fi
+
+  einfo "VirtualBox service detected, trying to set up."
+  service_wrapper vboxdrv restart >>"${DEBUG}" 2>&1 ; eend $?
+
+  config_userfstab
+
+  einfo "Adding user ${fstabuser:-grml} to group vboxusers."
+  adduser "${fstabuser:-grml}" vboxusers >>"${DEBUG}" 2>&1
+  eend $?
 }
 # }}}
 
@@ -1950,5 +1923,43 @@ fi
 }
 # }}}
 
+# {{{ Easteregg (for 20 years grml.org)
+display_easteregg() {
+  einfo "You found the birthday easter egg!" ; eend 0
+
+  if [[ -x /bin/toilet && -x /usr/games/lolcat ]] ; then
+    visualize() { printf "%s\n" "$*" | toilet | /usr/games/lolcat ; }
+  elif [[ -x /bin/toilet ]] ; then
+    visualize() { printf "%s\n" "$*" | toilet ; }
+  else
+    visualize() { printf "%s\n" "$*" ; }
+  fi
+
+  visualize "   \o/   "
+  visualize "20 years"
+  visualize "grml.org"
+}
+
+config_easteregg() {
+  checkbootparam 'noeasteregg' && return 0
+
+  zmodload zsh/datetime 2>/dev/null || return 0
+  zmodload zsh/mathfunc 2>/dev/null || return 0
+
+  local birthday=1694822400            # := 2023-09-16 -> TZ=UTC date -d "2023-09-16" +%s
+  local one_month=$[24*30*3600]
+  local pi=3.14159265358979323846
+  local magic=$(( one_month/(pi/2) ))  # normalization factor, used to map the [birthday;birthday+-one_month] range onto [0;+-pi/2]
+
+  if [[ $(( abs(birthday-EPOCHSECONDS) )) -le $one_month ]] ; then
+    if [[ $(( rand48() )) -le $(( cos((birthday-EPOCHSECONDS)/magic) )) ]] ; then
+      display_easteregg
+    fi
+  fi
+
+  return 0
+}
+# }}}
+
 ## END OF FILE #################################################################
 # vim:foldmethod=marker expandtab ai ft=zsh shiftwidth=2