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