48b2aa9d0d68db5e9105e588974cec99d2484c28
[grml-live.git] / rewrite / linuxrc
1 #!/static/sh
2 # Filename: /linuxrc
3 # Purpose:  minirt for kernel 2.6 running on grml live-cd
4 # Authors:  (c) Klaus Knopper <knoppix@knopper.net>, (c) Michael Prokop <mika@grml.org>
5 # Latest change: Tue Mar 27 23:05:51 CEST 2007
6 #######################################################################################
7
8 # hardcoded configurable options
9 # Default maximum size of dynamic ramdisk in kilobytes
10 RAMSIZE=1000000
11 # End of options
12
13 # Don't allow interrupt signals
14 trap "" 1 2 3 15
15
16 # Misc functions
17 INSMOD="/static/insmod"
18 # [ -x /modules/insmod ] && INSMOD="/modules/insmod"
19
20 RMMOD="/static/rmmod"
21 # [ -x /modules/rmmod ] && RMMOD="/modules/rmmod"
22
23 # Builin filesystems
24 # BUILTIN_FS="iso9660 ext2 vfat"
25 BUILTIN_FS="iso9660 ext2 ext3 reiserfs vfat xfs jfs reiser4"
26
27 mountit(){
28 # Usage: mountit src dst "options"
29 for fs in $BUILTIN_FS; do
30  if test -b $1; then
31   options="$3"
32   case "$fs" in vfat)
33    # We REALLY need this for Knoppix on DOS-filesystems
34    shortname="shortname=winnt"
35    [ -n "$options" ] && options="$options,$shortname" || options="-o $shortname"
36   ;;
37   esac
38   mount -t $fs $options $1 $2 >/dev/null 2>&1 && return 0
39  fi
40 done
41 return 1
42 }
43
44 FOUND_SCSI=""
45 FOUND_GRML=""
46 INTERACTIVE=""
47
48 # Clean input/output
49 exec >/dev/console </dev/console 2>&1
50
51 # Mount /proc and /dev/pts
52 mount -t proc /proc /proc
53
54 # Disable kernel messages while probing modules in autodetect mode
55 echo "0" > /proc/sys/kernel/printk
56
57 mount -t devpts /dev/pts /dev/pts
58 # Kernel 2.6
59 mount -t sysfs /sys /sys >/dev/null 2>&1
60
61 # Read boot command line with builtin cat command (shell read function fails in Kernel 2.4.19-rc1)
62 CMDLINE="$(cat /proc/cmdline)"
63
64 # Simple shell grep
65 stringinfile(){
66   case "$(cat $2)" in *$1*) return 0;; esac
67   return 1
68 }
69
70 # same for strings
71 stringinstring(){
72   case "$2" in *$1*) return 0;; esac
73   return 1
74 }
75
76 # Reread boot command line; echo last parameter's argument or return false.
77 getbootparam(){
78   stringinstring " $1=" "$CMDLINE" || return 1
79   result="${CMDLINE##*$1=}"
80   result="${result%%[   ]*}"
81   echo "$result"
82   return 0
83 }
84
85 # Check boot commandline for specified option
86 checkbootparam(){
87   stringinstring " $1" "$CMDLINE"
88   return "$?"
89 }
90
91 if checkbootparam "nocolor" ; then
92   echo "Disabling colors in bootsequence as requested on commandline."
93   # Reset fb color mode
94   RESET="\e]R"
95   # ANSI COLORS
96   # Erase to end of line
97   CRE="\r\e[K"
98   # Clear and reset Screen
99   CLEAR="\ec"
100 else
101   # Reset fb color mode
102   RESET="\e]R"
103   # ANSI COLORS
104   # Erase to end of line
105   CRE="\r\e[K"
106   # Clear and reset Screen
107   CLEAR="\ec"
108   # Normal color
109   NORMAL="\e[0;39m"
110   # RED: Failure or error message
111   RED="\e[1;31m"
112   # GREEN: Success message
113   GREEN="\e[1;32m"
114   # YELLOW: Descriptions
115   YELLOW="\e[1;33m"
116   # BLUE: System mesages
117   BLUE="\e[1;34m"
118   # MAGENTA: Found devices or drivers
119   MAGENTA="\e[1;35m"
120   # CYAN: Questions
121   CYAN="\e[1;36m"
122   # BOLD WHITE: Hint
123   WHITE="\e[1;37m"
124 fi
125
126 # don't output anything if running with bootsplash feature
127 if checkbootparam "splash" ; then
128   echo ""
129   echo "${WHITE}Welcome to"
130   echo ""
131   echo "${RED}   ____ ____  __  __ _     "
132   echo "${RED}  / ___|  _ \|  \/  | |    "
133   echo "${RED} | |  _| |_) | |\/| | |    "
134   echo "${RED} | |_| |  _ <| |  | | |___ "
135   echo "${RED}  \____|_| \_\_|  |_|_____|"
136   echo ""
137   echo "${WHITE}grml.org - Linux for users of texttools and sysadmins."
138   echo "${NORMAL}"
139   exec >/dev/null </dev/null 2>&1
140 fi
141
142 # helper functions {{{
143 #log_success_msg () {
144 #  echo " * $@"
145 #}
146
147 log_failure_msg () {
148   echo " ${RED}*${NORMAL} $@"
149 }
150
151 #log_warning_msg () {
152 # echo " ${BLUE}*${NORMAL} $@"
153 #}
154
155 # int log_begin_message (char *message)
156 log_begin_msg () {
157   echo -n " ${GREEN}*${NORMAL} $@"
158 }
159
160 log_warn_msg () {
161   echo -n " ${YELLOW}*${NORMAL} $@"
162 }
163
164 # int log_end_message (int exitstatus)
165 SUCCESS=" ${BLUE}[ ${GREEN}ok ${BLUE}]${NORMAL}"
166 FAILED=" ${NORMAL}[${RED}fail${NORMAL}]"
167 # }}}
168
169 # Clear screen with colormode reset
170 # echo "$CLEAR$RESET"
171 # echo "$CLEAR"
172 # Just go to the top of the screen
173 # echo -n "\e[H\e[J"
174 echo ""
175 echo "${WHITE}Welcome to"
176 echo ""
177 echo "${RED}   ____ ____  __  __ _     "
178 echo "${RED}  / ___|  _ \|  \/  | |    "
179 echo "${RED} | |  _| |_) | |\/| | |    "
180 echo "${RED} | |_| |  _ <| |  | | |___ "
181 echo "${RED}  \____|_| \_\_|  |_|_____|"
182 echo ""
183 echo "${WHITE}grml.org - Linux for users of texttools and sysadmins."
184 echo "${NORMAL}"
185
186 # We need the builtin commands and /static only starting at this point
187 PATH=/static
188 export PATH
189
190 umask 022
191
192 case "$CMDLINE" in *debuglinuxrc*) set -x; echo "linuxrc debugging activated"; DEBUG="yes"; ;; esac
193 case "$CMDLINE" in *BOOT_IMAGE=expert\ *) INTERACTIVE="yes"; :>/interactive; ;; esac
194 case "$CMDLINE" in *BOOT_IMAGE=vmware\ *) VMWARE="yes"; ;; esac
195 case "$CMDLINE" in *modules-disk*) INTERACTIVE="yes"; ;; esac
196 case "$CMDLINE" in *BOOT_IMAGE=debug\ *|*\ debug\ *) DEBUG="yes"; ;; esac
197 case "$CMDLINE" in *secure*) SECURE=",nosuid"; ;; esac
198 # Does the user want to skip scsi detection?
199 NOSCSI=""
200 NOUSB=""
201 NOFIREWIRE=""
202 case "$CMDLINE" in *noscsi*|*nobootscsi*) NOSCSI="yes"; ;; esac
203 case "$CMDLINE" in *nousb\ *|*nobootusb*) NOUSB="yes"; ;; esac
204 case "$CMDLINE" in *nofirewire*|*nobootfirewire*) NOFIREWIRE="yes"; ;; esac
205 NOCD=""
206 case "$CMDLINE" in *fromhd*) NOCD="yes"; ;; esac
207 case "$CMDLINE" in *fromdvd*) FROMDVD="yes"; ;; esac
208 case "$CMDLINE" in *idecd*|*atapicd*) IDECD="yes"; ;; esac
209 case "$CMDLINE" in *noideraid*) NOIDERAID="yes"; ;; esac
210 USB2="ehci-hcd.ko"
211 case "$CMDLINE" in *nousb2*) USB2="" NOUSB2="yes"; ;; esac
212 case "$CMDLINE" in *\ usb*) USB="yes"; ;; esac
213
214 GRML_DIR="GRML"
215 GRML_NAME="GRML"
216 case "$CMDLINE" in *grml_dir=*) GRML_DIR="$grml_dir"; ;; esac
217 case "$CMDLINE" in *grml_name=*) GRML_NAME="$grml_name"; ;; esac
218
219 # NFS
220 for i in $cmdline; do case "$i" in nfsdir=*|NFSDIR=*) eval $i;; esac; done
221 [ -n "$nfsdir" ] && NFS="$nfsdir"
222
223 if [ -n "$DEBUG" ]; then
224    log_begin_msg "Bootoption debug detected. Printing kernel command line:"
225    echo ""
226    cat /proc/cmdline
227 fi
228
229 # Run a shell if in debug mode
230 # echo "${BLUE}Dropping you to a busybox shell for debugging.${NORMAL}"
231 stage=1
232 rundebugshell(){
233  if [ -n "$DEBUG" ]; then
234   log_begin_msg "Starting intermediate shell stage $stage as requested by \"debug\" option."
235   echo ""
236   echo "   ${GREEN}-${NORMAL} Just exit the shell to continue boot process...${NORMAL}"
237   if [ -x /static/sh ]; then
238     /static/sh
239   else
240     /bin/bash
241   fi
242  fi
243 }
244 rundebugshell
245
246 # Mount module disk
247 mountmodules(){
248 TYPE="$1"; shift
249 echo -n "${CRE}Please insert ${TYPE} modules disk and hit Return."
250 read a
251 echo -n "${CRE}Mounting ${TYPE} modules disk... "
252 # We always mount over /modules/scsi (because it's there ;-)
253 if mountit /dev/fd0 /modules/scsi "-o ro"; then
254 echo "${GREEN}OK.${NORMAL}"
255 return 0
256 fi
257 echo "${RED}NOT FOUND.${NORMAL}"
258 return 1
259 }
260
261 # Unmount module disk
262 umountmodules(){
263 TYPE="$1"; shift
264 echo -n "${CRE}Unmounting ${TYPE} modules disk... "
265 umount /modules/scsi 2>/dev/null
266 echo "${GREEN}DONE.${NORMAL}"
267 }
268
269 # Ask user for modules
270 askmodules(){
271  TYPE="$1"; shift
272  echo "${TYPE} modules available:${WHITE}"
273  c=""
274  for m in "$@"; do
275   if test -f "/modules/scsi/$m"; then
276    test -z "$c"  && { echo -n " $m"; c="1"; } || { echo "               $m"; c=""; }
277   fi
278  done
279  [ -n "$c" ] && echo ""
280  echo "Load ${TYPE} Modules?"
281  echo "[Enter full filename(s) (space-separated), Return for autoprobe, n for none] "
282  echo -n "insmod module(s)> "
283  read MODULES
284  case "$MODULES" in n|N) MODULES=""; ;; y|"")  MODULES="$*"; ;; esac
285 }
286
287 # Try to load the given modules (full path or current directory)
288 loadmodules(){
289  TYPE="$1"; shift
290  test -n "$INTERACTIVE" && echo "6" > /proc/sys/kernel/printk
291  echo ""
292  for i in "$@"; do
293   echo -n "   Probing ${TYPE}... ${WHITE}$i${NORMAL}: "
294    if test -f /modules/scsi/$i.ko && $INSMOD /modules/scsi/$i.ko >/dev/null 2>&1 && echo "  $SUCCESS" || echo " failed " ; then
295      case "$TYPE" in scsi|SCSI) FOUND_SCSI="yes"; ;; esac
296    fi
297  done
298  test -n "$INTERACTIVE" && echo "0" > /proc/sys/kernel/printk
299  echo -n "${CRE}"
300 }
301
302 # Check for SCSI, use modules on bootfloppy first
303 # Trying to do kind of /proc/pci hardware detection
304 if checkbootparam oldscsi ; then
305   PROCPCI="`cat /proc/pci 2>/dev/null`"
306   case "$PROCPCI" in *[Aa][Ii][Cc]-*|*[Aa][Hh][Aa]-*) SCSI_PROBE="$SCSI_PROBE aic7xxx" ;; esac
307   case "$PROCPCI" in *[Bb][Uu][Ss][Ll][Oo][Gg][Ii][Cc]*) SCSI_PROBE="$SCSI_PROBE BusLogic" ;; esac
308   case "$PROCPCI" in *[Tt][Rr][Mm]-[Ss]1040*|*[Dd][Cc]395*|*[Dd][Cc]315*) SCSI_PROBE="$SCSI_PROBE dc395x" ;; esac
309   case "$PROCPCI" in *53[Cc]8*) SCSI_PROBE="$SCSI_PROBE sym53c8xx" ;; esac
310   case "$PROCPCI" in *53[Cc]9*) SCSI_PROBE="$SCSI_PROBE NCR53C9x" ;; esac
311   case "$PROCPCI" in *53[Cc]406*) SCSI_PROBE="$SCSI_PROBE NCR53c406a" ;; esac
312   case "$PROCPCI" in *[Ii][Nn][Ii][Tt][Ii][Oo]\ *|*[Ii][Nn][Ii]-[Aa]100[Uu]2[Ww]*) SCSI_PROBE="$SCSI_PROBE initio" ;; esac
313   case "$PROCPCI" in *[Mm][Pp][Tt]*[Ss][Cc][Ss][Ii]*) SCSI_PROBE="$SCSI_PROBE mptbase mptscsih" ;; esac
314   case "$PROCPCI" in *[Aa][Dd][Vv][Aa][Nn][Cc][Ee][Dd]\ [Ss][Yy][Ss]*) SCSI_PROBE="$SCSI_PROBE advansys" ;; esac
315   case "$PROCPCI" in *[Aa][Tt][Pp]8*|*[Aa][Ee][Cc]6*) SCSI_PROBE="$SCSI_PROBE atp870u" ;; esac
316   case "$PROCPCI" in *[Dd][Tt][Cc]*) SCSI_PROBE="$SCSI_PROBE dtc" ;; esac
317   case "$PROCPCI" in *[Ee][Aa][Tt][Aa]*) SCSI_PROBE="$SCSI_PROBE eata" ;; esac
318   case "$PROCPCI" in *[Ff]*[Dd][Oo][Mm][Aa][Ii][Nn]*) SCSI_PROBE="$SCSI_PROBE fdomain" ;; esac
319   case "$PROCPCI" in *[Gg][Dd][Tt]\ *) SCSI_PROBE="$SCSI_PROBE gdth" ;; esac
320   case "$PROCPCI" in *[Mm][Ee][Gg][Aa][Rr][Aa][Ii][Dd]*) SCSI_PROBE="$SCSI_PROBE megaraid_mm megaraid_mbox" ;; esac
321   case "$PROCPCI" in *[Qq][Ll][Oo][Gg][Ii][Cc]*) SCSI_PROBE="$SCSI_PROBE qlogicfas408 qlogicfas qlogicfc" ;; esac
322   case "$PROCPCI" in *53[Cc]974*) SCSI_PROBE="$SCSI_PROBE tmscsim" ;; esac
323   case "$PROCPCI" in *[Uu][Ll][Tt][Rr][Aa][Ss][Tt][Oo][Rr]*) SCSI_PROBE="$SCSI_PROBE ultrastor" ;; esac
324   case "$PROCPCI" in *3[Ww][Aa][Rr][Ee]*) SCSI_PROBE="$SCSI_PROBE 3w-xxxx" ;; esac
325 fi
326
327 # New sysfs based SCSI detection (thanks, Jörg Schirottke)
328 sysfsscsi(){
329 SYS=$(for x in $(find /sys/devices/ -name modalias); do grep pci: $x; done|cut -f2 -d:)
330 while read id driver; do
331  for sysid in $SYS; do
332   case $sysid in $id)
333    if [ -z "$SCSI_PROBE" ]; then
334     SCSI_PROBE="$driver"
335    else
336     SCSI_PROBE="$SCSI_PROBE $driver"
337    fi
338    ;;
339   esac
340  done
341 done <<EOF
342 $(cat /modules/scsi/scsi-modules.txt)
343 EOF
344 }
345
346 if test -n "$INTERACTIVE"; then
347 # Let the user select interactively
348   askmodules SCSI $(cd /modules/scsi; echo *.ko)
349 else
350 # these are the autoprobe-safe modules
351   sysfsscsi
352   MODULES="$SCSI_PROBE"
353 fi
354
355 if test -z "$NOSCSI" ; then
356  log_begin_msg "Scanning for SCSI devices."
357  $INSMOD /modules/scsi/firmware_class.ko 1>/dev/null
358  test -n "$MODULES" && loadmodules SCSI $MODULES &&  echo -n "" || echo "           ${BLUE}[${NORMAL} none found ${BLUE}]${NORMAL} (try bootoption scsi=probe)"
359 else
360   log_warn_msg "Not scanning for SCSI devices as requested on commandline." && echo " $SUCCESS"
361 fi
362
363 if checkbootparam scsi ; then
364   MODULE="$(getbootparam 'scsi' 2>/dev/null)"
365   if test "$MODULE" = "probe" ; then
366     log_begin_msg "Bootoption scsi=probe found. Trying to autoprobe SCSI modules:"
367     echo ""
368     echo -n "   Trying to load scsi_debug: " ; $INSMOD /modules/scsi/scsi_debug.ko 1>/dev/null && echo "           $SUCCESS" || echo " [ failed ]"
369     for module in /modules/scsi/*.ko ; do
370       echo -n "   Probing ${WHITE}${module}${NORMAL}..."
371       $INSMOD ${module} >/dev/null 2>&1 && echo " $SUCCESS" || echo " [ failed ]"
372     done
373   elif test "$MODULE" = "ask" ; then
374     askmodules SCSI $(cd /modules/scsi; echo *.ko)
375     test -z "$NOSCSI" && test -n "$MODULES" && loadmodules SCSI $MODULES
376   else
377     [ -n "$MODULE" ] || echo "   ${RED}Neither a specific module nor option probe nor option ask for SCSI module given. Skipping.${NORMAL}"
378     [ -n "$MODULE" ] && echo -n "   Trying to load module ${WHITE}${MODULE}${NORMAL}:" ; \
379       $INSMOD "/modules/scsi/${MODULE}.ko" 1>/dev/null && echo "  $SUCCESS" || echo " [ failed ]"
380   fi
381 fi
382 # End of SCSI check
383
384 if test -n "$VMWARE" ; then
385   log_begin_msg "Bootoption VMware detected. Trying to load SCSI modules:"
386   echo ""
387   for module in mptbase mptscsih mptspi BusLogic ; do
388     echo -n "   Trying to load ${WHITE}${module}${NORMAL}: "
389     $INSMOD /modules/scsi/${module}.ko >/dev/null 2>&1 && echo " $SUCCESS" || echo " [ failed ]"
390   done
391 fi
392
393 # Check for USB, use modules on bootfloppy first
394 if test -z "$NOUSB"; then
395   log_begin_msg "Checking for USB."
396   if test -f /modules/div/usbcore.ko; then
397     $INSMOD /modules/div/usbcore.ko >/dev/null 2>&1
398     FOUNDUSB=""
399     for i in $USB2 uhci-hcd.ko ohci-hcd.ko usbhid.ko ; do
400       test -f /modules/div/$i && $INSMOD /modules/div/$i >/dev/null 2>&1 && FOUNDUSB="yes"
401     done
402     if test -n "$FOUNDUSB"; then
403       test -f /modules/div/usb-storage.ko && $INSMOD /modules/div/usb-storage.ko >/dev/null 2>&1
404       echo "                   $SUCCESS"
405     else
406       echo "                   ${BLUE}[${NORMAL} not found ${BLUE}]${NORMAL}"
407       true
408     fi
409     if [ -n "$NOUSB2" ] ; then
410       echo "   Not loading usb2 module ehci-hcd as requested on commandline."
411       echo "   Notice: to skip loading of USB in initrd at all use bootoption nousb"
412     fi
413   fi
414 else
415   log_warn_msg "Not scanning for USB devices as requested on commandline." && echo " $SUCCESS"
416   echo "   Notice that bootoption nousb affects initrd only, it does *not*"
417   echo "   avoid loading of usb modules in userspace afterwards"
418   echo "   Boot using something like 'nousb blacklist=uhci-hcd' to avoid loading of usb modules at all"
419 fi
420 # End of USB check
421
422 # Check for Firewire, use modules on bootfloppy first
423 if test -z "$NOFIREWIRE" ; then
424   log_begin_msg "Checking for Firewire."
425   if test -f /modules/div/ieee1394.ko ; then
426     $INSMOD /modules/div/ieee1394.ko > /dev/null 2>&1
427     FOUNDFIREWIRE=""
428     test -f /modules/div/ohci1394.ko && $INSMOD /modules/div/ohci1394.ko > /dev/null 2>&1 && FOUNDFIREWIRE="yes"
429     if test -n "$FOUNDFIREWIRE" ; then
430       test -f /modules/div/sbp2.ko && $INSMOD /modules/div/sbp2.ko > /dev/null 2>&1
431       echo "              $SUCCESS"
432     else
433       echo "               ${BLUE}[${NORMAL} not found ${BLUE}]${NORMAL}"
434       true
435     fi
436   fi
437 else
438   log_warn_msg "Not scanning for firewire devices as requested on commandline." && echo " $SUCCESS"
439   echo "   Notice that bootoption nofirewire affects initrd only, it does *not*"
440   echo "   avoid loading of firewire modules in userspace afterwards"
441   echo "   Boot with something like 'nofirewire blacklist=ohci1394' to avoid loading of firewire modules at all"
442 fi
443 # End of FIREWIRE check
444
445 # Unfortunately, hotpluggable devices tend to need some time in order to register
446 if test -n "$FOUNDUSB" -o -n "$FOUNDFIREWIRE"; then
447   log_begin_msg "Scanning for USB/Firewire devices."
448   sleep 4
449   if test -n "$USB"; then
450     sleep 10
451   fi
452  echo "  $SUCCESS"
453 fi
454
455 if checkbootparam scandelay ; then
456   DELAY="$(getbootparam 'scandelay' 2>/dev/null)"
457   [ -z $DELAY ] && DELAY='10'
458   log_begin_msg "Delaying bootsequence as requested for ${WHITE}${DELAY}${NORMAL} seconds."
459   sleep $DELAY && echo "  $SUCCESS"
460 fi
461
462 # boot via pcmcia
463 if checkbootparam bootpcmcia ; then
464   log_begin_msg "Bootoption bootpcmcia found. Trying to load ${WHITE}PCMCIA${NORMAL} modules..."
465   if $INSMOD /modules/div/pcmcia_core.ko 1>/dev/null ; then
466      $INSMOD /modules/div/firmware_class.ko 1>/dev/null && \
467      $INSMOD /modules/div/pcmcia.ko         1>/dev/null && \
468      $INSMOD /modules/div/rsrc_nonstatic.ko 1>/dev/null && \
469      $INSMOD /modules/div/yenta_socket.ko   1>/dev/null && echo " $SUCCESS"
470   else
471     echo " [ failed ]"
472   fi
473 fi
474
475 # Check for misc modules in expert mode
476 if test -n "$INTERACTIVE" ; then
477   another=""
478   answer=""
479   while test "$answer" != "n" -a "$answer" != "N" ; do
480     echo -n "${CYAN}Do you want to load additional modules from$another floppy disk? [${WHITE}Y${CYAN}/n] ${NORMAL}"
481     another=" another"
482     read answer
483     case "$answer" in n*|N*) break; ;; esac
484     if mountmodules new ; then
485        askmodules new $(cd /modules/scsi; echo *.ko)
486        test -n "$MODULES" && loadmodules new $MODULES
487        umountmodules current
488     fi
489   done
490 fi
491 # All interactively requested modules should be loaded now.
492
493 # Check for ide-scsi supported CD-Roms et al.
494 test -f /proc/scsi/scsi && FOUND_SCSI="yes"
495
496 # Disable kernel messages again
497 echo "0" > /proc/sys/kernel/printk
498
499 # We now enable DMA right here, for faster reading/writing from/to IDE devices
500 # in FROMHD or TORAM mode
501 case "$CMDLINE" in *\ nodma*) ;; *)
502  for d in $(cd /proc/ide 2>/dev/null && echo hd[a-z]); do
503   if test -d /proc/ide/$d; then
504     MODEL="$(cat /proc/ide/$d/model 2>/dev/null)"
505     test -z "$MODEL" && MODEL="[GENERIC IDE DEVICE]"
506     log_begin_msg "Enabling DMA acceleration for: ${MAGENTA}$d   ${YELLOW}[${MODEL}]${NORMAL}"
507     echo "using_dma:1" >/proc/ide/$d/settings && echo ""
508   fi
509  done
510  ;;
511 esac
512
513 stage=2
514 rundebugshell
515 if [ -n "$NFS" ]; then
516   tmp_="$(getbootparam nfsdir)"
517   log_begin_msg "Bootoption NFS found." ; echo "$SUCCESS"
518
519   /static/cdir
520
521   log_begin_msg "Trying to load network driver(s)." ; echo
522   modLoad()
523   {
524     for mod in $@ ; do
525       if [ -n "$DEBUG" ] ; then
526          echo "Debug: trying to load $mod:"
527          modprobe -v $mod
528       else
529          modprobe $mod 2>/dev/null
530       fi
531     done
532   }
533   # modules.alias and modules.dep are in place so USE IT :)!
534   modLoad "$(for x in $(find /sys/devices/ -name modalias); do grep pci: $x; done |/static/xargs)"
535
536   # loading additional modules
537   modLoad sunrpc lockd af_packet nfs
538
539   dhcp_iface_=$(getbootparam dhcp_iface)
540   if [ -z "$dhcp_iface_" ]; then
541         dhcp_iface_=`ifconfig -a | grep '^eth' | sed 's/ .*//'`
542   fi
543
544   # make sure we have a udhcpc executable, if it's not present
545   # assume that busybox provides one
546   if ! [ -x /static/udhcpc ] ; then
547      ln -s /static/busybox /static/udhcpc
548   fi
549
550   for INTERFACE in $dhcp_iface_ ; do
551       log_begin_msg "Requesting network configuration using udhcp for ${INTERFACE}:" ; echo
552       /static/timeout 10 /static/udhcpc --interface="${INTERFACE}" --foreground --quit --script=/static/udhcp-config.sh
553       # echo "press <enter> to start a system shell and configure your system"
554       # sh
555   done
556
557   # recreate dir layout + remove extra modules
558   /static/rdir
559
560   log_begin_msg "Looking for GRML in: ${MAGENTA}$NFS${NORMAL}" ; echo "$SUCCESS"
561   if mount -t nfs "$NFS" -o "async,ro,nolock" /cdrom #>/dev/null 2>&1
562     then
563     if test -f /cdrom/$GRML_DIR/$GRML_NAME
564       then
565       log_begin_msg "Accessing grml CDROM at ${MAGENTA}$NFS${NORMAL}" ; echo "$SUCCESS"
566       FOUND_GRML="$NFS"
567       break
568     fi
569   fi
570 fi
571
572 # Now that the right SCSI driver is (hopefully) loaded, try to find CD-ROM
573 if test -z $NFS ; then
574   DEVICES="/dev/hd?"
575   test -n "$FOUND_SCSI" -a -z "$NOCD" && DEVICES="/dev/scd? /dev/scd?? $DEVICES"
576   # New: Also try parallel port CD-Roms [for Mike].
577   DEVICES="$DEVICES /dev/pcd?"
578   # New: also check HD partitions for a GRML/GRML image
579   # notice: use /dev/sd? for usb-sticks without partition(s)
580   test -n "$FOUND_SCSI" -a -z "$NOSCSI" && DEVICES="$DEVICES /dev/sd?[1-9] /dev/sd?[1-9][0-9] /dev/sd?"
581   DEVICES="$DEVICES /dev/hd?[1-9] /dev/hd?[1-9][0-9]"
582   case "$CMDLINE" in *fromhd=/dev/*) DEVICES="$fromhd"; ;; esac
583   for i in $DEVICES
584   do
585   log_begin_msg "${CRE} ${GREEN}*${NORMAL} Looking for CD-ROM in:    ${MAGENTA}$i${NORMAL}"
586   if mountit $i /cdrom "-o ro" >/dev/null 2>&1
587     then
588     echo "  $SUCCESS"
589     if test -f /cdrom/$GRML_DIR/$GRML_NAME
590       then
591       log_begin_msg "Accessing grml CD-ROM at: ${MAGENTA}$i${NORMAL}" ; echo "  $SUCCESS"
592       FOUND_GRML="$i"
593       break
594     fi
595     umount /cdrom
596   fi
597   done
598 fi
599
600 # Harddisk-installed script part version has been removed
601 # (GRML can be booted directly from HD now).
602 mount_grml()
603 {
604   if test -n "$FOUND_GRML" -a -f $1/$GRML_DIR/$GRML_NAME; then
605     # echo "6" > /proc/sys/kernel/printk
606     mount -t squashfs $1/$GRML_DIR/$GRML_NAME /GRML -o loop,ro$SECURE || FOUND_GRML=""
607   fi
608 }
609
610 remount_grml()
611 {
612   if test -f $TARGET/$GRML_DIR/$GRML_NAME; then
613     umount /GRML # unmount it
614     umount $SOURCE  # unmount CD
615     [ -n "$SOURCE2" ] && umount $SOURCE2  # umount possible loop-device
616     mount_grml $TARGET
617   else
618     log_failure_msg "Warning: Changing to $TARGET failed."
619     return 1
620   fi
621
622   return 0
623 }
624
625 boot_from()
626 {
627   # preparations
628   /bin/mkdir $TARGET
629
630   SOURCE_DEV=$(echo $CMDLINE | /usr/bin/tr ' ' '\n' | /bin/sed -n '/bootfrom=/s/.*=//p' | /usr/bin/tail -1)
631
632   LOOP_DEV=$(echo $SOURCE_DEV | /usr/bin/gawk -F/ '{ print $1 "/" $2 "/" $3 }')
633   ISO_PATH=$(echo $SOURCE_DEV | /bin/sed "s|$LOOP_DEV||g" )
634   case "$ISO_PATH" in /*.[iI][sS][oO]) ;; *) ISO_PATH="" ;; esac
635   LOOP_SOURCE=""
636
637   # load filesystems
638   /GRML/sbin/modprobe fuse
639   /GRML/sbin/modprobe ntfs
640   $INSMOD /modules/div/ntfs.ko 1>/dev/null
641
642   if [ -n "$ISO_PATH" ]; then
643      LOOP_SOURCE="$TARGET.loop"
644      LOOP_SOURCE2="$LOOP_SOURCE"
645      TARGET_DEV="$LOOP_SOURCE$ISO_PATH"
646      /bin/mkdir $LOOP_SOURCE
647      /bin/mount -o ro $LOOP_DEV $LOOP_SOURCE || LOOP_SOURCE=""
648      /bin/mount -n -o loop $LOOP_SOURCE2$ISO_PATH $TARGET
649   else
650      TARGET_DEV="$SOURCE_DEV"
651     /bin/mount -n -o ro $SOURCE_DEV $TARGET
652   fi
653   if [ $? -ne 0 ]; then
654      [ -n "$LOOP_SOURCE" ] && /bin/umount $LOOP_SOURCE
655      log_failure_msg "Accessing grml CD-ROM failed. ${MAGENTA}$TARGET_DEV${NORMAL} is not mountable."
656      sleep 2
657      return 1
658   fi
659
660   if [ -f $TARGET/$GRML_DIR/$GRML_NAME ]; then
661     log_begin_msg "Accessing grml CD-ROM at ${MAGENTA}$TARGET_DEV${NORMAL}." ; echo "  $SUCCESS"
662   else
663     log_failure_msg "Accessing grml CD-ROM failed. Could not find $GRML_DIR/$GRML_NAME on ${MAGENTA}$TARGET_DEV${RED}.${NORMAL}"
664     [ -n "$LOOP_SOURCE" ] && /bin/umount $LOOP_SOURCE
665     umount $TARGET
666     sleep 2
667     return 1
668   fi
669   # remount the CD
670   remount_grml
671 }
672
673 copy_to()
674 {
675   # preparations
676   /bin/mkdir $TARGET
677   COPY="$SOURCE/$GRML_DIR"
678
679   # look if we copy to hd or to ram
680   SIZE="$(/usr/bin/du -s $COPY | /usr/bin/gawk '{print int($1*1.1)}')"
681   test -n "$SIZE" || SIZE="800000"
682
683   case "$1" in
684     ram)
685       TARGET_DEV="/dev/shm"
686       TARGET_DEV_DESC="ramdisk"
687       FOUNDSPACE="$(/usr/bin/gawk '/MemTotal/{print $2}' /proc/meminfo)"
688       /bin/mount -n -t tmpfs -o size=${SIZE}k $TARGET_DEV $TARGET
689     ;;
690     hd)
691       TARGET_DEV=$(echo $CMDLINE | /usr/bin/tr ' ' '\n' | /bin/sed -n '/tohd=/s/.*=//p' | /usr/bin/tail -1)
692       TARGET_DEV_DESC="$TARGET_DEV"
693       # load filesystems
694       /GRML/sbin/modprobe fuse
695       /GRML/sbin/modprobe ntfs
696       FS="ext3 ext2 reiserfs reiser4 vfat ntfs"
697
698       MOUNTED=""
699       for i in $FS; do
700        if /GRML/bin/mount -o rw -t "$i" "$TARGET_DEV" "$TARGET"; then
701         MOUNTED="true"
702         break
703        fi
704       done
705       if test -z "$MOUNTED"; then
706         log_failure_msg "Copying grml CD-ROM failed. ${MAGENTA}$TARGET_DEV_DESC${NORMAL} is not mountable."
707         sleep 2
708         return 1
709       fi
710       # check for enough free space
711       USED_SPACE=0
712       [ -f $TARGET/$GRML_DIR/$GRML_NAME ] && USED_SPACE=$(/usr/bin/du -s $TARGET/$GRML_DIR/$GRML_NAME | /usr/bin/gawk '{ print $1 }')
713       FOUNDSPACE="$(/bin/df -k $TARGET | /usr/bin/tail -1 | /usr/bin/gawk '{ print $4+int('$USED_SPACE') }')"
714    ;;
715    *)
716      return 1
717    ;;
718   esac
719
720   # sanity check
721
722   if [ $FOUNDSPACE -lt $SIZE ]
723   then
724     log_failure_msg "Copying grml CD-ROM failed. Not enough free space on ${MAGENTA}${TARGET_DEV_DESC}${NORMAL}. Found: ${MAGENTA}${FOUNDSPACE}k${NORMAL} Need: ${MAGENTA}${SIZE}k${NORMAL}"
725     sleep 2
726     umount $TARGET
727     return 1
728   fi
729
730   # do the real copy
731
732   log_begin_msg "Copying grml CD-ROM to ${TARGET_DEV_DESC}... Please be patient."
733   echo
734   if [ -z "$use_cp" -a -x /usr/bin/rsync ]
735   then
736     # first cp the small files
737     /usr/bin/rsync -a --exclude="$GRML_DIR/$GRML_NAME" $COPY $TARGET # Copy grml to $TARGET
738     # then the big file with nice progress meter
739     [ -f $TARGET/$GRML_DIR/$GRML_NAME ] && /bin/rm -f $TARGET/$GRML_DIR/$GRML_NAME
740     /usr/bin/rsync -a --progress --include="$GRML_DIR/$GRML_NAME" --include="$GRML_DIR/" --exclude="*" $COPY $TARGET # Copy grml to $TARGET
741     #/usr/bin/rsync -avP $COPY $TARGET # Copy grml to $TARGET
742     # make sure to support directories from http://grml.org/config/
743     for dir in scripts bootparams config debs ; do
744         if [ -d "/cdrom/$dir" ] ; then
745            log_begin_msg "Customization directory $dir found, copying to $TARGET"
746            cp -a /cdrom/$dir $TARGET/ && echo "$SUCCESS" || echo "$FAILED"
747         fi
748     done
749   else
750     /bin/cp -a -f $COPY $TARGET # Copy grml to $TARGET
751   fi
752   if [ $? -ne 0 ]
753   then
754     log_failure_msg "Copying grml CD-ROM failed. ${MAGENTA}$TARGET_DEV_DESC${NORMAL} possibly has not enough space left."
755     sleep 2
756     return 1
757   fi
758   # remount r/o
759   /bin/mount -n -o remount,ro $TARGET 1>/dev/null 2>&1
760   remount_grml
761 }
762
763 mount_grml /cdrom
764
765 COPYTO=""
766 BOOTFROM=""
767 DO_REMOUNT=""
768 REAL_TARGET=""
769 UNIONFS=""
770
771 case "$CMDLINE" in *toram*) DO_REMOUNT="yes"; COPYTO="ram"; ;; esac
772 case "$CMDLINE" in *tohd=*) DO_REMOUNT="yes"; COPYTO="hd"; ;; esac
773 case "$CMDLINE" in *bootfrom=*) DO_REMOUNT="yes"; BOOTFROM="yes" ;; esac
774
775 # Remount later after copying/isoloading/driverloading?
776 # pre-test if everything succeeded
777 if  test -n "$DO_REMOUNT" -a -n "$FOUND_GRML" ; then
778   # copy library cache
779   cat /GRML/etc/ld.so.cache > /etc/ld.so.cache
780   echo ""
781
782   SOURCE="/cdrom"
783   TARGET="/cdrom2"
784
785   # first copy_to, then boot_from
786   if [ -n "$COPYTO" ]; then
787      copy_to $COPYTO && REAL_TARGET="$TARGET"
788   fi
789   if [ -n "$BOOTFROM" ]; then
790      boot_from
791      if [ "$?" -eq "0" ]; then
792         # set new source / target paths
793         REAL_TARGET="$TARGET"
794         SOURCE2="$LOOP_SOURCE"
795         SOURCE="/cdrom2"
796         TARGET="/cdrom3"
797     fi
798   fi
799 fi
800
801 # Final test if everything succeeded.
802 if test -n "$FOUND_GRML" ; then
803 # copy library cache
804 cat /GRML/etc/ld.so.cache > /etc/ld.so.cache
805
806 UNIONFS=""
807 if checkbootparam "unionfs" ; then
808    $INSMOD /modules/unionfs.ko 1>/dev/null
809    grep -q unionfs /proc/filesystems && UNIONFS=yes
810    unionfs='unionfs'
811    UNIONFS_FILETYPE='unionfs'
812    AUFS=''
813 else
814    $INSMOD /modules/aufs.ko 1>/dev/null
815    grep -q aufs /proc/filesystems && UNIONFS=yes
816    unionfs='unionfs (using aufs)'
817    UNIONFS_FILETYPE='aufs'
818    AUFS='yes'
819 fi
820
821 # Set paths
822 log_begin_msg "Setting paths"
823 PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:."
824 export PATH
825
826 # Make space: We don't need the modules anymore from here.
827 /GRML/bin/rm -rf /modules
828 # Debian weirdness
829 /GRML/bin/cp -a /GRML/etc/alternatives /etc/ 2>/dev/null
830
831 # Replace /sbin
832 /GRML/bin/rm -f /sbin
833 /GRML/bin/ln -sf /GRML/sbin /sbin
834
835 # From here, we should have all essential commands available.
836 hash -r
837
838 # Did we remount the source media?
839 if  test -n "$REAL_TARGET"; then
840   /bin/mount -n --move $REAL_TARGET /cdrom # move it back and go on to normal boot
841 fi
842
843 # Clean up /etc/mtab (and - just in case - make a nice entry for looped ISO)
844 egrep " /GRML | /cdrom " /proc/mounts 2>/dev/null | sed 's|/dev/loop0 /cdrom \(.*\) 0 0|'$LOOP_SOURCE$ISO_PATH' /cdrom/ \1,loop=/dev/loop0 0 0|g' >> /etc/mtab
845
846 # Clean up /
847 /GRML/bin/rm -rf /modules /static
848 echo "                       $SUCCESS"
849
850 # New in Kernel 2.4.x: tempfs with variable ramdisk size.
851 # We check for available memory anyways and limit the ramdisks
852 # to a reasonable size.
853 FOUNDMEM="$(awk '/MemTotal/{print $2}' /proc/meminfo)"
854 TOTALMEM="$(awk 'BEGIN{m=0};/MemFree|Cached/{m+=$2};END{print m}' /proc/meminfo)"
855
856 # Be verbose
857 if [ -n "$FOUNDMEM" ] ; then
858    log_begin_msg "Total memory found: $FOUNDMEM kB" ; echo "       $SUCCESS"
859 else
860    log_failure_msg "Could not fetch memory information." ; echo "     $FAILED"
861 fi
862
863 # Now we need to use a little intuition for finding a ramdisk size
864 # that keeps us from running out of space, but still doesn't crash the
865 # machine due to lack of Ram
866
867 # Minimum size of additional ram partitions
868 MINSIZE=20000
869 # At least this much memory minus 30% should remain when home and var are full.
870 MINLEFT=16000
871 # Maximum ramdisk size
872 [ -n "$TOTALMEM" ] && MAXSIZE="$(/usr/bin/expr $TOTALMEM - $MINLEFT)"
873 # Default ramdisk size for ramdisk
874 [ -n "$TOTALMEM" ] && RAMSIZE="$(/usr/bin/expr $TOTALMEM / 5)"
875
876 # Create additional dynamic ramdisk.
877 test -z "$RAMSIZE" -o "$RAMSIZE" -lt "$MINSIZE" && RAMSIZE="$MINSIZE"
878 mkdir -p /ramdisk
879 # tmpfs/varsize version, can use swap
880 RAMSIZE=$(/usr/bin/expr $RAMSIZE \* 4)
881 log_begin_msg "Creating /ramdisk (dynamic size=${RAMSIZE}k) on shared memory"
882 # We need /bin/mount here for the -o size= option
883 /bin/mount -t tmpfs -o "size=${RAMSIZE}k" /ramdisk /ramdisk  && echo "$SUCCESS"
884 mkdir -p /ramdisk/tmp /ramdisk/home/grml && chmod 1777 /ramdisk/tmp && chown grml.grml /ramdisk/home/grml && ln -snf /ramdisk/home /home && /bin/mv /tmp /tmp.old && ln -s /ramdisk/tmp /tmp && rm -rf /tmp.old
885
886 stage=3
887 rundebugshell
888 # unionfs
889 log_begin_msg "Creating $unionfs and symlinks on ramdisk"
890 mkdir -p /UNIONFS
891 if test -n "$UNIONFS" && /bin/mount -t $UNIONFS_FILETYPE -o noatime${SECURE},dirs=/ramdisk=rw:/GRML=ro /UNIONFS /UNIONFS ; then
892  # check architecture
893  if [ -f /GRML/lib/ld-linux.so.2 ] ; then
894    LDLINUX=/GRML/lib/ld-linux.so.2
895    GRMLLIB=/GRML/lib
896  elif [ -f /GRML/lib64/ld-linux-x86-64.so.2 ] ; then
897    LDLINUX=/GRML/lib64/ld-linux-x86-64.so.2
898    EMUL='emul'
899    LIB64='lib64'
900    GRMLLIB=/GRML/lib64
901  fi
902  # We now have unionfs, copy some data from the initial ramdisk first
903  cp -a /etc/fstab /etc/auto.mnt /etc/filesystems /etc/mtab /UNIONFS/etc/
904
905  # disable resolvconf on the terminalserver client
906  if [ -n "$NFS" ] ; then
907     rm /UNIONFS/etc/resolv.conf
908     cp -a /etc/resolv.conf /UNIONFS/etc
909     echo REPORT_ABSENT_SYMLINK=no >> /UNIONFS/etc/default/resolvconf
910  fi
911
912  for i in bin boot etc sbin var lib opt root usr; do # Move directories to unionfs
913   if test -d /$i; then
914    /bin/mv /$i /$i.old && \
915    # /GRML/lib/ld-linux.so.2 --library-path /GRML/lib /GRML/bin/ln -snf /UNIONFS/$i /$i && \
916    $LDLINUX --library-path $GRMLLIB /GRML/bin/ln -snf /UNIONFS/$i /$i 1>/dev/null 2>/dev/null
917    rm -rf /$i.old
918   else
919    ln -snf /UNIONFS/$i /$i
920   fi
921  done
922  [ -n "$AUFS" ] && echo "    $SUCCESS" || echo "                 $SUCCESS"
923  log_begin_msg "Merging read-only system with read-writeable /ramdisk."
924  for i in $(cd /UNIONFS; echo *); do # Create links for new stuff on /UNIONFS
925    test "$i" = "home" -o "$i" = "tmp" && continue
926    test -L "/$i" || test -d "/$i" || test -f "/$i" || ln -snf "/UNIONFS/$i" /$i
927  done && echo "   $SUCCESS" || echo "   $FAILED"
928 else
929  echo ""
930  log_failure_msg "ERROR: CANNOT UNITE READ-ONLY MEDIA AND INITIAL RAMDISK!"
931  echo "$FAILED"
932  sleep 2
933  echo "Can not continue booting, dropping you to a busybox shell."
934  stage=4
935  rundebugshell
936 fi
937
938 chown grml.grml /home/grml
939 # old:
940 # chmod 1777 /var/tmp
941 # new:
942 rm -rf /var/tmp/ ; mkdir /var/tmp ; chown root.root /var/tmp ; chmod 1777 /var/tmp
943
944 # Create empty utmp and wtmp
945 :> /var/run/utmp
946 :> /var/run/wtmp
947
948 # Make SURE that these are files, not links!
949 rm -rf /etc/ftpusers /etc/passwd /etc/shadow /etc/group \
950        /etc/ppp /etc/isdn /etc/ssh /etc/ioctl.save \
951        /etc/inittab /etc/network /etc/sudoers \
952        /etc/init /etc/localtime /etc/dhcpc /etc/pnm2ppa.conf 2>/dev/null
953 cp -a /GRML/etc/ftpusers /GRML/etc/passwd /GRML/etc/shadow /GRML/etc/group \
954       /GRML/etc/ppp /GRML/etc/isdn /GRML/etc/ssh \
955       /GRML/etc/inittab /GRML/etc/network /GRML/etc/sudoers \
956       /GRML/sbin/init /GRML/etc/dhcpc /etc/ 2>/dev/null
957
958 # Extremely important, init crashes on shutdown if this is only a link
959 :> /etc/ioctl.save
960 :> /etc/pnm2ppa.conf
961 # Must exist for samba to work
962 [ -d /var/lib/samba ] && :> /var/lib/samba/unexpected.tdb
963 # Diet libc bug workaround
964 # cp -f /GRML/etc/localtime /etc/localtime
965 # echo "${BLUE}Done.${NORMAL}"
966
967 # Now tell kernel where the real modprobe lives
968 echo "/sbin/modprobe" > /proc/sys/kernel/modprobe
969
970 # Change root device from /dev/fd0 to /dev/ram0
971 echo "0x100" > /proc/sys/kernel/real-root-dev
972
973 /bin/umount /sys # (remount in grml-autoconfig)
974
975 stage=4
976 rundebugshell
977 # Give control to the init process.
978 log_begin_msg "Starting init process. "
979 [ -r /mountit ] && rm -f /mountit
980 rm -f /linuxrc
981 exit 0
982
983 else
984  log_failure_msg "Error: Can't find grml filesystem, sorry."
985  echo "
986 Are you booting via USB or firewire?
987 ====================================
988 Try to boot with bootparam scandelay which delays the
989 bootup sequence so modules should have enough time
990 to initialize devices.
991
992 Usage examples on bootprompt of grml-iso:
993
994 grml scandelay       -> adds the default delay of 10 seconds
995 grml scandelay=13    -> adds a delay of 13 seconds
996
997 Are you booting via SCSI?
998 ====================================
999 Use the bootparam scsi.
1000 Usage examples on bootprompt of grml-iso:
1001
1002 grml scsi=probe      -> autoprobing of scsi modules
1003 grml scsi=ask        -> list modules and prompt for module which should be loaded
1004 grml scsi=modulename -> loads specified module (without .ko extension)
1005 expert               -> activate expert mode, similar to scsi=ask
1006
1007 Are you getting SquashFS/zlib errors?
1008 =====================================
1009 Try to boot with \"grml nodma\"
1010
1011 Still problems?
1012 ===============
1013 Make sure the ISO itself is ok.
1014 Check the md5sum of downloaded ISO.
1015 Used a CD-RW? Make sure the medium is ok!
1016
1017 Please report any problems you notice to the grml-team!
1018 http://grml.org/contact/
1019 "
1020  echo "${RED}Now dropping you to the busybox shell.${NORMAL}"
1021  echo "${RED}Press reset button to quit.${NORMAL}"
1022  echo ""
1023  PS1="grml# "
1024  export PS1
1025  echo "6" > /proc/sys/kernel/printk
1026  # Allow signals
1027  trap 1 2 3 15
1028  exec /static/sh
1029 fi
1030 # EOF