Subversion Repositories livecd

Rev

Rev 149 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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