removed modcalc_speed.py
[grml-terminalserver.git] / 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: Mon Sep 18 23:19:53 CEST 2006 [mika]
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 only need the builtin commands and /static 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 if [ -n "$DEBUG" ]; then
220    log_begin_msg "Bootoption debug detected. Printing kernel command line:"
221    echo ""
222    cat /proc/cmdline
223 fi
224
225 # Run a shell if in debug mode
226 # echo "${BLUE}Dropping you to a busybox shell for debugging.${NORMAL}"
227 stage=1
228 rundebugshell(){
229  if [ -n "$DEBUG" ]; then
230   log_begin_msg "Starting intermediate shell stage $stage as requested by \"debug\" option."
231   echo ""
232   echo "   ${GREEN}-${NORMAL} Just exit the shell to continue boot process...${NORMAL}"
233   if [ -x /static/sh ]; then
234     /static/sh
235   else
236     /bin/bash
237   fi
238  fi
239 }
240 rundebugshell
241
242 # Mount module disk
243 mountmodules(){
244 TYPE="$1"; shift
245 echo -n "${CRE}Please insert ${TYPE} modules disk and hit Return."
246 read a
247 echo -n "${CRE}Mounting ${TYPE} modules disk... "
248 # We always mount over /modules/scsi (because it's there ;-)
249 if mountit /dev/fd0 /modules/scsi "-o ro"; then
250 echo "${GREEN}OK.${NORMAL}"
251 return 0
252 fi
253 echo "${RED}NOT FOUND.${NORMAL}"
254 return 1
255 }
256
257 # Unmount module disk
258 umountmodules(){
259 TYPE="$1"; shift
260 echo -n "${CRE}Unmounting ${TYPE} modules disk... "
261 umount /modules/scsi 2>/dev/null
262 echo "${GREEN}DONE.${NORMAL}"
263 }
264
265 # Ask user for modules
266 askmodules(){
267  TYPE="$1"; shift
268  echo "${TYPE} modules available:${WHITE}"
269  c=""
270  for m in "$@"; do
271   if test -f "/modules/scsi/$m"; then
272    test -z "$c"  && { echo -n " $m"; c="1"; } || { echo "               $m"; c=""; }
273   fi
274  done
275  [ -n "$c" ] && echo ""
276  echo "Load ${TYPE} Modules?"
277  echo "[Enter full filename(s) (space-separated), Return for autoprobe, n for none] "
278  echo -n "insmod module(s)> "
279  read MODULES
280  case "$MODULES" in n|N) MODULES=""; ;; y|"")  MODULES="$*"; ;; esac
281 }
282
283 # Try to load the given modules (full path or current directory)
284 loadmodules(){
285  TYPE="$1"; shift
286  test -n "$INTERACTIVE" && echo "6" > /proc/sys/kernel/printk
287  echo ""
288  for i in "$@"; do
289   echo -n "   Probing ${TYPE}... ${WHITE}$i${NORMAL}: "
290    if test -f /modules/scsi/$i.ko && $INSMOD -f /modules/scsi/$i.ko >/dev/null 2>&1 && echo "  $SUCCESS" || echo " failed " ; then
291      case "$TYPE" in scsi|SCSI) FOUND_SCSI="yes"; ;; esac
292    fi
293  done
294  test -n "$INTERACTIVE" && echo "0" > /proc/sys/kernel/printk
295  echo -n "${CRE}"
296 }
297
298 # Check for SCSI, use modules on bootfloppy first
299 # Trying to do kind of /proc/pci hardware detection
300 if checkbootparam oldscsi ; then
301   PROCPCI="`cat /proc/pci 2>/dev/null`"
302   case "$PROCPCI" in *[Aa][Ii][Cc]-*|*[Aa][Hh][Aa]-*) SCSI_PROBE="$SCSI_PROBE aic7xxx" ;; esac
303   case "$PROCPCI" in *[Bb][Uu][Ss][Ll][Oo][Gg][Ii][Cc]*) SCSI_PROBE="$SCSI_PROBE BusLogic" ;; esac
304   case "$PROCPCI" in *[Tt][Rr][Mm]-[Ss]1040*|*[Dd][Cc]395*|*[Dd][Cc]315*) SCSI_PROBE="$SCSI_PROBE dc395x" ;; esac
305   case "$PROCPCI" in *53[Cc]8*) SCSI_PROBE="$SCSI_PROBE sym53c8xx" ;; esac
306   case "$PROCPCI" in *53[Cc]9*) SCSI_PROBE="$SCSI_PROBE NCR53C9x" ;; esac
307   case "$PROCPCI" in *53[Cc]406*) SCSI_PROBE="$SCSI_PROBE NCR53c406a" ;; esac
308   case "$PROCPCI" in *[Ii][Nn][Ii][Tt][Ii][Oo]\ *|*[Ii][Nn][Ii]-[Aa]100[Uu]2[Ww]*) SCSI_PROBE="$SCSI_PROBE initio" ;; esac
309   case "$PROCPCI" in *[Mm][Pp][Tt]*[Ss][Cc][Ss][Ii]*) SCSI_PROBE="$SCSI_PROBE mptbase mptscsih" ;; esac
310   case "$PROCPCI" in *[Aa][Dd][Vv][Aa][Nn][Cc][Ee][Dd]\ [Ss][Yy][Ss]*) SCSI_PROBE="$SCSI_PROBE advansys" ;; esac
311   case "$PROCPCI" in *[Aa][Tt][Pp]8*|*[Aa][Ee][Cc]6*) SCSI_PROBE="$SCSI_PROBE atp870u" ;; esac
312   case "$PROCPCI" in *[Dd][Tt][Cc]*) SCSI_PROBE="$SCSI_PROBE dtc" ;; esac
313   case "$PROCPCI" in *[Ee][Aa][Tt][Aa]*) SCSI_PROBE="$SCSI_PROBE eata" ;; esac
314   case "$PROCPCI" in *[Ff]*[Dd][Oo][Mm][Aa][Ii][Nn]*) SCSI_PROBE="$SCSI_PROBE fdomain" ;; esac
315   case "$PROCPCI" in *[Gg][Dd][Tt]\ *) SCSI_PROBE="$SCSI_PROBE gdth" ;; esac
316   case "$PROCPCI" in *[Mm][Ee][Gg][Aa][Rr][Aa][Ii][Dd]*) SCSI_PROBE="$SCSI_PROBE megaraid_mm megaraid_mbox" ;; esac
317   case "$PROCPCI" in *[Qq][Ll][Oo][Gg][Ii][Cc]*) SCSI_PROBE="$SCSI_PROBE qlogicfas408 qlogicfas qlogicfc" ;; esac
318   case "$PROCPCI" in *53[Cc]974*) SCSI_PROBE="$SCSI_PROBE tmscsim" ;; esac
319   case "$PROCPCI" in *[Uu][Ll][Tt][Rr][Aa][Ss][Tt][Oo][Rr]*) SCSI_PROBE="$SCSI_PROBE ultrastor" ;; esac
320   case "$PROCPCI" in *3[Ww][Aa][Rr][Ee]*) SCSI_PROBE="$SCSI_PROBE 3w-xxxx" ;; esac
321 fi
322
323 # New sysfs based SCSI detection (thanks, Jörg Schirottke)
324 sysfsscsi(){
325 SYS=$(for x in $(find /sys/devices/ -name modalias); do grep pci: $x; done|cut -f2 -d:)
326 while read id driver; do
327  for sysid in $SYS; do
328   case $sysid in $id)
329    if [ -z "$SCSI_PROBE" ]; then
330     SCSI_PROBE="$driver"
331    else
332     SCSI_PROBE="$SCSI_PROBE $driver"
333    fi
334    ;;
335   esac
336  done
337 done <<EOF
338 $(cat /modules/scsi/scsi-modules.txt)
339 EOF
340 }
341
342 if test -n "$INTERACTIVE"; then
343 # Let the user select interactively
344   askmodules SCSI $(cd /modules/scsi; echo *.ko)
345 else
346 # these are the autoprobe-safe modules
347   sysfsscsi
348   MODULES="$SCSI_PROBE"
349 fi
350
351 if test -z "$NOSCSI" ; then
352  log_begin_msg "Scanning for SCSI devices."
353  $INSMOD -f /modules/scsi/firmware_class.ko 1>/dev/null
354  test -n "$MODULES" && loadmodules SCSI $MODULES &&  echo -n "" || echo "           ${BLUE}[${NORMAL} none found ${BLUE}]${NORMAL} (try bootoption scsi=probe)"
355 else
356   log_warn_msg "Not scanning for SCSI devices as requested on commandline." && echo " $SUCCESS"
357 fi
358
359 if checkbootparam scsi ; then
360   MODULE="$(getbootparam 'scsi' 2>/dev/null)"
361   if test "$MODULE" = "probe" ; then
362     log_begin_msg "Bootoption scsi=probe found. Trying to autoprobe SCSI modules:"
363     echo ""
364     echo -n "   Trying to load scsi_debug: " ; $INSMOD -f /modules/scsi/scsi_debug.ko 1>/dev/null && echo "           $SUCCESS" || echo " [ failed ]"
365     for module in /modules/scsi/*.ko ; do
366       echo -n "   Probing ${WHITE}${module}${NORMAL}..."
367       $INSMOD -f ${module} >/dev/null 2>&1 && echo " $SUCCESS" || echo " [ failed ]"
368     done
369   elif test "$MODULE" = "ask" ; then
370     askmodules SCSI $(cd /modules/scsi; echo *.ko)
371     test -z "$NOSCSI" && test -n "$MODULES" && loadmodules SCSI $MODULES
372   else
373     [ -n "$MODULE" ] || echo "   ${RED}Neither a specific module nor option probe nor option ask for SCSI module given. Skipping.${NORMAL}"
374     [ -n "$MODULE" ] && echo -n "   Trying to load module ${WHITE}${MODULE}${NORMAL}:" ; \
375       $INSMOD -f "/modules/scsi/${MODULE}.ko" 1>/dev/null && echo "  $SUCCESS" || echo " [ failed ]"
376   fi
377 fi
378 # End of SCSI check
379
380 if test -n "$VMWARE" ; then
381   log_begin_msg "Bootoption VMware detected. Trying to load SCSI modules:"
382   echo ""
383   for module in mptbase mptscsih mptspi BusLogic ; do
384     echo -n "   Trying to load ${WHITE}${module}${NORMAL}: "
385     $INSMOD -f /modules/scsi/${module}.ko >/dev/null 2>&1 && echo " $SUCCESS" || echo " [ failed ]"
386   done
387 fi
388
389 # Check for USB, use modules on bootfloppy first
390 if test -z "$NOUSB"; then
391   log_begin_msg "Checking for USB."
392   if test -f /modules/div/usbcore.ko; then
393     $INSMOD /modules/div/usbcore.ko >/dev/null 2>&1
394     FOUNDUSB=""
395     for i in $USB2 uhci-hcd.ko ohci-hcd.ko ; do
396       test -f /modules/div/$i && $INSMOD /modules/div/$i >/dev/null 2>&1 && FOUNDUSB="yes"
397     done
398     if test -n "$FOUNDUSB"; then
399       test -f /modules/div/usb-storage.ko && $INSMOD /modules/div/usb-storage.ko >/dev/null 2>&1
400       echo "                   $SUCCESS"
401     else
402       echo "                   ${BLUE}[${NORMAL} not found ${BLUE}]${NORMAL}"
403       true
404     fi
405     if [ -n "$NOUSB2" ] ; then
406       echo "   Not loading usb2 module ehci-hcd as requested on commandline."
407       echo "   Notice: to skip loading of USB in initrd at all use bootoption nousb"
408     fi
409   fi
410 else
411   log_warn_msg "Not scanning for USB devices as requested on commandline." && echo " $SUCCESS"
412   echo "   Notice that bootoption nousb affects initrd only, it does *not*"
413   echo "   avoid loading of usb modules in userspace afterwards"
414   echo "   Boot using something like 'nousb blacklist=uhci-hcd' to avoid loading of usb modules at all"
415 fi
416 # End of USB check
417
418 # Check for Firewire, use modules on bootfloppy first
419 if test -z "$NOFIREWIRE" ; then
420   log_begin_msg "Checking for Firewire."
421   if test -f /modules/div/ieee1394.ko ; then
422     $insmoD /modules/div/ieee1394.ko > /dev/null 2>&1
423     FOUNDFIREWIRE=""
424     test -f /modules/div/ohci1394.ko && $INSMOD /modules/div/ohci1394.ko > /dev/null 2>&1 && FOUNDFIREWIRE="yes"
425     if test -n "$FOUNDFIREWIRE" ; then
426       test -f /modules/div/sbp2.ko && $INSMOD /modules/div/sbp2.ko > /dev/null 2>&1
427       echo "              $SUCCESS"
428     else
429       echo "               ${BLUE}[${NORMAL} not found ${BLUE}]${NORMAL}"
430       true
431     fi
432   fi
433 else
434   log_warn_msg "Not scanning for firewire devices as requested on commandline." && echo " $SUCCESS"
435   echo "   Notice that bootoption nofirewire affects initrd only, it does *not*"
436   echo "   avoid loading of firewire modules in userspace afterwards"
437   echo "   Boot with something like 'nofirewire blacklist=ohci1394' to avoid loading of firewire modules at all"
438 fi
439 # End of FIREWIRE check
440
441 # Unfortunately, hotpluggable devices tend to need some time in order to register
442 if test -n "$FOUNDUSB" -o -n "$FOUNDFIREWIRE"; then
443   log_begin_msg "Scanning for USB/Firewire devices."
444   sleep 4
445   if test -n "$USB"; then
446     sleep 10
447   fi
448  echo "  $SUCCESS"
449 fi
450
451 if checkbootparam scandelay ; then
452   DELAY="$(getbootparam 'scandelay' 2>/dev/null)"
453   [ -z $DELAY ] && DELAY='10'
454   log_begin_msg "Delaying bootsequence as requested for ${WHITE}${DELAY}${NORMAL} seconds."
455   sleep $DELAY && echo "  $SUCCESS"
456 fi
457
458 # boot via pcmcia
459 if checkbootparam bootpcmcia ; then
460   log_begin_msg "Bootoption bootpcmcia found. Trying to load ${WHITE}PCMCIA${NORMAL} modules..."
461   if $INSMOD -f /modules/div/pcmcia_core.ko 1>/dev/null ; then
462      $INSMOD -f /modules/div/firmware_class.ko 1>/dev/null && \
463      $INSMOD -f /modules/div/pcmcia.ko         1>/dev/null && \
464      $INSMOD -f /modules/div/rsrc_nonstatic.ko 1>/dev/null && \
465      $INSMOD -f /modules/div/yenta_socket.ko   1>/dev/null && echo " $SUCCESS"
466   else
467     echo " [ failed ]"
468   fi
469 fi
470
471 # Check for misc modules in expert mode
472 if test -n "$INTERACTIVE" ; then
473   another=""
474   answer=""
475   while test "$answer" != "n" -a "$answer" != "N" ; do
476     echo -n "${CYAN}Do you want to load additional modules from$another floppy disk? [${WHITE}Y${CYAN}/n] ${NORMAL}"
477     another=" another"
478     read answer
479     case "$answer" in n*|N*) break; ;; esac
480     if mountmodules new ; then
481        askmodules new $(cd /modules/scsi; echo *.ko)
482        test -n "$MODULES" && loadmodules new $MODULES
483        umountmodules current
484     fi
485   done
486 fi
487 # All interactively requested modules should be loaded now.
488
489 # Check for ide-scsi supported CD-Roms et al.
490 test -f /proc/scsi/scsi && FOUND_SCSI="yes"
491
492 # Disable kernel messages again
493 echo "0" > /proc/sys/kernel/printk
494
495 # We now enable DMA right here, for faster reading/writing from/to IDE devices
496 # in FROMHD or TORAM mode
497 case "$CMDLINE" in *\ nodma*) ;; *)
498  for d in $(cd /proc/ide 2>/dev/null && echo hd[a-z]); do
499   if test -d /proc/ide/$d; then
500     MODEL="$(cat /proc/ide/$d/model 2>/dev/null)"
501     test -z "$MODEL" && MODEL="[GENERIC IDE DEVICE]"
502     log_begin_msg "Enabling DMA acceleration for: ${MAGENTA}$d   ${YELLOW}[${MODEL}]${NORMAL}"
503     echo "using_dma:1" >/proc/ide/$d/settings && echo ""
504   fi
505  done
506  ;;
507 esac
508
509 stage=2
510 rundebugshell
511 # NFS
512 if checkbootparam "nfsdir" ; then
513   NFS="$(getbootparam nfsdir)"
514 #  if test -z $NFS
515 #    then
516 #    NFS=192.168.0.1
517 #    echo "${CRE}${BLUE}No NFS-server given, assuming default $NFS${NORMAL}"
518 #  fi
519   echo "${RED}Debug: NFS = ${NFS}${NORMAL}"
520   log_begin_msg "${CRE} ${GREEN}*${NORMAL} Looking for CD-ROM in: ${MAGENTA}$NFS${NORMAL}"
521   if mountit -t nfs $NFS "-o ro" >/dev/null 2>&1
522     then
523     if test -f /cdrom/$GRML_DIR/$GRML_NAME
524       then
525       log_begin_msg "Accessing grml CD-ROM at ${MAGENTA}${NFS}${NORMAL}" ; echo "     $SUCCESS"
526       FOUND_GRML="$NFS"
527       break
528     fi
529     umount /cdrom
530   fi
531 fi
532
533 # Now that the right SCSI driver is (hopefully) loaded, try to find CD-ROM
534 if test -z $NFS ; then
535   DEVICES="/dev/hd?"
536   test -n "$FOUND_SCSI" -a -z "$NOCD" && DEVICES="/dev/scd? /dev/scd?? $DEVICES"
537   # New: Also try parallel port CD-Roms [for Mike].
538   DEVICES="$DEVICES /dev/pcd?"
539   # New: also check HD partitions for a GRML/GRML image
540   # notice: use /dev/sd? for usb-sticks without partition(s)
541   test -n "$FOUND_SCSI" -a -z "$NOSCSI" && DEVICES="$DEVICES /dev/sd?[1-9] /dev/sd?[1-9][0-9] /dev/sd?"
542   DEVICES="$DEVICES /dev/hd?[1-9] /dev/hd?[1-9][0-9]"
543   case "$CMDLINE" in *fromhd=/dev/*) DEVICES="$fromhd"; ;; esac
544   for i in $DEVICES
545   do
546   log_begin_msg "${CRE} ${GREEN}*${NORMAL} Looking for CD-ROM in:    ${MAGENTA}$i${NORMAL}"
547   if mountit $i /cdrom "-o ro" >/dev/null 2>&1
548     then
549     echo "  $SUCCESS"
550     if test -f /cdrom/$GRML_DIR/$GRML_NAME
551       then
552       log_begin_msg "Accessing grml CD-ROM at: ${MAGENTA}$i${NORMAL}" ; echo "  $SUCCESS"
553       FOUND_GRML="$i"
554       break
555     fi
556     umount /cdrom
557   fi
558   done
559 fi
560
561 # Harddisk-installed script part version has been removed
562 # (GRML can be booted directly from HD now).
563 mount_grml()
564 {
565   if test -n "$FOUND_GRML" -a -f $1/$GRML_DIR/$GRML_NAME; then
566     # echo "6" > /proc/sys/kernel/printk
567     mount -t squashfs $1/$GRML_DIR/$GRML_NAME /GRML -o loop,ro$SECURE || FOUND_GRML=""
568   fi
569 }
570
571 remount_grml()
572 {
573   if test -f $TARGET/$GRML_DIR/$GRML_NAME; then
574     umount /GRML # unmount it
575     umount $SOURCE  # unmount CD
576     [ -n "$SOURCE2" ] && umount $SOURCE2  # umount possible loop-device
577     mount_grml $TARGET
578   else
579     echo "${CRE} ${RED}Warning: Changing to $TARGET failed.${NORMAL}"
580     return 1
581   fi
582
583   return 0
584 }
585
586 boot_from()
587 {
588   # preparations
589   /bin/mkdir $TARGET
590
591   SOURCE_DEV=$(echo $CMDLINE | /usr/bin/tr ' ' '\n' | /bin/sed -n '/bootfrom=/s/.*=//p' | /usr/bin/tail -1)
592
593   LOOP_DEV=$(echo $SOURCE_DEV | /usr/bin/gawk -F/ '{ print $1 "/" $2 "/" $3 }')
594   ISO_PATH=$(echo $SOURCE_DEV | /bin/sed "s|$LOOP_DEV||g" )
595   case "$ISO_PATH" in /*.[iI][sS][oO]) ;; *) ISO_PATH="" ;; esac
596   LOOP_SOURCE=""
597
598   # load filesystems
599   /GRML/sbin/modprobe fuse
600   /GRML/sbin/modprobe ntfs
601   $INSMOD -f /modules/div/ntfs.ko 1>/dev/null
602
603   if [ -n "$ISO_PATH" ]; then
604      LOOP_SOURCE="$TARGET.loop"
605      LOOP_SOURCE2="$LOOP_SOURCE"
606      TARGET_DEV="$LOOP_SOURCE$ISO_PATH"
607      /bin/mkdir $LOOP_SOURCE
608      /bin/mount -o ro $LOOP_DEV $LOOP_SOURCE || LOOP_SOURCE=""
609      /bin/mount -n -o loop $LOOP_SOURCE2$ISO_PATH $TARGET
610   else
611      TARGET_DEV="$SOURCE_DEV"
612     /bin/mount -n -o ro $SOURCE_DEV $TARGET
613   fi
614   if [ $? -ne 0 ]; then
615      [ -n "$LOOP_SOURCE" ] && /bin/umount $LOOP_SOURCE
616      echo -n "${CRE} ${RED}Accessing grml CD-ROM failed. ${MAGENTA}$TARGET_DEV${RED} is not mountable.${NORMAL}"
617      sleep 2
618      return 1
619   fi
620
621   if [ -f $TARGET/$GRML_DIR/$GRML_NAME ]; then
622     log_begin_msg "Accessing grml CD-ROM at ${MAGENTA}$TARGET_DEV${NORMAL}." ; echo "  $SUCCESS"
623   else
624     echo -n "${CRE} ${RED}Accessing grml CD-ROM failed. Could not find $GRML_DIR/$GRML_NAME on ${MAGENTA}$TARGET_DEV${RED}.${NORMAL}"
625     [ -n "$LOOP_SOURCE" ] && /bin/umount $LOOP_SOURCE
626     umount $TARGET
627     sleep 2
628     return 1
629   fi
630   # remount the CD
631   remount_grml
632 }
633
634 copy_to()
635 {
636   # preparations
637   /bin/mkdir $TARGET
638   COPY="$SOURCE/$GRML_DIR"
639
640   # look if we copy to hd or to ram
641   SIZE="$(/usr/bin/du -s $COPY | /usr/bin/gawk '{print int($1*1.1)}')"
642   test -n "$SIZE" || SIZE="800000"
643
644   case "$1" in
645     ram)
646       TARGET_DEV="/dev/shm"
647       TARGET_DEV_DESC="ramdisk"
648       FOUNDSPACE="$(/usr/bin/gawk '/MemTotal/{print $2}' /proc/meminfo)"
649       /bin/mount -n -t tmpfs -o size=${SIZE}k $TARGET_DEV $TARGET
650     ;;
651     hd)
652       TARGET_DEV=$(echo $CMDLINE | /usr/bin/tr ' ' '\n' | /bin/sed -n '/tohd=/s/.*=//p' | /usr/bin/tail -1)
653       TARGET_DEV_DESC="$TARGET_DEV"
654       # load filesystems
655       /GRML/sbin/modprobe fuse
656       /GRML/sbin/modprobe ntfs
657       FS="ext3 ext2 reiserfs reiser4 vfat ntfs"
658
659       MOUNTED=""
660       for i in $FS; do
661        if /GRML/bin/mount -o rw -t "$i" "$TARGET_DEV" "$TARGET"; then
662         MOUNTED="true"
663         break
664        fi
665       done
666       if test -z "$MOUNTED"; then
667         echo -n "${CRE} ${RED}Copying grml CD-ROM failed. ${MAGENTA}$TARGET_DEV_DESC${RED} is not mountable.${NORMAL}"
668         sleep 2
669         return 1
670       fi
671       # check for enough free space
672       USED_SPACE=0
673       [ -f $TARGET/$GRML_DIR/$GRML_NAME ] && USED_SPACE=$(/usr/bin/du -s $TARGET/$GRML_DIR/$GRML_NAME | /usr/bin/gawk '{ print $1 }')
674       FOUNDSPACE="$(/bin/df -k $TARGET | /usr/bin/tail -1 | /usr/bin/gawk '{ print $4+int('$USED_SPACE') }')"
675    ;;
676    *)
677      return 1
678    ;;
679   esac
680
681   # sanity check
682
683   if [ $FOUNDSPACE -lt $SIZE ]
684   then
685     echo -n "${CRE} ${RED}Copying grml CD-ROM failed. Not enough free space on ${MAGENTA}${TARGET_DEV_DESC}${RED}. Found: ${MAGENTA}${FOUNDSPACE}k${RED} Need: ${MAGENTA}${SIZE}k${RED} ${NORMAL}"
686     sleep 2
687     umount $TARGET
688     return 1
689   fi
690
691   # do the real copy
692
693   echo "${CRE} ${GREEN}Copying grml CD-ROM to ${MAGENTA}$TARGET_DEV_DESC${GREEN}... Please be patient. ${NORMAL}"
694   if [ -z "$use_cp" -a -x /usr/bin/rsync ]
695   then
696     # first cp the small files
697     /usr/bin/rsync -a --exclude="$GRML_DIR/$GRML_NAME" $COPY $TARGET # Copy grml to $TARGET
698     # then the big file with nice progress meter
699     [ -f $TARGET/$GRML_DIR/$GRML_NAME ] && /bin/rm -f $TARGET/$GRML_DIR/$GRML_NAME
700     /usr/bin/rsync -a --progress --include="$GRML_DIR/$GRML_NAME" --include="$GRML_DIR/" --exclude="*" $COPY $TARGET # Copy grml to $TARGET
701     #/usr/bin/rsync -avP $COPY $TARGET # Copy grml to $TARGET
702   else
703     /bin/cp -a -f $COPY $TARGET # Copy grml to $TARGET
704   fi
705   if [ $? -ne 0 ]
706   then
707     echo -n "${CRE} ${RED}Copying grml CD-ROM failed. ${MAGENTA}$TARGET_DEV_DESC${RED} possibly has not enough space left.${NORMAL}"
708     sleep 2
709     return 1
710   fi
711   # remount r/o
712   /bin/mount -n -o remount,ro $TARGET
713   remount_grml
714 }
715
716 mount_grml /cdrom
717
718 COPYTO=""
719 BOOTFROM=""
720 DO_REMOUNT=""
721 REAL_TARGET=""
722 UNIONFS=""
723
724 case "$CMDLINE" in *toram*) DO_REMOUNT="yes"; COPYTO="ram"; ;; esac
725 case "$CMDLINE" in *tohd=*) DO_REMOUNT="yes"; COPYTO="hd"; ;; esac
726 case "$CMDLINE" in *bootfrom=*) DO_REMOUNT="yes"; BOOTFROM="yes" ;; esac
727
728 # Remount later after copying/isoloading/driverloading?
729 # pre-test if everything succeeded
730 if  test -n "$DO_REMOUNT" -a -n "$FOUND_GRML" ; then
731   # copy library cache
732   cat /GRML/etc/ld.so.cache > /etc/ld.so.cache
733   echo ""
734
735   SOURCE="/cdrom"
736   TARGET="/cdrom2"
737
738   # first copy_to, then boot_from
739   if [ -n "$COPYTO" ]; then
740    copy_to $COPYTO && REAL_TARGET="$TARGET"
741   fi
742   if [ -n "$BOOTFROM" ]; then
743     boot_from
744     if [ "$?" -eq "0" ]; then
745       # set new source / target paths
746       REAL_TARGET="$TARGET"
747       SOURCE2="$LOOP_SOURCE"
748       SOURCE="/cdrom2"
749       TARGET="/cdrom3"
750     fi
751   fi
752 fi
753
754 # Final test if everything succeeded.
755 if test -n "$FOUND_GRML"
756 then
757 # copy library cache
758 cat /GRML/etc/ld.so.cache > /etc/ld.so.cache
759
760 UNIONFS=""
761 $INSMOD /modules/unionfs.ko 1>/dev/null
762 grep -q unionfs /proc/filesystems && UNIONFS=yes
763
764 # Enable kernel messages
765 echo "6" > /proc/sys/kernel/printk
766
767 # Set paths
768 log_begin_msg "Setting paths"
769 PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:."
770 export PATH
771
772 # Make space: We don't need the modules anymore from here.
773 /GRML/bin/rm -rf /modules
774 # Debian weirdness
775 /GRML/bin/cp -a /GRML/etc/alternatives /etc/ 2>/dev/null
776
777 # Replace /sbin
778 /GRML/bin/rm -f /sbin
779 /GRML/bin/ln -sf /GRML/sbin /sbin
780
781 # From here, we should have all essential commands available.
782 hash -r
783
784 # Did we remount the source media ?
785 if  test -n "$REAL_TARGET";
786 then
787    /bin/mount -n --move $REAL_TARGET /cdrom # move it back and go on to normal boot
788 fi
789
790 # Clean up /etc/mtab (and - just in case - make a nice entry for looped ISO)
791 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
792
793 # Clean up /
794 /GRML/bin/rm -rf /modules /static
795 echo "                       $SUCCESS"
796
797 # New in Kernel 2.4.x: tempfs with variable ramdisk size.
798 # We check for available memory anyways and limit the ramdisks
799 # to a reasonable size.
800 FOUNDMEM="$(awk '/MemTotal/{print $2}' /proc/meminfo)"
801 TOTALMEM="$(awk 'BEGIN{m=0};/MemFree|Cached/{m+=$2};END{print m}' /proc/meminfo)"
802
803 # Be verbose
804 log_begin_msg "Total memory found: $FOUNDMEM kB" ; echo "       $SUCCESS"
805
806 # Now we need to use a little intuition for finding a ramdisk size
807 # that keeps us from running out of space, but still doesn't crash the
808 # machine due to lack of Ram
809
810 # Minimum size of additional ram partitions
811 MINSIZE=20000
812 # At least this much memory minus 30% should remain when home and var are full.
813 MINLEFT=16000
814 # Maximum ramdisk size
815 MAXSIZE="$(expr $TOTALMEM - $MINLEFT)"
816 # Default ramdisk size for ramdisk
817 RAMSIZE="$(expr $TOTALMEM / 5)"
818
819 # Create additional dynamic ramdisk.
820 test -z "$RAMSIZE" -o "$RAMSIZE" -lt "$MINSIZE" && RAMSIZE="$MINSIZE"
821 mkdir -p /ramdisk
822 # tmpfs/varsize version, can use swap
823 RAMSIZE=$(expr $RAMSIZE \* 4)
824 log_begin_msg "Creating /ramdisk (dynamic size=${RAMSIZE}k) on shared memory"
825 # We need /bin/mount here for the -o size= option
826 /bin/mount -t tmpfs -o "size=${RAMSIZE}k" /ramdisk /ramdisk  && echo "$SUCCESS"
827 mkdir -p /ramdisk/tmp /ramdisk/home/grml && chmod 1777 /ramdisk/tmp && chown grml.grml /ramdisk/home/grml && ln -snf /ramdisk/home /home && mv /tmp /tmp.old && ln -s /ramdisk/tmp /tmp && rm -rf /tmp.old
828
829 stage=3
830 rundebugshell
831 # unionfs
832 log_begin_msg "Creating unionfs and symlinks on ramdisk"
833 mkdir -p /UNIONFS
834 if test -n "$UNIONFS" && /bin/mount -t unionfs -o noatime${SECURE},dirs=/ramdisk=rw:/GRML=ro /UNIONFS /UNIONFS; then
835  # We now have unionfs, copy some data from the initial ramdisk first
836  cp -a /etc/fstab /etc/auto.mnt /etc/filesystems /etc/mtab /UNIONFS/etc/
837  for i in bin boot etc sbin var lib opt root usr; do # Move directories to unionfs
838   if test -d /$i; then
839    mv /$i /$i.old && \
840    /GRML/lib/ld-linux.so.2 --library-path /GRML/lib /GRML/bin/ln -snf /UNIONFS/$i /$i && \
841    rm -rf /$i.old
842   else
843    ln -snf /UNIONFS/$i /$i
844   fi
845  done
846  #echo "${GREEN}done${NORMAL}"
847  echo "                 $SUCCESS"
848  log_begin_msg "Merging read-only system with read-writeable /ramdisk."
849  for i in $(cd /UNIONFS; echo *); do # Create links for new stuff on /UNIONFS
850    test "$i" = "home" -o "$i" = "tmp" && continue
851    test -L "/$i" || test -d "/$i" || test -f "/$i" || ln -snf "/UNIONFS/$i" /$i
852  done && echo "   $SUCCESS" || echo "   $FAILED"
853 else
854  echo ""
855  log_failure_msg "ERROR: CANNOT UNITE READ-ONLY MEDIA AND INITIAL RAMDISK!"
856  echo "$FAILED"
857  NOUNIONFS="yes"
858  /GRML/sbin/halt -f -n
859 fi
860
861 chown grml.grml /home/grml
862 # old:
863 # chmod 1777 /var/tmp
864 # new:
865 rm -rf /var/tmp/ ; mkdir /var/tmp ; chown root.root /var/tmp ; chmod 1777 /var/tmp
866
867 # Create empty utmp and wtmp
868 :> /var/run/utmp
869 :> /var/run/wtmp
870
871 # Make SURE that these are files, not links!
872 rm -rf /etc/ftpusers /etc/passwd /etc/shadow /etc/group \
873        /etc/ppp /etc/isdn /etc/ssh /etc/ioctl.save \
874        /etc/inittab /etc/network /etc/sudoers \
875        /etc/init /etc/localtime /etc/dhcpc /etc/pnm2ppa.conf 2>/dev/null
876 cp -a /GRML/etc/ftpusers /GRML/etc/passwd /GRML/etc/shadow /GRML/etc/group \
877       /GRML/etc/ppp /GRML/etc/isdn /GRML/etc/ssh \
878       /GRML/etc/inittab /GRML/etc/network /GRML/etc/sudoers \
879       /GRML/sbin/init /GRML/etc/dhcpc /etc/ 2>/dev/null
880
881 # Extremely important, init crashes on shutdown if this is only a link
882 :> /etc/ioctl.save
883 :> /etc/pnm2ppa.conf
884 # Must exist for samba to work
885 [ -d /var/lib/samba ] && :> /var/lib/samba/unexpected.tdb
886 # Diet libc bug workaround
887 # cp -f /GRML/etc/localtime /etc/localtime
888 # echo "${BLUE}Done.${NORMAL}"
889
890 # Now tell kernel where the real modprobe lives
891 echo "/sbin/modprobe" > /proc/sys/kernel/modprobe
892
893 # Change root device from /dev/fd0 to /dev/ram0
894 echo "0x100" > /proc/sys/kernel/real-root-dev
895
896 umount /sys # (remount in grml-autoconfig)
897
898 stage=4
899 rundebugshell
900 # Give control to the init process.
901 log_begin_msg "Starting init process."
902 [ -r /mountit ] && rm -f /mountit
903 rm -f /linuxrc
904 exit 0
905
906 else
907  echo "${CRE}${RED}Can't find grml filesystem, sorry.${NORMAL}"
908  echo "
909 Are you booting via USB or firewire?
910 ====================================
911 Try to boot with bootparam scandelay which delays the
912 bootup sequence so modules should have enough time
913 to initialize devices.
914
915 Usage examples on bootprompt of grml-iso:
916
917 grml scandelay       -> adds the default delay of 10 seconds
918 grml scandelay=13    -> adds a delay of 13 seconds
919
920 Are you booting via SCSI?
921 ====================================
922 Use the bootparam scsi.
923 Usage examples on bootprompt of grml-iso:
924
925 grml scsi=probe      -> autoprobing of scsi modules
926 grml scsi=ask        -> list modules and prompt for module which should be loaded
927 grml scsi=modulename -> loads specified module (without .ko extension)
928 expert               -> activate expert mode, similar to scsi=ask
929
930 Are you getting SquashFS/zlib errors?
931 =====================================
932 Try to boot with \"grml nodma\"
933
934 Still problems?
935 ===============
936 Make sure the ISO itself is ok.
937 Check the md5sum of downloaded ISO.
938 Used a CD-RW? Make sure the medium is ok!
939
940 Please report any problems you notice to the grml-team!
941 http://grml.org/contact/
942 "
943  echo "${RED}Now dropping you to the busybox shell.${NORMAL}"
944  echo "${RED}Press reset button to quit.${NORMAL}"
945  echo ""
946  PS1="grml# "
947  export PS1
948  echo "6" > /proc/sys/kernel/printk
949  # Allow signals
950  trap 1 2 3 15
951  exec /static/sh
952 fi
953 # EOF