Subversion Repositories livecd

Rev

Rev 37 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 beyerle@PS 1
#!/bin/ash
2
#
3
# Functions library :: for Linux Live scripts 5.x.y
4
# Author: Tomas M. <http://www.linux-live.org>
5
#
6
# modified by Urs Beyerle, PSI
7
# - to allow LiveCD mounted over nfs
8
# - add scsi_mod, sd_mod for usb-storage module
9
# - only with boot option "automount", all devices in fstab are rw automounted
10
# - remove detect of CD and Floppy (done by fstab-sync)
11
# - add sr_mod (USB CDROM support)
12
# - add SATA to modprobe_usb_modules -> modprobe_usb_sata_modules
13
# - add fscache (for SL5) 
14
# - add ide-cd, sr_mod, cdrom (for SL5 cdrom support)
37 beyerle@PS 15
# - add aufs (unionfs replacement)
16
# - to allow LiveCD mounted over NFS (for diskless client)
1 beyerle@PS 17
#
18
 
19
# ===========================================================
20
# user interface functions
21
# ===========================================================
22
 
23
# echolog
24
# $1 = text to show and to write to /var/log/messages
25
#
26
echolog()
27
{
28
   echo "LIVECD:" "$@" >>/var/log/livedbg
29
   echo "$@"
30
}
31
 
32
# debug
33
# commands executed when debug boot parameter is present
34
#
35
debug()
36
{
37
   echo
38
   echo "====="
39
   echo ": Debugging started. Here is the root shell for you."
40
   echo ": Type your desired command or hit Ctrl+D to continue booting."
41
   echo
42
   ash
43
}
44
 
45
# header
46
# $1 = text to show
47
#
48
header()
49
{
50
   echolog "$1"
51
}
52
 
53
fatal()
54
{
55
   header "Fatal error occured - $1"
56
   echolog "Something went wrong and we can't continue booting :("
57
   echolog "You may explore the system by using simple commands like ls, lsmod, mount, etc."
58
   echolog "You may also try to hit Ctrl+D. Booting will continue. Use at your own risk."
59
   echolog "To be safe, hit Ctrl+Alt+Delete to reboot."
60
   echolog
61
   ash
62
}
63
 
64
# ===========================================================
65
# text processing functions
66
# ===========================================================
67
 
68
# egrep_o is a replacement for "egrep -o". It prints only the last
69
# matching text
70
# $1 = regular expression
71
#
72
egrep_o()
73
{
74
   cat | egrep "$1" | sed -r "s/.*($1).*/\\1/"
75
}
76
 
77
# look into cmdline and echo $1 back if $1 is set
78
# $1 = value name, case sensitive, for example livecd_subdir
79
# $2 = file to use instead /proc/cmdline, optional
80
#
81
cmdline_parameter()
82
{
83
   CMDLINE=/proc/cmdline
84
   if [ "$2" != "" ]; then CMDLINE="$2"; fi
85
   cat "$CMDLINE" | egrep_o "(^|[[:space:]]+)$1(\$|=|[[:space:]]+)" | egrep_o "$1"
86
}
87
 
88
# look into cmdline and echo value of $1 option
89
# $1 = value name, case sensitive, for example livecd_subdir
90
# $2 = file to use instead /proc/cmdline, optional
91
#
92
cmdline_value()
93
{
94
   CMDLINE=/proc/cmdline
95
   if [ "$2" != "" ]; then CMDLINE="$2"; fi
96
   cat "$CMDLINE" | egrep_o "(^|[[:space:]]+)$1=([^[:space:]]+)" | egrep_o "=.*" | cut -b 2- | tail -n 1
97
}
98
 
99
# ===========================================================
100
# system functions
101
# ===========================================================
102
 
