Subversion Repositories livecd

Rev

Details | 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
#
169 beyerle@PS 6
# modified by Urs Beyerle
1 beyerle@PS 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)
48 beyerle@PS 17
# - add functions get_dhcp_lease() and load_network_modules()
85 beyerle@PS 18
# - add ata_piix to modprobe_usb_sata_modules
100 beyerle@PS 19
# - works with unionfs 2.x
20
# - better detection of network card in case of diskless client
126 beyerle@PS 21
# - add sg, sata_nv module
160 beyerle@PS 22
# - to allow LiveCD mounted over CIFS (for diskless client)
170 beyerle@PS 23
# - add function find_changes
205 beyerleu 24
# - add sata_svw
241 beyerleu 25
# - add $DHCPDELAY parameter
248 beyerleu 26
# - add $USBDELAY parameter
1 beyerle@PS 27
#
28
 
29
# ===========================================================
30
# user interface functions
31
# ===========================================================
32
 
33
# echolog
34
# $1 = text to show and to write to /var/log/messages
35
#
36
echolog()
37
{
38
   echo "LIVECD:" "$@" >>/var/log/livedbg
39
   echo "$@"
40
}
41
 
42
# debug
43
# commands executed when debug boot parameter is present
44
#
45
debug()
46
{
47
   echo
48
   echo "====="
49
   echo ": Debugging started. Here is the root shell for you."
50
   echo ": Type your desired command or hit Ctrl+D to continue booting."
51
   echo
52
   ash
53
}
54
 
55
# header
56
# $1 = text to show
57
#
58
header()
59
{
60
   echolog "$1"
61
}
62
 
63
fatal()
64
{
65
   header "Fatal error occured - $1"
66
   echolog "Something went wrong and we can't continue booting :("
67
   echolog "You may explore the system by using simple commands like ls, lsmod, mount, etc."
68
   echolog "You may also try to hit Ctrl+D. Booting will continue. Use at your own risk."
69
   echolog "To be safe, hit Ctrl+Alt+Delete to reboot."
70
   echolog
71
   ash
72
}
73
 
74
# ===========================================================
75
# text processing functions
76
# ===========================================================
77
 
78
# egrep_o is a replacement for "egrep -o". It prints only the last
79
# matching text
80
# $1 = regular expression
81
#
82
egrep_o()
83
{
84
   cat | egrep "$1" | sed -r "s/.*($1).*/\\1/"
85
}
86
 
87
# look into cmdline and echo $1 back if $1 is set
88
# $1 = value name, case sensitive, for example livecd_subdir
89
# $2 = file to use instead /proc/cmdline, optional
90
#
91
cmdline_parameter()
92
{
93
   CMDLINE=/proc/cmdline
94
   if [ "$2" != "" ]; then CMDLINE="$2"; fi
95
   cat "$CMDLINE" | egrep_o "(^|[[:space:]]+)$1(\$|=|[[:space:]]+)" | egrep_o "$1"
96
}
97
 
98
# look into cmdline and echo value of $1 option
99
# $1 = value name, case sensitive, for example livecd_subdir
100
# $2 = file to use instead /proc/cmdline, optional
101
#
102
cmdline_value()
103
{
104
   CMDLINE=/proc/cmdline
105
   if [ "$2" != "" ]; then CMDLINE="$2"; fi
106
   cat "$CMDLINE" | egrep_o "(^|[[:space:]]+)$1=([^[:space:]]+)" | egrep_o "=.*" | cut -b 2- | tail -n 1
107
}
108
 
109
# ===========================================================
110
# system functions
111
# ===========================================================
112
 
