Subversion Repositories livecd

Rev

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

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