Enable acpid.socket if we're running under systemd
[grml-autoconfig.git] / autoconfig.functions
1 #!/bin/zsh
2 # Filename:      autoconfig.functions
3 # Purpose:       basic system configuration and hardware setup for grml system
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 # {{{ path, variables, signals, umask, zsh
10 export PATH="/bin:/sbin:/usr/bin:/usr/sbin"
11 DEBUG="/dev/null"
12 KERNEL="$(uname -r)"
13 ARCH="$(uname -m)"
14 umask 022
15
16 # old linuxrc version:
17 [ -d /cdrom ]      && export LIVECD_PATH=/cdrom
18 # initramfs layout until around December 2012:
19 [ -d /live/image ] && export LIVECD_PATH=/live/image
20 # initramfs layout since around December 2012:
21 [ -d /lib/live/mount/medium ] && export LIVECD_PATH=/lib/live/mount/medium
22
23 # Ignore these signals in non-interactive mode: INT, TERM, SEGV
24 [ -z "$PS1" ] && trap "" 2 3 11
25
26 if [[ "$(realpath "$(cat /proc/1/cmdline)")" == "/lib/systemd/systemd" ]] ; then
27   SYSTEMD=true
28 else
29   SYSTEMD=false
30 fi
31
32 # zsh stuff
33 iszsh(){
34 if [ -n "$ZSH_VERSION" ] ; then
35   return 0
36 else
37   return 1
38 fi
39 }
40 # avoid 'no matches found: ...'
41 iszsh && setopt no_nomatch # || echo "Warning: not running under zsh!"
42 # }}}
43
44 # {{{ Read in boot parameters
45 if [ -z "$CMDLINE" ]; then
46   # if CMDLINE was set from the outside, we're debugging.
47   # otherwise, take CMDLINE from Kernel and config files.
48   CMDLINE="$(cat /proc/cmdline)"
49   [ -d ${LIVECD_PATH}/bootparams/ ] && CMDLINE="$CMDLINE $(cat ${LIVECD_PATH}/bootparams/* | tr '\n' ' ')"
50   modprobe 9p 2>/dev/null || true
51   if grep -q 9p /proc/filesystems ; then
52       TAG="grml-parameters"
53       if grep -q "$TAG" /sys/bus/virtio/devices/*/mount_tag 2>/dev/null ; then
54           MOUNTDIR="$(mktemp -d)"
55           mount -t 9p -o trans=virtio,ro "$TAG" "$MOUNTDIR"
56           CMDLINE="$CMDLINE $(cat "$MOUNTDIR"/* 2>/dev/null | tr '\n' ' ')"
57           umount "$MOUNTDIR"
58           rmdir "$MOUNTDIR"
59       fi
60   fi
61 fi
62 # }}}
63
64 ### {{{ Utility Functions
65
66 # Get a bootoption's parameter: read boot command line and either
67 # echo last parameter's argument or return false.
68 getbootparam(){
69   local line
70   local ws
71   ws='   '
72   line=" $CMDLINE "
73   case "$line" in
74     *[${ws}]"$1="*)
75       result="${line##*[$ws]$1=}"
76       result="${result%%[$ws]*}"
77       echo "$result"
78       return 0 ;;
79     *) # no match?
80       return 1 ;;
81   esac
82 }
83
84 # Check boot commandline for specified option
85 checkbootparam(){
86   [ -n "$1" ] || ( echo "Error: missing argument to checkbootparam()" ; return 1 )
87   local line
88   local ws
89   ws='   '
90   line=" $CMDLINE "
91   case "$line" in
92     *[${ws}]"$1"=*|*[${ws}]"$1"[${ws}]*)
93       return 0 ;;
94     *)
95       return 1 ;;
96   esac
97 }
98
99 # Check if currently using a framebuffer
100 hasfb() {
101     [ -e /dev/fb0 ] && return 0 || return 1
102 }
103
104 # Check wheter a configuration variable (like $CONFIG_TOHD) is
105 # enabled or not
106 checkvalue(){
107   case "$1" in
108     [yY][eE][sS])     return 0 ;; # it's set to 'yes'
109     [tT][rR][uU][eE]) return 0 ;; # it's set to 'true'
110                    *) return 1 ;; # default
111   esac
112 }
113
114 # Are we using grml-small?
115 checkgrmlsmall(){
116   grep -q small /etc/grml_version 2>>$DEBUG && return 0 || return 1
117 }
118
119 # if no password is set return a random password
120 set_passwd() {
121   [ -n "$PASSWD" ] && return 0
122
123   if [ -x /usr/bin/apg ] ; then
124     PASSWD="$(apg -M NL -a 0 -m 8 -x 12 -n 1)"
125   elif [ -x /usr/bin/gpw ] ; then
126     PASSWD="$(gpw 1)"
127   elif [ -x /usr/bin/pwgen ] ; then
128     PASSWD="$(pwgen -1 8)"
129   elif [ -x /usr/bin/hexdump ] ; then
130     PASSWD="$(dd if=/dev/urandom bs=14 count=1 2>/dev/null | hexdump | awk '{print $3 $4}')"
131   elif [ -n "$RANDOM" ] ; then
132     PASSWD="grml${RANDOM}"
133   else
134     PASSWD=''
135     eerror "Empty passphrase and neither apg, gpw, pwgen, hexdump nor \$RANDOM available. Skipping."
136     eend 1
137     return 1
138   fi
139 }
140
141 ### }}}
142
143 # {{{ filesystems (proc, pts, sys) and fixes
144 mount_proc(){
145   [ -f /proc/version ] || mount -t proc /proc /proc 2>/dev/null
146 }
147
148 mount_pts(){
149   grep -q "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
150 }
151
152 mount_sys(){
153   [ -d /sys/devices ] || mount -t sysfs /sys /sys 2>/dev/null
154 }
155 # }}}
156
157 # {{{ Check if we are running in live mode or from HD
158 INSTALLED=""
159 [ -e /etc/grml_cd ] || INSTALLED="yes"
160 # }}}
161
162 # {{{ provide information about virtual environments
163 VIRTUAL=false # assume physical system by default
164 KVM=false
165 VIRTUALBOX=false
166 VMWARE=false
167
168 if vmware-detect &>/dev/null; then
169   VIRTUAL=true; VMWARE=true; VIRTUAL_ENV='VMware'
170 elif [ "$(virt-what 2>/dev/null)" = "kvm" ] || \
171    [ "$(imvirt 2>/dev/null)" = "KVM" ] ; then
172   VIRTUAL=true; KVM=true; VIRTUAL_ENV='KVM'
173 elif [ "$(virt-what 2>/dev/null)" = "virtualbox" ] || \
174    [ "$(imvirt 2>/dev/null)" = "VirtualBox" ] ; then
175   VIRTUAL=true; VIRTUALBOX=true; VIRTUAL_ENV='VirtualBox'
176 fi
177 # }}}
178
179 # {{{ source lsb-functions , color handling
180 if checkbootparam 'nocolor'; then
181   RC_NOCOLOR=yes
182   . /etc/grml/lsb-functions
183   einfo "Disabling colors in bootsequence as requested on commandline." ; eend 0
184 else
185   . /etc/grml/lsb-functions
186   . /etc/grml_colors
187 fi
188 # }}}
189
190 # {{{ debug
191 config_debug(){
192  checkbootparam 'debug'            && BOOTDEBUG="yes"
193  checkbootparam "BOOT_IMAGE=debug" && BOOTDEBUG="yes"
194
195  rundebugshell(){
196   if [ -n "$BOOTDEBUG" ]; then
197      einfo "Starting intermediate shell stage $stage as requested by \"debug\" option."
198      if [ grep -q "debug=noscreen" "$CMDLINE" ] ; then
199         einfo "Notice that the shell does not provide job handling: ctrl-z, bg and fg won't work!"
200         einfo "Just exit the shell to continue boot process..."
201         /bin/zsh
202      else
203         eindent
204         if [ -r /etc/grml/screenrc ] ; then
205            einfo "Starting GNU screen to be able to use a full featured shell environment."
206            einfo "Just exit the shells (and therefore screen) to continue boot process..."
207            /bin/zsh -c "screen -c /etc/grml/screenrc"
208         else
209            einfo "Notice that the shell does not provide job handling: ctrl-z, bg and fg won't work!"
210            einfo "Just exit the shell to continue boot process..."
211            /bin/zsh
212         fi
213         eoutdent
214      fi
215   fi
216  }
217 }
218 # }}}
219
220 # {{{ log
221 config_log(){
222 if checkbootparam 'log' || checkbootparam 'debug' ; then
223    export DEBUG="/tmp/grml.log.`date +%Y%m%d`"
224    touch $DEBUG
225    einfo "Bootparameter log found. Log files: ${DEBUG} and /var/log/boot"
226    eindent
227      einfo "Starting bootlogd." # known to be *very* unreliable :(
228      bootlogd -r -c >>$DEBUG 2>&1 ; eend $?
229    eoutdent
230 else
231    DEBUG="/dev/null"
232 fi
233 }
234 # }}}
235
236 ### {{{ language configuration / localization
237 config_language(){
238
239  einfo "Activating language settings:"
240  eindent
241
242  # people can specify $LANGUAGE and $CONSOLEFONT in a config file
243  [ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig
244
245  # check for bootoption which overrides config from /etc/grml/autoconfig
246  BOOT_LANGUAGE="$(getbootparam 'lang' 2>>$DEBUG)"
247  [ -n "$BOOT_LANGUAGE" ] && LANGUAGE="$BOOT_LANGUAGE"
248
249  # set default to 'en' in live-cd mode iff $LANGUAGE is not set yet
250  if [ -z "$INSTALLED" ] ; then
251     [ -n "$LANGUAGE" ] || LANGUAGE='en'
252  fi
253
254  if [ -x /usr/sbin/grml-setlang ] ; then
255    # if bootoption lang is used update /etc/default/locale accordingly
256    if [ -n "$BOOT_LANGUAGE" ] ; then
257      /usr/sbin/grml-setlang "$LANGUAGE"
258    # otherwise default to lang=en
259    else
260      /usr/sbin/grml-setlang "en"
261    fi
262  fi
263
264  # set console font
265  if [ -z "$CONSOLEFONT" ] ; then
266     if ! checkbootparam 'nodefaultfont' >>$DEBUG 2>&1 ; then
267        if [ -r /usr/share/consolefonts/Uni3-Terminus16.psf.gz ] ; then
268           CONSOLEFONT='Uni3-Terminus16'
269        else
270           ewarn "/usr/share/consolefonts/Uni3-Terminus16.psf.gz not available. Please upgrade package console-terminus." ; eend 1
271        fi
272        if ! hasfb ; then
273           CONSOLEFONT='Lat15-Terminus16'
274        fi
275     fi
276  fi
277
278  # export it now, so error messages get translated, too
279  [ -r /etc/default/locale ] && . /etc/default/locale
280  export LANG LANGUAGE
281
282  # configure keyboard layout, read in already set values first:
283  [ -r /etc/sysconfig/keyboard ] && . /etc/sysconfig/keyboard
284
285  # now allow keyboard override by boot commandline for later use:
286  KKEYBOARD="$(getbootparam 'keyboard' 2>>$DEBUG)"
287  [ -n "$KKEYBOARD" ] && KEYTABLE="$KKEYBOARD"
288  # notce: de/at is a bad choice, so take de-latin1-nodeadkeys instead:
289  [[ "$KKEYBOARD" == 'de' ]] && KEYTABLE=de-latin1-nodeadkeys
290  [[ "$KKEYBOARD" == 'at' ]] && KEYTABLE=de-latin1-nodeadkeys
291
292  # modify /etc/sysconfig/keyboard only in live-cd mode:
293  if [ -z "$INSTALLED" ] ; then
294
295    local LANGUAGE="$BOOT_LANGUAGE"
296    . /etc/grml/language-functions
297    # allow setting xkeyboard explicitly different than console keyboard
298    KXKEYBOARD="$(getbootparam 'xkeyboard' 2>>$DEBUG)"
299    if [ -n "$KXKEYBOARD" ]; then
300       XKEYBOARD="$KXKEYBOARD"
301       KDEKEYBOARD="$KXKEYBOARD"
302    elif [ -n "$KKEYBOARD" ]; then
303       XKEYBOARD="$KKEYBOARD"
304       KDEKEYBOARD="$KKEYBOARD"
305    fi
306
307    # duplicate of previous code to make sure /etc/grml/language-functions
308    # does not overwrite our values....
309    # now allow keyboard override by boot commandline for later use:
310    KKEYBOARD="$(getbootparam 'keyboard' 2>>$DEBUG)"
311    [ -n "$KKEYBOARD" ] && KEYTABLE="$KKEYBOARD"
312    # notce: de/at is a bad choice, so take de-latin1-nodeadkeys instead:
313    [[ "$KKEYBOARD" == 'de' ]] && KEYTABLE=de-latin1-nodeadkeys
314    [[ "$KKEYBOARD" == 'at' ]] && KEYTABLE=de-latin1-nodeadkeys
315
316    # write keyboard related variables to file for later use
317    [ -d /etc/sysconfig ] || mkdir /etc/sysconfig
318    if ! [ -e /etc/sysconfig/keyboard ] ; then
319       echo "KEYTABLE=\"$KEYTABLE\""          > /etc/sysconfig/keyboard
320       echo "XKEYBOARD=\"$XKEYBOARD\""       >> /etc/sysconfig/keyboard
321       echo "KDEKEYBOARD=\"$KDEKEYBOARD\""   >> /etc/sysconfig/keyboard
322       echo "KDEKEYBOARDS=\"$KDEKEYBOARDS\"" >> /etc/sysconfig/keyboard
323    fi
324  fi
325
326  [ -r /etc/sysconfig/keyboard ] && . /etc/sysconfig/keyboard
327
328  # activate unicode console if running within utf8 environment
329  if [ -r /etc/default/locale ] ; then
330     if grep -q "LANG=.*UTF" /etc/default/locale ; then
331        einfo "Setting up unicode environment."
332        unicode_start >>$DEBUG 2>&1 ; eend $?
333     fi
334  fi
335
336  # Set default keyboard before interactive setup
337  if [ -n "$KEYTABLE" ] ; then
338     einfo "Running loadkeys for ${WHITE}${KEYTABLE}${NORMAL} in background"
339     loadkeys -q $KEYTABLE &
340     eend $?
341  fi
342
343  # we have to set up all consoles, therefore loop it over all ttys:
344  NUM_CONSOLES=$(fgconsole --next-available 2>/dev/null)
345  if [ -n "$NUM_CONSOLES" ] ; then
346     NUM_CONSOLES=$(expr ${NUM_CONSOLES} - 1)
347     [ ${NUM_CONSOLES} -eq 1 ] && NUM_CONSOLES=6
348  fi
349  CUR_CONSOLE=$(fgconsole 2>/dev/null)
350
351  if [ -x "$(which setfont)" ] ; then
352     use_setfont=true
353  elif [ -x "$(which consolechars)" ] ; then
354     use_consolechars=true
355  else
356     eerror "Neither setfont nor consolechars tool present, can not set font."
357     eend 1
358     return 1
359  fi
360
361  if [ -n "$CHARMAP" ] ; then
362     einfo "Setting font to ${CHARMAP}"
363     RC=0
364     for vc in $(seq 0 ${NUM_CONSOLES}) ; do
365         if $use_setfont ; then
366           setfont -C /dev/tty${vc} $CHARMAP ; RC=$?
367         elif $use_consolechars ; then
368           consolechars --tty=/dev/tty${vc} -m ${CHARMAP} ; RC=$?
369         fi
370     done
371     if [ -n "$CUR_CONSOLE" ] ; then
372        [ "$CUR_CONSOLE" != "serial" ] && chvt $CUR_CONSOLE
373     fi
374     eend $RC
375  fi
376
377  if checkbootparam 'noconsolefont' ; then
378     ewarn "Skipping setting console font as requested on boot commandline." ; eend 0
379  else
380     if [ -n "$CONSOLEFONT" ] ; then
381        einfo "Setting font to ${CONSOLEFONT}"
382        RC=0
383        for vc in $(seq 0 ${NUM_CONSOLES}) ; do
384            if $use_setfont ; then
385              setfont -C /dev/tty${vc} ${CONSOLEFONT} ; RC=$?
386            elif $use_consolechars ; then
387              consolechars --tty=/dev/tty${vc} -f ${CONSOLEFONT} ; RC=$?
388            fi
389        done
390        if [ -n "$CUR_CONSOLE" ] ; then
391           [ "$CUR_CONSOLE" != "serial" ] && chvt $CUR_CONSOLE
392        fi
393        eend $RC
394     fi
395  fi
396
397  eoutdent
398 }
399 # }}}
400
401 # {{{ Set hostname
402 config_hostname(){
403   if ! checkbootparam 'hostname' ; then
404     return 0
405   fi
406
407   HOSTNAME="$(getbootparam 'hostname' 2>>$DEBUG)"
408   if [ -z "$HOSTNAME" ] && [ -x /usr/bin/random-hostname ] ; then
409     einfo "Generating random hostname as no hostname was specified."
410     HOSTNAME="$(/usr/bin/random-hostname)"
411     eend $?
412   fi
413
414   einfo "Setting hostname to $HOSTNAME as requested."
415   grml-hostname $HOSTNAME >>$DEBUG
416   eend $?
417 }
418 # }}}
419
420 # fstabuser (needed when running from harddisk with username != grml {{{
421 config_userfstab(){
422   # force load of build-in and local config
423   [ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig
424   [ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig.local
425
426   # 1st. try configured fstab user
427   if [ -n "$CONFIG_FSTAB_USER" ] ; then
428      fstabuser=$(getent passwd $CONFIG_FSTAB_USER | cut -d: -f1)
429   fi
430
431   # 2nd. use standard user id
432   [ -n "$fstabuser" ] || fstabuser=$(getent passwd 1000 | cut -d: -f1)
433
434   # 3rd. use standard user name
435   [ -n "$fstabuser" ] || fstabuser=$(getent passwd grml | cut -d: -f1)
436
437   # if not yet set fall back to 'root' user, avoid bad /etc/fstab
438   [ -n "$fstabuser" ] || fstabuser='root'
439 }
440 # }}}
441
442 # local_user (needed when running with username != grml {{{
443 config_userlocal() {
444
445   # force load of build-in and local config
446   [ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig
447   [ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig.local
448
449   # 1st. try id of primary user
450   localuser=$(getent passwd 1000 | cut -d: -f1)
451
452   # 2nd. use name standard user
453   [ -n "$localuser" ] || localuser=$(getent passwd grml | cut -d: -f1)
454 }
455 # }}}
456
457 # {{{ Set clock (Local time is more often used than GMT, so it is default)
458 config_time(){
459  # don't touch the files if running from harddisk:
460  if [ -z "$INSTALLED" ]; then
461     # The default hardware clock timezone is stated as representing local time.
462     UTC="--localtime"
463
464     if [ -f /etc/default/rcS ] ; then
465       grep -q "^UTC=" /etc/default/rcS || echo "UTC=no" >> /etc/default/rcS
466       checkbootparam 'utc'       >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=yes|" /etc/default/rcS
467       checkbootparam 'gmt'       >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=yes|" /etc/default/rcS
468       checkbootparam 'localtime' >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=no|"  /etc/default/rcS
469       grep -q -i "^UTC=yes" /etc/default/rcS && UTC="-u"
470     # recent initscripts package versions don't ship /etc/default/rcS anymore, instead rely on /etc/adjtime
471     elif [ -f /etc/adjtime ] ; then
472       checkbootparam 'utc'       >>$DEBUG 2>&1 && sed -i "s/^LOCAL/UTC/" /etc/adjtime
473       checkbootparam 'gmt'       >>$DEBUG 2>&1 && sed -i "s/^LOCAL/UTC/" /etc/adjtime
474       checkbootparam 'localtime' >>$DEBUG 2>&1 && sed -i "s/^UTC$/LOCAL/" /etc/adjtime
475       grep -q "^UTC$" /etc/adjtime && UTC="-u"
476     fi
477
478     # hwclock uses the TZ variable
479     KTZ="$(getbootparam 'tz' 2>>$DEBUG)"
480     [ -z "$KTZ" ] && [ -r /etc/timezone ] && KTZ=$(cat /etc/timezone)
481     if [ ! -f "/usr/share/zoneinfo/$KTZ" ] ; then
482        ewarn "Warning: unknown timezone $KTZ" ; eend 1
483        KTZ="UTC"
484        ewarn "Falling back to timezone $KTZ" ; eend 0
485     fi
486
487     if ! [ -r /dev/rtc ] ; then
488       ewarn "Warning: realtime clock not available, trying to execute hwclock anyway." ; eend 0
489     fi
490
491     ERROR=$(TZ="$KTZ" hwclock $UTC -s 2>&1 | head -1) ; RC=$?
492     if [ -n "$ERROR" ] ; then
493        eindent
494        ERROR=$(TZ="$KTZ" hwclock $UTC -s --directisa 2>&1 | head -1)
495        if [ -n "$ERROR" ] ; then
496           eerror "Problem running hwclock: $ERROR" ; eend 1
497        fi
498        eoutdent
499     fi
500
501  fi
502 }
503 # }}}
504
505 # {{{ print kernel info
506 config_kernel(){
507   if $VIRTUAL && [ -n "$VIRTUAL_ENV" ] ; then
508     einfo "Running Linux Kernel $KERNEL inside $VIRTUAL_ENV" ; eend 0
509   else
510     einfo "Running Linux Kernel $KERNEL" ; eend 0
511   fi
512
513   if [ -r /proc/cpuinfo ] ; then
514     if egrep -q '^flags.*(vmx|svm)' /proc/cpuinfo ; then
515       eindent
516       einfo 'CPU(s) featuring virtualization technology detected' ; eend 0
517       eoutdent
518     fi
519   fi
520
521   if [ -d /proc/xen ] ; then
522     eindent
523     einfo 'Running kernel featuring support for Xen detected' ; eend 0
524     eoutdent
525   fi
526 }
527 # }}}
528
529 # {{{ timezone
530 config_timezone(){
531  # don't touch the files if running from harddisk:
532  if [ -z "$INSTALLED" ]; then
533     KTZ="$(getbootparam 'tz' 2>>$DEBUG)"
534     if [ -n "$KTZ" ] ; then
535        if [ ! -f "/usr/share/zoneinfo/$KTZ" ]
536        then
537           ewarn "Warning: unknown timezone $KTZ"; eend 0
538        else
539           einfo "Setting timezone."
540           # update debconf
541           area=$(echo $KTZ | cut -d '/' -f1)
542           zone=$(echo $KTZ | cut -d '/' -f2)
543           echo "tzdata tzdata/Areas       select $area" | debconf-set-selections
544           echo "tzdata tzdata/Zones/$area select $zone" | debconf-set-selections
545           # update files
546           echo $KTZ > /etc/timezone
547           rm -f /etc/localtime
548           cp "/usr/share/zoneinfo/$KTZ" /etc/localtime ; eend $?
549        fi
550     fi
551  fi
552 }
553 # }}}
554
555 # activate serial console {{{
556 config_console(){
557 if checkbootparam 'console'; then
558   local line
559   local ws
560   ws='   '
561
562   einfo "Bootoption for serial console detected:"
563
564   line="$CMDLINE x "
565   this=""
566   line="${line#*[$ws]}"
567   local telinitq=""
568   while [ -n "$line" ]; do
569     case "$this" in
570       console=*)
571         local serial="$this"
572         local device="${this%%,*}"
573         local device="${device##*=}"
574         if echo $serial | grep -q ttyS ; then
575           local option="${serial##*,}"
576           # default (works for kvm & CO):
577           local speed="115200,57600,38400,19200,9600,4800,2400,1200";
578           # ... unless overriden by command line:
579           case "$option" in
580             115200*) speed=115200 ;;
581              57600*) speed=57600 ;;
582              38400*) speed=38400 ;;
583              19200*) speed=19200 ;;
584               9600*) speed=9600 ;;
585               4800*) speed=4800 ;;
586               2400*) speed=2400 ;;
587               1200*) speed=1200 ;;
588           esac
589           eindent
590             einfo "Activating console login on device ${device} with speed ${speed}."
591             local number="${device#ttyS}"
592             sed -i "/^T$number:/d;/^#grmlserial#/iT$number:23:respawn:/bin/bash -c \"/sbin/getty -L $device -l /usr/share/grml-scripts/run-welcome $speed vt100 || sleep 30\"" /etc/inittab
593             eend $?
594             telinitq="1"
595           eoutdent
596         fi
597         ;;
598     esac
599     this="${line%%[$ws]*}"
600     line="${line#*[$ws]}"
601   done
602
603   if [ -n "$telinitq" ]; then
604     /sbin/telinit q
605   fi
606   eend $?
607 fi
608 }
609 # }}}
610
611 # {{{ CD Checker
612 config_testcd(){
613 if checkbootparam 'testcd' ; then
614   einfo "Checking CD data integrity as requested by '${WHITE}testcd${NORMAL}' boot option."
615   eindent
616
617   local ERROR=true
618   local FOUND_FILE=false
619   local logfile='/tmp/md5sum.log'
620
621   rm -f "$logfile"
622
623   for md5 in $(find "${LIVECD_PATH}" -name md5sums) ; do
624     einfo "Checking files against $md5, this may take a while..."
625
626     FOUND_FILE=true
627     OLD_PWD=$(pwd)
628     cd $(dirname "$md5")
629     md5sum -c $(basename "$md5") |& tee -a "${logfile}"
630     if [ $pipestatus[1] -eq 0 ] ; then
631       ERROR=false
632     fi
633     cd "${OLD_PWD}"
634   done
635
636   if ! $FOUND_FILE ; then
637     eerror 'Error: Could not find md5sum file' ; eend 1
638     return
639   fi
640
641   if ! $ERROR ; then
642     einfo "Everything looks OK" ; eend 0
643   else
644     eerror 'Checksum failed for theses files:' ; eend 1
645     egrep -v '(^md5sum:|OK$)' "${logfile}"
646     eerror 'Data on the medium is possibly incomplete/damaged or RAM of your system is broken.' ; eend 1
647     einfon "Hit return to continue, or press the power button to shut down system."
648     read a
649   fi
650
651   eoutdent
652 fi
653 }
654 # }}}
655
656 # {{{ blacklist specific module [ used in /etc/init.d/udev ]
657 config_blacklist(){
658 if checkbootparam 'blacklist' ; then
659  if [ -z "$INSTALLED" ]; then
660   einfo "Bootoption blacklist found."
661   BLACK="$(getbootparam 'blacklist' 2>>$DEBUG)"
662   BLACKLIST_FILE='/etc/modprobe.d/grml.conf'
663   if [ -n "$BLACK" ] ; then
664     for module in $(echo ${BLACK//,/ }) ; do
665         einfo "Blacklisting module ${module} via ${BLACKLIST_FILE}."
666         echo "# begin entry generated by config_blacklist of grml-autoconfig" >> "$BLACKLIST_FILE"
667         echo "blacklist $module"     >> "$BLACKLIST_FILE"
668         echo "alias     $module off" >> "$BLACKLIST_FILE"
669         echo "# end   entry generated by config_blacklist of grml-autoconfig" >> "$BLACKLIST_FILE" ; eend $?
670     done
671   else
672    eerror "No given module for blacklist found. Blacklisting will not work therefore."
673   fi
674  else
675   ewarn "Backlisting via bootoption is not intended for use on harddisk installations." ; eend 1
676   eindent
677    einfo "Please blacklist the module(s) manually using the 'blacklist' script."
678   eoutdent
679  fi
680 fi
681 }
682 # }}}
683
684 # {{{ ACPI
685 config_acpi(){
686 if checkbootparam 'noacpi'; then
687   ewarn "ACPI: Not loading modules as requested by boot option \"noacpi\"." ; eend 0
688 elif checkbootparam 'nogrmlacpi' ; then
689   ewarn "ACPI: Not loading modules as requested by boot option \"nogrmlacpi\"." ; eend 0
690 elif [ ! -d /proc/acpi ] ; then
691   ewarn "ACPI: Kernel support not present." ; eend 0
692 else
693   einfo "ACPI: Loading modules (disable with boot option noacpi / nogrmlacpi): "
694   eindent
695   found=""
696   for a in /lib/modules/$KERNEL/kernel/drivers/acpi/*; do
697     basename="${a##*/}"
698     basename="${basename%%.*}"
699     case "$basename" in *_acpi)
700      egrep -qi "${basename%%_acpi}" /proc/acpi/dsdt 2>>$DEBUG || continue ;;
701     esac
702     modprobe $basename >>$DEBUG 2>&1 && found="yes"
703     local BASE="$BASE $basename"
704   done
705   if [ -n "$found" ] ; then
706     einfo "$BASE"  ; eend 0
707   else
708     ewarn "(none)" ; eend 1
709   fi
710   if ! pgrep acpid >/dev/null ; then
711     einfo "Starting acpi daemon."
712     $SYSTEMD && systemctl start acpid.socket
713     /etc/init.d/acpid start >>$DEBUG 2>&1 ; eend $?
714   else
715     ewarn "acpi daemon already running."
716     eend 0
717   fi
718   eoutdent
719 fi
720 }
721 # }}}
722
723 # {{{ Start brltty
724 config_brltty() {
725   if checkbootparam 'brltty' ; then
726     [ -x /lib/brltty/brltty.sh ] && /lib/brltty/brltty.sh
727   fi
728 }
729 # }}}
730
731 # {{{ Start creating /etc/fstab with HD partitions and USB SCSI devices now
732 config_fstab(){
733
734 NOSWAP="yes" # we do not use swap by default!
735 if checkbootparam 'swap' || checkbootparam 'anyswap' ; then
736    NOSWAP=''
737    checkbootparam 'anyswap' && export ANYSWAP='yes' || export ANYSWAP=""
738 fi
739
740 # Scan for swap, config, homedir - but only in live-mode
741 if [ -z "$INSTALLED" ] ; then
742    [ -z "$NOSWAP" ] && einfo "Searching for swap partition(s) as requested."
743    GRML_IMG=""
744    GRML_SWP=""
745    HOMEDIR="$(getbootparam 'home')"
746    if [ -n "$partitions" ]; then
747       while read p m f relax; do
748         case "$p" in *fd0*|*proc*|*sys*|*\#*) continue;; esac
749         partoptions="users,exec"
750         fnew=""
751         # it's a swap partition?
752         case "$f" in swap)
753           eindent
754           if [ -n "$NOSWAP" ]; then
755              ewarn "Ignoring swap partition ${WHITE}$p${NORMAL}. (Force usage via boot option 'swap', or execute grml-swapon)"
756              eend 0
757           else
758              case "$(dd if=$p bs=1 count=6 skip=4086 2>/dev/null)" in
759                    S1SUSP|S2SUSP|pmdisk|[zZ]*)
760                      if [ -n "$ANYSWAP" ] ; then
761                         einfo "Using swap partition ${WHITE}${p}${NORMAL} [bootoption anyswap found]."
762                         swapon $p 2>>$DEBUG ; eend $?
763                      else
764                         ewarn "Suspend signature on ${WHITE}${p}${NORMAL} found, not using as swap. (Force usage via boot option: anyswap)"
765                      fi
766                      ;;
767                    *)
768                      if [[ "$p" == LABEL* ]] ; then
769                         p=$(blkid -t $p | awk -F: '{print $1}')
770                      fi
771                      if grep -q $p /proc/swaps ; then
772                         ewarn "Not using swap partition ${WHITE}${p}${NORMAL} as it is already in use." ; eend 0
773                      else
774                         if [ -b "$p" ] ; then
775                         einfo "Using swap partition ${WHITE}${p}${NORMAL}."
776                         swapon $p 2>>$DEBUG ; eend $?
777                         else
778                         ewarn "$p is not a valid block device - not using it therefore." ; eend 0
779                         fi
780                      fi
781                      ;;
782              esac # dd-check
783           fi # -n "$NOSWAP
784           eoutdent
785           continue
786           ;;
787         esac # it's a swap partition?
788
789         # mount read-only
790         MOUNTOPTS="ro"
791         case "$f" in
792           vfat|msdos|ntfs) MOUNTOPTS="$MOUNTOPTS,uid=${fstabuser},gid=${fstabuser}" ;;
793           ext2|ext3|reiserfs|jfs|reiser4|xfs) MOUNTOPTS="$MOUNTOPTS,noatime" ;;
794           *) continue ;;
795           # *) NONEFOUND='1'; continue ;;
796         esac
797
798         # use a swapfile
799         if [ -z "$NOSWAP" ] ; then
800            mount -o "$MOUNTOPTS" -t $f $p $m 2>>$DEBUG && MOUNTED=1 || continue
801            # Activate swapfile, if exists
802            SWAPFILE="$(/bin/ls -1d $m/[Gg][Rr][Mm][Ll].[Ss][Ww][Pp] 2>/dev/null)"
803         fi
804         if [ -z "$NOSWAP" -a -n "$SWAPFILE" -a -f "$SWAPFILE" ]; then
805            mount -o remount,rw $m && MOUNTED=1
806            if swapon "$SWAPFILE" 2>>$DEBUG ; then
807               eindent
808                 einfo "Using GRML swapfile ${WHITE}${SWAPFILE}${NORMAL}."
809               eoutdent
810               fnew="$SWAPFILE swap swap defaults 0 0"
811               grep -q "$fnew" "/etc/fstab" || echo "$fnew" >> /etc/fstab
812               GRML_SWP="$GRML_SWP $SWAPFILE"
813               eend 0
814            fi
815            mount -o remount,ro $m 2>>$DEBUG && MOUNTED=1
816         fi
817
818         # use a image as home
819         IMAGE="$(/bin/ls -1d $m/[Gg][Rr][Mm][Ll].[Ii][Mm][Gg] 2>/dev/null)"
820         if [ -z "$GRML_IMG" -a -n "$IMAGE" -a -f "$IMAGE" ]; then
821            if [ -n "$HOMEDIR" ]; then
822               if [ "$HOMEDIR" != "scan" -a "$HOMEDIR" != "$IMAGE" -a "$HOMEDIR" != "${IMAGE%/*.*}" ]; then
823                  continue
824               fi
825            fi
826            if type -a grml-image >/dev/null 2>&1 && grml-image "$IMAGE" </dev/console >/dev/console 2>&1; then
827               GRML_IMG="$IMAGE"
828               mount -o remount,ro $m 2>>$DEBUG && MOUNTED=1
829            fi
830         fi
831         eend 0
832
833         # Umount, if not in use
834         [ -n "$MOUNTED" ] && umount -r $m 2>/dev/null
835
836       done <<EOT
837       $(cat /etc/fstab)
838 EOT
839    fi # -n $partitions
840 fi # -z $INSTALLED
841 }
842 # }}}
843
844 # {{{ CPU-detection
845 config_cpu(){
846 if checkbootparam 'nocpu'; then
847   ewarn "Skipping CPU detection as requested on boot commandline." ; eend 0
848   return 0
849 fi
850
851 if [[ $(grep -c processor /proc/cpuinfo) -gt 1 ]] ; then
852    einfo "Found CPU:"
853    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)
854    echo $CPU | sed 's/ \{1,\}/ /g'
855    eend 0
856 else
857    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
858 fi
859
860 # no cpufreq setup inside VirtualBox
861 if $VIRTUALBOX ; then
862    einfo 'Virtual Box detected, skipping cpufreq setup.' ; eend 0
863    return 0
864 fi
865
866 if [ -x /etc/init.d/loadcpufreq ] ; then
867    einfo "Trying to set up cpu frequency scaling:"
868    eindent
869    SKIP_CPU_GOVERNOR=''
870    LOADCPUFREQ=$(mktemp)
871    /etc/init.d/loadcpufreq start >"$LOADCPUFREQ" 2>&1 ; RC=$?
872    if grep -q FATAL "$LOADCPUFREQ" ; then
873       eindent
874         SKIP_CPU_GOVERNOR=1
875         oldIFS="$IFS"
876         IFS="
877 "
878          for line in $(grep FATAL "$LOADCPUFREQ" | sed 's/.*FATAL: //; s/ (.*)//') ; do
879              eerror "$line" ; eend $RC
880          done
881          IFS="$oldIFS"
882       eoutdent
883    elif grep -q done "$LOADCPUFREQ" ; then
884       MODULE=$(grep done "$LOADCPUFREQ" | sed 's/.*done (\(.*\))./\1/')
885       if [ -n "$MODULE" -a "$MODULE" != none ]; then
886          einfo "Loading cpufreq kernel module $MODULE" ; eend 0
887       else
888          SKIP_CPU_GOVERNOR=1
889          ewarn "Could not find an appropriate kernel module for cpu frequency scaling." ; eend 1
890       fi
891    fi
892
893    rm -f "$LOADCPUFREQ"
894
895    if [ -z "$SKIP_CPU_GOVERNOR" ] ; then
896      if [ -r /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors ] ; then
897        if ! grep -q ondemand /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors ; then
898          einfo "Ondemand governor not available for CPU(s), not modifying governor configuration"
899        else
900          einfo "Setting ondemand governor"
901          RC=0
902          for file in $(find /sys/devices/system/cpu/ -name scaling_governor 2>/dev/null) ; do
903            echo ondemand > $file || RC=1
904          done
905          eend $RC
906        fi
907      fi
908    fi
909
910    eoutdent
911 fi
912 }
913 # }}}
914
915 # {{{ autostart of ssh
916 config_ssh(){
917 if checkbootparam 'ssh' ; then
918    local PASSWD
919    PASSWD="$(getbootparam 'ssh' 2>>$DEBUG)"
920
921    config_userlocal
922    einfo "Bootoption ssh found, trying to set password for root and user $localuser"
923    [ -z "$localuser" ] && eend 1
924
925    eindent
926    if [ -z "$PASSWD" ] ; then
927      set_passwd && ewarn "No given password for found. Using random password: $PASSWD" && eend 0
928    fi
929    eoutdent
930
931    if [ -n "$PASSWD" ] ; then
932       chpass_options=""
933       if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
934         chpass_options="-m"
935       fi
936
937       echo "$localuser:$PASSWD" | chpasswd $chpass_options
938       echo "root:$PASSWD" | chpasswd $chpass_options
939
940       eindent
941       ewarn "Warning: please change the password for root and user $localuser as soon as possible!"
942       eoutdent
943    fi
944
945    einfo "Starting secure shell server in background for root and user $localuser"
946    /etc/init.d/rmnologin start >>$DEBUG 2>>$DEBUG
947    /etc/init.d/ssh start >>$DEBUG 2>>$DEBUG &
948    eend $?
949
950 fi
951 }
952
953 # }}}
954
955 # {{{ display hostkeys of SSH server
956 config_display_ssh_fingerprints() {
957   if ! ls /etc/ssh/ssh_host_\*_key >/dev/null 2>&1 ; then
958     return 0 # no SSH host keys present
959   fi
960
961   einfo "SSH key fingerprints:"
962   for file in /etc/ssh/ssh_host_*_key ; do
963     einfon
964     ssh-keygen -l -f $file
965   done | column -t
966   eend $?
967 }
968 # }}}
969
970 # {{{ autostart of x11vnc
971 config_vnc(){
972 if checkbootparam 'vnc' ; then
973    config_userlocal
974    VNC_PASSWD=''
975    VNC_PASSWD="$(getbootparam 'vnc' 2>>$DEBUG)"
976    einfo "Bootoption vnc found, trying to set password for user $localuser."
977    eindent
978    if [ -z "$VNC_PASSWD" ] ; then
979       if [ -x /usr/bin/apg ] ; then
980          VNC_PASSWD="$(apg -M NL -a 0 -m 8 -x 12 -n 1)"
981       elif [ -x /usr/bin/gpw ] ; then
982          VNC_PASSWD="$(gpw 1)"
983       elif [ -x /usr/bin/pwgen ] ; then
984          VNC_PASSWD="$(pwgen -1 8)"
985       elif [ -x /usr/bin/hexdump ] ; then
986          VNC_PASSWD="$(dd if=/dev/urandom bs=14 count=1 2>/dev/null | hexdump | awk '{print $3 $4}')"
987       elif [ -n "$RANDOM" ] ; then
988          VNC_PASSWD="${localuser}${RANDOM}"
989       else
990          VNC_PASSWD=''
991          eerror "Empty passphrase and neither pwgen nor hexdump nor \$RANDOM found. Skipping."
992          eend 1
993       fi
994
995       if [ -n "$VNC_PASSWD" ] ; then
996          ewarn "No given password for vnc found. Using random password: $VNC_PASSWD" ; eend 0
997       fi
998    fi
999    eoutdent
1000
1001    # finally check if we have a password we can use:
1002    if [ -n "$VNC_PASSWD" ] ; then
1003
1004       VNCDIR="/home/${localuser}/.vnc"
1005       [ -d "$VNCDIR" ] || mkdir "$VNCDIR"
1006
1007       if [ ! -x /usr/bin/x11vnc ] ; then
1008          eerror "Error: x11vnc not found - can not set up vnc. Please make sure to install the x11vnc package."
1009          eend 1
1010       else
1011          /usr/bin/x11vnc -storepasswd "$VNC_PASSWD" "$VNCDIR"/passwd ; eend $?
1012          /bin/chown -R "$localuser": "$VNCDIR"
1013       fi
1014    fi
1015    if checkbootparam 'vnc_connect' ; then
1016       VNC_CONNECT=''
1017       VNC_CONNECT="$(getbootparam 'vnc_connect' 2>>$DEBUG)"
1018       einfo "Bootoption vnc_connect found, will start vnc with connect to $VNC_CONNECT."
1019       #store the options in a file
1020       VNCDIR="/home/${localuser}/.vnc"
1021       [ -d "$VNCDIR" ] || mkdir "$VNCDIR"
1022       echo " --connect $VNC_CONNECT " >> $VNCDIR/options
1023    fi
1024 fi
1025 }
1026 # }}}
1027
1028 # {{{ set password for root and default user
1029 config_passwd(){
1030 if checkbootparam 'passwd' >>$DEBUG 2>&1; then
1031   local PASSWD
1032   PASSWD="$(getbootparam 'passwd' 2>>$DEBUG)"
1033
1034   config_userlocal
1035   einfo "Bootoption passwd found, trying to set password for root and user $localuser"
1036   [ -z "$localuser" ] && eend 1
1037
1038   eindent
1039   if [ -z "$PASSWD" ] ; then
1040     set_passwd && ewarn "No given password for found. Using random password: $PASSWD" && eend 0
1041   fi
1042   eoutdent
1043
1044   if [ -n "$PASSWD" ] ; then
1045     chpass_options=""
1046     if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
1047       chpass_options="-m"
1048     fi
1049
1050     echo "$localuser:$PASSWD" | chpasswd $chpass_options
1051     echo "root:$PASSWD" | chpasswd $chpass_options
1052
1053     eindent
1054     ewarn "Warning: please change the password for root and user $localuser as soon as possible!"
1055     eoutdent
1056   fi
1057
1058 fi
1059
1060 if checkbootparam 'encpasswd' >>$DEBUG 2>&1; then
1061   local PASSWD
1062   PASSWD="$(getbootparam 'encpasswd' 2>>$DEBUG)"
1063
1064   if [ -z "$PASSWD" ] ; then
1065     eerror "No hashed password found, can not set password."
1066     eend 1
1067     return
1068   fi
1069
1070   config_userlocal
1071   einfo "Bootoption encpasswd found, trying to set hashed password for root and user $localuser"
1072   [ -z "$localuser" ] && eend 1
1073
1074   if [ -n "$PASSWD" ] ; then
1075     chpass_options="-e"
1076
1077     echo "$localuser:$PASSWD" | chpasswd $chpass_options
1078     echo "root:$PASSWD" | chpasswd $chpass_options
1079
1080     eindent
1081     ewarn "Warning: please change the password for root and user $localuser as soon as possible!"
1082     eoutdent
1083   fi
1084
1085 fi
1086 }
1087 # }}}
1088
1089 # {{{ Sound
1090 config_mixer () {
1091    if ! [ -x /usr/bin/amixer ] ; then
1092       eerror "amixer binary not available. Can not set sound volumes therefore."
1093       eend 1
1094    else
1095       if ! [ -r /proc/asound/cards ] ; then
1096          ewarn "No soundcard present, skipping mixer settings therefore."
1097          eend 0
1098          return
1099       fi
1100
1101       for card in $(cat /proc/asound/cards| grep -e '^\s*[0-9]' | awk '{print $1}') ; do
1102          einfo "Configuring soundcard \"$(awk -F\[ '/^ *'$card' \[/{ FS=" "; $0=$2; print $1}' < /proc/asound/cards)\""
1103          eindent
1104
1105          if checkbootparam 'vol' ; then
1106             VOL="$(getbootparam 'vol' 2>>$DEBUG)"
1107             if [ -z "$VOL" ] ; then
1108                eerror "Bootoption vol found but no volume level/parameter given. Using defaults (75%)."
1109                VOL='75'
1110                eend 1
1111             fi
1112          else
1113             VOL='75'
1114          fi
1115
1116          if checkbootparam 'nosound' ; then
1117             einfo "Muting sound devices on request."
1118             ERROR=$(amixer -q set Master mute)
1119             RC=$?
1120             if [ -n "$ERROR" ] ; then
1121                eindent
1122                eerror "Problem muting sound devices: $ERROR"
1123                eoutdent
1124             fi
1125             eend $RC
1126          elif [ -z "$INSTALLED" ] ; then
1127             einfo "Setting mixer volumes to level ${WHITE}${VOL}${NORMAL}."
1128
1129             if checkbootparam 'micvol' ; then
1130                MICVOL="$(getbootparam 'micvol' 2>>$DEBUG)"
1131                einfo "Setting microphone to ${WHITE}${MICVOL}${NORMAL}."
1132             else
1133                MICVOL=0
1134             fi
1135
1136             CONTROLS=$(amixer -c $card scontrols | awk -F"Simple mixer control " '{print $2}')
1137             IFSOLD=${IFS:-}
1138             IFS='
1139 '
1140             for CONTROL in ${=CONTROLS} ; do
1141                # such devices can not be controlled with amixer ... unmute
1142                [[ "$CONTROL" == *Console* ]] && continue
1143
1144                if ! echo "${CONTROL}" | grep -q -i "mic" ; then
1145                    if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*pswitch' ; then
1146                       amixer -c $card -q set "${CONTROL}" unmute
1147                    fi
1148                    if amixer -c $card sget "${CONTROL}" | grep -q -P 'Capabilities:.*(pvolume| volume)' ; then
1149                       amixer -c $card -q set "${CONTROL}" "${VOL}"%
1150                    fi
1151                fi
1152
1153                if [ ${MICVOL} -ne 0 ] ; then
1154                   if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*cswitch' ; then
1155                      amixer -c $card -q set "${CONTROL}" unmute
1156                   fi
1157                   if amixer -c $card sget "${CONTROL}" | grep -q 'Capabilities:.*cvolume' ; then
1158                      amixer -c $card -q set "${CONTROL}" $MICVOL%
1159                   fi
1160                   eend $?
1161                fi
1162             done
1163             IFS=$IFSOLD
1164          fi # checkbootparam 'nosound'
1165          eoutdent
1166       done
1167    fi
1168 }
1169 # }}}
1170
1171 # {{{ syslog service
1172 config_syslog(){
1173  if checkbootparam 'nosyslog'; then
1174     ewarn "Not starting syslog daemon as requested on boot commandline." ; eend 0
1175  else
1176     SYSLOGD=''
1177     [ -x /etc/init.d/syslog-ng ] && SYSLOGD='syslog-ng'
1178     [ -x /etc/init.d/rsyslog   ] && SYSLOGD='rsyslog'
1179     [ -x /etc/init.d/dsyslog   ] && SYSLOGD='dsyslog'
1180     [ -x /etc/init.d/sysklogd  ] && SYSLOGD='sysklogd'
1181     [ -x /etc/init.d/inetutils-syslogd ] && SYSLOGD='inetutils-syslogd'
1182
1183     if [ -z "$SYSLOGD" ] ; then
1184        eerror "No syslog daemon found." ; eend 1
1185     else
1186        einfo "Starting $SYSLOGD in background."
1187        /etc/init.d/$SYSLOGD start >>$DEBUG &
1188        eend 0
1189     fi
1190  fi
1191 }
1192 # }}}
1193
1194 # {{{ gpm
1195 config_gpm(){
1196  if checkbootparam 'nogpm'; then
1197   ewarn "Not starting GPM as requested on boot commandline." ; eend 0
1198  else
1199    if ! [ -r /dev/input/mice ] ; then
1200       eerror "No mouse found - not starting GPM." ; eend 1
1201    else
1202       einfo "Starting gpm in background."
1203       /etc/init.d/gpm start >>$DEBUG &
1204       # ( while [ ! -e /dev/psaux ]; do sleep 5; done; /etc/init.d/gpm start >>$DEBUG ) &
1205       eend 0
1206    fi
1207  fi
1208 }
1209 # }}}
1210
1211 # {{{ services
1212 config_services(){
1213  if checkbootparam 'services' ; then
1214     SERVICE="$(getbootparam 'services' 2>>$DEBUG)"
1215     SERVICELIST=$(echo "$SERVICE" | sed 's/,/\\n/g')
1216     SERVICENL=$(echo "$SERVICE" | sed 's/,/ /g')
1217     for service in $(echo -e $SERVICELIST) ; do
1218       # support running (custom) init scripts in non-blocking mode
1219       # if they contain the keyword "DO_NO_RUN_IN_BACKGROUND".
1220       if grep -q 'DO_NO_RUN_IN_BACKGROUND' "/etc/init.d/${service}" 2>>$DEBUG ; then
1221         einfo "Starting service ${service}."
1222         /etc/init.d/${service} start >>$DEBUG
1223       else
1224         einfo "Starting service ${service} in background."
1225         /etc/init.d/${service} start >>$DEBUG &
1226       fi
1227     done
1228     eend $?
1229  fi
1230 }
1231 # }}}
1232
1233 # {{{ remote files
1234 get_remote_file() {
1235   [ "$#" -eq 2 ] || ( echo "Error: wrong parameter for get_remote_file()" ; return 1 )
1236   SOURCE=$(eval echo "$1")
1237   TARGET="$2"
1238   getconfig() {
1239   wget --timeout=10 --dns-timeout=10  --connect-timeout=10 --tries=1 \
1240        --read-timeout=10 ${SOURCE} -O ${TARGET} && return 0 || return 1
1241   }
1242   einfo "Trying to get ${WHITE}${TARGET}${NORMAL}"
1243
1244   if checkbootparam 'getfile.retries' ; then
1245     local counter="$(getbootparam 'getfile.retries' 2>>$DEBUG)"
1246   else
1247     local counter=10
1248   fi
1249
1250   while ! getconfig && [[ "$counter" != 0 ]] ; do
1251     echo -n "Sleeping for 1 second and trying to get config again... "
1252     counter=$(( counter-1 ))
1253     echo "$counter tries left" ; sleep 1
1254   done
1255   if [ -s "$TARGET" ] ; then
1256     einfo "Downloading was successfull." ; eend 0
1257     einfo "md5sum of ${WHITE}${TARGET}${NORMAL}: "
1258     md5sum ${TARGET} ; eend 0
1259     return 0;
1260   else
1261     einfo "Sorry, could not fetch ${SOURCE}" ; eend 1
1262     return 1;
1263  fi
1264 }
1265 # }}}
1266
1267 # {{{ config files
1268 config_netconfig(){
1269  if checkbootparam 'netconfig' ; then
1270   CONFIG="$(getbootparam 'netconfig' 2>>$DEBUG)"
1271   CONFIGFILE='/tmp/netconfig.grml'
1272
1273   if get_remote_file ${CONFIG} ${CONFIGFILE} ; then
1274     cd / && einfo "Unpacking ${WHITE}${CONFIGFILE}${NORMAL}:" && /usr/bin/unp $CONFIGFILE $EXTRACTOPTIONS ; eend $?
1275   fi
1276
1277  fi
1278 }
1279 # }}}
1280
1281 # {{{ remote scripts
1282 config_netscript() {
1283  if checkbootparam 'netscript' ; then
1284   CONFIG="$(getbootparam 'netscript' 2>>$DEBUG)"
1285   SCRIPTFILE='/tmp/netscript.grml'
1286
1287   if get_remote_file ${CONFIG} ${SCRIPTFILE} ; then
1288     chmod +x ${SCRIPTFILE}
1289     einfo "Running ${WHITE}${SCRIPTFILE}${NORMAL}:" && NETSCRIPT=${CONFIG} ${SCRIPTFILE} ; eend $?
1290   fi
1291
1292  fi
1293 }
1294 # }}}
1295
1296 # {{{ stats
1297 config_stats() {
1298  if ! checkbootparam 'nostats' ; then
1299    BASE_URL="http://stats.grml.org/report/"
1300    ACTION_NAME=Boot
1301
1302    HOST_ID=$(cat /proc/sys/kernel/random/boot_id)
1303
1304    grep -q " lm " /proc/cpuinfo && HAS_64BIT="1" || HAS_64BIT="0"
1305    DATE_STRING=$(date +'h=%H&m=%M&s=%S')
1306    [ -e /etc/grml_version ] && VERSION=$(cat /etc/grml_version) || \
1307      VERSION=$(lsb_release -d | awk -F: '{gsub(/^[ \t]+/, "", $2); print $2}')
1308
1309    PARAMS="$( echo "$CMDLINE" | sed -e 's/=[^ ]*/=x/g' | tr " " "\n"|sort|tr "\n" " " )"
1310
1311    echo "$CMDLINE" | grep -q -e "fetch" -e "nfsroot" && BOOT="remote"
1312    [ -z "$BOOT" ] && BOOT="local"
1313
1314    ADDITIONAL_PARAMS=""
1315    ( [ -n "$COLUMNS" ] && [ -n "$LINES" ] ) && \
1316      ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS&res=$((COLUMNS * 8))x$((LINES * 16))"
1317
1318    URI='$BASE_URL?action=${ACTION_NAME}\&$DATE_STRING\&unique_id=${HOST_ID}\&support_64bit=$HAS_64BIT\&version=$VERSION\&bootup=$BOOT\&params=$PARAMS$ADDITIONAL_PARAMS'
1319
1320    get_remote_file "$URI" "/dev/null"  >/dev/null 2>&1 &!
1321  fi
1322 }
1323 # }}}
1324
1325 # {{{ start X window system via grml-x
1326 config_x_startup(){
1327 # make sure we start X only if startx is used *before* a nostartx option
1328 # so it's possible to disable automatic X startup using nostart
1329 if checkbootparam 'startx' && ! echo "$CMDLINE" | grep -q 'startx.*nostartx' ; then
1330  if [ -x "$(which X)" ] ; then
1331   if [ -z "$INSTALLED" ] ; then
1332    WINDOWMANAGER="$(getbootparam 'startx' 2>>$DEBUG)"
1333    if [ -z "$WINDOWMANAGER" ] ; then
1334      einfo "No window manager specified. Using default one." && eend 0
1335    else
1336      einfo "Window manager ${WHITE}${WINDOWMANAGER}${NORMAL} found as bootoption." && eend 0
1337    fi
1338    einfo "Setting up and invoking grml-x ${WINDOWMANAGER}. Just exit X windows system to get full featured consoles."
1339    config_userlocal
1340  cat>|/etc/init.d/xstartup<<EOF
1341 #!/bin/sh
1342 su $localuser -c "/usr/bin/grml-x ${WINDOWMANAGER}"
1343 EOF
1344    chmod 755 /etc/init.d/xstartup
1345
1346    # adjust inittab for xstartup
1347    if grep -q '^6:' /etc/inittab ; then
1348       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
1349    else # just append tty6 to inittab if no definition is present:
1350       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
1351    fi
1352
1353    /sbin/telinit q ; eend $?
1354
1355    if grep -q '^allowed_users=' /etc/X11/Xwrapper.config ; then
1356       sed -i 's/^allowed_users=.*/allowed_users=anybody/' /etc/X11/Xwrapper.config
1357    else
1358       echo 'allowed_users=anybody' >> /etc/X11/Xwrapper.config
1359    fi
1360
1361   else
1362     eerror "We are not running in live mode - startx will not work, skipping it."
1363     eerror " -> Please use something like xdm, gdm or kdm for starting X on a harddisk system!" ; eend 1
1364   fi
1365  else
1366    eerror "/usr/bin/X is not present on this grml flavour."
1367    eerror "  -> Boot parameter startx does not work therefore." ; eend 1
1368  fi
1369 fi
1370 }
1371 # }}}
1372
1373 # {{{ configuration framework
1374 config_extract(){
1375 if checkbootparam 'extract' ; then
1376  EXTRACT="$(getbootparam 'extract' 2>>$DEBUG)"
1377  EXTRACTOPTIONS="-- -x $EXTRACT"
1378 fi
1379 }
1380
1381 config_finddcsdir() {
1382 #  - If no GRMLCFG partition is found and noautoconfig is _not_ given
1383 #    on the command line, nothing is changed and the dcs files are
1384 #    searched within the .iso, $dcs-dir is set to the root directory
1385 #    within the .iso
1386 #  - If a GRMLCFG partition is found, $dcs-dir is set to the root of
1387 #    the GRMLCFG partition unless noautoconfig is set. If noautoconfig is
1388 #    set, $dcs-dir is set to the root directory within the .iso.
1389 #  - If myconfig=foo is set on the command line, $dcs-dir is set to
1390 #    foo, even if a GRMLCFG partition is present.
1391 DCSDIR=""
1392 DCSMP="/mnt/grml"
1393 # autoconfig, see issue673
1394 GRMLCFG="$(getbootparam 'autoconfig' 2>>$DEBUG)"
1395 [ -n "$GRMLCFG" ] || GRMLCFG="GRMLCFG"
1396 if checkbootparam 'noautoconfig' ; then
1397   DCSDIR="${LIVECD_PATH}" # set default so it works for "scripts" boot option as expected
1398   ewarn "Skipping running automount of device(s) labeled $GRMLCFG as requested." ; eend 0
1399 else
1400   if [ -z "$INSTALLED" ] ; then
1401     if checkbootparam 'myconfig' ; then
1402       DCSDEVICE="$(getbootparam 'myconfig' 2>>$DEBUG)"
1403       if [ -z "$DCSDEVICE" ]; then
1404         eerror "Error: No device for bootoption myconfig provided." ; eend 1
1405       fi # [ -z "$DCSDEVICE" ]
1406     elif checkvalue $CONFIG_MYCONFIG; then # checkbootparam myconfig
1407       einfo "Searching for device(s) labeled with $GRMLCFG. (Disable this via boot option: noautoconfig)" ; eend 0
1408       eindent
1409       DCSDEVICE=$(blkid -t LABEL=$GRMLCFG | head -1 | awk -F: '{print $1}')
1410
1411       modprobe 9p 2>/dev/null || true
1412       if [ -z "$DCSDEVICE" ] && grep -q 9p /proc/filesystems ; then
1413           if grep -q "$GRMLCFG" /sys/bus/virtio/devices/*/mount_tag 2>/dev/null ; then
1414             einfo "Found 9p-virtio fs with mount_tag $GRMLCFG"
1415             DCSDEVICE="$GRMLCFG"
1416             MOUNTOPTIONS="ro,trans=virtio"
1417             DCSFS="9p"
1418           fi
1419       fi
1420
1421       if [ -n "$DCSDEVICE" ]; then
1422         DCSMP="/mnt/grmlcfg"
1423       fi
1424       eoutdent
1425     fi
1426
1427     # if not specified/present then assume default:
1428     if [ -z "$DCSDEVICE" ]; then
1429       DCSDIR="${LIVECD_PATH}"
1430     else
1431       eindent
1432       einfo "debs, config, scripts are read from $DCSDEVICE." ; eend 0
1433       DCSDIR="$(< /proc/mounts awk -v DCSDEV=$DCSDEVICE '{if ($1 == DCSDEV) { print $2 }}')"
1434       if [ -n "$DCSDIR" ]; then
1435         ewarn "$DCSDEVICE already mounted on $DCSDIR"; eend 0
1436       else
1437         [ -d $DCSMP ] || mkdir $DCSMP
1438         umount $DCSMP >>$DEBUG 2>&1 # make sure it is not mounted
1439         mount -o ${MOUNTOPTIONS:-ro} -t ${DCSFS:-auto} $DCSDEVICE  $DCSMP ; RC="$?"
1440         if [[ $RC == 0 ]]; then
1441           einfo "Successfully mounted $DCSDEVICE to $DCSMP (readonly)." ; eend 0
1442         else
1443           eerror "Error: mounting $DCSDEVICE to $DCSMP (readonly) failed." ; eend 1
1444         fi
1445         DCSDIR="$DCSMP"
1446       fi
1447       eoutdent
1448     fi
1449   fi
1450 fi
1451
1452 if [ -n "$DCSDIR" -a "$DCSDIR" != "${LIVECD_PATH}" ] ; then
1453   einfo "Debs, config, scripts (if present) will be read from $DCSDIR." ; eend 0
1454 elif checkbootparam 'debs' || checkbootparam 'config' || checkbootparam 'scripts'; then
1455   einfo "Debs, config, scripts will be read from the live image directly." ; eend 0
1456 fi
1457 }
1458
1459
1460 config_partconf() {
1461 if checkbootparam 'partconf' ; then
1462  MOUNTDEVICE="$(getbootparam 'partconf' 2>>$DEBUG)"
1463  if [ -n "$MOUNTDEVICE" ]; then
1464    [ -d /mnt/grml ] || mkdir /mnt/grml
1465    mount -o ro -t auto $MOUNTDEVICE /mnt/grml ; RC="$?"
1466     if [[ $RC == 0 ]]; then
1467       einfo "Successfully mounted $MOUNTDEVICE to /mnt/grml (readonly)." ; eend 0
1468       einfo "Copying files from $MOUNTDEVICE over grml system."
1469       for file in `cat /etc/grml/partconf` ; do
1470         [ -d /mnt/grml/$file ] && cp -a /mnt/grml/${file}* ${file} && echo "copied: $file"
1471         [ -f /mnt/grml/$file ] && cp -a /mnt/grml/${file}  ${file} && echo "copied: $file"
1472       done && eend 0
1473     else
1474       einfo "Could not mount $MOUNTDEVICE to /mnt/grml - sorry." ; eend 1
1475     fi # mount $MOUNTDEVICE
1476    grep -q '/mnt/grml' /proc/mounts && umount /mnt/grml
1477  else
1478    einfo "Sorry, no device for bootoption partconf provided. Skipping." ; eend 1
1479  fi # [ -n "$MOUNTDEVICE" ]
1480 fi
1481 }
1482 # }}}
1483
1484 # {{{ /cdrom/.*-options
1485 config_debs(){
1486 if checkbootparam 'debs' ; then
1487    iszsh && setopt localoptions shwordsplit
1488    DEBS="$(getbootparam 'debs' 2>>$DEBUG)"
1489    if [ -z "$DEBS" ] ; then
1490       DEBS="*.deb"
1491    fi
1492    if ! echo $DEBS | grep -q '/'; then
1493      # backwards compatibility: if no path is given get debs from debs/
1494      DEBS="debs/$DEBS"
1495    fi
1496    einfo "Trying to install Debian package(s) ${DEBS}"
1497    DEBS="$(eval echo ${DCSDIR}/$DEBS)"
1498    dpkg -i $DEBS ; eend $?
1499 fi
1500 }
1501
1502 config_scripts(){
1503 if checkbootparam 'scripts' || [ "$DCSMP" = "/mnt/grmlcfg" ]; then
1504    SCRIPTS="$(getbootparam 'scripts' 2>>$DEBUG)"
1505    if [ -d ${DCSDIR}/scripts ] && [ -z "$SCRIPTS" ]; then
1506      SCRIPTS="$(cd ${DCSDIR}/scripts; /bin/ls -1d [Gg][Rr][Mm][Ll].[Ss][Hh] 2>>$DEBUG)"
1507    fi
1508    if ! echo $SCRIPTS | grep -q '/'; then
1509      # backwards compatibility: if no path is given get scripts from scripts/
1510      SCRIPTS="scripts/$SCRIPTS"
1511    fi
1512    if [ -n "$SCRIPTS" ]; then
1513      SCRIPTS="${DCSDIR}/$SCRIPTS"
1514      if [ "$DCSMP" = "/mnt/grmlcfg" ]; then
1515        einfo "Trying to execute ${SCRIPTS}"
1516        sh -c $SCRIPTS
1517        eend $?
1518      elif [ -d "$SCRIPTS" ]; then
1519        einfo "Bootparameter scripts found. Trying to execute from directory ${SCRIPTS}:"
1520        run-parts --regex '.*' $SCRIPTS
1521        eend $?
1522      else
1523        einfo "Bootparameter scripts found. Trying to execute ${SCRIPTS}:"
1524        sh -c $SCRIPTS
1525        eend $?
1526      fi
1527    fi
1528 fi
1529 }
1530
1531 config_config(){
1532 if checkbootparam 'config' || [ "$DCSMP" = "/mnt/grmlcfg" ]; then
1533   CONFIG="$(getbootparam 'config' 2>>$DEBUG)"
1534   if [ -z "$CONFIG" ]; then
1535     CONFIG="$(cd ${DCSDIR}; ls -1d [Cc][Oo][Nn][Ff][Ii][Gg].[Tt][Bb][Zz] 2>>$DEBUG)"
1536   fi
1537   if [ -n "$CONFIG" ]; then
1538     if [ -d "${DCSDIR}/${CONFIG}" ] ; then
1539       einfo "Taking configuration from directory ${DCSDIR}/${CONFIG}"
1540
1541       cp -a ${DCSDIR}/${CONFIG}/* /
1542     elif [ -f "${DCSDIR}/${CONFIG}" ]; then
1543       einfo "Extracting configuration from file ${DCSDIR}/${CONFIG}"
1544
1545       cd /
1546       unp ${DCSDIR}/${CONFIG} $EXTRACTOPTIONS ; eend $?
1547     else
1548       ewarn "Sorry, could not find configuration file or directory ${DCSDIR}/${FILENAME}." ; eend 1
1549     fi
1550   fi
1551 fi
1552 }
1553 # }}}
1554
1555 # {{{ confing_umount_dcsdir
1556 config_umount_dcsdir(){
1557    # umount $DCSMP if it was mounted by finddcsdir
1558    grep -q "$DCSMP" /proc/mounts && umount "$DCSMP"
1559 }
1560 # }}}
1561
1562 # {{{ mypath
1563 config_mypath(){
1564 if checkbootparam 'mypath' ; then
1565    MY_PATH="$(getbootparam 'mypath' 2>>$DEBUG)"
1566    einfo "Bootparameter mypath found, adding ${MY_PATH} to /etc/grml/my_path"
1567    touch /etc/grml/my_path
1568    chmod 644 /etc/grml/my_path
1569    # make sure the directories exist:
1570    eindent
1571    for i in $(echo $MY_PATH | sed 's/:/\n/g') ; do
1572        if ! [ -d "$i" ] ; then
1573           einfo "Creating directory $i"
1574           mkdir -p "$i" ; eend $?
1575        fi
1576    done
1577    grep -q "${MY_PATH}" /etc/grml/my_path || echo "${MY_PATH}" >> /etc/grml/my_path ; eend $?
1578    eoutdent
1579 fi
1580 }
1581 # }}}
1582
1583 # {{{ SW-RAID
1584 config_swraid(){
1585   [ -n "$INSTALLED" ] && return 0
1586
1587   if checkbootparam 'noraid'   || checkbootparam 'noswraid' || \
1588      checkbootparam 'raid=noautodetect' ; then
1589      ewarn "Skipping SW-RAID code as requested on boot commandline." ; eend 0
1590   else
1591     [ -e /proc/mdstat ] || modprobe md_mod
1592     if ! [ -x /sbin/mdadm ] ; then
1593        eerror "mdadm not available, can not execute it." ; eend 1
1594     else
1595
1596        # if ! egrep -qv '^(MAILADDR.*|#.*|)$' /etc/mdadm/mdadm.conf 2>>$DEBUG ; then
1597        # find out whether we have a valid configuration file already
1598        if ! grep -q ARRAY /etc/mdadm/mdadm.conf 2>>$DEBUG ; then
1599           einfo "Creating /etc/mdadm/mdadm.conf for use with mdadm."
1600           [ -r /etc/mdadm/mdadm.conf ] && mv /etc/mdadm/mdadm.conf /etc/mdadm/mdadm.conf.old
1601           MDADM_MAILADDR__='root' /usr/share/mdadm/mkconf > /etc/mdadm/mdadm.conf ; eend $?
1602         else
1603           ewarn "/etc/mdadm/mdadm.conf looks like a configured mdadm setup, will not touch it." ; eend 0
1604        fi
1605
1606        if ! checkbootparam 'swraid' ; then
1607           eindent
1608           einfo "Just run 'Start mdadm-raid' to assemble md arrays or boot using 'swraid' as bootoption for autostart."
1609           eoutdent
1610        else
1611           einfo "Bootoption swraid found. Searching for software RAID arrays:"
1612           eindent
1613            IFSOLD=${IFS:-}
1614            IFS='
1615 '
1616            for line in $(mdadm --assemble --scan --auto=yes --symlink=no 2>&1) ; do
1617                case $line in
1618                  *'No arrays found'*)
1619                    ewarn "$line" ; eend 0
1620                    ;;
1621                  *)
1622                    einfo "$line" ; eend 0
1623                    ;;
1624                esac
1625            done
1626            IFS=$IFSOLD
1627          eoutdent
1628
1629          if [ -r /proc/mdstat ] ; then
1630             eindent
1631             MDSTAT=$(grep '^md[0-9]' /proc/mdstat)
1632             if [ -z "$MDSTAT" ] ; then
1633                ewarn "No active arrays found" ; eend 0
1634             else
1635                IFSOLD=${IFS:-}
1636                IFS='
1637 '
1638                for line in $(grep '^md[0-9]' /proc/mdstat) ; do
1639                    einfo "active arrays: $line" ; eend 0
1640                done
1641                IFS=$IFSOLD
1642             fi
1643             eoutdent
1644          fi # /proc/mdstat
1645        fi # bootoption swraid
1646
1647      fi # is /sbin/mdadm executable?
1648   fi # check for bootoptions
1649 }
1650 # }}}
1651
1652 # {{{ dmraid
1653 config_dmraid(){
1654   [ -n "$INSTALLED" ] && return 0
1655
1656   if checkbootparam 'nodmraid' ; then
1657     ewarn "Skipping dmraid code as requested on boot commandline." ; eend 0
1658     return 0
1659   fi
1660
1661   if ! [ -x /sbin/dmraid ] ; then
1662     eerror "dmraid not available, can not execute it." ; eend 1
1663     return
1664   fi
1665
1666   dmraid_wrapper() {
1667     # usage: dmraid_wrapper <dmraid_option>
1668     [ -n "$1" ] || return 1
1669
1670     IFSOLD=${IFS:-}
1671     IFS='
1672 '
1673     eindent
1674
1675     for line in $(dmraid $1 ; echo errcode:$?); do
1676       case $line in
1677         *'no block devices found'*)
1678           einfo "No block devices found" ; eend 0
1679           break
1680           ;;
1681         *'no raid disks'*)
1682           einfo "No active dmraid devices found" ; eend 0
1683           break
1684           ;;
1685         errcode:0)
1686           eend 0;
1687           ;;
1688         errcode:1)
1689           eend 1
1690           ;;
1691         *)
1692           einfo "$line"
1693           ;;
1694       esac
1695     done
1696
1697     eoutdent
1698     IFS=$IFSOLD
1699   }
1700
1701   if checkbootparam 'dmraid' ; then
1702     local ACTION="$(getbootparam 'dmraid' 2>>$DEBUG)"
1703     if [ "$ACTION" = "off" ] ; then
1704       # Deactivates all active software RAID sets:
1705       einfo "Deactivating present dmraid sets (as requested via dmraid=off):"
1706       dmraid_wrapper -an
1707     else
1708       # Activate all software RAID sets discovered:
1709       einfo "Activating present dmraid sets (as requested via dmraid):"
1710       dmraid_wrapper -ay
1711     fi
1712
1713     return
1714   fi
1715
1716   # by default (no special bootoptions) discover all software RAID devices:
1717   einfo "Searching for any present dmraid sets:"
1718   dmraid_wrapper -r
1719 }
1720 # }}}
1721
1722 # {{{ LVM (Logical Volumes)
1723 config_lvm(){
1724   [ -n "$INSTALLED" ] && return 0
1725
1726   if checkbootparam 'nolvm' ; then
1727      ewarn "Skipping LVM code as requested on boot commandline." ; eend 0
1728   else
1729     # Debian etch provides /etc/init.d/lvm only, newer suites provide /etc/init.d/lvm2
1730     if ! [ -x /sbin/lvm -a -x /sbin/lvdisplay ] || ! [ -x /etc/init.d/lvm2 -o -x /etc/init.d/lvm ] ; then
1731        eerror "LVM not available, can not execute it." ; eend 1
1732     else
1733        if lvdisplay 2>&1 | grep -v 'No volume groups found' >/dev/null 2>&1 ; then
1734           einfo "You seem to have logical volumes (LVM) on your system."
1735           eindent
1736           einfo "Just run 'Start lvm2' to activate them or boot using 'lvm' as bootoption for autostart."
1737           eend 0
1738           if checkbootparam 'lvm' ; then
1739              einfo "Bootoption LVM found. Searching for logical volumes:"
1740              /etc/init.d/lvm2 start ; eend $?
1741           fi
1742           eoutdent
1743        fi
1744     fi # check for lvm binary
1745   fi # check for bootoption nolvm
1746 }
1747 # }}}
1748
1749 # {{{ debnet: setup network based on an existing one found on a partition
1750 config_debnet(){
1751 if checkbootparam 'debnet' ; then
1752  einfo "Bootoption 'debnet' found. Searching for Debian network configuration: "
1753  /usr/sbin/debnet
1754 fi
1755 }
1756 # }}}
1757
1758 # {{{ disable console blanking
1759 config_blanking(){
1760 if checkbootparam 'noblank' ; then
1761   einfo "Bootoption noblank found. Disabling monitor blanking."
1762   setterm -blank 0 ; eend $?
1763 fi
1764 }
1765 # }}}
1766
1767 # {{{ debootstrap: automatic installation
1768 config_debootstrap(){
1769
1770 if checkbootparam "BOOT_IMAGE=debian2hd" || checkbootparam "debian2hd" ; then
1771
1772 einfo "Bootoption debian2hd found. Setting up environment for automatic installation via grml-debootstrap." ; eend 0
1773
1774 if ! [ -x /usr/sbin/grml-debootstrap ] ; then
1775    eindent
1776    eerror "Bootoption debian2hd found, but grml-debootstrap is not available." ; eend 1
1777    eoutdent
1778    exit 1
1779 fi
1780
1781 if checkbootparam 'target' ; then
1782   TARGET=''
1783   TARGET="$(getbootparam 'target' 2>>$DEBUG)"
1784   # notice: the following checks whether the given partition is available, if not the skip
1785   # execution of grml-debootstrap as it might result in data loss...
1786   if ! [ -r "$TARGET" ] ; then
1787      eerror "Target $TARGET does not exist. Skipping execution of grml-debootstrap therefore." ; eend 1
1788   fi
1789 else
1790   eindent
1791   eerror "No bootoption named target found, can not continue execution of grml-debootstrap." ; eend 1
1792   eoutdent
1793   exit 1
1794 fi
1795
1796 if checkbootparam 'grub' ; then
1797   GRUB=''
1798   GRUB="$(getbootparam 'grub' 2>>$DEBUG)"
1799 fi
1800
1801 if checkbootparam 'groot' ; then
1802   GROOT=''
1803   GROOT="$(getbootparam 'groot' 2>>$DEBUG)"
1804 fi
1805
1806 if checkbootparam 'release' ; then
1807   RELEASE=''
1808   RELEASE="$(getbootparam 'release' 2>>$DEBUG)"
1809 fi
1810
1811 if checkbootparam 'mirror' ; then
1812   MIRROR=''
1813   MIRROR="$(getbootparam 'mirror' 2>>$DEBUG)"
1814 fi
1815
1816 if checkbootparam 'boot_append' ; then
1817   BOOT_APPEND=''
1818   BOOT_APPEND="$(getbootparam 'boot_append' 2>>$DEBUG)"
1819 fi
1820
1821 if checkbootparam 'password' ; then
1822   PASSWORD=''
1823   PASSWORD="$(getbootparam 'password' 2>>$DEBUG)"
1824 fi
1825
1826 # now check which options are available
1827 if [ -n "TARGET" ] ; then
1828    TARGETCMD="--target $TARGET"
1829 else
1830    TARGETCMD=''
1831    eindent
1832    eerror "Target not set via bootoption. Skipping execution of grml-debootstrap therefore."; eend 1
1833    eoutdent
1834    exit 1
1835 fi
1836 [ -n "$GRUB" ]     && GRUBCMD="--grub $GRUB"               || GRUBCMD=''
1837 [ -n "$GROOT" ]    && GROOTCMD="--groot $GROOT"            || GROOTCMD=''
1838 [ -n "$RELEASE" ]  && RELEASECMD="--release $RELEASE"      || RELEASECMD=''
1839 [ -n "$MIRROR" ]   && MIRRORCMD="--mirror $MIRROR"         || MIRRORCMD=''
1840 [ -n "$PASSWORD" ] && PASSWORDCMD="--password $PASSWORD"   || PASSWORDCMD=''
1841 [ -n "$BOOT_APPEND" ] && BOOT_APPEND="--boot_append $BOOT_APPEND" || BOOT_APPEND=''
1842
1843 # and finally write script and execute it
1844 cat>|/usr/bin/grml-debootstrap_noninteractive<<EOF
1845 #!/bin/sh
1846 AUTOINSTALL='yes' grml-debootstrap $TARGETCMD $GRUBCMD $GROOTCMD $RELEASECMD $MIRRORCMD $PASSWORDCMD $BOOT_APPEND
1847 EOF
1848
1849 chmod 750  /usr/bin/grml-debootstrap_noninteractive
1850
1851 screen /usr/bin/grml-debootstrap_noninteractive
1852 einfo "Invoking a shell, just exit to continue booting..."
1853 /bin/zsh
1854
1855 fi # checkbootparam "BOOT_IMAGE=debian2hd
1856 }
1857 # }}}
1858
1859 # {{{ virtualbox shared folders
1860 config_virtualbox_shared_folders() {
1861 if $VIRTUALBOX ; then
1862   einfo "VirtualBox detected, trying to set up Shared Folders."
1863   if ! modinfo vboxsf &>/dev/null ; then
1864     ewarn "vboxsf driver not present, not setting up VirtualBox Shared Folders."
1865     eend 0
1866   elif ! [ -x /usr/sbin/VBoxService ] ; then
1867     ewarn "virtualbox-guest-utils not installed, not setting up VirtualBox Shared Folders."
1868     eend 0
1869   else
1870     eindent
1871
1872       einfo "Loading vboxsf driver."
1873       lsmod | grep -q vboxsf || modprobe vboxsf
1874       eend $?
1875
1876       einfo "Adjusting /dev/vboxguest."
1877       chown root:vboxsf /dev/vboxguest
1878       chmod 660 /dev/vboxguest
1879       eend $?
1880
1881       config_userfstab
1882
1883       einfo "Adding $fstabuser to group vboxsf."
1884       adduser grml vboxsf &>/dev/null
1885       eend $?
1886
1887       einfo "Starting VBoxService."
1888       VBoxService >/dev/null
1889       eend $?
1890
1891       local vbautomation='automation'
1892       if checkbootparam 'vbautomation'; then
1893         vbautomation="$(getbootparam 'vbautomation' 2>>$DEBUG)"
1894       fi
1895
1896       if ! VBoxControl sharedfolder list | egrep -q "^[0-9]+ - ${vbautomation}$" ; then
1897         ewarn "No automount shared folder '$vbautomation' available"
1898         eend 0
1899       else
1900         einfo "Found automount shared folder '$vbautomation'"
1901         eend 0
1902
1903         local distri="$(getbootparam 'distri' 2>>$DEBUG)"
1904         [ -n "$distri" ] || distri='grml'
1905
1906         local vbox_auto_sf="/media/sf_${vbautomation}"
1907
1908         sleep 1 # ugly but necessary
1909
1910         counter=10
1911         eindent
1912         while ! [ -d "${vbox_auto_sf}" ] && [[ "$counter" != 0 ]]; do
1913           einfo "Waiting another second to retry access to ${vbox_auto_sf}"
1914           sleep 1
1915           counter=$(( counter-1 ))
1916           eend 0
1917         done
1918         eoutdent
1919
1920         if ! [ -d "${vbox_auto_sf}" ] ; then
1921           eerror "Giving up trying to access folder ${vbox_auto_sf} which doesn't seem to exist"
1922           eend 1
1923         else
1924           einfo "Found shared folders automation directory $vbox_auto_sf"
1925           eend 0
1926
1927           eindent
1928           if checkbootparam 'novbautomation' ; then
1929             einfo "Bootoption novbautomation found. Disabling automation script execution."
1930             eend 0
1931           else
1932             if ! [ -x "${vbox_auto_sf}/${distri}" ] ; then
1933               ewarn "Couldn't find an automation script named ${vbox_auto_sf}/${distri}"
1934               eend 1
1935             else
1936               einfo "Executing '${vbox_auto_sf}/${distri}' now:"
1937               "${vbox_auto_sf}/${distri}"
1938               eend $?
1939             fi
1940           fi
1941           eoutdent
1942         fi
1943       fi
1944
1945     eoutdent
1946   fi
1947 fi
1948 }
1949 # }}}
1950
1951 # {{{ Support customization
1952 config_distri(){
1953 if checkbootparam 'distri'; then
1954   DISTRI="$(getbootparam 'distri' 2>>$DEBUG)"
1955   if [ -r "${LIVECD_PATH}"/desktop/"$DISTRI".jpg ] ; then
1956      [ -n "$BOOTDEBUG" ] && einfo "Debug: bootoption distri found and file ${LIVECD_PATH}/desktop/${DISTRI} present" && eend 0
1957      # make sure the desktop.jpg file is not a symlink, so copying does not file then
1958      [ -L /usr/share/grml/desktop.jpg ] && rm /usr/share/grml/desktop.jpg
1959      cp "${LIVECD_PATH}"/desktop/"$DISTRI".jpg /usr/share/grml/desktop.jpg
1960   fi
1961 fi
1962 }
1963 # }}}
1964
1965 ## END OF FILE #################################################################
1966 # vim:foldmethod=marker expandtab ai ft=zsh shiftwidth=2