Subversion Repositories livecd

Rev

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