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