113
# modprobe module $1, including all dependencies, suppress all messages
114
# (own function because modprobe in busybox doesn't work with gzipped modules)
115
# $1 = module name, eg. ehci-hcd
116
# $2 = optional argument
117
#
118
modprobe_module()
119
{
120
  if [ "$1" = "" ]; then return 1; fi
121
  PRINTK=`cat /proc/sys/kernel/printk`
122
  echo "0" >/proc/sys/kernel/printk
123
 
124
  KERNEL="`uname -r`"; LSMOD=/tmp/lsmod
125
  MODULEDEPS="`cat /lib/modules/$KERNEL/modules.dep | egrep \"$1\\.ko(\\.gz)?:\"`"
126
 
127
  for MODULE in `echo $MODULEDEPS | cut -d ":" -f 2-` `echo $MODULEDEPS | cut -d ":" -f 1`; do
128
     MODULE=${MODULE%%:};   # remove : at the end, a bug 
129
     TMPMOD="/tmp/`basename $MODULE .gz`";
130
     # if the module is not loaded already
131
     if [ "`cat $LSMOD 2>/dev/null | egrep \"^$TMPMOD\\\$\"`" = "" ]; then
132
        gunzip -c $MODULE 2>/dev/null >$TMPMOD
100 beyerle@PS 133
        if [ $? -ne 0 ]; then cp $MODULE $TMPMOD; fi # can't gunzip? copy
1 beyerle@PS 134
        insmod $TMPMOD $2; err=$?
135
        ### insmod $TMPMOD $2 >/dev/null 2>/dev/null; err=$?
136
        if [ "$err" -eq 0 ]; then echo $TMPMOD >>$LSMOD; fi # module log
137
        rm $TMPMOD
138
     fi
139
  done
140
 
141
  echo "$PRINTK" >/proc/sys/kernel/printk
142
  if [ "$err" -ne 0 ]; then echolog "error inserting module $1 ($err)"; fi
143
  return $err
144
}
145
 
146
# Mount device $1 to $2
147
# $1 = /dev device to mount, eg. /dev/hda1
148
# $2 = mountpoint, eg. /mnt/hda1
149
# $3 = mount options, for example "loop", "ro", or "remount,rw"
150
#
151
mount_device()
152
{
153
  mkdir -p $2
154
  if [ "$3" != "" ]; then OPTIONS="-o $3"; else OPTIONS=""; fi
155
 
156
  PRINTK=`cat /proc/sys/kernel/printk`
157
  echo "0" >/proc/sys/kernel/printk
158
 
159
  mount -t auto $1 $2 $OPTIONS >/dev/null 2>/dev/null
160
  err=$?
161
 
162
  if [ "$err" -ne 0 ]; then rmdir $2 2>/dev/null; fi
163
  echo "$PRINTK" >/proc/sys/kernel/printk
164
  return $err
165
}
166
 
167
# ===========================================================
168
# live module functions
169
# ===========================================================
170
 
171
# Create module
172
# call mksquashfs with apropriate arguments
173
# $1 = directory which will be compressed to squashfs module
174
# $2 = output .mo file
175
# $3 = optional -keep-as-directory argument
176
#
177
create_module()
178
{
179
   mksquashfs $1 $2 $3 >/dev/null
180
   if [ $? -ne 0 ]; then return 1; fi
181
   chmod oga-x $2 # remove execute attrib
182
}
183
 
184
# Mount .mo module to destination directory
185
# $1 = path to .mo livecd compressed module
186
# $2 = destination folder
187
#
188
mount_module()
189
{
190
   mount -t squashfs -o loop,ro $1 $2
191
}
192
 
193
# Insert a directory tree $2 to an union specified by $1
194
# Top-level read-write branch is specified by it's index 0
195
# $1 = union absolute path (starting with /)
196
# $2 = path to data directory
197
#
198
union_insert_dir()
199
{
100 beyerle@PS 200
   which unionctl >/dev/null 2>&1
201
   if [ $? -eq 0 ]; then
202
       # unionfs 1.x or aufs
203
       unionctl "$1" --add --after 0 --mode ro "$2"
204
   else
205
       # unionfs 2.x
206
       mount -t unionfs -o remount,add=:${2}=ro none "$1"
207
   fi
1 beyerle@PS 208
}
209
 
210
# List all modules in all directories (base, modules, optional)
211
# and filter out unneeded optional modules (not specified by load= kernel parameter)
212
# $1 = root directory of mounted DATAdir
213
#
214
list_modules()
215
{
216
   LOAD="`cmdline_value load`"
217
   ls -A1d $1/*.mo $1/*/*.mo 2>/dev/null | while read LINE; do
218
      MODNAME="`basename $LINE .mo`"
219
      if [ "$LOAD" != "*" -a "`echo $LINE | grep optional`" != "" -a "`echo $LOAD | egrep \"(^|,)$MODNAME(\\\$|,)\"`" = "" ]; then continue
220
        else echo $LINE; fi
221
   done
222
}
223
 
