Subversion Repositories livecd

Rev

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

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