Subversion Repositories livecd

Rev

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

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