Subversion Repositories livecd

Rev

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