224
# Insert one single .mo module to the union
225
# $1 = union absolute path (starting with /)
226
# $2 = module.mo full path
227
# $3 = destination folder, where images will be mounted to
228
#
229
union_insert_module()
230
{
231
   TARGET="$3/`basename $2`"
232
   while [ -e $TARGET ]; do TARGET=$TARGET.X; done
233
   mkdir -p $TARGET
234
   mount_module $2 $TARGET
235
   if [ $? -ne 0 ]; then echo "can't read module data"; return 1; fi
236
   union_insert_dir $1 $TARGET; 
237
}
238
 
239
# Insert all .mo modules, in $2 directory and subdirectories, to the union
240
# $1 = union absolute path (starting with /)
241
# $2 = LiveCD data dir (with directories /base, /modules, etc.)
242
# $3 = destination folder, where images will be mounted to
243
#
244
union_insert_modules()
245
{
38 beyerle@PS 246
   PRINTK=`cat /proc/sys/kernel/printk`
247
   echo "0" >/proc/sys/kernel/printk
248
 
1 beyerle@PS 249
   list_modules $2 | while read MODULE; do
250
      echo " -> `basename $MODULE`"
251
      union_insert_module $1 $MODULE $3
252
   done
38 beyerle@PS 253
 
254
   echo "$PRINTK" >/proc/sys/kernel/printk
1 beyerle@PS 255
}
256
 
257
# Copy modules to RAM directory
258
# $1 = data directory
259
# $2 = target directory in RAM
260
#
261
copy_to_ram()
262
{
263
   cp -R $1/* $2
100 beyerle@PS 264
   if [ $? -ne 0 ]; then fatal "can't copy to RAM, not enough memory?"; fi
1 beyerle@PS 265
}
266
 
267
# Copy content of "rootcopy" directory on the CD to $2 (union, usually)
268
# $1 = source
269
# $2 = destination
270
#
271
copy_rootchanges()
272
{
273
   cp -a $1/rootcopy/* $2 2>/dev/null # could be empty
274
}
275
 
276
# ===========================================================
277
# discovery functions
278
# ===========================================================
279
 
280
# List all CD-ROMs
281
# by using /proc entries
282
#
283
list_cdrom_devices()
284
{
285
   if [ "`cmdline_parameter nocd`" != "" ]; then return 1; fi
286
   for CDDEVICE in `cat /proc/sys/dev/cdrom/info | head -n 3 | tail -n 1 | cut -d ":" -f 2`; do
287
      echo "/dev/$CDDEVICE"
288
   done
289
}
290
 
291
# List all partition devices
292
# take list of all partitions and output unique disks.
293
# Return empty result when nohd parameter was given.
294
#
295
list_partition_devices()
296
{
297
   if [ "`cmdline_parameter nohd`" != "" ]; then return 1; fi
298
   cat /proc/partitions | grep -v loop | sed -r "s/^[0-9[:space:]]+/\/dev\//" | grep /dev/
299
}
300
 
301
# List all disk devices
302
#
303
list_disk_devices()
304
{
305
   list_partition_devices | egrep -v "[0-9]"
306
}
307
 
308
# List all block devices
309
#
310
list_block_devices()
311
{
312
   list_cdrom_devices
313
   list_partition_devices
314
}
315
 
316
# Try to mount all disks, partitions and cdroms and Find where the LiveCD is.
317
# If LiveCD found in the device, echo dirname of it's directory,
318
# and leave the device mounted. Mounting is not ro, but without any argument.
319
# $1 = directory where devices will be mounted
320
# added: mount $NFSROOT to /$1/nfs if NFSROOT is set. and search there for LiveCD
321
#
322
find_live_data_dir()
323
{
324
   if [ "$NFSROOT" != "" ]; then 
325
      DIR="/$1/nfs"
326
      mkdir -p $DIR
108 beyerle@PS 327
      # try to mount nfs dir
185 beyerle@PS 328
      mount -t nfs $NFSROOT $DIR -o $NFSOPTS 
162 beyerle@PS 329
   fi
108 beyerle@PS 330
 
162 beyerle@PS 331
   if [ "$CIFSROOT" != "" ]; then
160 beyerle@PS 332
      DIR="/$1/cifs"
333
      mkdir -p $DIR
334
      # try to mount cifs dir
161 beyerle@PS 335
      mount -t cifs $CIFSROOT $DIR -o ro,$CIFSOPTS
162 beyerle@PS 336
   fi
160 beyerle@PS 337
 
162 beyerle@PS 338
   if [ "$DIR" = "" ]; then
339
       list_block_devices | while read DEVICE; do
340
	   DIR="/$1/`basename $DEVICE`"
341
	   mount_device $DEVICE $DIR
342
	   if [ $? -ne 0 ]; then continue; fi
167 beyerle@PS 343
	   # FOUND=`find $DIR -name livecd.sgn -type f | head -n 1`
344
	   FOUND=`ls -A1d $DIR/livecd.sgn $DIR/*/livecd.sgn 2>/dev/null | head -n 1`
162 beyerle@PS 345
	   if [ "$FOUND" = "" ]; then umount $DIR 2>/dev/null; rmdir $DIR 2>/dev/null
346
	   else dirname "$FOUND"; return 1; fi
347
       done
1 beyerle@PS 348
   else
167 beyerle@PS 349
       # FOUND=`find $DIR -name livecd.sgn -type f | head -n 1`
350
       FOUND=`ls -A1d $DIR/livecd.sgn $DIR/*/livecd.sgn 2>/dev/null | head -n 1`
162 beyerle@PS 351
       if [ "$FOUND" != "" ]; then 
352
	   dirname "$FOUND"
353
       fi
1 beyerle@PS 354
   fi
355
}
356
 
357
# ===========================================================
358
# hardware preparation functions
359
# ===========================================================
360
 
361
# Create block devices to /dev described by /sys entries
362
#
363
create_block_devices()
364
{
365
   echolog "creating /dev entries for block devices"
366
   ls -A1d /sys/block/*/dev /sys/block/*/*/dev 2>/dev/null | grep -v loop | while read BLOCK; do
367
      DEVICE="/dev/`basename \`dirname $BLOCK\``"
368
      if [ ! -b $DEVICE ]; then
369
         MINORMAJOR="`head -n 1 $BLOCK | tr ':' ' '`"
370
         mknod $DEVICE b $MINORMAJOR
371
      fi
372
   done
373
}
374
 
