Subversion Repositories livecd

Rev

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

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