Subversion Repositories livecd

Rev

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

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