103
# modprobe module $1, including all dependencies, suppress all messages
104
# (own function because modprobe in busybox doesn't work with gzipped modules)
105
# $1 = module name, eg. ehci-hcd
106
# $2 = optional argument
107
#
108
modprobe_module()
109
{
110
  if [ "$1" = "" ]; then return 1; fi
111
  PRINTK=`cat /proc/sys/kernel/printk`
112
  echo "0" >/proc/sys/kernel/printk
113
 
114
  KERNEL="`uname -r`"; LSMOD=/tmp/lsmod
115
  MODULEDEPS="`cat /lib/modules/$KERNEL/modules.dep | egrep \"$1\\.ko(\\.gz)?:\"`"
116
 
117
  for MODULE in `echo $MODULEDEPS | cut -d ":" -f 2-` `echo $MODULEDEPS | cut -d ":" -f 1`; do
118
     MODULE=${MODULE%%:};   # remove : at the end, a bug 
119
     TMPMOD="/tmp/`basename $MODULE .gz`";
120
     # if the module is not loaded already
121
     if [ "`cat $LSMOD 2>/dev/null | egrep \"^$TMPMOD\\\$\"`" = "" ]; then
122
        gunzip -c $MODULE 2>/dev/null >$TMPMOD
123
        if [ "$?" -ne 0 ]; then cp $MODULE $TMPMOD; fi # can't gunzip? copy
124
        insmod $TMPMOD $2; err=$?
125
        ### insmod $TMPMOD $2 >/dev/null 2>/dev/null; err=$?
126
        if [ "$err" -eq 0 ]; then echo $TMPMOD >>$LSMOD; fi # module log
127
        rm $TMPMOD
128
     fi
129
  done
130
 
131
  echo "$PRINTK" >/proc/sys/kernel/printk
132
  if [ "$err" -ne 0 ]; then echolog "error inserting module $1 ($err)"; fi
133
  return $err
134
}
135
 
136
# Mount device $1 to $2
137
# $1 = /dev device to mount, eg. /dev/hda1
138
# $2 = mountpoint, eg. /mnt/hda1
139
# $3 = mount options, for example "loop", "ro", or "remount,rw"
140
#
141
mount_device()
142
{
143
  mkdir -p $2
144
  if [ "$3" != "" ]; then OPTIONS="-o $3"; else OPTIONS=""; fi
145
 
146
  PRINTK=`cat /proc/sys/kernel/printk`
147
  echo "0" >/proc/sys/kernel/printk
148
 
149
  mount -t auto $1 $2 $OPTIONS >/dev/null 2>/dev/null
150
  err=$?
151
 
152
  if [ "$err" -ne 0 ]; then rmdir $2 2>/dev/null; fi
153
  echo "$PRINTK" >/proc/sys/kernel/printk
154
  return $err
155
}
156
 
157
# ===========================================================
158
# live module functions
159
# ===========================================================
160
 
161
# Create module
162
# call mksquashfs with apropriate arguments
163
# $1 = directory which will be compressed to squashfs module
164
# $2 = output .mo file
165
# $3 = optional -keep-as-directory argument
166
#
167
create_module()
168
{
169
   mksquashfs $1 $2 $3 >/dev/null
170
   if [ $? -ne 0 ]; then return 1; fi
171
   chmod oga-x $2 # remove execute attrib
172
}
173
 
174
# Mount .mo module to destination directory
175
# $1 = path to .mo livecd compressed module
176
# $2 = destination folder
177
#
178
mount_module()
179
{
180
   mount -t squashfs -o loop,ro $1 $2
181
}
182
 
183
# Insert a directory tree $2 to an union specified by $1
184
# Top-level read-write branch is specified by it's index 0
185
# $1 = union absolute path (starting with /)
186
# $2 = path to data directory
187
#
188
union_insert_dir()
189
{
37 beyerle@PS 190
   unionctl "$1" --add --after 0 --mode ro "$2"
1 beyerle@PS 191
}
192
 
193
# List all modules in all directories (base, modules, optional)
194
# and filter out unneeded optional modules (not specified by load= kernel parameter)
195
# $1 = root directory of mounted DATAdir
196
#
197
list_modules()
198
{
199
   LOAD="`cmdline_value load`"
200
   ls -A1d $1/*.mo $1/*/*.mo 2>/dev/null | while read LINE; do
201
      MODNAME="`basename $LINE .mo`"
202
      if [ "$LOAD" != "*" -a "`echo $LINE | grep optional`" != "" -a "`echo $LOAD | egrep \"(^|,)$MODNAME(\\\$|,)\"`" = "" ]; then continue
203
        else echo $LINE; fi
204
   done
205
}
206
 
207
# Insert one single .mo module to the union
208
# $1 = union absolute path (starting with /)
209
# $2 = module.mo full path
210
# $3 = destination folder, where images will be mounted to
211
#
212
union_insert_module()
213
{
214
   TARGET="$3/`basename $2`"
215
   while [ -e $TARGET ]; do TARGET=$TARGET.X; done
216
   mkdir -p $TARGET
217
   mount_module $2 $TARGET
218
   if [ $? -ne 0 ]; then echo "can't read module data"; return 1; fi
219
   union_insert_dir $1 $TARGET; 
220
}
221
 
