Subversion Repositories livecd

Rev

Rev 1 | 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
{
229
   list_modules $2 | while read MODULE; do
230
      echo " -> `basename $MODULE`"
231
      union_insert_module $1 $MODULE $3
232
   done
233
}
234
 
235
# Copy modules to RAM directory
236
# $1 = data directory
237
# $2 = target directory in RAM
238
#
239
copy_to_ram()
240
{
241
   cp -R $1/* $2
242
   if [ "$?" -ne 0 ]; then fatal "can't copy to RAM, not enough memory?"; fi
243
}
244
 
245
# Copy content of "rootcopy" directory on the CD to $2 (union, usually)
246
# $1 = source
247
# $2 = destination
248
#
249
copy_rootchanges()
250
{
251
   cp -a $1/rootcopy/* $2 2>/dev/null # could be empty
252
}
253
 
254
# ===========================================================
255
# discovery functions
256
# ===========================================================
257
 
258
# List all CD-ROMs
259
# by using /proc entries
260
#
261
list_cdrom_devices()
262
{
263
   if [ "`cmdline_parameter nocd`" != "" ]; then return 1; fi
264
   for CDDEVICE in `cat /proc/sys/dev/cdrom/info | head -n 3 | tail -n 1 | cut -d ":" -f 2`; do
265
      echo "/dev/$CDDEVICE"
266
   done
267
}
268
 
269
# List all partition devices
270
# take list of all partitions and output unique disks.
271
# Return empty result when nohd parameter was given.
272
#
273
list_partition_devices()
274
{
275
   if [ "`cmdline_parameter nohd`" != "" ]; then return 1; fi
276
   cat /proc/partitions | grep -v loop | sed -r "s/^[0-9[:space:]]+/\/dev\//" | grep /dev/
277
}
278
 
279
# List all disk devices
280
#
281
list_disk_devices()
282
{
283
   list_partition_devices | egrep -v "[0-9]"
284
}
285
 
286
# List all block devices
287
#
288
list_block_devices()
289
{
290
   list_cdrom_devices
291
   list_partition_devices
292
}
293
 
294
# Try to mount all disks, partitions and cdroms and Find where the LiveCD is.
295
# If LiveCD found in the device, echo dirname of it's directory,
296
# and leave the device mounted. Mounting is not ro, but without any argument.
297
# $1 = directory where devices will be mounted
298
# added: mount $NFSROOT to /$1/nfs if NFSROOT is set. and search there for LiveCD
299
#
300
find_live_data_dir()
301
{
302
   if [ "$NFSROOT" != "" ]; then 
303
      DIR="/$1/nfs"
304
      mkdir -p $DIR
305
      mount -t nfs -o nolock,ro,rsize=8192,wsize=8192,hard,intr $NFSROOT $DIR
306
      FOUND=`ls -A1d $DIR/livecd.sgn $DIR/*/livecd.sgn 2>/dev/null | head -n 1`
307
      if [ "$FOUND" != "" ]; then 
308
         dirname "$FOUND"
309
      fi
310
   else
311
      list_block_devices | while read DEVICE; do
312
         DIR="/$1/`basename $DEVICE`"
313
         mount_device $DEVICE $DIR
314
         if [ $? -ne 0 ]; then continue; fi
315
         FOUND=`ls -A1d $DIR/livecd.sgn $DIR/*/livecd.sgn 2>/dev/null | head -n 1`
316
         if [ "$FOUND" = "" ]; then umount $DIR 2>/dev/null; rmdir $DIR 2>/dev/null
317
         else dirname "$FOUND"; return 1; fi
318
      done
319
   fi
320
}
321
 
322
# ===========================================================
323
# hardware preparation functions
324
# ===========================================================
325
 
326
# Create block devices to /dev described by /sys entries
327
#
328
create_block_devices()
329
{
330
   echolog "creating /dev entries for block devices"
331
   ls -A1d /sys/block/*/dev /sys/block/*/*/dev 2>/dev/null | grep -v loop | while read BLOCK; do
332
      DEVICE="/dev/`basename \`dirname $BLOCK\``"
333
      if [ ! -b $DEVICE ]; then
334
         MINORMAJOR="`head -n 1 $BLOCK | tr ':' ' '`"
335
         mknod $DEVICE b $MINORMAJOR
336
      fi
337
   done
338
}
339
 