375
# modprobe kernel modules needed for the LiveCD
376
#
377
modprobe_essential_modules()
378
{
379
   echolog "starting loop device support"
109 beyerle@PS 380
   modprobe_module loop max_loop=32
1 beyerle@PS 381
   echolog "starting cdrom support"
382
   modprobe_module ide_cd
383
   modprobe_module ide-cd
384
   modprobe_module sr_mod
385
   modprobe_module cdrom
386
   echolog "starting cdrom filesystem support"
387
   modprobe_module isofs
388
   echolog "starting squashfs support"
389
   modprobe_module squashfs
37 beyerle@PS 390
   echolog "starting unionfs/aufs support"
1 beyerle@PS 391
   modprobe_module unionfs
37 beyerle@PS 392
   modprobe_module aufs
1 beyerle@PS 393
   echolog "starting vfat support"
120 beyerle@PS 394
   # modprobe_module nls_cp437
395
   # modprobe_module nls_iso8859-1
396
   # modprobe_module nls_iso8859-2
1 beyerle@PS 397
   modprobe_module vfat
398
   echolog "starting ntfs support"
399
   modprobe_module ntfs
166 beyerle@PS 400
   modprobe_module nls_utf8
1 beyerle@PS 401
   create_block_devices
402
}
403
 
404
 
