Subversion Repositories livecd

Rev

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

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