222
# Insert all .mo modules, in $2 directory and subdirectories, to the union
223
# $1 = union absolute path (starting with /)
224
# $2 = LiveCD data dir (with directories /base, /modules, etc.)
225
# $3 = destination folder, where images will be mounted to
226
#
227
union_insert_modules()
228
{
38 beyerle@PS 229
   PRINTK=`cat /proc/sys/kernel/printk`
230
   echo "0" >/proc/sys/kernel/printk
231
 
1 beyerle@PS 232
   list_modules $2 | while read MODULE; do
233
      echo " -> `basename $MODULE`"
234
      union_insert_module $1 $MODULE $3
235
   done
38 beyerle@PS 236
 
237
   echo "$PRINTK" >/proc/sys/kernel/printk
1 beyerle@PS 238
}
239
 
240
# Copy modules to RAM directory
241
# $1 = data directory
242
# $2 = target directory in RAM
243
#
244
copy_to_ram()
245
{
246
   cp -R $1/* $2
247
   if [ "$?" -ne 0 ]; then fatal "can't copy to RAM, not enough memory?"; fi
248
}
249
 
250
# Copy content of "rootcopy" directory on the CD to $2 (union, usually)
251
# $1 = source
252
# $2 = destination
253
#
254
copy_rootchanges()
255
{
256
   cp -a $1/rootcopy/* $2 2>/dev/null # could be empty
257
}
258
 
259
# ===========================================================
260
# discovery functions
261
# ===========================================================
262
 
263
# List all CD-ROMs
264
# by using /proc entries
265
#
266
list_cdrom_devices()
267
{
268
   if [ "`cmdline_parameter nocd`" != "" ]; then return 1; fi
269
   for CDDEVICE in `cat /proc/sys/dev/cdrom/info | head -n 3 | tail -n 1 | cut -d ":" -f 2`; do
270
      echo "/dev/$CDDEVICE"
271
   done
272
}
273
 
274
# List all partition devices
275
# take list of all partitions and output unique disks.
276
# Return empty result when nohd parameter was given.
277
#
278
list_partition_devices()
279
{
280
   if [ "`cmdline_parameter nohd`" != "" ]; then return 1; fi
281
   cat /proc/partitions | grep -v loop | sed -r "s/^[0-9[:space:]]+/\/dev\//" | grep /dev/
282
}
283
 
284
# List all disk devices
285
#
286
list_disk_devices()
287
{
288
   list_partition_devices | egrep -v "[0-9]"
289
}
290
 
291
# List all block devices
292
#
293
list_block_devices()
294
{
295
   list_cdrom_devices
296
   list_partition_devices
297
}
298
 
299
# Try to mount all disks, partitions and cdroms and Find where the LiveCD is.
300
# If LiveCD found in the device, echo dirname of it's directory,
301
# and leave the device mounted. Mounting is not ro, but without any argument.
302
# $1 = directory where devices will be mounted
303
# added: mount $NFSROOT to /$1/nfs if NFSROOT is set. and search there for LiveCD
304
#
305
find_live_data_dir()
306
{
307
   if [ "$NFSROOT" != "" ]; then 
308
      DIR="/$1/nfs"
309
      mkdir -p $DIR
310
      mount -t nfs -o nolock,ro,rsize=8192,wsize=8192,hard,intr $NFSROOT $DIR
311
      FOUND=`ls -A1d $DIR/livecd.sgn $DIR/*/livecd.sgn 2>/dev/null | head -n 1`
312
      if [ "$FOUND" != "" ]; then 
313
         dirname "$FOUND"
314
      fi
315
   else
316
      list_block_devices | while read DEVICE; do
317
         DIR="/$1/`basename $DEVICE`"
318
         mount_device $DEVICE $DIR
319
         if [ $? -ne 0 ]; then continue; fi
320
         FOUND=`ls -A1d $DIR/livecd.sgn $DIR/*/livecd.sgn 2>/dev/null | head -n 1`
321
         if [ "$FOUND" = "" ]; then umount $DIR 2>/dev/null; rmdir $DIR 2>/dev/null
322
         else dirname "$FOUND"; return 1; fi
323
      done
324
   fi
325
}
326
 
327
# ===========================================================
328
# hardware preparation functions
329
# ===========================================================
330
 
331
# Create block devices to /dev described by /sys entries
332
#
333
create_block_devices()
334
{
335
   echolog "creating /dev entries for block devices"
336
   ls -A1d /sys/block/*/dev /sys/block/*/*/dev 2>/dev/null | grep -v loop | while read BLOCK; do
337
      DEVICE="/dev/`basename \`dirname $BLOCK\``"
338
      if [ ! -b $DEVICE ]; then
339
         MINORMAJOR="`head -n 1 $BLOCK | tr ':' ' '`"
340
         mknod $DEVICE b $MINORMAJOR
341
      fi
342
   done
343
}
344
 