405
# modprobe kernel modules needed for USB masstorage devices
406
#
407
modprobe_usb_sata_modules()
408
{
409
   echolog "starting USB and SATA support"
410
   modprobe_module ehci-hcd
411
   modprobe_module ohci-hcd
412
   modprobe_module uhci-hcd
413
   modprobe_module scsi_mod
414
 
415
   modprobe_module libata
85 beyerle@PS 416
   modprobe_module ata_piix
1 beyerle@PS 417
   modprobe_module ahci
126 beyerle@PS 418
   modprobe_module sata_nv
205 beyerleu 419
   modprobe_module sata_svw
116 beyerle@PS 420
   modprobe_module sg
1 beyerle@PS 421
 
422
   modprobe_module sd_mod
423
   modprobe_module sr_mod
424
   modprobe_module usb-storage
248 beyerleu 425
   echolog "waiting $USBDELAY seconds for USB devices"
426
   sleep $USBDELAY
1 beyerle@PS 427
   create_block_devices
428
}
429
 
430
# modprobe nfs kernel modules
431
#
432
modprobe_nfs_modules()
433
{
434
   echolog "starting nfs support"
435
   modprobe_module lockd
436
   modprobe_module fscache
437
   modprobe_module nfs_acl
438
   modprobe_module nfs
439
}
440
 
160 beyerle@PS 441
# modprobe cifs kernel module
442
#
443
modprobe_cifs_modules()
444
{
445
  echolog "starting cifs support"
446
  modprobe_module cifs
447
}
448
 
1 beyerle@PS 449
# enable/disable CD autoejecting when unmounted
450
# $1 = 1|0 ... enable|disable
451
#
452
cd_autoeject()
453
{
454
   echo $1 >/proc/sys/dev/cdrom/autoeject
455
}
456
 
457
# Disable DMA if nodma boot parameter is present
458
#
459
setup_dma()
460
{
461
   if [ ! "`cmdline_parameter nodma`" = "" ]; then
462
      for DEVICE in `list_cdrom_devices` `list_disk_devices`; do
463
         echolog "setting DMA support off for $DEVICE"
464
         hdparm -d 0 $DEVICE
465
      done
466
   fi
467
}
468
 
469
# create correct fstab file in $1/etc/fstab and create apropriate
470
# mount directories in $1
471
# $1 = root directory (union)
472
#
473
activate_fstab()
474
{
475
   mkdir -p $1/etc
476
   FSTAB="$1/etc/fstab"
477
   echo "tmpfs            /                tmpfs       defaults         0   0" >$FSTAB
478
   echo "devpts           /dev/pts         devpts      gid=5,mode=620   0   0" >>$FSTAB
479
   echo "proc             /proc            proc        defaults         0   0" >>$FSTAB
480
 
48 beyerle@PS 481
   # all the rest will be done by runlast or by fstab-sync
482
   # search for SWAP done later in /etc/rc.sysinit
483
}
1 beyerle@PS 484
 
485
 
173 beyerle@PS 486
# find partition and/or folder that stores the changes
170 beyerle@PS 487
# return false, if changes not set or changes folder/partition not found
176 beyerle@PS 488
# Urs Beyerle
489
#
170 beyerle@PS 490
find_changes()
491
{
176 beyerle@PS 492
    echolog "looking for changes in $CHANGESVAL"
170 beyerle@PS 493
 
494
    CHANGESDEV="`echo "$CHANGESVAL" | egrep -o "^/dev/[^/]+"`"
495
    CHANGESFOLDER="`echo "$CHANGESVAL" | sed -r 's:^/dev/[^/]+(.*):\1:'`"
496
    # (re-)define CHANGES if CHANGESFOLDER is set
497
    if [ "$CHANGESFOLDER" != "" ]; then CHANGES=$MEMORY/$CHANGESFOLDER; fi
498
 
173 beyerle@PS 499
    # we may need usb and sata support
170 beyerle@PS 500
    modprobe_usb_sata_modules
501
 
502
    # device defined?
503
    if [ "$CHANGESDEV" != "" ]; then
504
	mount_device $CHANGESDEV $MEMORY
177 beyerle@PS 505
	if [ $? -eq 0 ]; then 
506
	    echolog "mounted $CHANGESDEV to $MEMORY"
507
	else
508
	    echolog "WARNING: device $CHANGESDEV could not be mounted! Continue booting in 10 sec. ..."
509
	    CHANGESVAL=""
510
	    sleep 10
511
	fi
170 beyerle@PS 512
	return
177 beyerle@PS 513
    fi
170 beyerle@PS 514
 
515
    # if no device is specified, search all devices for folder CHANGESFOLDER
516
    list_block_devices | while read DEVICE; do
517
	mount_device $DEVICE $MEMORY
518
	if [ $? -ne 0 ]; then continue; fi
173 beyerle@PS 519
	FOUND_FOLDER=`ls -1d $MEMORY/$CHANGESFOLDER 2>/dev/null`
170 beyerle@PS 520
	if [ "$FOUND_FOLDER" = "" ]; then 
521
	    umount $MEMORY 2>/dev/null
522
	else
175 beyerle@PS 523
	    echolog "found folder $CHANGESFOLDER on $DEVICE"
177 beyerle@PS 524
	    break
170 beyerle@PS 525
	fi
526
    done
175 beyerle@PS 527
 
177 beyerle@PS 528
    FOUND_FOLDER=`ls -1d $MEMORY/$CHANGESFOLDER 2>/dev/null`
175 beyerle@PS 529
 
177 beyerle@PS 530
    if [ "$FOUND_FOLDER" = "" ]; then 
531
	echolog "WARNING: changes=$CHANGESVAL not found! Continue booting in 10 sec. ..."
532
	CHANGESVAL=""
533
	sleep 10
534
    fi
535
 
170 beyerle@PS 536
}
537
 