340
# modprobe kernel modules needed for the LiveCD
341
#
342
modprobe_essential_modules()
343
{
344
   echolog "starting loop device support"
345
   modprobe_module loop max_loop=32
346
   echolog "starting cdrom support"
347
   modprobe_module ide_cd
348
   modprobe_module ide-cd
349
   modprobe_module sr_mod
350
   modprobe_module cdrom
351
   echolog "starting cdrom filesystem support"
352
   modprobe_module isofs
353
   echolog "starting squashfs support"
354
   modprobe_module squashfs
37 beyerle@PS 355
   echolog "starting unionfs/aufs support"
1 beyerle@PS 356
   modprobe_module unionfs
37 beyerle@PS 357
   modprobe_module aufs
1 beyerle@PS 358
   echolog "starting vfat support"
359
   modprobe_module nls_cp437
360
   modprobe_module nls_iso8859-1
361
   modprobe_module nls_iso8859-2
362
   modprobe_module vfat
363
   echolog "starting ntfs support"
364
   modprobe_module ntfs
365
   create_block_devices
366
}
367
 
368
 
369
# modprobe kernel modules needed for USB masstorage devices
370
#
371
modprobe_usb_sata_modules()
372
{
373
   echolog "starting USB and SATA support"
374
   modprobe_module ehci-hcd
375
   modprobe_module ohci-hcd
376
   modprobe_module uhci-hcd
377
   modprobe_module scsi_mod
378
 
379
   modprobe_module libata
380
   modprobe_module ahci
381
 
382
   modprobe_module sd_mod
383
   modprobe_module sr_mod
384
   modprobe_module usb-storage
385
   echolog "waiting for USB devices, max 9 seconds"
386
   sleep 9
387
   create_block_devices
388
}
389
 
390
# modprobe nfs kernel modules
391
#
392
modprobe_nfs_modules()
393
{
394
   echolog "starting nfs support"
395
   modprobe_module lockd
396
   modprobe_module fscache
397
   modprobe_module nfs_acl
398
   modprobe_module nfs
399
}
400
 
401
# enable/disable CD autoejecting when unmounted
402
# $1 = 1|0 ... enable|disable
403
#
404
cd_autoeject()
405
{
406
   echo $1 >/proc/sys/dev/cdrom/autoeject
407
}
408
 
409
# Disable DMA if nodma boot parameter is present
410
#
411
setup_dma()
412
{
413
   if [ ! "`cmdline_parameter nodma`" = "" ]; then
414
      for DEVICE in `list_cdrom_devices` `list_disk_devices`; do
415
         echolog "setting DMA support off for $DEVICE"
416
         hdparm -d 0 $DEVICE
417
      done
418
   fi
419
}
420
 
