Subversion Repositories livecd

Rev

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