538
 
48 beyerle@PS 539
# ===========================================================
540
# functions used for LiveCD on NFS 
541
# ===========================================================
1 beyerle@PS 542
 
37 beyerle@PS 543
# load network modules, if NFSROOT is set
169 beyerle@PS 544
# Urs Beyerle
48 beyerle@PS 545
#
100 beyerle@PS 546
 
147 beyerle@PS 547
# network devices found ?
548
#
549
found_nic()
550
{
551
    found="1"
552
    for iface in eth0 eth1 eth2 eth3 eth4 eth5; do
553
	# nic already found ?
554
	echo $FOUND_IFACE | grep -q $iface 
555
	if [ $? -ne 0 ]; then
241 beyerleu 556
	    ifconfig $iface up > /dev/null 2>&1
147 beyerle@PS 557
	    if [ $? -eq 0 ]; then 
558
		FOUND_IFACE="$FOUND_IFACE $iface"
559
		found="0"
560
	    fi
561
	fi
562
    done
563
    return $found
564
}
565
 
104 beyerle@PS 566
# 1. load network driver defined by kernel parameter nic
567
#
568
load_network_module_nic()
37 beyerle@PS 569
{
147 beyerle@PS 570
    NIC="`cmdline_value nic`"
571
    if [ -n "$NIC" ]; then
100 beyerle@PS 572
	echolog "load module $NIC"
104 beyerle@PS 573
	modprobe_module $NIC
100 beyerle@PS 574
    fi
104 beyerle@PS 575
}
100 beyerle@PS 576
 
577
 
147 beyerle@PS 578
# 2. auto probe for network card drivers
104 beyerle@PS 579
#
580
load_network_module_auto()
581
{
582
    echolog "auto-detection of network driver ..."
147 beyerle@PS 583
    FOUND_NICS=""
104 beyerle@PS 584
    KERNEL="`uname -r`"
246 beyerleu 585
    NETDRIVERS="`find /lib/modules/$KERNEL/kernel/drivers/net | grep -v mii | grep -v 8021q | grep .ko | cut -d"/" -f8 | cut -d"." -f1 | uniq`"
104 beyerle@PS 586
    for driver in $NETDRIVERS; do
148 beyerle@PS 587
       	modprobe_module $driver > /dev/null 2>&1
147 beyerle@PS 588
	found_nic
589
	if [ $? -eq 0 ]; then
590
	    echolog "found network card $driver"
591
	    echolog "load module $driver"
592
	    FOUND_NICS="$FOUND_NICS $driver"
593
	else
594
	    rmmod $driver > /dev/null 2>&1
595
	fi
104 beyerle@PS 596
    done
597
 
598
    # clean up: remove unused modules until driver "mii"
147 beyerle@PS 599
    LOADED_DRIVERS="`lsmod | grep -v Module | cut -d" " -f1`"
104 beyerle@PS 600
    for driver in $LOADED_DRIVERS; do
601
	[ "$driver" = "mii" ]  && break
602
	[ "$driver" = "ntfs" ] && break
603
	[ "$driver" = "vfat" ] && break
147 beyerle@PS 604
	do_not_remove=""
605
	for nic in $FOUND_NICS; do
606
	    echo $driver | grep -q $nic 
607
	    [ $? -eq 0 ] && do_not_remove="$driver"
608
	done
148 beyerle@PS 609
	[ ! $do_not_remove ] && rmmod $driver > /dev/null 2>&1
104 beyerle@PS 610
    done
37 beyerle@PS 611
}
612
 
