Provide checks for vmware-detect/imvirt/virt-what for setting environment variables
[grml-autoconfig.git] / autoconfig.functions
index d8856e9..2a3dd66 100755 (executable)
@@ -40,6 +40,17 @@ if [ -z "$CMDLINE" ]; then
   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' ' ')"
+  modprobe 9p 2>/dev/null || true
+  if grep -q 9p /proc/filesystems ; then
+      local TAG="grml-parameters"
+      if grep -q "$TAG" /sys/bus/virtio/devices/*/mount_tag 2>/dev/null ; then
+          local MOUNTDIR="$(mktemp -d)"
+          mount -t 9p -o trans=virtio,ro "$TAG" "$MOUNTDIR"
+          CMDLINE="$CMDLINE $(cat "$MOUNTDIR"/* 2>/dev/null | tr '\n' ' ')"
+          umount "$MOUNTDIR"
+          rmdir "$MOUNTDIR"
+      fi
+  fi
 fi
 # }}}
 
@@ -117,10 +128,23 @@ mount_sys(){
 # {{{ Check if we are running in live mode or from HD
 INSTALLED=""
 [ -e /etc/grml_cd ] || INSTALLED="yes"
+# }}}
 
-# testcd
-TESTCD=""
-checkbootparam 'testcd' >>$DEBUG 2>&1 && TESTCD="yes"
+# {{{ provide information about virtual environments
+VIRTUAL=false # assume physical system by default
+KVM=false
+VIRTUALBOX=false
+VMWARE=false
+
+if vmware-detect &>/dev/null; then
+  VIRTUAL=true; VMWARE=true; VIRTUAL_ENV='VMware'
+elif [ "$(virt-what 2>/dev/null)" = "kvm" ] || \
+   [ "$(imvirt 2>/dev/null)" = "KVM" ] ; then
+  VIRTUAL=true; KVM=true; VIRTUAL_ENV='KVM'
+elif [ "$(virt-what 2>/dev/null)" = "virtualbox" ] || \
+   [ "$(imvirt 2>/dev/null)" = "VirtualBox" ] ; then
+  VIRTUAL=true; VIRTUALBOX=true; VIRTUAL_ENV='VirtualBox'
+fi
 # }}}
 
 # {{{ source lsb-functions , color handling
@@ -219,10 +243,10 @@ config_language(){
  if [ -x /usr/sbin/grml-setlang ] ; then
    # if bootoption lang is used update /etc/default/locale accordingly
    if [ -n "$BOOT_LANGUAGE" ] ; then
-     checkgrmlsmall && /usr/sbin/grml-setlang "POSIX" || /usr/sbin/grml-setlang "$LANGUAGE"
+     /usr/sbin/grml-setlang "$LANGUAGE"
    # otherwise default to lang=en
    else
-     checkgrmlsmall && /usr/sbin/grml-setlang "POSIX" || /usr/sbin/grml-setlang "en"
+     /usr/sbin/grml-setlang "en"
    fi
  fi
 
@@ -241,12 +265,8 @@ config_language(){
  fi
 
  # export it now, so error messages get translated, too
- if checkgrmlsmall ; then
-    export LANG='C' # grml-small does not provide any further locales
- else
-    [ -r /etc/default/locale ] && . /etc/default/locale
-    export LANG LANGUAGE
- fi
+ [ -r /etc/default/locale ] && . /etc/default/locale
+ export LANG LANGUAGE
 
  # configure keyboard layout, read in already set values first:
  [ -r /etc/sysconfig/keyboard ] && . /etc/sysconfig/keyboard
@@ -369,33 +389,57 @@ config_language(){
 
 # {{{ Set hostname
 config_hostname(){
- if checkbootparam 'hostname' ; then
+  if ! checkbootparam 'hostname' ; then
+    return 0
+  fi
+
   HOSTNAME="$(getbootparam 'hostname' 2>>$DEBUG)"
   if [ -z "$HOSTNAME" ] && [ -x /usr/bin/random-hostname ] ; then
-     einfo "Generating random hostname as no hostname was specified."
-     HOSTNAME="$(/usr/bin/random-hostname)"
-     eend $?
+    einfo "Generating random hostname as no hostname was specified."
+    HOSTNAME="$(/usr/bin/random-hostname)"
+    eend $?
   fi
+
   einfo "Setting hostname to $HOSTNAME as requested."
-  grml-hostname $HOSTNAME >>$DEBUG ; RC=$?
-  [ "$RC" = "0" ] && hostname $HOSTNAME
-  eend $RC
- else
-  hostname --file /etc/hostname
- fi
+  grml-hostname $HOSTNAME >>$DEBUG
+  eend $?
 }
 # }}}
 
 # fstabuser (needed when running from harddisk with username != grml {{{
 config_userfstab(){
+  # force load of build-in and local config
   [ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig
+  [ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig.local
+
+  # 1st. try configured fstab user
   if [ -n "$CONFIG_FSTAB_USER" ] ; then
-     fstabuser="$CONFIG_FSTAB_USER"
-  else
-     fstabuser=$(getent passwd 1000 | cut -d: -f1)
+     fstabuser=$(getent passwd $CONFIG_FSTAB_USER | cut -d: -f1)
   fi
-  # if not yet set fall back to default 'grml' user
-  [ -n "$fstabuser" ] || fstabuser='grml'
+
+  # 2nd. use standard user id
+  [ -n "$fstabuser" ] || fstabuser=$(getent passwd 1000 | cut -d: -f1)
+
+  # 3rd. use standard user name
+  [ -n "$fstabuser" ] || fstabuser=$(getent passwd grml | cut -d: -f1)
+
+  # if not yet set fall back to 'root' user, avoid bad /etc/fstab
+  [ -n "$fstabuser" ] || fstabuser='root'
+}
+# }}}
+
+# local_user (needed when running with username != grml {{{
+config_userlocal() {
+
+  # force load of build-in and local config
+  [ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig
+  [ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig.local
+
+  # 1st. try id of primary user
+  localuser=$(getent passwd 1000 | cut -d: -f1)
+
+  # 2nd. use name standard user
+  [ -n "$localuser" ] || localuser=$(getent passwd grml | cut -d: -f1)
 }
 # }}}
 
@@ -456,20 +500,24 @@ config_time(){
 
 # {{{ print kernel info
 config_kernel(){
-  vmware-detect &>/dev/null && VMWARE="inside ${WHITE}VMware/Qemu${NORMAL}"
-  [ -d /proc/xen ] && VMWARE='' # vmware-detect returns '0' when running with a Xen-enabled kernel
-  einfo "Running Linux Kernel $KERNEL $VMWARE" ; eend 0
+  if $VIRTUAL ; then
+    einfo "Running Linux Kernel $KERNEL $VMWARE" ; eend 0
+  else
+    einfo "Running Linux Kernel $KERNEL inside $VIRTUAL_ENV" ; eend 0
+  fi
+
   if [ -r /proc/cpuinfo ] ; then
-     if egrep -q '^flags.*(vmx|svm)' /proc/cpuinfo ; then
-       eindent
-       einfo 'CPU(s) featuring virtualization technology detected' ; eend 0
-       eoutdent
-     fi
+    if egrep -q '^flags.*(vmx|svm)' /proc/cpuinfo ; then
+      eindent
+      einfo 'CPU(s) featuring virtualization technology detected' ; eend 0
+      eoutdent
+    fi
   fi
+
   if [ -d /proc/xen ] ; then
-     eindent
-     einfo 'Running kernel featuring support for Xen detected' ; eend 0
-     eoutdent
+    eindent
+    einfo 'Running kernel featuring support for Xen detected' ; eend 0
+    eoutdent
   fi
 }
 # }}}
@@ -641,18 +689,30 @@ config_fix_passwd(){
 
 # {{{ CD Checker
 config_testcd(){
-if [ -n "$TESTCD" ]; then
+if checkbootparam 'testcd' ; then
    einfo "Checking CD data integrity as requested by '${WHITE}testcd${NORMAL}' boot option."
-   einfo "Reading files and checking against GRML/md5sums, this may take a while..."
-   echo -n "${RED}"
 
-   if [ -n "${LIVECD_PATH}"/GRML ] ; then
-      ( cd "${LIVECD_PATH}"/GRML ; rm -f /tmp/md5sum.log ; md5sum -c md5sums 2>&1 | tee /tmp/md5sum.log ; RC=$? )
-   else
+   ERROR=0
+   FOUND_FILE=0
+
+   rm -f /tmp/md5sum.log
+   for md5 in $(find "${LIVECD_PATH}" -name md5sums) ; do
+      einfo "Checking files against $md5, this may take a while..."
+
+      FOUND_FILE=1
+      ( cd $(dirname "$md5") && md5sum -c $(basename "$md5") ; RC=$?)  |& tee -a /tmp/md5sum.log
+
+      if [ $RC -ne 0 ] ; then
+         ERROR=1
+      fi
+   done
+
+   if [ $FOUND_FILE -eq 0 ] ; then
       echo "${RED} *** Error: Could not find md5sum file.                           ***"
+      return
    fi
 
-   if [ "$RC" = "0" ]; then
+   if [ "$ERROR" -eq 0 ]; then
       einfo "Everything looks OK" ; eend 0
    else
       eerror 'Checksum failed for theses files:' ; eend 1
@@ -754,6 +814,14 @@ EOT
 check_partitions >/dev/null 2>&1 # avoid output "check_partitions:3: read-only file system"
 # }}}
 
+# {{{ Start brltty
+config_brltty() {
+  if checkbootparam 'brltty' ; then
+    [ -x /lib/brltty/brltty.sh ] && /lib/brltty/brltty.sh
+  fi
+}
+# }}}
+
 # {{{ Enable DMA for all IDE drives now if not disabled
 # Notice: Already done by linuxrc, but make sure it's done also on harddisk-installed systems
 config_dma(){
@@ -942,7 +1010,7 @@ else
 fi
 
 # no cpufreq setup inside VirtualBox
-if [ -r /proc/acpi/battery/BAT0/info ] && grep -q 'OEM info:.*innotek' /proc/acpi/battery/BAT0/info ; then
+if $VIRTUALBOX ; then
    einfo 'Virtual Box detected, skipping cpufreq setup.' ; eend 0
    return 0
 fi
@@ -1001,7 +1069,9 @@ config_ssh(){
 if checkbootparam 'ssh' ; then
    SSH_PASSWD=''
    SSH_PASSWD="$(getbootparam 'ssh' 2>>$DEBUG)"
-   einfo "Bootoption ssh found, trying to set password for user grml."
+   config_userlocal
+   einfo "Bootoption ssh found, trying to set password for root and user $localuser"
+   [ -z "$localuser" ] && eend 1
    eindent
    if [ -z "$SSH_PASSWD" ] ; then
       if [ -x /usr/bin/apg ] ; then
@@ -1029,20 +1099,21 @@ if checkbootparam 'ssh' ; then
    # finally check if we have a password we can use:
    if [ -n "$SSH_PASSWD" ] ; then
       # chpasswd sucks, seriously.
+      chpass_options=""
       if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
-        echo "grml:$SSH_PASSWD" | chpasswd -m
-      else
-        echo "grml:$SSH_PASSWD" | chpasswd
+        chpass_options="-m"
       fi
+      echo "$localuser:$SSH_PASSWD" | chpasswd $chpass_options
+      echo "root:$SSH_PASSWD" | chpasswd $chpass_options
    fi
 
-   einfo 'Starting secure shell server in background.'
+   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 &
    eend $?
 
    eindent
-   ewarn 'Warning: please change the password for user grml as soon as possible!'
+   ewarn "Warning: please change the password for root and user $localuser as soon as possible!"
    eoutdent
 fi
 }
@@ -1050,12 +1121,11 @@ fi
 
 # {{{ autostart of x11vnc
 config_vnc(){
-
-USER=grml # TODO: make it dynamically configurable
 if checkbootparam 'vnc' ; then
+   config_userlocal
    VNC_PASSWD=''
    VNC_PASSWD="$(getbootparam 'vnc' 2>>$DEBUG)"
-   einfo "Bootoption vnc found, trying to set password for user $USER."
+   einfo "Bootoption vnc found, trying to set password for user $localuser."
    eindent
    if [ -z "$VNC_PASSWD" ] ; then
       if [ -x /usr/bin/apg ] ; then
@@ -1067,7 +1137,7 @@ if checkbootparam 'vnc' ; then
       elif [ -x /usr/bin/hexdump ] ; then
          VNC_PASSWD="$(dd if=/dev/urandom bs=14 count=1 2>/dev/null | hexdump | awk '{print $3 $4}')"
       elif [ -n "$RANDOM" ] ; then
-         VNC_PASSWD="${USER}${RANDOM}"
+         VNC_PASSWD="${localuser}${RANDOM}"
       else
          VNC_PASSWD=''
          eerror "Empty passphrase and neither pwgen nor hexdump nor \$RANDOM found. Skipping."
@@ -1083,7 +1153,7 @@ if checkbootparam 'vnc' ; then
    # finally check if we have a password we can use:
    if [ -n "$VNC_PASSWD" ] ; then
 
-      VNCDIR="/home/${USER}/.vnc"
+      VNCDIR="/home/${localuser}/.vnc"
       [ -d "$VNCDIR" ] || mkdir "$VNCDIR"
 
       if [ ! -x /usr/bin/x11vnc ] ; then
@@ -1091,7 +1161,7 @@ if checkbootparam 'vnc' ; then
          eend 1
       else
          /usr/bin/x11vnc -storepasswd "$VNC_PASSWD" "$VNCDIR"/passwd ; eend $?
-         /bin/chown -R "$USER": "$VNCDIR"
+         /bin/chown -R "$localuser": "$VNCDIR"
       fi
    fi
    if checkbootparam 'vnc_connect' ; then
@@ -1099,7 +1169,7 @@ if checkbootparam 'vnc' ; then
       VNC_CONNECT="$(getbootparam 'vnc_connect' 2>>$DEBUG)"
       einfo "Bootoption vnc_connect found, will start vnc with connect to $VNC_CONNECT."
       #store the options in a file
-      VNCDIR="/home/${USER}/.vnc"
+      VNCDIR="/home/${localuser}/.vnc"
       [ -d "$VNCDIR" ] || mkdir "$VNCDIR"
       echo " --connect $VNC_CONNECT " >> $VNCDIR/options
    fi
@@ -1107,13 +1177,15 @@ fi
 }
 # }}}
 
-# {{{ set password for user grml
+# {{{ set password for default user
 config_passwd(){
 if checkbootparam 'passwd' >>$DEBUG 2>&1; then
-  einfo "Bootoption passwd found."
   PASSWD="$(getbootparam 'passwd' 2>>$DEBUG)"
+  config_userlocal
+  einfo "Bootoption passwd found, change password for user '$localuser'."
+  [ -z "$localuser" ] && eend 1
   if [ -n "$PASSWD" ] ; then
-    echo "grml:$PASSWD" | chpasswd -m ; eend $?
+    echo "$localuser:$PASSWD" | chpasswd -m ; eend $?
   else
     eerror "No given password for ssh found. Autostart of SSH will not work." ; eend 1
   fi
@@ -1419,10 +1491,10 @@ if checkbootparam 'startx' && ! echo "$CMDLINE" | grep -q 'startx.*nostartx' ; t
      einfo "Window manager ${WHITE}${WINDOWMANAGER}${NORMAL} found as bootoption." && eend 0
    fi
    einfo "Setting up and invoking grml-x ${WINDOWMANAGER}. Just exit X windows system to get full featured consoles."
-   config_userfstab || fstabuser='grml'
+   config_userlocal
  cat>|/etc/init.d/xstartup<<EOF
 #!/bin/sh
-su $fstabuser -c "/usr/bin/grml-x"
+su $localuser -c "/usr/bin/grml-x ${WINDOWMANAGER}"
 EOF
    chmod 755 /etc/init.d/xstartup
 
@@ -1496,6 +1568,17 @@ else
         fi
       fi
       DCSDEVICE=$(blkid -t LABEL=$GRMLCFG | head -1 | awk -F: '{print $1}')
+
+      modprobe 9p 2>/dev/null || true
+      if [ -z "$DCSDEVICE" ] && grep -q 9p /proc/filesystems ; then
+          if grep -q "$GRMLCFG" /sys/bus/virtio/devices/*/mount_tag 2>/dev/null ; then
+            einfo "Found 9p-virtio fs with mount_tag $GRMLCFG"
+            DCSDEVICE="$GRMLCFG"
+            MOUNTOPTIONS="ro,trans=virtio"
+            DCSFS="9p"
+          fi
+      fi
+
       if [ -n "$DCSDEVICE" ]; then
         DCSMP="/mnt/grmlcfg"
       fi
