Subversion Repositories livecd

Rev

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