147 beyerle@PS 613
# 3. detecting network card from pcitable list
614
#
615
load_network_module_pcitable()
616
{
617
    PCITABLE=/bin/pcitable
618
    NICS=`lspci -n | awk '/Class 0200/ {print $4}' | tr ':' ' ' \
619
	| while read x y ; do grep "0x$x.*0x$y" $PCITABLE \
620
	| awk '$3 !~ /"unknown"/ {print $3}' | tr -d '"' ; done`
621
 
622
    if [ -n "$NICS" ]; then
623
	echolog "found network card(s): $NICS"
624
	for driver in $NICS; do
625
	    echolog "load module $driver"
626
	    modprobe_module $driver
627
	done
628
    fi
629
}
630
 
104 beyerle@PS 631
# 4. ask the user for a network driver
632
#
633
load_network_module_ask()
634
{
635
    echo "ERROR: No network card detected!"
636
    echo "Type in your network card driver (e.g. tg3, e1000). "
637
    echo "Or ask your system administrator for help."
638
    echo -n "Network card driver: "
639
    read driver
148 beyerle@PS 640
    echo "load module $driver"
104 beyerle@PS 641
    modprobe_module $driver
642
}
643
 
644
# Try to find a network driver
645
#
646
load_network_modules()
647
{
163 beyerle@PS 648
    echolog "load network modules"
649
 
246 beyerleu 650
    # mii and 8021q maybe need by NIC driver
651
    modprobe_module 8021q
104 beyerle@PS 652
    modprobe_module mii
653
 
147 beyerle@PS 654
    FOUND_IFACE=""
104 beyerle@PS 655
    load_network_module_nic
149 beyerle@PS 656
    found_nic          || load_network_module_auto
148 beyerle@PS 657
    [ "$FOUND_IFACE" ] || load_network_module_pcitable
658
    [ "$FOUND_IFACE" ] || load_network_module_ask
104 beyerle@PS 659
 
246 beyerleu 660
    # remove mii and 8021q, if not needed
661
    rmmod 8021q > /dev/null 2>&1
662
    rmmod mii   > /dev/null 2>&1
104 beyerle@PS 663
}
664
 
147 beyerle@PS 665
 
37 beyerle@PS 666
# get DHCP lease
169 beyerle@PS 667
# Urs Beyerle
48 beyerle@PS 668
#
37 beyerle@PS 669
get_dhcp_lease()
670
{
671
    # create /dev/urandom (needed by udhcpc)
672
    mknod /dev/urandom c 1 9
241 beyerleu 673
 
674
    # wait for interface coming up
675
    if [ $DHCPDELAY -gt 0 ]; then 
676
        echolog "delaying DHCP for $DHCPDELAY seconds (dhcpdelay=$DHCPDELAY) ..."
677
        sleep $DHCPDELAY
678
        echolog "if getting dhcp lease freezes, try define a higher dhcpdelay value as boot parameter."
679
    fi
147 beyerle@PS 680
    for iface in eth0 eth1 eth2 eth3 eth4 eth5; do
681
	# interface up ?
682
	ifconfig $iface > /dev/null 2>&1
683
	if [ $? -eq 0 ]; then
148 beyerle@PS 684
	    # get dhcp lease
685
	    echolog "try to get DHCP lease on $iface"
686
	    udhcpc --now --quit --interface=$iface --script=/bin/udhcpc.script
687
	    [ $? -eq 0 ] && return
241 beyerleu 688
	    echolog "ERROR: couldn't get DHCP lease on $iface."
147 beyerle@PS 689
	fi
102 beyerle@PS 690
    done
37 beyerle@PS 691
}