@@ -1514,7 +1597,7 @@ else
       else
         [ -d $DCSMP ] || mkdir $DCSMP
         umount $DCSMP >>$DEBUG 2>&1 # make sure it is not mounted
-        mount -o ro -t auto $DCSDEVICE  $DCSMP ; RC="$?"
+        mount -o ${MOUNTOPTIONS:-ro} -t ${DCSFS:-auto} $DCSDEVICE  $DCSMP ; RC="$?"
         if [[ $RC == 0 ]]; then
           einfo "Successfully mounted $DCSDEVICE to $DCSMP (readonly)." ; eend 0
         else
@@ -2085,9 +2168,9 @@ fi # checkbootparam "BOOT_IMAGE=debian2hd
 # }}}
 
 config_virtualbox_shared_folders() {
-if [ -r /proc/acpi/battery/BAT0/info ] && grep -q 'OEM info:.*innotek' /proc/acpi/battery/BAT0/info ; then
+if $VIRTUALBOX ; then
   einfo "VirtualBox detected, trying to set up Shared Folders."
-  if ! modprobe -l | grep -q vboxsf.ko ; then
+  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
@@ -2105,11 +2188,8 @@ if [ -r /proc/acpi/battery/BAT0/info ] && grep -q 'OEM info:.*innotek' /proc/acp
       chmod 660 /dev/vboxguest
       eend $?
 
-      if [ -n "$CONFIG_FSTAB_USER" ] ; then
-        fstabuser="$CONFIG_FSTAB_USER"
-      else
-        fstabuser=$(getent passwd 1000 | cut -d: -f1)
-      fi
+      config_userfstab
+
       einfo "Adding $fstabuser to group vboxsf."
       adduser grml vboxsf &>/dev/null
       eend $?
@@ -2138,4 +2218,4 @@ fi
 # }}}
 
 ## END OF FILE #################################################################
-# vim:foldmethod=marker expandtab ai ft=zsh shiftwidth=3
+# vim:foldmethod=marker expandtab ai ft=zsh shiftwidth=2