Move grml-autoconfig.txt to doc/grml-autoconfig.txt; whitespace cleanup
[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:/usr/X11R6/bin"
11 DEBUG="/dev/null"
12 KERNEL="$(uname -r)"
13 umask 022
14
15 # old linuxrc version:
16 [ -d /cdrom ]      && export LIVECD_PATH=/cdrom
17 # new initramfs layout:
18 [ -d /live/image ] && export LIVECD_PATH=/live/image
19
20 # Ignore these signals in non-interactive mode: INT, TERM, SEGV
21 [ -z "$PS1" ] && trap "" 2 3 11
22
23 # zsh stuff
24 iszsh(){
25 if [ -n "$ZSH_VERSION" ] ; then
26   return 0
27 else
28   return 1
29 fi
30 }
31 # avoid 'no matches found: ...'
32 iszsh && setopt no_nomatch # || echo "Warning: not running under zsh!"
33 # }}}
34
35 # {{{ Read in boot parameters
36 if [ -z "$CMDLINE" ]; then
37   # if CMDLINE was set from the outside, we're debugging.
38   # otherwise, take CMDLINE from Kernel and config files.
39   CMDLINE="$(cat /proc/cmdline)"
40   [ -d /cdrom/bootparams/ ]      && CMDLINE="$CMDLINE $(cat /cdrom/bootparams/* | tr '\n' ' ')"
41   [ -d /live/image/bootparams/ ] && CMDLINE="$CMDLINE $(cat /live/image/bootparams/* | tr '\n' ' ')"
42 fi
43 # }}}
44
45 ### {{{ Utility Functions
46
47 # Get a bootoption's parameter: read boot command line and either
48 # echo last parameter's argument or return false.
49 getbootparam(){
50   case "$CMDLINE" in
51     *$1=*)
52       result="${CMDLINE##* $1=}"
53       result="${result%%[       ]*}"
54       echo "$result"
55       return 0 ;;
56     *) # no match?
57       return 1 ;;
58   esac
59 }
60
61 # Check boot commandline for specified option
62 checkbootparam(){
63   [ -n "$1" ] || { echo "Error: missing argument to checkbootparam()" ; return 1 }
64   # make sure we match beginning and end of line as well
65   # but don't match 'vg' if we want to check for 'vga'
66   if echo "$CMDLINE" | grep -qw "$1" ; then
67      return 0
68   else
69      return 1
70   fi
71 }
72
73 # Check wheter a configuration variable (like $CONFIG_TOHD) is
74 # enabled or not
75 checkvalue(){
76   case "$1" in
77     [yY][eE][sS])     return 0 ;; # it's set to 'yes'
78     [tT][rR][uU][eE]) return 0 ;; # it's set to 'true'
79                    *) return 1 ;; # default
80   esac
81 }
82
83 # Are we using grml-small?
84 checkgrmlsmall(){
85   grep -q small /etc/grml_version 2>>$DEBUG && return 0 || return 1
86 }
87
88 # execute flite only if it's present
89 flitewrapper() {
90    [ -x /usr/bin/flite ] && flite -o play -t "$*"
91 }
92 ### }}}
93
94 # {{{ filesystems (proc, pts, sys) and fixes
95 mount_proc(){
96   [ -f /proc/version ] || mount -t proc /proc /proc 2>/dev/null
97 }
98
99 mount_pts(){
100   grep -q "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
101 }
102
103 mount_sys(){
104   [ -d /sys/devices ] || mount -t sysfs /sys /sys 2>/dev/null
105 }
106 # }}}
107
108 # {{{ Check if we are running in live mode or from HD
109 INSTALLED=""
110 [ -e /etc/grml_cd ] || INSTALLED="yes"
111
112 # testcd
113 TESTCD=""
114 checkbootparam 'testcd' >>$DEBUG 2>&1 && TESTCD="yes"
115 # }}}
116
117 # {{{ source lsb-functions , color handling
118 if checkbootparam 'nocolor'; then
119   RC_NOCOLOR=yes
120   . /etc/grml/lsb-functions
121   einfo "Disabling colors in bootsequence as requested on commandline." ; eend 0
122 else
123   . /etc/grml/lsb-functions
124   . /etc/grml_colors
125 fi
126 # }}}
127
128 # {{{ debug
129 config_debug(){
130  checkbootparam 'debug'            && BOOTDEBUG="yes"
131  checkbootparam "BOOT_IMAGE=debug" && BOOTDEBUG="yes"
132
133  rundebugshell(){
134   if [ -n "$BOOTDEBUG" ]; then
135      einfo "Starting intermediate shell stage $stage as requested by \"debug\" option."
136      if [ grep -q "debug=noscreen" "$CMDLINE" ] ; then
137         einfo "Notice that the shell does not provide job handling: ctrl-z, bg and fg won't work!"
138         einfo "Just exit the shell to continue boot process..."
139         /bin/zsh
140      else
141         eindent
142         if [ -r /etc/grml/screenrc ] ; then
143            einfo "Starting GNU screen to be able to use a full featured shell environment."
144            einfo "Just exit the shells (and therefore screen) to continue boot process..."
145            /bin/zsh -c "screen -c /etc/grml/screenrc"
146         else
147            einfo "Notice that the shell does not provide job handling: ctrl-z, bg and fg won't work!"
148            einfo "Just exit the shell to continue boot process..."
149            /bin/zsh
150         fi
151         eoutdent
152      fi
153   fi
154  }
155 }
156 # }}}
157
158 # {{{ log
159 config_log(){
160 if checkbootparam 'log' || checkbootparam 'debug' ; then
161    export DEBUG="/tmp/grml.log.`date +%Y%m%d`"
162    touch $DEBUG
163    einfo "Bootparameter log found. Log files: ${DEBUG} and /var/log/boot"
164    eindent
165      einfo "Starting bootlogd." # known to be *very* unreliable :(
166      bootlogd -r -c 1>>$DEBUG 2>&1 ; eend $?
167    eoutdent
168 else
169    DEBUG="/dev/null"
170 fi
171 }
172 # }}}
173
174 # {{{ set firmware timeout via bootparam
175 config_fwtimeout(){
176  if checkbootparam 'fwtimeout' ; then
177    TIMEOUT="$(getbootparam 'fwtimeout' 2>>$DEBUG)"
178    einfo "Bootoption fwtimeout found. (Re)Loading firmware_class module."
179    rmmod firmware_class 1>>$DEBUG 2>&1
180    modprobe firmware_class ; eend $?
181  fi
182  if [ -z "$TIMEOUT" ] ; then
183    TIMEOUT="100" # linux kernel default: 10
184  fi
185  if [ -f /sys/class/firmware/timeout ] ; then
186    einfo "Setting timeout for firmware loading to ${TIMEOUT}."
187    echo 100 > /sys/class/firmware/timeout ; eend $?
188  fi
189 }
190 # }}}
191
192 ### {{{ language configuration / localization
193 config_language(){
194
195  einfo "Activating language settings:"
196  eindent
197
198  # people can specify $LANGUAGE and $CONSOLEFONT in a config file:
199  [ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig
200
201  grep -q ' lang=.*-utf8' /proc/cmdline && UTF8='yes' || UTF8=''
202
203  # check for bootoption which overrides config from /etc/grml/autoconfig:
204  BOOT_LANGUAGE="$(getbootparam 'lang' 2>>$DEBUG)"
205  [ -n "$BOOT_LANGUAGE" ] && LANGUAGE="$BOOT_LANGUAGE"
206
207  # set default to 'en' in live-cd mode if $LANGUAGE is not yet set:
208  if [ -z "$INSTALLED" ] ; then
209     [ -n "$LANGUAGE" ] || LANGUAGE='us'
210  fi
211
212  # if bootoption lang is used update /etc/default/locale, otherwise *not*!
213  if [ -n "$BOOT_LANGUAGE" -a -x /usr/sbin/grml-setlang ] ; then
214     if checkgrmlsmall ; then
215        /usr/sbin/grml-setlang "POSIX"
216     else
217        /usr/sbin/grml-setlang "$LANGUAGE"
218     fi
219  fi
220
221  # set console font
222  if [ -z "$CONSOLEFONT" ] ; then
223     if ! checkbootparam 'nodefaultfont' >>$DEBUG 2>&1 ; then
224        # [ -n "$UTF8" ] && CONSOLEFONT='LatArCyrHeb-16' || CONSOLEFONT='Lat15-Terminus16'
225        # if [ -r /usr/share/consolefonts/Lat15-Terminus16.psf.gz ] ; then
226        if [ -r /usr/share/consolefonts/Uni3-Terminus16.psf.gz ] ; then
227           CONSOLEFONT='Uni3-Terminus16'
228        else
229           ewarn "/usr/share/consolefonts/Uni3-Terminus16.psf.gz not available. Please upgrade package console-terminus." ; eend 1
230        fi
231     fi
232  fi
233
234  # export it now, so error messages get translated, too
235  if checkgrmlsmall ; then
236     export LANG='C' # grml-small does not provide any further locales
237  else
238     [ -r /etc/default/locale ] && . /etc/default/locale
239     export LANG LANGUAGE
240  fi
241
242  # configure keyboard layout, read in already set values first:
243  [ -r /etc/sysconfig/keyboard ] && . /etc/sysconfig/keyboard
244
245  # now allow keyboard override by boot commandline for later use:
246  KKEYBOARD="$(getbootparam 'keyboard' 2>>$DEBUG)"
247  [ -n "$KKEYBOARD" ] && KEYTABLE="$KKEYBOARD"
248  # notce: de/at is a bad choice, so take de-latin1-nodeadkeys instead:
249  [[ "$KKEYBOARD" == 'de' ]] && KEYTABLE=de-latin1-nodeadkeys
250  [[ "$KKEYBOARD" == 'at' ]] && KEYTABLE=de-latin1-nodeadkeys
251
252  # modify /etc/sysconfig/keyboard only in live-cd mode:
253  if [ -z "$INSTALLED" ] ; then
254
255    local LANGUAGE="$BOOT_LANGUAGE"
256    . /etc/grml/language-functions
257    # allow setting xkeyboard explicitly different than console keyboard
258    KXKEYBOARD="$(getbootparam 'xkeyboard' 2>>$DEBUG)"
259    if [ -n "$KXKEYBOARD" ]; then
260       XKEYBOARD="$KXKEYBOARD"
261       KDEKEYBOARD="$KXKEYBOARD"
262    elif [ -n "$KKEYBOARD" ]; then
263       XKEYBOARD="$KKEYBOARD"
264       KDEKEYBOARD="$KKEYBOARD"
265    fi
266
267    # duplicate of previous code to make sure /etc/grml/language-functions
268    # does not overwrite our values....
269    # now allow keyboard override by boot commandline for later use:
270    KKEYBOARD="$(getbootparam 'keyboard' 2>>$DEBUG)"
271    [ -n "$KKEYBOARD" ] && KEYTABLE="$KKEYBOARD"
272    # notce: de/at is a bad choice, so take de-latin1-nodeadkeys instead:
273    [[ "$KKEYBOARD" == 'de' ]] && KEYTABLE=de-latin1-nodeadkeys
274    [[ "$KKEYBOARD" == 'at' ]] && KEYTABLE=de-latin1-nodeadkeys
275
276    # write keyboard related variables to file for later use
277    [ -d /etc/sysconfig ] || mkdir /etc/sysconfig
278    if ! [ -e /etc/sysconfig/keyboard ] ; then
279       echo "KEYTABLE=\"$KEYTABLE\""          > /etc/sysconfig/keyboard
280       echo "XKEYBOARD=\"$XKEYBOARD\""       >> /etc/sysconfig/keyboard
281       echo "KDEKEYBOARD=\"$KDEKEYBOARD\""   >> /etc/sysconfig/keyboard
282       echo "KDEKEYBOARDS=\"$KDEKEYBOARDS\"" >> /etc/sysconfig/keyboard
283    fi
284  fi
285
286  [ -r /etc/sysconfig/keyboard ] && . /etc/sysconfig/keyboard
287
288  # activate unicode console if running within utf8 environment
289  if [ -r /etc/default/locale ] ; then
290     if grep -q "LANG=.*UTF" /etc/default/locale ; then
291        einfo "Setting up unicode environment."
292        unicode_start 2>>$DEBUG ; eend $?
293     fi
294  fi
295
296  # Set default keyboard before interactive setup
297  if [ -n "$KEYTABLE" ] ; then
298     einfo "Running loadkeys for ${WHITE}${KEYTABLE}${NORMAL} in background"
299     loadkeys -q $KEYTABLE &
300     eend $?
301  fi
302
303  # we have to set up all consoles, therefore loop it over all ttys:
304  NUM_CONSOLES=$(fgconsole --next-available)
305  NUM_CONSOLES=$(expr ${NUM_CONSOLES} - 1)
306  [ ${NUM_CONSOLES} -eq 1 ] && NUM_CONSOLES=6
307  CUR_CONSOLE=$(fgconsole)
308
309  if [ -n "$CHARMAP" ] ; then
310     einfo "Running consolechars for ${CHARMAP}"
311     for vc in `seq 0 ${NUM_CONSOLES}`  ; do
312         consolechars --tty=/dev/tty${vc} -m ${CHARMAP} ; RC=$?
313     done
314     [ -n "$CUR_CONSOLE" ] && chvt $CUR_CONSOLE
315     eend $RC
316  fi
317
318  if checkbootparam 'noconsolefont' ; then
319     ewarn "Skipping setting console font as requested on boot commandline." ; eend 0
320  else
321     if [ -n "$CONSOLEFONT" ] ; then
322        einfo "Running consolechars using ${CONSOLEFONT}"
323        for vc in `seq 0 ${NUM_CONSOLES}`  ; do
324            consolechars --tty=/dev/tty${vc} -f $CONSOLEFONT ; RC=$?
325        done
326        [ -n "$CUR_CONSOLE" ] && chvt $CUR_CONSOLE
327        eend $?
328     fi
329  fi
330
331  eoutdent
332 }
333 # }}}
334
335 # {{{ Set hostname
336 config_hostname(){
337  if checkbootparam 'hostname' ; then
338   HOSTNAME="$(getbootparam 'hostname' 2>>$DEBUG)"
339   einfo "Setting hostname to $HOSTNAME as requested."
340   sed -i "s/^127.0.0.1.*localhost/127.0.0.1 $HOSTNAME localhost/" /etc/hosts
341   hostname $HOSTNAME ; eend $?
342  else
343   hostname --file /etc/hostname
344  fi
345 }
346 # }}}
347
348 # fstabuser (needed when running from harddisk with username != grml {{{
349 config_userfstab(){
350   [ -r /etc/grml/autoconfig ] && . /etc/grml/autoconfig
351   if [ -n "$CONFIG_FSTAB_USER" ] ; then
352      fstabuser="$CONFIG_FSTAB_USER"
353   else
354      fstabuser=$(getent passwd 1000 | cut -d: -f1)
355   fi
356   # if not yet set fall back to default 'grml' user
357   [ -n "$fstabuser" ] || fstabuser='grml'
358 }
359 # }}}
360
361 # {{{ Set clock (Local time is more often used than GMT, so it is default)
362 config_time(){
363  # don't touch the files if running from harddisk:
364  if [ -z "$INSTALLED" ]; then
365     # The default hardware clock timezone is stated as representing local time.
366     UTC="--localtime"
367     grep -q "^UTC=" /etc/default/rcS || echo "UTC=no" >> /etc/default/rcS
368     checkbootparam 'utc'       >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=yes|" /etc/default/rcS
369     checkbootparam 'gmt'       >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=yes|" /etc/default/rcS
370     checkbootparam 'localtime' >>$DEBUG 2>&1 && sed -i "s|^UTC=.*$|UTC=no|"  /etc/default/rcS
371     grep -q -i "^UTC=yes" /etc/default/rcS && UTC="-u"
372     # hwclock uses the TZ variable
373     KTZ="$(getbootparam 'tz' 2>>$DEBUG)"
374     [ -z "$KTZ" ] && [ -r /etc/timezone ] && KTZ=$(cat /etc/timezone)
375     if [ ! -f "/usr/share/zoneinfo/$KTZ" ] ; then
376        ewarn "Warning: unknown timezone $KTZ" ; eend 1
377        KTZ="Europe/Vienna"
378        ewarn "Falling back to timezone $KTZ" ; eend 0
379     fi
380
381     if ! [ -r /dev/rtc ] ; then
382       ewarn "Warning: realtime clock not available, trying to execute hwclock anyway." ; eend 0
383     fi
384
385     ERROR=$(TZ="$KTZ" hwclock $UTC -s 2>&1 | head -1) ; RC=$?
386     if [ -n "$ERROR" ] ; then
387        eindent
388        ERROR=$(TZ="$KTZ" hwclock $UTC -s --directisa 2>&1 | head -1)
389        if [ -n "$ERROR" ] ; then
390           eerror "Problem running hwclock: $ERROR" ; eend 1
391        fi
392        eoutdent
393     fi
394
395  fi
396 }
397 # }}}
398
399 # {{{ print kernel info
400 config_kernel(){
401   vmware-detect &>/dev/null && VMWARE="inside ${WHITE}VMware/Qemu${NORMAL}"
402   [ -d /proc/xen ] && VMWARE='' # vmware-detect returns '0' when running with a Xen-enabled kernel
403   einfo "Running Linux Kernel $KERNEL $VMWARE" ; eend 0
404   if [ -r /proc/cpuinfo ] ; then
405      if egrep -q '^flags.*(vmx|svm)' /proc/cpuinfo ; then
406        eindent
407        einfo 'CPU(s) featuring virtualization technology detected' ; eend 0
408        eoutdent
409      fi
410   fi
411   if [ -d /proc/xen ] ; then
412      eindent
413      einfo 'Running kernel featuring support for Xen detected' ; eend 0
414      eoutdent
415   fi
416 }
417 # }}}
418
419 # {{{ vmware specific stuff
420 config_vmware(){
421 if checkbootparam 'novmware' ; then
422    ewarn "Skipping running vmware specific stuff as requested on boot commandline." ; eend 0
423 else
424    if [ -z "$INSTALLED" ] ; then
425       if vmware-detect || checkbootparam "BOOT_IMAGE=vmware" ; then
426          if ! checkbootparam 'qemu' ; then
427             if [ -r /etc/X11/xorg.conf.vmware ] ; then
428                einfo "VMware: Copying /etc/X11/xorg.conf.vmware to /etc/X11/xorg.conf"
429                cp /etc/X11/xorg.conf.vmware /etc/X11/xorg.conf ; eend $?
430             fi
431          fi
432       elif [ -r /proc/acpi/battery/BAT0/info -a -r /etc/X11/xorg.conf.virtualbox ] ; then
433          if grep -q 'OEM info:                innotek' /proc/acpi/battery/BAT0/info ; then
434             einfo 'Virtual Box: Copying /etc/X11/xorg.conf.virtualbox to /etc/X11/xorg.conf'
435             cp /etc/X11/xorg.conf.virtualbox /etc/X11/xorg.conf ; eend $?
436          fi
437       fi
438    fi
439 fi
440 }
441 # }}}
442
443 # {{{ qemu specific stuff
444 config_qemu(){
445 if checkbootparam 'qemu' ; then
446    if [ -r /etc/X11/xorg.conf.example ] ; then
447       einfo "Qemu: Copying /etc/X11/xorg.conf.example to /etc/X11/xorg.conf"
448       cp /etc/X11/xorg.conf.example /etc/X11/xorg.conf ; eend $?
449    fi
450 fi
451 }
452 # }}}
453
454 # {{{ ld.so.cache + depmod
455 config_ld_mod(){
456 if [ -n "$INSTALLED" ]; then
457  if ! [ -r /etc/grml.first.boot ] ; then
458   einfo "Running from HD for the first time, regenerate ld.so.cache and modules.dep:"
459   eindent
460 # Regenerate ld.so.cache and module dependencies on HD
461     einfo "Running ldconfig" ; ldconfig  ; eend $?
462     einfo "Running depmod"   ; depmod -a ; eend $?
463     touch /etc/grml.first.boot
464     eend 0
465   eoutdent
466  fi
467 fi
468 }
469 # }}}
470
471 # {{{ timezone
472 config_timezone(){
473  # don't touch the files if running from harddisk:
474  if [ -z "$INSTALLED" ]; then
475     KTZ="$(getbootparam 'tz' 2>>$DEBUG)"
476     if [ -n "$KTZ" ] ; then
477        if [ ! -f "/usr/share/zoneinfo/$KTZ" ]
478        then
479           ewarn "Warning: unknown timezone $KTZ"; eend 0
480        else
481           einfo "Setting timezone."
482           # update debconf
483           area=$(echo $KTZ | cut -d '/' -f1)
484           zone=$(echo $KTZ | cut -d '/' -f2)
485           echo "tzdata tzdata/Areas       select $area" | debconf-set-selections
486           echo "tzdata tzdata/Zones/$area select $zone" | debconf-set-selections
487           # update files
488           echo $KTZ > /etc/timezone
489           rm -f /etc/localtime
490           cp "/usr/share/zoneinfo/$KTZ" /etc/localtime ; eend $?
491        fi
492     fi
493  fi
494 }
495 # }}}
496
497 # small computer / nearly no ram {{{
498 config_small(){
499
500 RAM=$(/usr/bin/gawk '/MemTotal/{print $2}' /proc/meminfo)
501 # MEM=$(/usr/bin/gawk 'BEGIN{m=0};/MemFree|Cached|SwapFree/{m+=$2};END{print m}' /proc/meminfo)
502 eindent
503
504 if checkbootparam 'small'; then
505   einfo "Information: ${RAM} kB of RAM available." ; eend 0
506   einfo "Bootoption small detected. Activating small system."
507   if [ -r /etc/inittab.small ] ; then
508     mv /etc/inittab /etc/inittab.normal
509     mv /etc/inittab.small /etc/inittab
510   else
511     sed -i 's/^9/#&/' /etc/inittab
512     sed -i 's/^10/#&/' /etc/inittab
513     sed -i 's/^11/#&/' /etc/inittab
514     sed -i 's/^12/#&/' /etc/inittab
515   fi
516   /sbin/telinit q ; eend $?
517 else
518   if checkgrmlsmall ; then
519     if [[ $RAM -lt 25000 ]] ; then
520       ewarn "Information: ${RAM} kB of RAM available." ; eend 1
521       ewarn "At least 32MB of RAM should be available for grml-small." ; eend 1
522       ewarn "Use the bootoption small to save some more MB of memory usage." ; eend 0
523       ewarn "Dropping you into a rescue shell. To continue booting exit the shell." ; eend 0
524       /bin/zsh --login
525     else
526       einfo "Information: ${RAM} kB of RAM available." ; eend 0
527     fi
528   else
529     if [[ $RAM -lt 58000 ]] ; then
530       ewarn "Information: ${RAM} kB of RAM available." ; eend 1
531       ewarn "At least 64MB of RAM should be available for grml." ; eend 1
532       ewarn "Use the bootoption small to save some more MB of memory usage." ; eend 0
533       ewarn "Dropping you into a rescue shell. To continue booting exit the shell." ; eend 0
534       /bin/zsh --login
535     else
536       einfo "Information: ${RAM} kB of RAM available." ; eend 0
537     fi
538   fi
539 fi
540 eoutdent
541 }
542 # }}}
543
544 # skip startup of w3m {{{
545 config_fast(){
546 if checkbootparam 'fast'; then
547   ewarn "Bootoption fast detected. Skipping startup of grml-quickconfig."
548     sed -i 's#^1:.*#1:12345:respawn:/usr/bin/openvt -f -c 1 -w -- /bin/zsh#' /etc/inittab
549   /sbin/telinit q ; eend $?
550 fi
551 }
552 # }}}
553
554 # activate serial console {{{
555 config_console(){
556 if checkbootparam 'console'; then
557   einfo "Bootoption (for serial) console detected."
558   eindent
559     if [ -r /etc/mgetty/mgetty.config ] ; then
560        MODE=$(getbootparam 'console' | awk -F, '{print $2}')
561        MODE=${MODE%%n*}
562        [ -n "$MODE" ] || MODE=9600 # default mode
563        einfo "Setting speed in /etc/mgetty/mgetty.config to $MODE bps"
564        sed -i "s/speed [0-9]*/speed $MODE/" /etc/mgetty/mgetty.config ; eend $?
565     fi
566
567     einfo "Activating mgetty."
568     sed -i 's/^#T0/T0/' /etc/inittab
569     sed -i 's/^#T1/T1/' /etc/inittab
570     /sbin/telinit q ; eend $?
571   eoutdent
572 fi
573 }
574 # }}}
575
576 # For burning on IDE-CD-Roms, k3b (and others) check for special permissions {{{
577 config_cdrom_perm(){
578 CDROMS=""
579 for DEVICE in /proc/ide/hd?; do
580  [ "$(cat $DEVICE/media 2>/dev/null)" = "cdrom" ] && CDROMS="$CDROMS /dev/${DEVICE##*/}"
581 done
582 [ -n "$CDROMS" ] && { chown root.cdrom $CDROMS; chmod 666 $CDROMS; } 2>/dev/null
583 }
584 # }}}
585
586 # {{{ Bring up loopback interface now
587 config_local_net(){
588  if [ -z "$INSTALLED" ] ; then
589     if grep -q 'iface lo inet loopback' /etc/network/interfaces 2>/dev/null ; then
590        grep -q lo=lo /etc/network/run/ifstate 2>/dev/null || ifup lo
591     else
592        ifconfig lo up
593     fi
594  fi
595 }
596 # }}}
597
598 # firewire devices {{{
599 # the raw1394 driver does not yet export info into SYSFS,
600 # so let's create raw1394 device manually
601 # http://www.michael-prokop.at/blog/index.php?p=352
602 config_firewire_dev(){
603 if checkbootparam 'nofirewiredev' ; then
604   ewarn "Skipping creating some firewire devices as requested on boot commandline." ; eend 0
605 else
606 #if [ "${KERNEL%-*}" == "2.6.11" ] ; then
607   einfo "Creating some firewire devices (fix kernel 2.6-bug)."
608 #  cd /dev && MAKEDEV video1394 raw1394
609   [ -r /dev/raw1394 ]   || mknod /dev/raw1394 c 171 0
610   [ -r /dev/video1394 ] || mknod -m 666 /dev/video1394 c 171 16
611 # mknod -m 666 /dev/dv1394 c 171 32 # for NTSC
612   [ -r /dev/dv1394 ]    || mknod -m 666 /dev/dv1394 c 171 34 # for PAL
613   chown -R root:video /dev/raw1394 /dev/video1394 /dev/dv1394
614   chmod -R 664 /dev/raw1394 /dev/video1394 /dev/dv1394 ; eend $?
615 fi
616 #fi
617 }
618 # }}}
619
620 # {{{ copy passwd-lockfile to ramdisk (fix unionfs-behaviour)
621 # otherwise we will get: passwd: Authentication token lock busy
622 config_fix_passwd(){
623  if [ -z "$INSTALLED" ] ; then
624   touch /etc/.pwd.lock
625  fi
626 }
627 # }}}
628
629 # {{{ CD Checker
630 config_testcd(){
631 if [ -n "$TESTCD" ]; then
632    einfo "Checking CD data integrity as requested by '${WHITE}testcd${NORMAL}' boot option."
633    einfo "Reading files and checking against GRML/md5sums, this may take a while..."
634    echo -n "${RED}"
635
636    if [ -n "${LIVECD_PATH}"/GRML ] ; then
637       ( cd "${LIVECD_PATH}"/GRML ; rm -f /tmp/md5sum.log ; md5sum -c md5sums 2>&1 | tee /tmp/md5sum.log ; RC=$? )
638    else
639       echo "${RED} *** Error: Could not find md5sum file.                           ***"
640    fi
641
642    if [ "$RC" = "0" ]; then
643       einfo "Everything looks OK" ; eend 0
644    else
645       eerror 'Checksum failed for theses files:' ; eend 1
646       egrep -v '(^md5sum:|OK$)' /tmp/md5sum.log
647       eerror 'Data on the grml medium is possibly incomplete/damaged or...'
648       eerror '... RAM of your computer is broken.' ; eend 1
649       einfon "Hit return to continue, or press the reset button to quit."
650      read a
651    fi
652
653    eend 0
654 fi
655 }
656 # }}}
657
658 # {{{ hardware detection via discover
659 config_discover(){
660 if checkbootparam 'nodisc' ; then
661   ewarn "Skipping hardware detection via discover as requested on boot commandline." ; eend 0
662 else
663  if [ -x /sbin/discover ] ; then
664   einfo "Discovering hardware. Trying to load the following modules in background:"
665    eindent
666    einfo "$(discover --data-path=linux/module/name --data-path=linux/modules/options --format="%s %s" --data-version=`uname -r` --enable-bus all | sort -u | xargs echo)"
667    eoutdent
668   /sbin/discover-modprobe -v 1>>$DEBUG 2>&1 &
669   eend 0
670  else
671   eerror "Application discover not available. Information: udev should handle hardware recognition." ; eend 0
672  fi
673 fi
674 }
675 # }}}
676
677 # {{{ hardware detection via hwinfo
678 config_hwinfo(){
679 if checkbootparam 'hwinfo' >>$DEBUG 2>&1; then
680   einfo "Discovering hardware via hwinfo:"
681   MODULES=$(su grml hwinfo | grep "Cmd: \"modprobe" | awk '{print $5}' | sed 's/"//')
682   echo -n "  Loading modules: "
683   for i in `echo $MODULES` ; do echo -n $i && modprobe $i ; done
684   eend 0
685 fi
686 }
687 # }}}
688
689 # {{{ disable hotplug agents on request
690 config_hotplug_agent(){
691 if checkbootparam 'noagent' ; then
692   AGENT="$(getbootparam 'noagent' 2>>$DEBUG)"
693   AGENTLIST=$(echo "$AGENT" | sed 's/,/\\n/g')
694   AGENTNL=$(echo "$AGENT" | sed 's/,/ /g')
695   einfo "Disabling hotplug-agent(s) $AGENTNL"
696   for agent in $(echo -e $AGENTLIST) ; do
697     mv /etc/hotplug/${agent}.rc /etc/hotplug/${agent}.norc
698   done
699   [ "$?" == "0" ] ; eend $?
700 fi
701 }
702 # }}}
703
704 # {{{ blacklist of hotplug-modules
705 config_hotplug_blacklist(){
706 if checkbootparam 'black' ; then
707   BLACK="$(getbootparam 'black' 2>>$DEBUG)"
708   BLACKLIST=$(echo "$BLACK" | sed 's/,/\\n/g')
709   BLACKNL=$(echo "$BLACK" | sed 's/,/ /g')
710   einfo "Blacklisting $BLACKNL via /etc/hotplug/blacklist.d/hotplug-light"
711   echo -e "$BLACKLIST" >> /etc/hotplug/blacklist.d/hotplug-light
712   echo -e "$BLACKLIST" >> /etc/hotplug/blacklist
713   eend 0
714 fi
715 }
716 # }}}
717
718 # {{{ run hotplug
719 config_hotplug(){
720 if checkbootparam 'nohotplug' ; then
721   ewarn "Skipping running hotplug as requested on boot commandline." ; eend 0
722 else
723   if [ -r /etc/init.d/hotplug ] ; then
724     einfo "Starting hotplug system in background."
725     /etc/init.d/hotplug start 1>>$DEBUG 2>>$DEBUG &
726     eend 0
727   elif [ -r /etc/init.d/hotplug-light ] ; then
728     einfo "Starting hotplug-light system in background."
729     /etc/init.d/hotplug-light start 1>>$DEBUG 2>>$DEBUG &
730     eend 0
731   else
732     ewarn "No hotplug system found. Should be handled by udev. Skipping execution." ; eend 0
733   fi
734 fi
735 }
736 # }}}
737
738 # {{{ blacklist specific module [ used in /etc/init.d/udev ]
739 config_blacklist(){
740 if checkbootparam 'blacklist' ; then
741  if [ -z "$INSTALLED" ]; then
742   einfo "Bootoption blacklist found."
743   BLACK="$(getbootparam 'blacklist' 2>>$DEBUG)"
744   BLACKLIST_FILE='/etc/modprobe.d/grml.conf'
745   if [ -n "$BLACK" ] ; then
746     for module in $(echo ${BLACK//,/ }) ; do
747         einfo "Blacklisting module ${module} via ${BLACKLIST_FILE}."
748         echo "# begin entry generated by config_blacklist of grml-autoconfig" >> "$BLACKLIST_FILE"
749         echo "blacklist $module"     >> "$BLACKLIST_FILE"
750         echo "alias     $module off" >> "$BLACKLIST_FILE"
751         echo "# end   entry generated by config_blacklist of grml-autoconfig" >> "$BLACKLIST_FILE" ; eend $?
752     done
753   else
754    eerror "No given module for blacklist found. Blacklisting will not work therefore."
755   fi
756  else
757   ewarn "Backlisting via bootoption is not intended for use on harddisk installations." ; eend 1
758   eindent
759    einfo "Please blacklist the module(s) manually using the 'blacklist' script."
760   eoutdent
761  fi
762 fi
763 }
764 # }}}
765
766 # {{{ ACPI
767 config_acpi_apm(){
768 if [ -d /proc/acpi ]; then
769   if checkbootparam 'noacpi'; then
770     ewarn "Skipping ACPI Bios detection as requested via noacpi on boot commandline." ; eend 0
771   elif checkbootparam 'nogrmlacpi' ; then
772     ewarn "Skipping ACPI Bios detection as requested via nogrmlacpi on boot commandline." ; eend 0
773   else
774     einfo "ACPI Bios found, activating modules (disable via bootoption noacpi / nogrmlacpi): "
775     eindent
776     found=""
777     for a in /lib/modules/$KERNEL/kernel/drivers/acpi/*; do
778       basename="${a##*/}"
779       basename="${basename%%.*}"
780       case "$basename" in *_acpi)
781        egrep -qi "${basename%%_acpi}" /proc/acpi/dsdt 2>>$DEBUG || continue ;;
782       esac
783       modprobe $basename >>$DEBUG 2>&1 && found="yes"
784       local BASE="$BASE $basename"
785     done
786     if [ -n "$found" ] ; then
787       einfo "$BASE"  ; eend 0
788     else
789       ewarn "(none)" ; eend 1
790     fi
791     if ! ps x | grep -q /usr/sbin/acpid ; then
792       if ! [ -r /var/run/dbus/pid ] ; then
793         einfo "Starting acpi daemon."
794         /etc/init.d/acpid start 1>>$DEBUG 2>&1 ; eend $?
795       else
796         eerror "acpid error: it seems you are running d-bus/hal, but acpid needs to be started before d-bus."
797         eerror "Solution: please activate acpid via /etc/runlevel.conf"
798         eend 1
799       fi
800     else
801       ewarn "acpi daemon already running."
802       eend 0
803     fi
804     eoutdent
805   fi
806 else
807 # APM
808   if checkbootparam 'noapm'; then
809     ewarn "Skipping APM Bios detection as requested on boot commandline." ; eend 0
810   else
811     modprobe apm power_off=1 >>$DEBUG 2>&1
812     if [ "$?" = "0" ] ; then
813        if [ -x /etc/init.d/apmd ] ;then
814           einfo "APM Bios found, enabling power management functions."
815           /etc/init.d/apmd start ; eend $?
816        fi
817     else
818       eerror "Loading apm module failed." ; eend 1
819     fi
820   fi
821 fi
822 }
823 # }}}
824
825 # {{{ PCMCIA Check/Setup
826 # This needs to be done before other modules are being loaded (by hwsetup)
827 config_pcmcia(){
828 if checkbootparam 'nopcmcia'; then
829   ewarn "Skipping PCMCIA detection as requested on boot commandline." ; eend 0
830 else
831   if /usr/sbin/laptop-detect ; then
832     einfo "Detected Laptop - checking for PCMCIA." && eend 0
833     modprobe pcmcia_core >>$DEBUG 2>&1
834     # Try Cardbus or normal PCMCIA socket drivers
835     modprobe yenta_socket >>$DEBUG 2>&1 || modprobe i82365 >>$DEBUG 2>&1 || modprobe pd6729 >>$DEBUG 2>&1 || modprobe tcic >>$DEBUG 2>&1
836     if [ "$?" = "0" ]; then
837       modprobe ds >>$DEBUG 2>&1
838       if [ -d /proc/bus/pccard ] ; then
839        if [ -x /sbin/cardmgr ] ; then
840         einfo "PCMCIA found, starting cardmgr."
841         cardmgr >>$DEBUG 2>&1 && sleep 6 && eend 0
842        else
843         eerror "No cardmgr found. Make sure package pcmciautils is installed, it should handle it instead." ; eend 1
844        fi
845       fi
846     fi
847   fi
848 fi
849 }
850 # }}}
851
852 # {{{ run software synthesizer via speakup
853 config_swspeak(){
854    if checkbootparam 'swspeak' ; then
855       einfo "Bootoption swspeak found."
856
857       if [ ! -d /proc/speakup/ ] && ! grep -q speakup_soft /proc/modules ; then
858          ewarn "Kernel does not support software speakup - trying to load kernel module:" ; eend 0
859          eindent
860          einfo "Loading speakup_soft"
861          if modprobe speakup_soft ; then
862             eend 0
863          else
864             flitewrapper "Fatal error setting up software speakup"
865             eend 1
866             return 1
867          fi
868          eoutdent
869       fi
870
871       if [ -d /proc/speakup/ ] || grep -q speakup_soft /proc/modules ; then
872          einfo "Kernel supports speakup." ; eend 0
873          eindent
874             einfo "Just run swspeak if you want to use software synthesizer via speakup."
875             flitewrapper "Finished activating software speakup. Just run swspeak when booting finished."
876          eoutdent
877       else
878          eerror "Kernel does not seem to support speakup. Skipping swspeak." ; eend 1
879          flitewrapper "Kernel does not seem to support speakup. Sorry."
880       fi
881    fi
882 }
883 # }}}
884
885 # {{{ support hardware synthesizer via speakup
886 config_hwspeak(){
887    if checkbootparam 'speakup.synth' ; then
888       einfo "Bootoption speakup.synth found."
889       eindent
890
891       module="$(getbootparam 'speakup.synth' 2>>$DEBUG)"
892       if [ -z "$module" ] ; then
893          eerror "Sorry, no speakup module specified for bootoption speakup.synth."
894          flitewrapper "Sorry, no speakup module specified for bootoption speakup.synth."
895       else
896          einfo "Trying to load $module"
897          modprobe "speakup_${module}"
898          eend $?
899       fi
900
901       if [ -d /proc/speakup/ ] || grep -q speakup /proc/modules ; then
902          einfo "Kernel should support speakup now." ; eend 0
903          flitewrapper "Kernel should support speakup now."
904       else
905          eerror "Kernel or hardware do not seem to support speakup. Skipping hwspeak." ; eend 1
906          flitewrapper "Kernel or hardware do not seem to support speakup. Sorry."
907       fi
908
909       eoutdent
910
911    # hwspeak:
912    elif checkbootparam 'hwspeak' ; then
913       einfo "Bootoption hwspeak found."
914
915       if [ ! -d /proc/speakup/ ] && ! grep -q speakup /proc/modules ; then
916          ewarn "Kernel does not support hardware speakup - trying to load kernel modules:" ; eend 0
917          eindent
918          if ! [ -d "/lib/modules/${KERNEL}/extra/speakup/" ] ; then
919             eerror "Kernel does not provide speakup modules, sorry." ; eend 1
920          else
921            for module in $(find "/lib/modules/${KERNEL}/extra/speakup/" -name \*.ko | \
922                            sed 's#.*speakup/##g ; s#.ko$##g' | \
923                            grep -ve speakup_soft -ve speakup_dummy | sort -u) ; do
924               einfo "Trying to load $module"
925               modprobe $module
926               eend $?
927            done
928          fi
929          eoutdent
930       fi
931
932       if [ -d /proc/speakup/ ] || grep -q speakup /proc/modules ; then
933          einfo "Kernel should support speakup now." ; eend 0
934          flitewrapper "Kernel should support speakup now."
935       else
936          eerror "Kernel or hardware do not seem to support speakup. Skipping hwspeak." ; eend 1
937          flitewrapper "Kernel or hardware do not seem to support speakup. Sorry."
938       fi
939    fi
940 }
941 # }}}
942
943 # {{{ Check for blind option or brltty
944 config_blind(){
945 BLIND=""
946 checkbootparam 'blind' && BLIND="yes"
947 BRLTTY="$(getbootparam 'brltty' 2>>$DEBUG)"
948
949 if [ -n "$BLIND" -o -n "$BRLTTY" ]; then
950   if [ -x /sbin/brltty ]; then
951     # Blind option detected, start brltty now.
952     # modprobe serial_core parport_serial generic_serial && echo "done"
953     CMD=brltty
954     BRLTYPE=""
955     BRLDEV=""
956     BRLTEXT=""
957     if [ -n "$BRLTTY" ]; then
958       # Extra options
959       BRLTYPE="${BRLTTY%%,*}"
960       R="${BRLTTY#*,}"
961       if [ -n "$R" -a "$R" != "$BRLTTY" ]; then
962         BRLTTY="$R"
963         BRLDEV="${BRLTTY%%,*}"
964         R="${BRLTTY#*,}"
965         if [ -n "$R" -a "$R" != "$BRLTTY" ]; then
966           BRLTTY="$R"
967           BRLTEXT="${BRLTTY%%,*}"
968           R="${BRLTTY#*,}"
969         fi
970       fi
971     fi
972     [ -n "$BRLTYPE" ] && CMD="$CMD -b $BRLTYPE"
973     [ -n "$BRLDEV"  ] && CMD="$CMD -d $BRLDEV"
974     [ -n "$BRLTEXT" ] && CMD="$CMD -t $BRLTEXT"
975     einfo "Starting braille-display manager."
976 #    ( exec $CMD & )
977     ( sh -c "$CMD" & )
978     sleep 2 && BLINDSOUND="yes"
979     eend 0
980   fi
981 fi
982 }
983 # }}}
984
985 # {{{ Interactive configuration
986 config_interactive(){
987   ewarn "config_interactive is deprecated nowadays."
988   ewarn "Please set CONFIG_INTERACTIVE='no' in /etc/grml/autoconfig" ; eend 0
989 }
990 # }}}
991
992 # {{{ AGP
993 config_agp(){
994 if checkbootparam 'forceagp' ; then
995 # Probe for AGP. Hope this can fail safely
996   grep -q "AGP" "/proc/pci" 2>>$DEBUG && { modprobe agpgart || modprobe agpgart agp_try_unsupported=1; } >>$DEBUG 2>&1 && einfo "AGP bridge detected." ; eend 0
997 fi
998 }
999 # }}}
1000
1001 # {{{ automount(er)
1002 config_automounter(){
1003 if checkbootparam 'automounter' ; then
1004   RUNLEVEL="$(runlevel)"
1005   AUTOMOUNTER=""
1006   [ -x /etc/init.d/autofs ] && [ "$RUNLEVEL" != "N 1" ] && [ "$RUNLEVEL" != "N S" ] && AUTOMOUNTER="yes"
1007
1008 addautomount(){
1009 # /dev/ice  options
1010   d="${1##*/}"
1011   if [ -n "$AUTOMOUNTER" ]; then
1012     [ -d "/mnt/$d" -a ! -L "/mnt/$d" ] && rmdir /mnt/$d
1013     [ -d "/mnt/auto/$d" ] || mkdir -p "/mnt/auto/$d"
1014     [ -L "/mnt/$d" ]      || ln -s "/mnt/auto/$d" "/mnt/$d"
1015     anew="$d        -fstype=auto,$2 :$i"
1016     grep -q "$anew" "/etc/auto.mnt" || echo "$anew" >> /etc/auto.mnt
1017     AUTOMOUNTS="$AUTOMOUNTS $d"
1018     new="$1 /mnt/auto/$d  auto   users,noauto,exec,$2 0 0"
1019   else
1020     [ -d /mnt/$d ] && mkdir -p /mnt/$d
1021     new="$1 /mnt/$d  auto   users,noauto,exec,$2 0 0"
1022   fi
1023   grep -q "$new" "/etc/fstab" || echo "$new" >> /etc/fstab
1024 }
1025
1026   AUTOMOUNTS="floppy cdrom"
1027 # Add new devices to /etc/fstab and /etc/auto.mnt
1028   for i in /dev/cdrom?*; do
1029     if [ -L $i ]; then
1030       addautomount "$i" "ro"
1031     fi
1032   done
1033 fi
1034
1035 if [ -n "$AUTOMOUNTER" ]; then
1036 # Check for floppy dir, reinstall with automounter
1037   [ -d /mnt/floppy -a ! -L /mnt/floppy ] && rmdir /mnt/floppy
1038   [ -d /mnt/auto/floppy ] || mkdir -p /mnt/auto/floppy
1039   [ -L /mnt/floppy ] || ln -s /mnt/auto/floppy /mnt/floppy
1040   [ -d /mnt/cdrom -a ! -L /mnt/cdrom ] && rmdir /mnt/cdrom
1041   [ -d /mnt/auto/cdrom ] || mkdir -p /mnt/auto/cdrom
1042   [ -L /mnt/cdrom ] || ln -s /mnt/auto/cdrom /mnt/cdrom
1043   rm -f /etc/fstab.new
1044 # Replace paths from bootfloppy
1045   sed 's|/mnt/cdrom|/mnt/auto/cdrom|g;s|/mnt/floppy|/mnt/auto/floppy|g' /etc/fstab > /etc/fstab.new
1046   mv -f /etc/fstab.new /etc/fstab
1047 # Start automounter now
1048   einfo "Starting automounter for ${AUTOMOUNTS}."
1049   /etc/init.d/autofs start >>$DEBUG ; eend $?
1050 fi
1051 }
1052 # }}}
1053
1054 # {{{ Collect partitions from /proc/partitions first for enabling DMA
1055 check_partitions(){
1056 partitions=""
1057 IDEDISKS=""
1058 while read major minor blocks partition relax; do
1059   partition="${partition##*/}"
1060   [ -z "$partition" -o ! -e "/dev/$partition" ] && continue
1061   case "$partition" in
1062     hd?) IDEDISKS="$IDEDISKS $partition";;                # IDE  Harddisk, entire disk
1063     sd?) ;;                                               # SCSI Harddisk, entire disk
1064 #    [hs]d*) partitions="$partitions /dev/$partition";;    # IDE or SCSI disk partition
1065     [hs]d*|ub*) partitions="$partitions /dev/$partition";;    # IDE, USB or SCSI disk partition
1066   esac
1067 done <<EOT
1068 $(awk 'BEGIN{old="__start"}{if($0==old){exit}else{old=$0;if($4&&$4!="name"){print $0}}}' /proc/partitions)
1069 EOT
1070 }
1071 check_partitions 1>/dev/null 2>&1 # avoid output "check_partitions:3: read-only file system"
1072 # }}}
1073
1074 # {{{ Enable DMA for all IDE drives now if not disabled
1075 # Notice: Already done by linuxrc, but make sure it's done also on harddisk-installed systems
1076 config_dma(){
1077 if checkbootparam 'nodma'; then
1078   ewarn "Skipping DMA accelleration as requested on boot commandline." ; eend 0
1079 else
1080   for d in $(cd /proc/ide 2>>$DEBUG && echo hd[a-z]); do
1081     if test -d /proc/ide/$d; then
1082       if egrep -q 'using_dma[ \t]+0' /proc/ide/$d/settings 2>>$DEBUG; then
1083         MODEL="$(cat /proc/ide/$d/model 2>>$DEBUG)"
1084         test -z "$MODEL" && MODEL="[GENERIC IDE DEVICE]"
1085         einfo "Enabling DMA acceleration for: ${WHITE}$d        ${YELLOW}[${MODEL}]${NORMAL}"
1086         echo "using_dma:1" >/proc/ide/$d/settings
1087         eend 0
1088       fi
1089     fi
1090   done
1091 fi
1092 }
1093 # }}}
1094
1095 # {{{ Start creating /etc/fstab with HD partitions and USB SCSI devices now
1096 config_fstab(){
1097
1098 NOSWAP="yes" # we do not use swap by default!
1099 if checkbootparam 'swap' || checkbootparam 'anyswap' ; then
1100    NOSWAP=''
1101    checkbootparam 'anyswap' && export ANYSWAP='yes' || export ANYSWAP=""
1102 fi
1103
1104 if checkbootparam 'nofstab' || checkbootparam 'forensic' ; then
1105   ewarn "Skipping /etc/fstab creation as requested on boot commandline." ; eend 0
1106 else
1107   einfo "Scanning for harddisk partitions and creating /etc/fstab. (Disable this via boot option: nofstab)"
1108   iszsh && setopt nonomatch
1109   if [ -x /usr/sbin/rebuildfstab ] ; then
1110      config_userfstab || fstabuser=grml
1111      /usr/sbin/rebuildfstab -r -u $fstabuser -g $fstabuser ; eend $?
1112   else
1113      ewarn "Command rebuildfstab not found. Install package grml-rebuildfstab." ; eend 1
1114   fi
1115 fi # checkbootparam nofstab/forensic
1116
1117 # Scan for swap, config, homedir - but only in live-mode
1118 if [ -z "$INSTALLED" ] ; then
1119    [ -z "$NOSWAP" ] && einfo "Searching for swap partition(s) as requested."
1120    GRML_IMG=""
1121    GRML_SWP=""
1122    HOMEDIR="$(getbootparam 'home')"
1123    if [ -n "$partitions" ]; then
1124       while read p m f relax; do
1125         case "$p" in *fd0*|*proc*|*sys*|*\#*) continue;; esac
1126         partoptions="users,exec"
1127         fnew=""
1128         # it's a swap partition?
1129         case "$f" in swap)
1130           eindent
1131           if [ -n "$NOSWAP" ]; then
1132              ewarn "Ignoring swap partition ${WHITE}$p${NORMAL}. (Force usage via boot option 'swap', or execute grml-swapon)"
1133              eend 0
1134           else
1135              case "$(dd if=$p bs=1 count=6 skip=4086 2>/dev/null)" in
1136                    S1SUSP|S2SUSP|pmdisk|[zZ]*)
1137                      if [ -n "$ANYSWAP" ] ; then
1138                         einfo "Using swap partition ${WHITE}${p}${NORMAL} [bootoption anyswap found]."
1139                         swapon $p 2>>$DEBUG ; eend $?
1140                      else
1141                         ewarn "Suspend signature on ${WHITE}${p}${NORMAL} found, not using as swap. (Force usage via boot option: anyswap)"
1142                      fi
1143                      ;;
1144                    *)
1145                      if [[ "$p" == LABEL* ]] ; then
1146                         p=$(blkid -t $p | awk -F: '{print $1}')
1147                      fi
1148                      if grep -q $p /proc/swaps ; then
1149                         ewarn "Not using swap partition ${WHITE}${p}${NORMAL} as it is already in use." ; eend 0
1150                      else
1151                         if [ -b "$p" ] ; then
1152                         einfo "Using swap partition ${WHITE}${p}${NORMAL}."
1153                         swapon $p 2>>$DEBUG ; eend $?
1154                         else
1155                         ewarn "$p is not a valid block device - not using it therefore." ; eend 0
1156                         fi
1157                      fi
1158                      ;;
1159              esac # dd-check
1160           fi # -n "$NOSWAP
1161           eoutdent
1162           continue
1163           ;;
1164         esac # it's a swap partition?
1165
1166         # mount read-only
1167         MOUNTOPTS="ro"
1168         case "$f" in
1169           vfat|msdos|ntfs) MOUNTOPTS="$MOUNTOPTS,uid=${fstabuser},gid=${fstabuser}" ;;
1170           ext2|ext3|reiserfs|jfs|reiser4|xfs) MOUNTOPTS="$MOUNTOPTS,noatime" ;;
1171           *) continue ;;
1172           # *) NONEFOUND='1'; continue ;;
1173         esac
1174
1175         # use a swapfile
1176         if [ -z "$NOSWAP" ] ; then
1177            mount -o "$MOUNTOPTS" -t $f $p $m 2>>$DEBUG && MOUNTED=1 || continue
1178            # Activate swapfile, if exists
1179            SWAPFILE="$(/bin/ls -1d $m/[Gg][Rr][Mm][Ll].[Ss][Ww][Pp] 2>/dev/null)"
1180         fi
1181         if [ -z "$NOSWAP" -a -n "$SWAPFILE" -a -f "$SWAPFILE" ]; then
1182            mount -o remount,rw $m && MOUNTED=1
1183            if swapon "$SWAPFILE" 2>>$DEBUG ; then
1184               eindent
1185                 einfo "Using GRML swapfile ${WHITE}${SWAPFILE}${NORMAL}."
1186               eoutdent
1187               fnew="$SWAPFILE swap swap defaults 0 0"
1188               grep -q "$fnew" "/etc/fstab" || echo "$fnew" >> /etc/fstab
1189               GRML_SWP="$GRML_SWP $SWAPFILE"
1190               eend 0
1191            fi
1192            mount -o remount,ro $m 2>>$DEBUG && MOUNTED=1
1193         fi
1194
1195         # use a image as home
1196         IMAGE="$(/bin/ls -1d $m/[Gg][Rr][Mm][Ll].[Ii][Mm][Gg] 2>/dev/null)"
1197         if [ -z "$GRML_IMG" -a -n "$IMAGE" -a -f "$IMAGE" ]; then
1198            if [ -n "$HOMEDIR" ]; then
1199               if [ "$HOMEDIR" != "scan" -a "$HOMEDIR" != "$IMAGE" -a "$HOMEDIR" != "${IMAGE%/*.*}" ]; then
1200                  continue
1201               fi
1202            fi
1203            if type -a grml-image >/dev/null 2>&1 && grml-image "$IMAGE" </dev/console >/dev/console 2>&1; then
1204               GRML_IMG="$IMAGE"
1205               mount -o remount,ro $m 2>>$DEBUG && MOUNTED=1
1206            fi
1207         fi
1208         eend 0
1209
1210         # Umount, if not in use
1211         [ -n "$MOUNTED" ] && umount -r $m 2>/dev/null
1212
1213       done <<EOT
1214       $(cat /etc/fstab)
1215 EOT
1216    fi # -n $partitions
1217 fi # -z $INSTALLED
1218 }
1219 # }}}
1220
1221 # {{{ Mouse
1222 config_mouse(){
1223 if [ -n "$MOUSE_DEVICE" ] ; then
1224   einfo "Detecting mouse: ${MOUSE_FULLNAME} at ${MOUSE_DEVICE}" ; eend $?
1225 fi
1226 }
1227 # }}}
1228
1229 # {{{ IPv6 configuration
1230 # Load IPv6 kernel module and print IP adresses
1231 config_ipv6(){
1232 if checkbootparam 'ipv6'; then
1233   einfo "Enabling IPv6 as requested on boot commandline (sleeping for 2 seconds)"
1234   modprobe ipv6
1235   # we probably need some time until stateless autoconfiguration has happened
1236   sleep 2
1237   NETDEVICES="$(awk -F: '/eth.:|tr.:|wlan.:/{print $1}' /proc/net/dev 2>>$DEBUG)"
1238   for DEVICE in `echo "$NETDEVICES"`; do
1239     eindent
1240       einfo "$DEVICE:"
1241       ADDRESSES="$(ifconfig $DEVICE | awk '/.*inet6 addr:.*/{print $3}')"
1242       COUNT="$(ifconfig $DEVICE | awk '/.*inet6 addr:.*/{ sum += 1};END {print sum }')"
1243       eindent
1244         for ADDR in `echo "$ADDRESSES"` ; do
1245             einfo "$ADDR"
1246         done
1247         if [ "$COUNT" -eq "0" ] ; then
1248            einfo "(none)" ; eend 1
1249         fi
1250       eoutdent
1251     eoutdent
1252   done
1253   eend 0
1254 fi
1255 }
1256 # }}}
1257
1258 # {{{ Fat-Client-Version: DHCP Broadcast for IP address
1259 config_dhcp(){
1260 if checkbootparam 'nodhcp'; then
1261   ewarn "Skipping DHCP broadcast/network detection as requested on boot commandline." ; eend 0
1262 else
1263   NETDEVICES="$(awk -F: '/eth.:|tr.:|wlan.:/{print $1}' /proc/net/dev 2>>$DEBUG)"
1264   rm -rf /etc/network/status ; mkdir -p /etc/network/status
1265   for DEVICE in `echo "$NETDEVICES"` ; do
1266     einfo "Network device ${WHITE}${DEVICE}${NORMAL} detected, DHCP broadcasting for IP. (Backgrounding)"
1267     trap 2 3 11
1268     ifconfig $DEVICE up >>$DEBUG 2>&1
1269     ( pump -i $DEVICE >>$DEBUG 2>&1 && echo finished_running_pump > /etc/network/status/$DEVICE ) &
1270     trap "" 2 3 11
1271     sleep 1
1272     eend 0
1273   done
1274   if [ -n "$INSTALLED" ] ; then
1275      ewarn 'If you want to disable automatic DHCP requests set CONFIG_DHCP=no in /etc/grml/autoconfig.'
1276      eend 0
1277   fi
1278 fi
1279 }
1280 # }}}
1281
1282 # {{{ helper functions
1283 findfile(){
1284 FOUND=""
1285 # search all partitions for a file in the root directory
1286 for i in /mnt/[sh]d[a-z] /mnt/[sh]d[a-z][1-9] /mnt/[sh]d[a-z][1-9]?*; do
1287 # See if it's already mounted
1288   [ -f "$i/$1" ] &&  { echo "$i/$1"; return 0; }
1289   if [ -d "$i" ] && mount -r "$i" 2>>$DEBUG; then
1290     [ -f "$i/$1" ] && FOUND="$i/$1"
1291     umount -l "$i" 2>>$DEBUG
1292     [ -n "$FOUND" ] && { echo "$FOUND"; return 0; }
1293   fi
1294 done
1295 return 2
1296 }
1297
1298 fstype(){
1299 case "$(file -s $1)" in
1300   *[Ff][Aa][Tt]*|*[Xx]86*) echo "vfat"; return 0;;
1301   *[Rr][Ee][Ii][Ss][Ee][Rr]*)  echo "reiserfs"; return 0;;
1302   *[Xx][Ff][Ss]*)  echo "xfs"; return 0;;
1303   *[Ee][Xx][Tt]3*) echo "ext3"; return 0;;
1304   *[Ee][Xx][Tt]2*) echo "ext2"; return 0;;
1305   *data*)          echo "invalid"; return 0;;
1306   *) echo "auto"; return 0;;
1307 esac
1308 }
1309
1310 # Try to mount this filesystem read-only, without or with encryption
1311 trymount(){
1312 # Check if already mounted
1313 case "$(cat /proc/mounts)" in *\ $2\ *) return 0;; esac
1314 # Apparently, mount-aes DOES autodetect AES loopback files.
1315 [ -b "$1" ] && { mount -t auto -o ro "$1" "$2" 2>>$DEBUG; RC="$?"; }
1316 # We need to mount crypto-loop files with initial rw support
1317 [ -f "$1" ] && { mount -t auto -o loop,rw "$1" "$2" 2>>$DEBUG; RC="$?"; }
1318 # Mount succeeded?
1319 [ "$RC" = "0" ] && return 0
1320 echo ""
1321 einfo "Filesystem not autodetected, trying to mount $1 with AES256 encryption."
1322 a="y"
1323 while [ "$a" != "n" -a "$a" != "N" ]; do
1324 # We need to mount crypto-loop files with initial rw support
1325  mount -t auto -o loop,rw,encryption=AES256 "$1" "$2" && return 0
1326  echo -n "${RED}Mount failed, retry? [Y/n] ${NORMAL}"
1327  # Problem with ioctl() from getpasswd()?
1328  # read a
1329  read a
1330 done
1331 return 1
1332 }
1333 # }}}
1334
1335 # {{{ CPU-detection
1336 config_cpu(){
1337 if checkbootparam 'nocpu'; then
1338   ewarn "Skipping CPU detection as requested on boot commandline." ; eend 0
1339 else
1340   # check module dependencies
1341   cpufreq_check() {
1342    if ! [ -e /lib/modules/${KERNEL}/kernel/arch/x86/kernel/cpu/cpufreq ] ; then
1343       if [ -e /lib64 ] ; then
1344          [ -e /lib/modules/${KERNEL}/kernel/arch/x86_64/kernel/cpufreq ] || return 1
1345       else
1346          [ -e /lib/modules/${KERNEL}/kernel/arch/i386/kernel/cpu/cpufreq -o ! -e /lib/modules/${KERNEL}/kernel/drivers/cpufreq ] || return 1
1347       fi
1348    fi
1349   }
1350
1351   if [[ `grep -c processor /proc/cpuinfo` -gt 1 ]] ; then
1352      einfo "Detecting CPU:"
1353      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)
1354      echo $CPU | sed 's/ \{1,\}/ /g'
1355      eend 0
1356   else
1357      einfo "Detecting 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
1358   fi
1359
1360   # Disclaimer: sorry for the tons of if/then/else... but this makes sure we use:
1361   # * it only if we have the according kernel modules available
1362   # * cpufreq only on laptops (check via /usr/sbin/laptop-detect) and not inside Virtual Box
1363   # * current version of /etc/init.d/loadcpufreq from Debian (to stay in sync)
1364   #   -> parse output of the initscript and output it according to our look'n'feel
1365   # * our own cpufreq-detect.sh if /etc/init.d/loadcpufreq isn't present
1366   if ! cpufreq_check ; then
1367     ewarn "Skipping cpufreq setup as module dependencies are not fulfilled." ; eend 1
1368   else
1369     if /usr/sbin/laptop-detect 1>/dev/null 2>&1 ; then
1370        # Virtual Box supports ACPI and laptop-detect returns with '0', so check for it:
1371        if [ -r /proc/acpi/battery/BAT0/info ] ; then
1372           if grep -q 'OEM info:                innotek' /proc/acpi/battery/BAT0/info ; then
1373              einfo 'Virtual Box detected, skipping cpufreq setup.' ; eend 0
1374              return 0
1375           fi
1376        fi
1377        einfo "Detected Laptop - trying to use cpu frequency scaling:"
1378        eindent
1379        if [ -x /etc/init.d/loadcpufreq ] ; then
1380           SKIP_CPU_GOVERNOR=''
1381           LOADCPUFREQ=$(mktemp)
1382           /etc/init.d/loadcpufreq start >"$LOADCPUFREQ" 2>&1 ; RC=$?
1383           if grep -q FATAL "$LOADCPUFREQ" ; then
1384              eindent
1385                SKIP_CPU_GOVERNOR=1
1386                oldIFS="$IFS"
1387                IFS="
1388 "
1389                 for line in $(grep FATAL "$LOADCPUFREQ" | sed 's/.*FATAL: //; s/ (.*)//') ; do
1390                     eerror "$line" ; eend $RC
1391                 done
1392                 IFS="$oldIFS"
1393              eoutdent
1394            elif grep -q done "$LOADCPUFREQ" ; then
1395              MODULE=$(grep done "$LOADCPUFREQ" | sed 's/.*done (\(.*\))./\1/')
1396              if [ -n "$MODULE" -a "$MODULE" != none ]; then
1397                 einfo "Loading cpufreq kernel module $MODULE" ; eend 0
1398              else
1399                 ewarn "Could not find an appropriate kernel module for cpu frequency scaling." ; eend 1
1400              fi
1401           fi
1402           rm -f $LOADCPUFREQ
1403        elif [ -r /usr/bin/cpufreq-detect.sh ] ; then
1404           . /usr/bin/cpufreq-detect.sh
1405           if [ -n "$MODULE" -a "$MODULE" != none ]; then
1406              einfo "Loading modules ${MODULE}"
1407              modprobe "$MODULE" 1>>$DEBUG || modprobe "$MODULE_FALLBACK" 1>>$DEBUG
1408              RC=$?
1409              if [[ $RC == 0 ]]; then
1410                 eend 0
1411              else
1412                 SKIP_CPU_GOVERNOR=1
1413                 eend $1
1414              fi
1415           else
1416              ewarn "Could not detect an appropriate CPU for use with cpu frequency scaling - skipping." && eend 1
1417           fi # $MODULE
1418        fi # loadcpufreq
1419
1420        if [ -z "$SKIP_CPU_GOVERNOR" ] ; then
1421           einfo "Loading cpufreq_ondemand, setting ondemand governor"
1422           if modprobe cpufreq_ondemand ; RC=$? ; then
1423              for file in $(find /sys/devices/system/cpu/ -name scaling_governor 2>/dev/null) ; do
1424                  echo ondemand > $file
1425              done
1426           fi
1427           eend $RC
1428        fi # cpu-governor
1429
1430        eoutdent
1431
1432     fi # laptop-detect
1433   fi # cpufreq_check
1434 fi # checkbootparam nocpu
1435 }
1436 # }}}
1437
1438 # {{{ autostart of ssh
1439 config_ssh(){
1440 if checkbootparam 'ssh' ; then
1441    SSH_PASSWD=''
1442    SSH_PASSWD="$(getbootparam 'ssh' 2>>$DEBUG)"
1443    einfo "Bootoption ssh found, trying to set password for user grml."
1444    eindent
1445    if [ -z "$SSH_PASSWD" ] ; then
1446       if [ -x /usr/bin/apg ] ; then
1447          SSH_PASSWD="$(apg -M NL -a 0 -m 8 -x 12 -n 1)"
1448       elif [ -x /usr/bin/gpw ] ; then
1449          SSH_PASSWD="$(gpw 1)"
1450       elif [ -x /usr/bin/pwgen ] ; then
1451          SSH_PASSWD="$(pwgen -1 8)"
1452       elif [ -x /usr/bin/hexdump ] ; then
1453          SSH_PASSWD="$(dd if=/dev/urandom bs=14 count=1 2>/dev/null | hexdump | awk '{print $3 $4}')"
1454       elif [ -n "$RANDOM" ] ; then
1455          SSH_PASSWD="grml${RANDOM}"
1456       else
1457          SSH_PASSWD=''
1458          eerror "Empty passphrase and neither pwgen nor hexdump nor \$RANDOM found. Skipping."
1459          eend 1
1460       fi
1461
1462       if [ -n "$SSH_PASSWD" ] ; then
1463          ewarn "No given password for ssh found. Using random password: $SSH_PASSWD" ; eend 0
1464       fi
1465    fi
1466    eoutdent
1467
1468    # finally check if we have a password we can use:
1469    if [ -n "$SSH_PASSWD" ] ; then
1470       # chpasswd sucks, seriously.
1471       if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
1472         echo "grml:$SSH_PASSWD" | chpasswd -m
1473       else
1474         echo "grml:$SSH_PASSWD" | chpasswd
1475       fi
1476    fi
1477
1478    einfo 'Starting secure shell server in background.'
1479    /etc/init.d/rmnologin start 1>>$DEBUG 2>>$DEBUG
1480    /etc/init.d/ssh start 1>>$DEBUG 2>>$DEBUG &
1481    eend $?
1482
1483    eindent
1484    ewarn 'Warning: please change the password for user grml as soon as possible!'
1485    eoutdent
1486 fi
1487 }
1488 # }}}
1489
1490 # {{{ set password for user grml
1491 config_passwd(){
1492 if checkbootparam 'passwd' >>$DEBUG 2>&1; then
1493   einfo "Bootoption passwd found."
1494   PASSWD="$(getbootparam 'passwd' 2>>$DEBUG)"
1495   if [ -n "$PASSWD" ] ; then
1496     echo "grml:$PASSWD" | chpasswd -m ; eend $?
1497   else
1498     eerror "No given password for ssh found. Autostart of SSH will not work." ; eend 1
1499   fi
1500   eindent
1501     ewarn "Warning: please change the password for user grml set via bootparameter as soon as possible!"
1502   eoutdent
1503 fi
1504 }
1505 # }}}
1506
1507 # {{{ Check for persistent homedir option and eventually mount /home from there, or use a loopback file.
1508 config_homedir(){
1509 if checkbootparam 'home' ; then
1510    HOMEDIR="$(getbootparam 'home')"
1511    MYHOMEDEVICE=""
1512    MYHOMEMOUNTPOINT=""
1513    MYHOMEDIR=""
1514    if [ -n "$HOMEDIR" ]; then
1515       einfo "Bootoption home detected." && eend 0
1516       case "$HOMEDIR" in
1517         /dev/*)
1518         MYHOMEDEVICE="${HOMEDIR##/dev/}"
1519         MYHOMEDEVICE="/dev/${MYHOMEDEVICE%%/*}"
1520         MYHOMEMOUNTPOINT="/mnt/${MYHOMEDEVICE##/dev/}"
1521         MYHOMEDIR="/mnt/${HOMEDIR##/dev/}"
1522       ;;
1523         /mnt/*)
1524         MYHOMEDEVICE="${HOMEDIR##/mnt/}"
1525         MYHOMEDEVICE="/dev/${MYHOMEDEVICE%%/*}"
1526         MYHOMEMOUNTPOINT="/mnt/${MYHOMEDEVICE##/dev/}"
1527         MYHOMEDIR="$HOMEDIR"
1528       ;;
1529         [Aa][Uu][Tt][Oo]|[Ss][Cc][Aa][Nn]|[Ff][Ii][Nn][Dd])
1530         MYHOMEDIR="$(findfile grml.img)"
1531         MYHOMEDEVICE="${MYHOMEDIR##/mnt/}"
1532         MYHOMEDEVICE="/dev/${MYHOMEDEVICE%%/*}"
1533         MYHOMEMOUNTPOINT="/mnt/${MYHOMEDEVICE##/dev/}"
1534       ;;
1535       *)
1536         eerror "Invalid home= option '$HOMEDIR' specified (must start with /dev/ or /mnt/ or 'scan')." ; eend 1
1537         eerror "Option ignored." ; eend 1
1538       ;;
1539       esac
1540    fi # -n $HOMEDIR
1541
1542    if [ -n "$MYHOMEDIR" ]; then
1543       if trymount "$MYHOMEDEVICE" "$MYHOMEMOUNTPOINT"; then
1544          [ -f "$MYHOMEMOUNTPOINT/grml.img" ] && MYHOMEDIR="$MYHOMEMOUNTPOINT/grml.img"
1545          while read device mountpoint fs relax; do
1546            case "$mountpoint" in *$MYHOMEMOUNTPOINT*)
1547              case "$fs" in
1548                *[Nn][Tt][Ff][Ss]*)
1549                  umount "$MYHOMEMOUNTPOINT"; eerror "Error: will not mount NTFS filesystem on $MYHOMEDEVICE read/write!" ; eend 1
1550                  break
1551                  ;;
1552                *[Ff][Aa][Tt]*)
1553                  # Note: This currently won't work with encrypted partitions
1554                  umount "$MYHOMEMOUNTPOINT"; mount -t vfat -o rw,uid=grml,gid=grml,umask=002 "$MYHOMEDEVICE" "$MYHOMEMOUNTPOINT"
1555                  if [ ! -f "$MYHOMEDIR" ]; then
1556                     ewarn "WARNING: FAT32 is not a good filesystem option for /home/grml (missing socket/symlink support)."
1557                     ewarn "WARNING: Better use an ext2 loopback file on this device, and boot with home=$MYHOMEDEVICE/grml.img."
1558                  fi
1559                  ;;
1560              esac
1561
1562              if mount -o remount,rw "$MYHOMEMOUNTPOINT"; then
1563                 einfo "Mounting ${WHITE}$MYHOMEDIR${NORMAL} as ${WHITE}/home/grml${NORMAL}."
1564                 if [ -f "$MYHOMEDIR" ]; then
1565                    # It's a loopback file, mount it over the /home/grml directory
1566                    trymount "$MYHOMEDIR" /home/grml
1567                    RC="$?"
1568                    [ "$RC" = "0" ] && ERROR="$(mount -o remount,rw /home/grml 2>&1)"
1569                    RC="$?"
1570                 else
1571                    # Do a --bind mount
1572                    ERROR="$(mount --bind "$MYHOMEDIR" /home/grml 2>&1)"
1573                    RC="$?"
1574                 fi # -f $MYHOMEDIR
1575
1576                 [ "$RC" = "0" ] && eend 0 || ( eerror "${ERROR}" ; eend 1 )
1577
1578              fi #  mount -o remount,rw,...
1579            break
1580            ;;
1581            esac # case "$mountpoint" in *$MYHOMEMOUNTPOINT*)
1582          done <<EOT
1583          $(cat /proc/mounts)
1584 EOT
1585      fi # if trymount ...
1586    fi # -n $MYHOMEDIR
1587 fi # checkbootparam home
1588 }
1589 # }}}
1590
1591 # {{{ Sound
1592 config_mixer(){
1593 if ! [ -x /usr/bin/aumix ] ; then
1594   eerror "aumix binary not available. Can not set sound volumes therefore." ; eend 1
1595 else
1596
1597   if ! [ -r /proc/asound/cards ] ; then
1598      ewarn "No soundcard present, skipping mixer settings therefore." ; eend 0
1599      return
1600   fi
1601
1602   if checkbootparam 'vol' ; then
1603     VOL="$(getbootparam 'vol' 2>>$DEBUG)"
1604     if [ -z "$VOL" ] ; then
1605       eerror "Bootoption vol found but no volume level/parameter given. Using defaults." ; eend 1
1606       VOL='75' # default
1607     fi
1608   else
1609     VOL='75' # default
1610   fi
1611
1612   if checkbootparam 'nosound' ; then
1613     einfo "Muting sound devices on request."
1614     # some IBM notebooks require the following stuff:
1615     if [ -x /usr/bin/amixer ] ; then
1616        if amixer get Front 1>/dev/null 2>>$DEBUG ; then
1617           amixer set Front unmute 1>/dev/null
1618           amixer set Front 0% 1>/dev/null
1619        fi
1620     fi
1621     ERROR=$(aumix -w 0 -v 0 -p 0 -m 0 2>&1) ; RC=$?
1622     if [ -n "$ERROR" ] ; then
1623        eindent
1624        eerror "Problem muting sound devices: $ERROR"
1625        eoutdent
1626     fi
1627     eend $RC
1628   elif [ -z "$INSTALLED" ]; then
1629       einfo "Setting mixer volumes to level ${WHITE}${VOL}${NORMAL}."
1630       # some IBM notebooks require the following stuff:
1631       if [ -x /usr/bin/amixer ] ; then
1632          if amixer get Front 1>/dev/null 2>>$DEBUG ; then
1633             amixer set Front unmute 1>/dev/null
1634             amixer set Front ${VOL}% 1>/dev/null
1635          fi
1636       fi
1637       # by default assume '0' as volume for microphone:
1638       if checkbootparam 'micvol' ; then
1639          MICVOL="$(getbootparam 'micvol' 2>>$DEBUG)"
1640       else
1641          MICVOL=0
1642       fi
1643
1644       # finally set the volumes:
1645       ERROR=$(aumix -w $VOL -v $VOL -p $VOL -m $MICVOL 2>&1) ; RC=$?
1646       if [ -n "$ERROR" ] ; then
1647          eindent
1648          eerror "Problem setting mixer volumes: $ERROR (no soundcard?)"
1649          eoutdent
1650       fi
1651       eend $RC
1652   fi
1653
1654 fi
1655 }
1656 # }}}
1657
1658 # {{{ modem detection
1659 config_modem(){
1660 if checkbootparam 'nomodem'; then
1661   ewarn "Skipping check for AC97 modem controller as requested on boot commandline." ; eend 0
1662 else
1663   if [ -x /etc/init.d/sl-modem-daemon ] ; then
1664      if lspci | grep Intel | grep -q "AC'97 Modem Controller" ; then
1665         einfo "AC97 modem controller detected. Start it running 'Start sl-modem-daemon'."
1666         eend 0
1667      fi
1668   fi
1669 fi
1670 }
1671 # }}}
1672
1673 # {{{ keyboard add-ons
1674 config_setkeycodes(){
1675 if checkbootparam 'setkeycodes' ; then
1676  einfo "Setting keycodes as requested via bootparameter 'setkeycodes'."
1677   # MS MM keyboard add-on
1678   # fix
1679   setkeycodes e001 126 &>/dev/null
1680   setkeycodes e059 127 &>/dev/null
1681   # fn keys
1682   setkeycodes e03b 59 &>/dev/null
1683   setkeycodes e008 60 &>/dev/null
1684   setkeycodes e007 61 &>/dev/null
1685   setkeycodes e03e 62 &>/dev/null
1686   setkeycodes e03f 63 &>/dev/null
1687   setkeycodes e040 64 &>/dev/null
1688   setkeycodes e041 65 &>/dev/null
1689   setkeycodes e042 66 &>/dev/null
1690   setkeycodes e043 67 &>/dev/null
1691   setkeycodes e023 68 &>/dev/null
1692   setkeycodes e057 87 &>/dev/null
1693   setkeycodes e058 88 &>/dev/null
1694   # hp keycodes
1695   setkeycodes e00a 89 e008 90 &>/dev/null
1696  eend 0
1697 fi
1698 }
1699 # }}}
1700
1701 # {{{ wondershaper
1702 config_wondershaper(){
1703  if checkbootparam 'wondershaper' ; then
1704     WONDER="$(getbootparam 'wondershaper' 2>>$DEBUG)"
1705     CMD=wondershaper
1706     DEVICE=""
1707     DOWNSTREAM=""
1708     UPSTREAM=""
1709     if [ -n "$WONDER" ]; then
1710       # Extra options
1711       DEVICE="${WONDER%%,*}"
1712       R="${WONDER#*,}"
1713       if [ -n "$R" -a "$R" != "$WONDER" ]; then
1714         WONDER="$R"
1715         DOWNSTREAM="${WONDER%%,*}"
1716         R="${WONDER#*,}"
1717         if [ -n "$R" -a "$R" != "$WONDER" ]; then
1718           WONDER="$R"
1719           UPSTREAM="${WONDER%%,*}"
1720           R="${WONDER#*,}"
1721         fi
1722       fi
1723     fi
1724     [ -n "$DEVICE" ]     && CMD="$CMD $DEVICE"
1725     [ -n "$DOWNSTREAM" ] && CMD="$CMD $DOWNSTREAM"
1726     [ -n "$UPSTREAM" ]   && CMD="$CMD $UPSTREAM"
1727     einfo "Starting wondershaper (${CMD}) in background."
1728     ( sh -c $CMD & ) && eend 0
1729  fi
1730 }
1731 # }}}
1732
1733 # {{{ syslog-ng
1734 config_syslog(){
1735  if checkbootparam 'nosyslog'; then
1736     ewarn "Not starting syslog daemon as requested on boot commandline." ; eend 0
1737  else
1738     SYSLOGD=''
1739     [ -x /etc/init.d/syslog-ng ] && SYSLOGD='syslog-ng'
1740     [ -x /etc/init.d/rsyslog   ] && SYSLOGD='rsyslog'
1741     [ -x /etc/init.d/dsyslog   ] && SYSLOGD='dsyslog'
1742     [ -x /etc/init.d/sysklogd  ] && SYSLOGD='sysklogd'
1743     [ -x /etc/init.d/inetutils-syslogd ] && SYSLOGD='inetutils-syslogd'
1744
1745     if [ -z "$SYSLOGD" ] ; then
1746        eerror "No syslog daemon found." ; eend 1
1747     else
1748        einfo "Starting $SYSLOGD in background."
1749        /etc/init.d/$SYSLOGD start 1>>$DEBUG &
1750        eend 0
1751     fi
1752  fi
1753 }
1754 # }}}
1755
1756 # {{{ gpm
1757 config_gpm(){
1758  if checkbootparam 'nogpm'; then
1759   ewarn "Not starting GPM as requested on boot commandline." ; eend 0
1760  else
1761    if ! [ -r /dev/input/mice ] ; then
1762       eerror "No mouse found - not starting GPM." ; eend 1
1763    else
1764       einfo "Starting gpm in background."
1765       /etc/init.d/gpm start 1>>$DEBUG &
1766       # ( while [ ! -e /dev/psaux ]; do sleep 5; done; /etc/init.d/gpm start 1>>$DEBUG ) &
1767       eend 0
1768    fi
1769  fi
1770 }
1771 # }}}
1772
1773 # {{{ services
1774 config_services(){
1775  if checkbootparam 'services' ; then
1776     SERVICE="$(getbootparam 'services' 2>>$DEBUG)"
1777     SERVICELIST=$(echo "$SERVICE" | sed 's/,/\\n/g')
1778     SERVICENL=$(echo "$SERVICE" | sed 's/,/ /g')
1779     einfo "Starting service(s) ${SERVICENL} in background."
1780     for service in $(echo -e $SERVICELIST) ; do
1781        /etc/init.d/${service} start 1>>$DEBUG &
1782     done
1783     [ "$?" == "0" ] ; eend $?
1784  fi
1785 }
1786 # }}}
1787
1788 # {{{ config files
1789 config_netconfig(){
1790  if checkbootparam 'netconfig' ; then
1791   CONFIG="$(getbootparam 'netconfig' 2>>$DEBUG)"
1792   CONFIGFILE='/tmp/netconfig.grml'
1793
1794   getconfig() {
1795   wget --timeout=10 --dns-timeout=10  --connect-timeout=10 \
1796        --read-timeout=10 $CONFIG -O $CONFIGFILE && return 0 || return 1
1797   }
1798   einfo "Trying to get ${WHITE}${CONFIG}${NORMAL}"
1799   counter=10
1800   while ! getconfig && [[ "$counter" != 0 ]] ; do
1801     echo -n "Sleeping for 5 seconds and trying to get config again... "
1802     counter=$(( counter-1 ))
1803     echo "$counter tries left" ; sleep 1
1804   done
1805   if [ -r "$CONFIGFILE" ] ; then
1806     einfo "Downloading was successfull." ; eend 0
1807     einfo "md5sum of ${WHITE}${CONFIG}${NORMAL}: "
1808     md5sum $CONFIGFILE ; eend 0
1809     cd / && einfo "Unpacking ${WHITE}${CONFIGFILE}${NORMAL}:" && /usr/bin/unp $CONFIGFILE $EXTRACTOPTIONS ; eend $?
1810   else
1811     einfo "Sorry, could not fetch $CONFIG" ; eend 1
1812   fi
1813  fi
1814 }
1815 # }}}
1816
1817 # {{{ blindsound
1818 config_blindsound(){
1819  if checkbootparam 'blind' ; then
1820     beep
1821     flitewrapper "welcome to the gremel system"
1822  fi
1823 }
1824 # }}}
1825
1826 # {{{ welcome sound
1827 config_welcome(){
1828  if checkbootparam 'welcome' ; then
1829     flitewrapper "welcome to the gremel system"
1830  fi
1831 }
1832 # }}}
1833
1834 # {{{ fix/workaround for unionfs
1835 fix_unionfs(){
1836   if [ -z "$INSTALLED" ]; then
1837    touch /var/cache/apt/*cache.bin
1838   fi
1839 }
1840 # }}}
1841
1842 # {{{ create all /mnt-directories
1843 create_mnt_dirs(){
1844   ewarn "create_mnt_dirs is deprecated as grml-rebuildfstab does all we need."
1845   ewarn "Please set CONFIG_CREATE_MNT_DIRS='no' in /etc/grml/autoconfig" ; eend 0
1846 }
1847 # }}}
1848
1849 # {{{ start X window system via grml-x
1850 config_x_startup(){
1851 # make sure we start X only if startx is used *before* a nostartx option
1852 # so it's possible to disable automatic X startup using nostart
1853 if checkbootparam 'startx' && ! grep -q 'startx.*nostartx' "$CMDLINE" ; then
1854  if [ -x /usr/X11R6/bin/X ] ; then
1855   if [ -z "$INSTALLED" ] ; then
1856    WINDOWMANAGER="$(getbootparam 'startx' 2>>$DEBUG)"
1857    if [ -z "$WINDOWMANAGER" ] ; then
1858      einfo "No window manager specified. Taking ${WHITE}wm-ng${NORMAL} as default." && eend 0
1859      WINDOWMANAGER="wm-ng"
1860    else
1861      einfo "Window manager ${WHITE}${WINDOWMANAGER}${NORMAL} found as bootoption." && eend 0
1862    fi
1863    einfo "Changing to runlevel 5 for starting grml-x ${WINDOWMANAGER}. Just exit X windows system to get full featured consoles."
1864    config_userfstab || fstabuser='grml'
1865  cat>|/etc/init.d/xstartup<<EOF
1866 #!/bin/sh
1867 # su - $fstabuser -c 'grml-x "$WINDOWMANAGER"'
1868 sudo -u $fstabuser -i /usr/bin/grml-x $WINDOWMANAGER 1>>$DEBUG
1869 EOF
1870    chmod 755 /etc/init.d/xstartup
1871
1872    # adjust inittab for xstartup
1873    if grep -q '^6:' /etc/inittab ; then
1874       sed -i 's|^6:.*|6:2345:respawn:/bin/zsh --login -c "/etc/init.d/xstartup ; /bin/zsh"|' /etc/inittab
1875    else # just append tty6 to inittab if no definition is present:
1876       echo '6:2345:respawn:/bin/zsh --login -c "/etc/init.d/xstartup ; /bin/zsh"' >> /etc/inittab
1877    fi
1878
1879    /sbin/telinit q ; eend $?
1880
1881    if grep -q '^allowed_users=' /etc/X11/Xwrapper.config ; then
1882       sed -i 's/^allowed_users=.*/allowed_users=anybody/' /etc/X11/Xwrapper.config
1883    else
1884       echo 'allowed_users=anybody' >> /etc/X11/Xwrapper.config
1885    fi
1886
1887   else
1888     eerror "We are not running in live mode - startx will not work, skipping it."
1889     eerror " -> Please use something like xdm, gdm or kdm for starting X on a harddisk system!" ; eend 1
1890   fi
1891  else
1892    eerror "/usr/X11R6/bin/X is not present on this grml flavour."
1893    eerror "  -> Boot parameter startx does not work therefore." ; eend 1
1894  fi
1895 fi
1896 }
1897 # }}}
1898
1899 # {{{ configuration framework
1900 config_extract(){
1901 if checkbootparam 'extract' ; then
1902  EXTRACT="$(getbootparam 'extract' 2>>$DEBUG)"
1903  EXTRACTOPTIONS="-- -x $EXTRACT"
1904 fi
1905 }
1906
1907 config_finddcsdir() {
1908 #  - If no GRMLCFG partition is found and noautoconfig is _not_ given
1909 #    on the command line, nothing is changed and the dcs files are
1910 #    searched within the .iso, $dcs-dir is set to the root directory
1911 #    within the .iso
1912 #  - If a GRMLCFG partition is found, $dcs-dir is set to the root of
1913 #    the GRMLCFG partition unless noautoconfig is set. If noautoconfig is
1914 #    set, $dcs-dir is set to the root directory within the .iso.
1915 #  - If myconfig=foo is set on the command line, $dcs-dir is set to
1916 #    foo, even if a GRMLCFG partition is present.
1917 DCSDIR=""
1918 if checkbootparam 'noautoconfig' || checkbootparam 'forensic' ; then
1919   ewarn "Skipping running automount of device(s) labeled GRMLCFG as requested." ; eend 0
1920 else
1921   if [ -z "$INSTALLED" ] ; then
1922     if checkbootparam 'myconfig' ; then
1923       DCSDEVICE="$(getbootparam 'myconfig' 2>>$DEBUG)"
1924       if [ -z "$DCSDEVICE" ]; then
1925         einfo "No device for bootoption myconfig provided." ; eend 1
1926       fi # [ -z "$DCSDEVICE" ]
1927     elif checkvalue $CONFIG_MYCONFIG; then # checkbootparam myconfig
1928       einfo "Searching for device(s) labeled with GRMLCFG. (Disable this via boot option: noautoconfig)" ; eend 0
1929       eindent
1930       # We do need the following fix so floppy disk is available to blkid in any case :-/
1931       if [ -r /dev/fd0 ] ; then
1932         einfo "Floppy device detected. Trying to access floppy disk."
1933         if timeout 4 dd if=/dev/fd0 of=/dev/null bs=512 count=1 1>>$DEBUG 2>&1 ; then
1934            blkid /dev/fd0 1>>$DEBUG 2>&1
1935         fi
1936       fi
1937       DCSDEVICE=$(blkid -t LABEL=GRMLCFG | head -1 | awk -F: '{print $1}')
1938     fi
1939     if [ -n "$DCSDEVICE" ]; then
1940       einfo "debs, config, scripts are read from $DCSDEVICE." ; eend 0
1941       DCSDIR="$(< /proc/mounts awk -v DCSDEV=$DCSDEVICE '{if ($1 == DCSDEV) { print $2 }}')"
1942       if [ -n "$DCSDIR" ]; then
1943         einfo "$DCSDEVICE already mounted on $DCSDIR"; eend 0
1944       else
1945         [ -d /mnt/grml ] || mkdir /mnt/grml
1946         umount /mnt/grml 1>>$DEBUG 2>&1 # make sure it is not mounted
1947         mount -o ro -t auto $DCSDEVICE  /mnt/grml ; RC="$?"
1948         if [[ $RC == 0 ]]; then
1949           einfo "Successfully mounted $DCSDEVICE to /mnt/grml (readonly)." ; eend 0
1950           eindent
1951         fi
1952         DCSDIR="/mnt/grml"
1953       fi
1954     fi
1955   fi
1956 fi
1957
1958 if [ -n "$DCSDIR" ]; then
1959   einfo "Debs, config, scripts will be read from $DCSDIR." ; eend 0
1960 elif checkbootparam 'debs' || checkbootparam 'config' || checkbootparam 'scripts'; then
1961   einfo "Debs, config, scripts will be read from the live image directly." ; eend 0
1962 fi
1963 }
1964
1965
1966 config_partconf() {
1967 if checkbootparam 'partconf' ; then
1968  MOUNTDEVICE="$(getbootparam 'partconf' 2>>$DEBUG)"
1969  if [ -n "$MOUNTDEVICE" ]; then
1970    [ -d /mnt/grml ] || mkdir /mnt/grml
1971    mount -o ro -t auto $MOUNTDEVICE /mnt/grml ; RC="$?"
1972     if [[ $RC == 0 ]]; then
1973       einfo "Successfully mounted $MOUNTDEVICE to /mnt/grml (readonly)." ; eend 0
1974       einfo "Copying files from $MOUNTDEVICE over grml system."
1975       for file in `cat /etc/grml/partconf` ; do
1976         [ -d /mnt/grml/$file ] && cp -a /mnt/grml/${file}* ${file} && echo "copied: $file"
1977         [ -f /mnt/grml/$file ] && cp -a /mnt/grml/${file}  ${file} && echo "copied: $file"
1978       done && eend 0
1979     else
1980       einfo "Could not mount $MOUNTDEVICE to /mnt/grml - sorry." ; eend 1
1981     fi # mount $MOUNTDEVICE
1982    grep -q '/mnt/grml' /proc/mounts && umount /mnt/grml
1983  else
1984    einfo "Sorry, no device for bootoption partconf provided. Skipping." ; eend 1
1985  fi # [ -n "$MOUNTDEVICE" ]
1986 fi
1987 }
1988 # }}}
1989
1990 # {{{ /cdrom/.*-options
1991 config_debs(){
1992 if checkbootparam 'debs' ; then
1993    DEBS="$(getbootparam 'debs' 2>>$DEBUG)"
1994    if ! echo $DEBS | grep -q '/'; then
1995      # backwards compatibility: if no path is given get debs from debs/
1996      DEBS="debs/$DEBS"
1997    fi
1998    einfo "Tring to install debian package(s) ${DEBS}"
1999    DEBS="$(eval echo ${DCSDIR}/$DEBS)"
2000    dpkg -i $DEBS ; eend $?
2001 fi
2002 }
2003
2004 config_scripts(){
2005 if checkbootparam 'scripts' ; then
2006    SCRIPTS="$(getbootparam 'scripts' 2>>$DEBUG)"
2007    if [ -z "$SCRIPTS" ]; then
2008      SCRIPTS="$(cd ${DCSDIR}/scripts; /bin/ls -1d [Gg][Rr][Mm][Ll].[Ss][Hh] 2>>$DEBUG)"
2009    fi
2010    if ! echo $SCRIPTS | grep -q '/'; then
2011      # backwards compatibility: if no path is given get scripts from scripts/
2012      SCRIPTS="scripts/$SCRIPTS"
2013    fi
2014    SCRIPTS="${DCSDIR}/$SCRIPTS"
2015    if [ -d "$SCRIPTS" ]; then
2016      einfo "Bootparameter scripts found. Trying to execute from directory ${SCRIPTS}:"
2017      run-parts $SCRIPTS
2018    else
2019      einfo "Bootparameter scripts found. Trying to execute ${SCRIPTS}:"
2020      sh -c $SCRIPTS
2021    fi
2022 fi
2023 }
2024
2025 config_config(){
2026 if checkbootparam 'config' ; then
2027   CONFIG="$(getbootparam 'config' 2>>$DEBUG)"
2028   if [ -z "$CONFIG" ]; then
2029     CONFIG="$(cd ${DCSDIR}; ls -1d [Cc][Oo][Nn][Ff][Ii][Gg].[Tt][Bb][Zz] 2>>$DEBUG)"
2030   fi
2031   if [ -n "$CONFIG" ]; then
2032     if [ -d "${DCSDIR}/${CONFIG}" ] ; then
2033       einfo "Taking configuration from directory ${DCSDIR}/${CONFIG}"
2034
2035       cp -a ${DCSDIR}/${CONFIG}/* /
2036     elif [ -f "${DCSDIR}/${CONFIG}" ]; then
2037       einfo "Extracting configuration from file ${DCSDIR}/${CONFIG}"
2038
2039       cd /
2040       unp ${DCSDIR}/${CONFIG} $EXTRACTOPTIONS ; eend $?
2041     else
2042       ewarn "Sorry, could not find configuration file or directory ${DCSDIR}/${FILENAME}." ; eend 1
2043     fi
2044   fi
2045 fi
2046 # umount /mnt/grml if it was mounted by finddcsdir
2047 # this doesn't really belong here
2048 grep -q '/mnt/grml' /proc/mounts && umount /mnt/grml
2049 }
2050 # }}}
2051
2052 # {{{ mypath
2053 config_mypath(){
2054 if checkbootparam 'mypath' ; then
2055    MY_PATH="$(getbootparam 'mypath' 2>>$DEBUG)"
2056    einfo "Bootparameter mypath found, adding ${MY_PATH} to /etc/grml/my_path"
2057    touch /etc/grml/my_path
2058    chmod 644 /etc/grml/my_path
2059    # make sure the directories exist:
2060    eindent
2061    for i in $(echo $MY_PATH | sed 's/:/\n/g') ; do
2062        if ! [ -d "$i" ] ; then
2063           einfo "Creating directory $i"
2064           mkdir -p "$i" ; eend $?
2065        fi
2066    done
2067    grep -q "${MY_PATH}" /etc/grml/my_path || echo "${MY_PATH}" >> /etc/grml/my_path ; eend $?
2068    eoutdent
2069 fi
2070 }
2071 # }}}
2072
2073 # {{{ distcc
2074 config_distcc(){
2075 if checkbootparam 'distcc' ; then
2076  OPTIONS="$(getbootparam 'distcc' 2>>$DEBUG)"
2077  if [ -n "$OPTIONS" ]; then
2078     NET=""
2079     INTERFACE=""
2080     if [ -n "$OPTIONS" ]; then
2081       NET="${OPTIONS%%,*}"
2082       R="${OPTIONS#*,}"
2083       if [ -n "$R" -a "$R" != "$OPTIONS" ]; then
2084         OPTIONS="$R"
2085         INTERFACE="${OPTIONS%%,*}"
2086         R="${OPTIONS#*,}"
2087       fi
2088     fi
2089  fi
2090  CONFIG=/etc/default/distcc
2091  sed -i "s#^STARTDISTCC=.*#STARTDISTCC=YES#"  $CONFIG
2092  sed -i "s#^ALLOWEDNETS=.*#ALLOWEDNETS=$NET#" $CONFIG
2093
2094  if [ -n "$INTERFACE" ] ; then
2095    IP=$(LANG=C ifconfig $INTERFACE | gawk -F: /"inet addr"/'{print $2}' | gawk '{print $1}')
2096
2097    counter=10
2098    while [ -z "$IP" ] && [[ "$counter" != 0 ]] ; do
2099      counter=$(( counter-1 ))
2100      ewarn "No ip address for $INTERFACE found. Sleeping for 3 seconds. $counter tries left."
2101      sleep 3
2102      IP=$(LANG=C ifconfig $INTERFACE | gawk -F: /"inet addr"/'{print $2}' | gawk '{print $1}')
2103    done
2104  fi
2105
2106  if [ -n "$IP" ] ; then
2107    sed -i "s#^LISTENER=.*#LISTENER=$IP#"      $CONFIG
2108
2109    einfo "Bootoption distcc found. Preparing setup for distcc daemon."
2110    eindent
2111     id distccd >/dev/null 2>&1 || \
2112     (
2113       einfo "Creating distcc user" ; \
2114       adduser --quiet --system --ingroup nogroup --home / --no-create-home distccd ; eend $?
2115     )
2116
2117     einfo "Starting distcc for network ${NET}, listening on ${IP}."
2118    /etc/init.d/distcc start 1>/dev/null ; eend $?
2119    eoutdent
2120  else
2121    eerror "No ip address for $INTERFACE found. distcc can not be used without it." ; eend 1
2122  fi
2123 fi
2124
2125 if checkbootparam 'gcc'; then
2126  GCC="$(getbootparam 'gcc' 2>>$DEBUG)"
2127  eindent
2128  einfo "Pointing /usr/bin/gcc to /usr/bin/gcc-${GCC}."
2129  eoutdent
2130  rm -f /usr/bin/gcc
2131  ln -s /usr/bin/gcc-${GCC} /usr/bin/gcc ; eend $?
2132 fi
2133
2134 if checkbootparam 'gpp'; then
2135  GPP="$(getbootparam 'gpp' 2>>$DEBUG)"
2136  eindent
2137   einfo "Pointing /usr/bin/g++ to /usr/bin/g++-${GPP}."
2138   if [ -x /usr/bin/g++-${GPP} ] ; then
2139      rm -f /usr/bin/g++
2140      ln -s /usr/bin/g++-${GPP} /usr/bin/g++ ; eend $?
2141   fi
2142   einfo "Pointing /usr/bin/cpp to /usr/bin/cpp-${GPP}."
2143   if [ -x /usr/bin/cpp-${GPP} ] ; then
2144      rm -f /usr/bin/cpp
2145      ln -s /usr/bin/cpp-${GPP} /usr/bin/cpp ; eend $?
2146   fi
2147  eoutdent
2148 fi
2149
2150 }
2151 # }}}
2152
2153 # {{{ load modules
2154 # Notice: use it only on live-cd system, if running from harddisk please
2155 # add modules to /etc/modules and activate /etc/init.d/module-init-tools
2156 # in /etc/runlevel.conf
2157 config_modules(){
2158 MODULES_FILE=/etc/grml/modules
2159 if checkbootparam 'nomodules' ; then
2160   ewarn "Skipping loading of modules defined in ${MODULES_FILE} as requested." ; eend 0
2161 elif [ -z "$INSTALLED" ]; then
2162  if [ -r $MODULES_FILE ] ; then
2163   einfo "Loading modules specified in ${MODULES_FILE}:"
2164   eindent
2165   grep '^[^#]' $MODULES_FILE | \
2166   while read module args; do
2167     [ "$module" ] || continue
2168       einfo "${module}"
2169       modprobe $module $args ; eend $?
2170   done
2171   eoutdent
2172  else
2173   ewarn "File $MODULES_FILE does not exist. Skipping loading of specific modules." ; eend 1
2174  fi
2175 fi
2176 }
2177 # }}}
2178
2179 # {{{ 915resolution
2180 config_915resolution(){
2181 if checkbootparam '915resolution' ; then
2182  OPTIONS="$(getbootparam '915resolution' 2>>$DEBUG)"
2183   if [ -x /usr/sbin/915resolution ]; then
2184     CMD=915resolution
2185     MODE=""
2186     XRESO=""
2187     YRESO=""
2188     if [ -n "$OPTIONS" ]; then
2189       # Extra options
2190       MODE="${OPTIONS%%,*}"
2191       R="${OPTIONS#*,}"
2192       if [ -n "$R" -a "$R" != "$OPTIONS" ]; then
2193         OPTIONS="$R"
2194         XRESO="${OPTIONS%%,*}"
2195         R="${OPTIONS#*,}"
2196         if [ -n "$R" -a "$R" != "$OPTIONS" ]; then
2197           OPTIONS="$R"
2198           YRESO="${OPTIONS%%,*}"
2199           R="${OPTIONS#*,}"
2200         fi
2201       fi
2202     fi
2203     einfo "Running 915resolution with options ${MODE} ${XRESO} ${YRESO}."
2204     [ -n "$MODE" ] && [ -n "$XRESO"  ] && [ -n "$YRESO" ]  && ( sh -c "$CMD $MODE $XRESO $YRESO" & )
2205     eend 0
2206   fi
2207 fi
2208 }
2209 # }}}
2210
2211 # {{{ SW-RAID
2212 config_swraid(){
2213   if [ -z "$INSTALLED" ] ; then
2214   # notice: checkbootparam "forensic" is just for users who don't know how to really use the bootoption
2215   if checkbootparam 'noraid'   || checkbootparam 'noswraid' -o \
2216      checkbootparam 'forensic' || checkbootparam 'raid=noautodetect' ; then
2217      ewarn "Skipping SW-RAID code as requested on boot commandline." ; eend 0
2218   else
2219     if ! [ -x /sbin/mdadm ] ; then
2220        eerror "mdadm not available, can not execute it." ; eend 1
2221     else
2222
2223        # if ! egrep -qv '^(MAILADDR.*|#.*|)$' /etc/mdadm/mdadm.conf 2>>$DEBUG ; then
2224        # find out whether we have a valid configuration file already
2225        if ! grep -q ARRAY /etc/mdadm/mdadm.conf 2>>$DEBUG ; then
2226           einfo "Creating /etc/mdadm/mdadm.conf for use with mdadm."
2227           [ -r /etc/mdadm/mdadm.conf ] && mv /etc/mdadm/mdadm.conf /etc/mdadm/mdadm.conf.old
2228           MDADM_MAILADDR__='root' /usr/share/mdadm/mkconf > /etc/mdadm/mdadm.conf ; eend $?
2229         else
2230           ewarn "/etc/mdadm/mdadm.conf looks like a configured mdadm setup, will not touch it." ; eend 0
2231        fi
2232
2233        if ! checkbootparam 'swraid' ; then
2234           eindent
2235           einfo "Just run 'Start mdadm-raid' to assemble md arrays or boot using 'swraid' as bootoption for autostart."
2236           eoutdent
2237        else
2238           einfo "Bootoption swraid found. Searching for software RAID arrays:"
2239           eindent
2240            IFSOLD=${IFS:-}
2241            IFS='
2242 '
2243            for line in $(mdadm --assemble --scan --auto=yes --symlink=no 2>&1) ; do
2244                case $line in
2245                  *'No arrays found'*)
2246                    ewarn "$line" ; eend 0
2247                    ;;
2248                  *)
2249                    einfo "$line" ; eend 0
2250                    ;;
2251                esac
2252            done
2253            IFS=$IFSOLD
2254          eoutdent
2255
2256          if [ -r /proc/mdstat ] ; then
2257             eindent
2258             MDSTAT=$(grep '^md[0-9]' /proc/mdstat)
2259             if [ -z "$MDSTAT" ] ; then
2260                ewarn "No active arrays found" ; eend 0
2261             else
2262                IFSOLD=${IFS:-}
2263                IFS='
2264 '
2265                for line in $(grep '^md[0-9]' /proc/mdstat) ; do
2266                    einfo "active arrays: $line" ; eend 0
2267                done
2268                IFS=$IFSOLD
2269             fi
2270             eoutdent
2271          fi # /proc/mdstat
2272        fi # bootoption swraid
2273
2274      fi # is /sbin/mdadm executable?
2275   fi # check for bootoptions
2276   fi # run only in live-cd mode
2277 }
2278 # }}}
2279
2280 # {{{ LVM (Logical Volumes)
2281 config_lvm(){
2282   if [ -z "$INSTALLED" ] ; then
2283   # notice: checkbootparam "forensic" is just for users who don't know how to really use the bootoption
2284   if checkbootparam 'nolvm' ; then
2285      ewarn "Skipping LVM code as requested on boot commandline." ; eend 0
2286   else
2287     # Debian etch provides /etc/init.d/lvm only, newer suites provide /etc/init.d/lvm2
2288     if ! [ -x /sbin/lvm -a -x /sbin/lvdisplay ] || ! [ -x /etc/init.d/lvm2 -o -x /etc/init.d/lvm ] ; then
2289        eerror "LVM not available, can not execute it." ; eend 1
2290     else
2291        if lvdisplay 2>&1 | grep -v 'No volume groups found' 1>/dev/null 2>&1 ; then
2292           einfo "You seem to have logical volumes (LVM) on your system."
2293           eindent
2294           einfo "Just run 'Start lvm2' to activate them or boot using 'lvm' as bootoption for autostart."
2295           eend 0
2296           if checkbootparam 'lvm' ; then
2297              einfo "Bootoption LVM found. Searching for logical volumes:"
2298              /etc/init.d/lvm2 start ; eend $?
2299           fi
2300           eoutdent
2301        fi
2302     fi # check for lvm binary
2303   fi # check for bootoption nolvm
2304   fi # run only in live-cd mode
2305 }
2306 # }}}
2307
2308 # {{{ debnet: setup network based on an existing one found on a partition
2309 config_debnet(){
2310 if checkbootparam 'debnet' ; then
2311  iszsh && setopt shwordsplit
2312  DEVICES="$(< /proc/partitions tail -n +3 | awk '{print "/dev/"$4}' | tr "\n" " ")"
2313  DEVICES="$DEVICES $(ls /dev/mapper/*)"
2314  FOUND_DEBNET=""
2315
2316  einfo "Bootoption 'debnet' found. Searching for Debian network configuration: "
2317  eindent
2318  if ! mount | grep '/mnt ' 1>/dev/null 2>&1 ; then
2319     for i in $DEVICES; do
2320      if mount -o ro -t auto "$i" /mnt >/dev/null 2>&1; then
2321          einfo "Scanning on $i"
2322        if [ -f /mnt/etc/network/interfaces ]; then
2323          einfo "/etc/network/interfaces found on ${i}" ; eend 0
2324          FOUND_DEBNET="$i"
2325          break
2326        fi
2327        umount /mnt
2328      fi
2329     done
2330
2331    if [ -n "$FOUND_DEBNET" ]; then
2332      einfo "Stopping network."
2333        pump -k 1>/dev/null 2>&1
2334        /etc/init.d/networking stop 1>/dev/null 2>&1 ; eend $?
2335      einfo "Copying Debian network configuration from $FOUND_DEBNET to running system."
2336        rm -rf /etc/network/run
2337        cp -a /mnt/etc/network /etc
2338        rm -rf /etc/network/run
2339        mkdir /etc/network/run
2340        umount /mnt ; eend $?
2341      einfo "Starting network."
2342        invoke-rc.d networking start ; eend $?
2343    else
2344      eerror "/etc/network/interfaces not found." ; eend 1
2345    fi
2346    eoutdent
2347  else
2348   eerror "Error: /mnt already mounted." ; eend 1
2349  fi
2350 fi
2351 }
2352 # }}}
2353
2354 # {{{ check for broken ipw3945 driver which causes problems (especially on hd install)
2355 config_ipw3945() {
2356   if grep -q ipw3945 /proc/modules ; then
2357      if ! iwconfig 2>/dev/null| grep -qe 'IEEE 802' -qe 'unassociated' ; then
2358         ewarn "Broken ipw3945 network interface found, reloading module."
2359         rmmod ipw3945
2360         modprobe ipw3945
2361         eend $?
2362      fi
2363   fi
2364 }
2365 # }}}
2366
2367 # {{{ disable console blanking
2368 config_blanking(){
2369 if checkbootparam 'noblank' ; then
2370   einfo "Bootoption noblank found. Disabling monitor blanking."
2371   setterm -blank 0 ; eend $?
2372 fi
2373 }
2374 # }}}
2375
2376 # {{{ tohd= bootoption
2377 config_tohd()
2378 {
2379   if checkbootparam 'tohd' ; then
2380      local TARGET="$(getbootparam 'tohd' 2>>$DEBUG)"
2381      if [ -z "$TARGET" ] ; then
2382         eerror "Error: tohd specified without any partition, can not continue." ; eend 1
2383         eerror "Please use something like tohd=/dev/sda9." ; eend 1
2384         return 1
2385      fi
2386
2387      if ! [ -b "$TARGET" ] ; then
2388         eerror "Error: $TARGET is not a valid block device, sorry." ; eend 1
2389         return 1
2390      fi
2391
2392      if grep -q $TARGET /proc/mounts ; then
2393         eerror "$TARGET already mounted, skipping execution of tohd therefore."
2394         eend 1
2395         return 1
2396      fi
2397
2398      local MOUNTDIR=$(mktemp -d)
2399
2400      if mount -o rw "$TARGET" "$MOUNTDIR" ; then
2401         einfo "Copyring live system to $TARGET - this might take a while"
2402         rsync -a --progress /live/image/live $MOUNTDIR
2403         sync
2404         umount "$MOUNTDIR"
2405         eend $?
2406         einfo "Booting with \"grml bootfrom=$TARGET\" should work now." ; eend 0
2407      else
2408         eerror "Error when trying to mount $TARGET, sorry."; eend 1
2409         return 1
2410      fi
2411
2412      rmdir "$MOUNTDIR"
2413   fi
2414 }
2415 # }}}
2416
2417 # {{{ grml2hd: automatic installation
2418 config_grml2hd(){
2419
2420 if checkbootparam "BOOT_IMAGE=grml2hd" ; then
2421
2422 if checkbootparam 'user' ; then
2423    NEWUSER=''
2424    NEWUSER="$(getbootparam 'user' 2>>$DEBUG)"
2425    sed -i "s/^NEWUSER=.*/NEWUSER=$NEWUSER/" /etc/grml2hd/config || export GRML2HD_FAIL=1
2426 fi
2427
2428 if checkbootparam 'filesystem' ; then
2429    FILESYSTEM=''
2430    FILESYSTEM="$(getbootparam 'filesystem' 2>>$DEBUG)"
2431    sed -i "s/^FILESYSTEM=.*/FILESYSTEM=$FILESYSTEM/" /etc/grml2hd/config || export GRML2HD_FAIL=1
2432 fi
2433
2434 if checkbootparam 'partition' ; then
2435    PARTITION=''
2436    PARTITION="$(getbootparam 'partition' 2>>$DEBUG)"
2437    # notice: the following checks whether the given partition is available, if not the skip
2438    # execution of grml2hd as it might result in data loss...
2439    if [ -r $PARTITION ] ; then
2440       sed -i "s#^PARTITION=.*#PARTITION=$PARTITION#" /etc/grml2hd/config || export GRML2HD_FAIL=1
2441    else
2442       ewarn "Partition $PARTITION does not exist. Skipping execution of grml2hd therefore." ; eend 1
2443    fi
2444 fi
2445
2446 if checkbootparam 'mbr' ; then
2447    BOOT_PARTITION=''
2448    BOOT_PARTITION="$(getbootparam 'mbr' 2>>$DEBUG)"
2449    sed -i "s#^BOOT_PARTITION=.*#BOOT_PARTITION=$BOOT_PARTITION#" /etc/grml2hd/config || export GRML2HD_FAIL=1
2450 fi
2451
2452 cat>|/usr/bin/grml2hd_noninteractive<<EOF
2453 #!/bin/sh
2454 GRML2HD_NONINTERACTIVE='yes' grml2hd
2455 EOF
2456
2457 chmod 755 /usr/bin/grml2hd_noninteractive
2458 einfo "Bootoption grml2hd found. Running automatic installation via grml2hd using /etc/grml2hd/config." && eend 0
2459 if [ -z "$GRML2HD_FAIL" ] ; then
2460    screen /usr/bin/grml2hd_noninteractive ; einfo "Invoking a shell, just exit to continue booting..." ; /bin/zsh
2461 else
2462    ewarn "There was an error adjusting /etc/grml2hd/config. Skipping execution of grml2hd for security reasons." ; eend 1
2463 fi
2464
2465 fi # if checkbootparam "BOOT_IMAGE=grml2hd ...
2466 }
2467 # }}}
2468
2469 # {{{ debootstrap: automatic installation
2470 config_debootstrap(){
2471
2472 if checkbootparam "BOOT_IMAGE=debian2hd" ; then
2473
2474 einfo "Bootoption debian2hd found. Setting up environment for automatic installation via grml-debootstrap." ; eend 0
2475
2476 if ! [ -x /usr/sbin/grml-debootstrap ] ; then
2477    eindent
2478    eerror "Bootoption debian2hd found, but grml-debootstrap is not available." ; eend 1
2479    eoutdent
2480    exit 1
2481 fi
2482
2483 if checkbootparam 'target' ; then
2484   TARGET=''
2485   TARGET="$(getbootparam 'target' 2>>$DEBUG)"
2486   # notice: the following checks whether the given partition is available, if not the skip
2487   # execution of grml-debootstrap as it might result in data loss...
2488   if ! [ -r "$TARGET" ] ; then
2489      eerror "Target $TARGET does not exist. Skipping execution of grml-debootstrap therefore." ; eend 1
2490   fi
2491 else
2492   eindent
2493   eerror "No bootoption named target found, can not continue execution of grml-debootstrap." ; eend 1
2494   eoutdent
2495   exit 1
2496 fi
2497
2498 if checkbootparam 'grub' ; then
2499   GRUB=''
2500   GRUB="$(getbootparam 'grub' 2>>$DEBUG)"
2501 fi
2502
2503 if checkbootparam 'groot' ; then
2504   GROOT=''
2505   GROOT="$(getbootparam 'groot' 2>>$DEBUG)"
2506 fi
2507
2508 if checkbootparam 'release' ; then
2509   RELEASE=''
2510   RELEASE="$(getbootparam 'release' 2>>$DEBUG)"
2511 fi
2512
2513 if checkbootparam 'mirror' ; then
2514   MIRROR=''
2515   MIRROR="$(getbootparam 'mirror' 2>>$DEBUG)"
2516 fi
2517
2518 if checkbootparam 'boot_append' ; then
2519   BOOT_APPEND=''
2520   BOOT_APPEND="$(getbootparam 'boot_append' 2>>$DEBUG)"
2521 fi
2522
2523 if checkbootparam 'password' ; then
2524   PASSWORD=''
2525   PASSWORD="$(getbootparam 'password' 2>>$DEBUG)"
2526 fi
2527
2528 # now check which options are available
2529 if [ -n "TARGET" ] ; then
2530    TARGETCMD="--target $TARGET"
2531 else
2532    TARGETCMD=''
2533    eindent
2534    eerror "Target not set via bootoption. Skipping execution of grml-debootstrap therefore."; eend 1
2535    eoutdent
2536    exit 1
2537 fi
2538 [ -n "$GRUB" ]     && GRUBCMD="--grub $GRUB"               || GRUBCMD=''
2539 [ -n "$GROOT" ]    && GROOTCMD="--groot $GROOT"            || GROOTCMD=''
2540 [ -n "$RELEASE" ]  && RELEASECMD="--release $RELEASE"      || RELEASECMD=''
2541 [ -n "$MIRROR" ]   && MIRRORCMD="--mirror $MIRROR"         || MIRRORCMD=''
2542 [ -n "$PASSWORD" ] && PASSWORDCMD="--password $PASSWORD"   || PASSWORDCMD=''
2543 [ -n "$BOOT_APPEND" ] && BOOT_APPEND="--boot_append $BOOT_APPEND" || BOOT_APPEND=''
2544
2545 # and finally write script and execute it
2546 cat>|/usr/bin/grml-debootstrap_noninteractive<<EOF
2547 #!/bin/sh
2548 AUTOINSTALL='yes' grml-debootstrap $TARGETCMD $GRUBCMD $GROOTCMD $RELEASECMD $MIRRORCMD $PASSWORDCMD $BOOT_APPEND
2549 EOF
2550
2551 chmod 750  /usr/bin/grml-debootstrap_noninteractive
2552
2553 screen /usr/bin/grml-debootstrap_noninteractive
2554 einfo "Invoking a shell, just exit to continue booting..."
2555 /bin/zsh
2556
2557 fi # checkbootparam "BOOT_IMAGE=debian2hd
2558 }
2559 # }}}
2560
2561 # {{{ Support customization
2562 config_distri(){
2563 if checkbootparam 'distri'; then
2564   DISTRI="$(getbootparam 'distri' 2>>$DEBUG)"
2565   if [ -r "${LIVECD_PATH}"/desktop/"$DISTRI".jpg ] ; then
2566      [ -n "$BOOTDEBUG" ] && einfo "Debug: bootoption distri found and file ${LIVECD_PATH}/desktop/${DISTRI} present" && eend 0
2567      # make sure the desktop.jpg file is not a symlink, so copying does not file then
2568      [ -L /usr/share/grml/desktop.jpg ] && rm /usr/share/grml/desktop.jpg
2569      cp "${LIVECD_PATH}"/desktop/"$DISTRI".jpg /usr/share/grml/desktop.jpg
2570   fi
2571 fi
2572 }
2573 # }}}
2574
2575 ### {{{ backwards compatible stuff
2576 config_environment(){
2577   ewarn "config_environment is deprecated. Please set CONFIG_ENVIRONMENT in /etc/grml/autoconfig to 'no'." ; eend 0
2578 }
2579 config_keyboard(){
2580   ewarn "config_keyboard is deprecated. Please set CONFIG_KEYBOARD in /etc/grml/autoconfig to 'no'." ; eend 0
2581 }
2582 # }}}
2583
2584 ## END OF FILE #################################################################
2585 # vim:foldmethod=marker expandtab ai ft=zsh shiftwidth=3