Subversion Repositories livecd

Rev

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

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