345
# modprobe kernel modules needed for the LiveCD
346
#
347
modprobe_essential_modules()
348
{
349
   echolog "starting loop device support"
350
   modprobe_module loop max_loop=32
351
   echolog "starting cdrom support"
352
   modprobe_module ide_cd
353
   modprobe_module ide-cd
354
   modprobe_module sr_mod
355
   modprobe_module cdrom
356
   echolog "starting cdrom filesystem support"
357
   modprobe_module isofs
358
   echolog "starting squashfs support"
359
   modprobe_module squashfs
37 beyerle@PS 360
   echolog "starting unionfs/aufs support"
1 beyerle@PS 361
   modprobe_module unionfs
37 beyerle@PS 362
   modprobe_module aufs
1 beyerle@PS 363
   echolog "starting vfat support"
364
   modprobe_module nls_cp437
365
   modprobe_module nls_iso8859-1
366
   modprobe_module nls_iso8859-2
367
   modprobe_module vfat
368
   echolog "starting ntfs support"
369
   modprobe_module ntfs
370
   create_block_devices
371
}
372
 
373
 
374
# modprobe kernel modules needed for USB masstorage devices
375
#
376
modprobe_usb_sata_modules()
377
{
378
   echolog "starting USB and SATA support"
379
   modprobe_module ehci-hcd
380
   modprobe_module ohci-hcd
381
   modprobe_module uhci-hcd
382
   modprobe_module scsi_mod
383
 
384
   modprobe_module libata
385
   modprobe_module ahci
386
 
387
   modprobe_module sd_mod
388
   modprobe_module sr_mod
389
   modprobe_module usb-storage
390
   echolog "waiting for USB devices, max 9 seconds"
391
   sleep 9
392
   create_block_devices
393
}
394
 
395
# modprobe nfs kernel modules
396
#
397
modprobe_nfs_modules()
398
{
399
   echolog "starting nfs support"
400
   modprobe_module lockd
401
   modprobe_module fscache
402
   modprobe_module nfs_acl
403
   modprobe_module nfs
404
}
405
 
406
# enable/disable CD autoejecting when unmounted
407
# $1 = 1|0 ... enable|disable
408
#
409
cd_autoeject()
410
{
411
   echo $1 >/proc/sys/dev/cdrom/autoeject
412
}
413
 
414
# Disable DMA if nodma boot parameter is present
415
#
416
setup_dma()
417
{
418
   if [ ! "`cmdline_parameter nodma`" = "" ]; then
419
      for DEVICE in `list_cdrom_devices` `list_disk_devices`; do
420
         echolog "setting DMA support off for $DEVICE"
421
         hdparm -d 0 $DEVICE
422
      done
423
   fi
424
}
425
 