421
# create correct fstab file in $1/etc/fstab and create apropriate
422
# mount directories in $1
423
# $1 = root directory (union)
424
#
425
activate_fstab()
426
{
427
   mkdir -p $1/etc
428
   FSTAB="$1/etc/fstab"
429
   echo "tmpfs            /                tmpfs       defaults         0   0" >$FSTAB
430
   echo "devpts           /dev/pts         devpts      gid=5,mode=620   0   0" >>$FSTAB
431
   echo "proc             /proc            proc        defaults         0   0" >>$FSTAB
432
 
433
   # now done by fstab-sync
434
   # list_cdrom_devices | while read DEVICE; do
435
   #   MOUNTDIR="/mnt/`basename $DEVICE`_cdrom"
436
   #   mkdir -p $1/$MOUNTDIR
437
   #   echo "$DEVICE $MOUNTDIR iso9660 noauto,users,exec 0 0" >>$FSTAB
438
   # done
439
 
440
   # now done in runlast
441
   # # should we auto mount all found devices?
442
   # if [ "`cmdline_parameter automount`" ]; then
443
   #    MOUNTOPT="auto,users"
444
   # else
445
   #    MOUNTOPT="noauto,users,ro"
446
   # fi
447
 
448
   # list_partition_devices | while read DEVICE; do
449
   #    unset REMOVABLE; DEV="`basename $DEVICE`"; DEV0="`echo $DEV | cut -b 1-3`"
450
   #    if [ "`cat /sys/block/$DEV0/removable`" != "0" ]; then
451
   #      REMOVABLE="_removable"
452
   #   fi
453
 
454
   #   # skip this device if mountpoint exists
455
   #   MOUNTDIR="/mnt/$DEV$REMOVABLE"
456
   #   if [ -d "$1/$MOUNTDIR" ]; then continue; fi
457
 
458
   #   # try to mount the device and unmount it. If OK, we can add it to fstab
459
   #   mount_device "$DEVICE" "$1/$MOUNTDIR"
460
   #   umount "$1/$MOUNTDIR" 2>/dev/null
461
   #   if [ $? -eq 0 ]; then
462
   #      # noauto,nousers (default)
463
   #      echo "$DEVICE $MOUNTDIR auto $MOUNTOPT,suid,dev,exec 0 0" >>$FSTAB
464
   #   else # remove empty directory
465
   #      rmdir "$1/$MOUNTDIR" 2>/dev/null
466
   #   fi
467
   # done
468
 
469
   # search for SWAP done later in /etc/rc.sysinit
470
   # fdisk -l 2>/dev/null | grep -i "Linux swap" | egrep "^/dev/" \
471
   #   | cut -f 1 -d " " | sed -r "s/(.+)/\\1 swap swap defaults 0 0/" >>$FSTAB
472
 
473
   # now done by fstab-sync
474
   # echo "/dev/fd0 /mnt/floppy auto noauto,users,suid,dev,exec 0 0" >>$FSTAB
475
   # mkdir -p $1/mnt/floppy
476
}
37 beyerle@PS 477
 
478
# load network modules, if NFSROOT is set
479
# Urs Beyerle, PSI
480
load_network_modules()
481
{
482
    # mii maybe need by NIC
483
    echolog "load module mii"
484
    modprobe_module mii
485
 
486
    # detecting network card
487
    PCITABLE=/bin/pcitable
488
    NICS=`lspci -n | awk '/Class 0200/ {print $4}' | tr ':' ' ' \
489
	| while read x y ; do grep "0x$x.*0x$y" $PCITABLE \
490
	| awk '$3 !~ /"unknown"/ {print $3}' | tr -d '"' ; done`
491
    echolog "Found network card(s): $NICS"
492
 
493
    if [ -n "$NICS" ]; then
494
	echo $NICS | while read nic ; do
495
	    echo "Loading module $nic"
496
	    modprobe_module $nic
497
	done
498
    else
499
	echo "ERROR: No network card detected!"
500
	echo "Type in your network card driver (e.g. tg3, e1000). "
501
	echo "Or ask your system administrator for help."
502
	echo -n "Network card driver: "
503
	read nic
504
	echo "Loading module $nic"
505
	modprobe_module $nic
506
    fi
507
}
508
 
509
# get DHCP lease
510
# Urs Beyerle, PSI
511
get_dhcp_lease()
512
{
513
    # create /dev/urandom (needed by udhcpc)
514
    mknod /dev/urandom c 1 9
515
    echolog "Try to get DHCP lease on eth0"
516
    udhcpc --now --quit --interface=eth0 --script=/bin/udhcpc.script
517
    if [ $? -ne 0 ]; then
518
	echo "ERROR: couldn't get DHCP lease, trying again"
519
	udhcpc --now --quit --interface=eth0 --script=/bin/udhcpc.script
520
	if [ $? -ne 0 ]; then
521
	    echo "ERROR: couldn't get DHCP lease, trying eth1"
522
	    udhcpc --now --quit --interface=eth1 --script=/bin/udhcpc.script
523
	    if [ $? -ne 0 ]; then
524
		echo "ERROR: couldn't get DHCP lease, trying again eth1"
525
		udhcpc --now --quit --interface=eth1 --script=/bin/udhcpc.script
526
		if [ $? -ne 0 ]; then
527
		    echo "ERROR: can't get DHCP lease on eth0 and eth1"
528
		fi
529
	    fi
530
	fi
531
    fi
532
}
533