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