426
# create correct fstab file in $1/etc/fstab and create apropriate
427
# mount directories in $1
428
# $1 = root directory (union)
429
#
430
activate_fstab()
431
{
432
   mkdir -p $1/etc
433
   FSTAB="$1/etc/fstab"
434
   echo "tmpfs            /                tmpfs       defaults         0   0" >$FSTAB
435
   echo "devpts           /dev/pts         devpts      gid=5,mode=620   0   0" >>$FSTAB
436
   echo "proc             /proc            proc        defaults         0   0" >>$FSTAB
437
 
438
   # now done by fstab-sync
439
   # list_cdrom_devices | while read DEVICE; do
440
   #   MOUNTDIR="/mnt/`basename $DEVICE`_cdrom"
441
   #   mkdir -p $1/$MOUNTDIR
442
   #   echo "$DEVICE $MOUNTDIR iso9660 noauto,users,exec 0 0" >>$FSTAB
443
   # done
444
 
445
   # now done in runlast
446
   # # should we auto mount all found devices?
447
   # if [ "`cmdline_parameter automount`" ]; then
448
   #    MOUNTOPT="auto,users"
449
   # else
450
   #    MOUNTOPT="noauto,users,ro"
451
   # fi
452
 
453
   # list_partition_devices | while read DEVICE; do
454
   #    unset REMOVABLE; DEV="`basename $DEVICE`"; DEV0="`echo $DEV | cut -b 1-3`"
455
   #    if [ "`cat /sys/block/$DEV0/removable`" != "0" ]; then
456
   #      REMOVABLE="_removable"
457
   #   fi
458
 
459
   #   # skip this device if mountpoint exists
460
   #   MOUNTDIR="/mnt/$DEV$REMOVABLE"
461
   #   if [ -d "$1/$MOUNTDIR" ]; then continue; fi
462
 
463
   #   # try to mount the device and unmount it. If OK, we can add it to fstab
464
   #   mount_device "$DEVICE" "$1/$MOUNTDIR"
465
   #   umount "$1/$MOUNTDIR" 2>/dev/null
466
   #   if [ $? -eq 0 ]; then
467
   #      # noauto,nousers (default)
468
   #      echo "$DEVICE $MOUNTDIR auto $MOUNTOPT,suid,dev,exec 0 0" >>$FSTAB
469
   #   else # remove empty directory
470
   #      rmdir "$1/$MOUNTDIR" 2>/dev/null
471
   #   fi
472
   # done
473
 
474
   # search for SWAP done later in /etc/rc.sysinit
475
   # fdisk -l 2>/dev/null | grep -i "Linux swap" | egrep "^/dev/" \
476
   #   | cut -f 1 -d " " | sed -r "s/(.+)/\\1 swap swap defaults 0 0/" >>$FSTAB
477
 
478
   # now done by fstab-sync
479
   # echo "/dev/fd0 /mnt/floppy auto noauto,users,suid,dev,exec 0 0" >>$FSTAB
480
   # mkdir -p $1/mnt/floppy
481
}
37 beyerle@PS 482
 
483
# load network modules, if NFSROOT is set
484
# Urs Beyerle, PSI
485
load_network_modules()
486
{
487
    # mii maybe need by NIC
488
    echolog "load module mii"
489
    modprobe_module mii
490
 
491
    # detecting network card
492
    PCITABLE=/bin/pcitable
493
    NICS=`lspci -n | awk '/Class 0200/ {print $4}' | tr ':' ' ' \
494
	| while read x y ; do grep "0x$x.*0x$y" $PCITABLE \
495
	| awk '$3 !~ /"unknown"/ {print $3}' | tr -d '"' ; done`
496
    echolog "Found network card(s): $NICS"
497
 
498
    if [ -n "$NICS" ]; then
499
	echo $NICS | while read nic ; do
500
	    echo "Loading module $nic"
501
	    modprobe_module $nic
502
	done
503
    else
504
	echo "ERROR: No network card detected!"
505
	echo "Type in your network card driver (e.g. tg3, e1000). "
506
	echo "Or ask your system administrator for help."
507
	echo -n "Network card driver: "
508
	read nic
509
	echo "Loading module $nic"
510
	modprobe_module $nic
511
    fi
512
}
513
 
514
# get DHCP lease
515
# Urs Beyerle, PSI
516
get_dhcp_lease()
517
{
518
    # create /dev/urandom (needed by udhcpc)
519
    mknod /dev/urandom c 1 9
520
    echolog "Try to get DHCP lease on eth0"
521
    udhcpc --now --quit --interface=eth0 --script=/bin/udhcpc.script
522
    if [ $? -ne 0 ]; then
523
	echo "ERROR: couldn't get DHCP lease, trying again"
524
	udhcpc --now --quit --interface=eth0 --script=/bin/udhcpc.script
525
	if [ $? -ne 0 ]; then
526
	    echo "ERROR: couldn't get DHCP lease, trying eth1"
527
	    udhcpc --now --quit --interface=eth1 --script=/bin/udhcpc.script
528
	    if [ $? -ne 0 ]; then
529
		echo "ERROR: couldn't get DHCP lease, trying again eth1"
530
		udhcpc --now --quit --interface=eth1 --script=/bin/udhcpc.script
531
		if [ $? -ne 0 ]; then
532
		    echo "ERROR: can't get DHCP lease on eth0 and eth1"
533
		fi
534
	    fi
535
	fi
536
    fi
537
}
538