]>
Commit | Line | Data |
---|---|---|
bb529f94 JK |
1 | #!/bin/sh |
2 | ||
3 | # geninitrd | |
4 | # | |
45d4d8cb | 5 | # by PLD Linux Team |
bb529f94 | 6 | # |
9e1ceabe | 7 | # based on mkinitrd from RedHat Linux |
e4b07ddc | 8 | # |
c31050f3 | 9 | |
476f99fc | 10 | GENINITRD_RCSID='$Revision$ $Date:: $' |
96fbf9e6 | 11 | R=${GENINITRD_RCSID#* *}; VERSION=${R%% *} |
ca2c2012 | 12 | PROGRAM=${0##*/} |
bb529f94 JK |
13 | |
14 | . /etc/rc.d/init.d/functions | |
ded87775 | 15 | . /lib/geninitrd/functions |
e2405b29 | 16 | . /etc/sysconfig/system |
bb529f94 | 17 | |
c124d0cf ER |
18 | # list of geninitrd modules which need setup routine after commandline args parsing |
19 | GENINITRD_MODS="" | |
c3667d07 | 20 | COMPRESS=yes |
bb529f94 JK |
21 | target="" |
22 | kernel="" | |
23 | force="" | |
24 | verbose="" | |
25 | MODULES="" | |
26 | img_vers="" | |
c3667d07 | 27 | fstab=/etc/fstab |
c3667d07 | 28 | modext=.o |
4e9eb79c | 29 | rootdev_nr=0 |
738c05d8 ER |
30 | # device node for rootfs from fstab |
31 | rootdev="" | |
bb529f94 | 32 | |
8bd582f1 ER |
33 | # internal variables |
34 | # is /dev on tmpfs | |
b64f015b ER |
35 | dev_mounted=no |
36 | # is /proc mounted | |
37 | proc_mounted=no | |
38 | # is /sys mounted | |
39 | sys_mounted=no | |
40 | # is /tmp mounted on tmpfs | |
41 | tmp_mounted=no | |
42 | ||
43 | # are /dev nodes already created from /proc/devices info? | |
8bd582f1 | 44 | proc_partitions=no |
6b013929 | 45 | |
b64f015b | 46 | usage() { |
46768f45 | 47 | echo "Usage: $PROGRAM [--version] [-v] [-f] [--ifneeded] [--preload <module>]" |
ac085800 | 48 | echo " [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]" |
8a6f083b | 49 | echo " [--compress=yes|xz|lzma|bzip2|gzip|lzo]" |
92ed99b6 | 50 | echo " [--initrdfs=rom|initramfs|ext2|cram] [--modules-conf=<modules.conf>]" |
ba5d09f0 | 51 | echo " [--with-bootsplash] [--without-bootsplash]" |
6c69a2d4 ER |
52 | echo " [--with-fbsplash] [--without-fbsplash]" |
53 | echo " [--with-fbcondecor] [--without-fbcondecor]" | |
e4b07ddc | 54 | echo " [--lvmtoolsversion=1|2] [--with-udev] [--without-udev]" |
ba5d09f0 | 55 | echo " [--with-suspend] [--without-suspend]" |
1cd2aabd | 56 | echo " [--with-tuxonice] [--without-tuxonice]" |
ecd7bf46 ER |
57 | echo " [--without-dmraid]" |
58 | echo " [--with-multipath=DEVPATH] [--without-multipath]" | |
3ef3d717 | 59 | echo " [--without-blkid] [--without-luks]" |
ac085800 | 60 | echo " <initrd-image> <kernel-version>" |
553786c4 | 61 | echo "" |
46768f45 ER |
62 | echo "Example:" |
63 | ||
64 | local kdir kver | |
65 | for kdir in /lib/modules/*; do | |
66 | [ -d $kdir ] || continue | |
67 | kver=${kdir##*/} | |
e87b2881 | 68 | echo " $0 -f --initrdfs=rom /boot/initrd-$kver.gz $kver $verbose" |
3f53322c | 69 | done |
bb529f94 JK |
70 | } |
71 | ||
779a218a | 72 | msg() { |
f4010f7f AM |
73 | echo "$PROGRAM: $*" |
74 | } | |
75 | ||
76 | warn() { | |
4a60c01f | 77 | msg "WARNING: $*" >&2 |
779a218a AM |
78 | } |
79 | ||
94769f11 | 80 | debug() { |
4a60c01f | 81 | [ -n "$verbose" ] && msg "$*" >&2 |
94769f11 | 82 | } |
aa69da6e | 83 | |
bf6c3fcb ER |
84 | # aborts program abnormally |
85 | die() { | |
b5f5c089 | 86 | local rc=${2:-1} |
046c68c9 | 87 | msg "ERROR: $1" >&2 |
bf6c3fcb ER |
88 | exit $rc |
89 | } | |
90 | ||
684d5d2a ER |
91 | # find program from specified paths |
92 | find_tool() { | |
93 | local x | |
94 | for x in "$@"; do | |
95 | if [ -x "$x" ]; then | |
96 | echo $x | |
97 | debug "find_tool: found $x" | |
98 | return 0 | |
99 | fi | |
100 | done | |
101 | debug "find_tool: did not found any of: $@" | |
102 | return 1 | |
103 | } | |
104 | ||
c124d0cf ER |
105 | # loads geninitrd modules |
106 | geninitrd_load_mods() { | |
107 | local mod | |
108 | for mod in "$@"; do | |
109 | if [ ! -f /lib/geninitrd/mod-$mod.sh ]; then | |
110 | die "$mod geninitrd module can't be loaded" | |
111 | fi | |
112 | . /lib/geninitrd/mod-$mod.sh | |
113 | ||
c34c6a69 | 114 | GENINITRD_MODS="$GENINITRD_MODS $mod" |
c124d0cf ER |
115 | done |
116 | } | |
117 | ||
118 | # setup geninitrd modules | |
119 | geninitrd_setup_mods() { | |
c34c6a69 | 120 | local mod rcsid |
c124d0cf ER |
121 | |
122 | for mod in $GENINITRD_MODS; do | |
c34c6a69 ER |
123 | eval rcsid=$(echo \$$mod | LC_ALL=C tr '[a-z]' '[A-Z]')_RCSID |
124 | debug "# $rcsid (mod-$mod)" | |
125 | ||
126 | # some mods want init | |
127 | if type setup_mod_$mod > /dev/null; then | |
128 | eval setup_mod_$mod | |
129 | fi | |
c124d0cf ER |
130 | done |
131 | } | |
132 | ||
c6c6ce01 ER |
133 | # append text to /linuxrc |
134 | # takes STDIN as input | |
135 | add_linuxrc() { | |
b64f015b | 136 | cat >> "$RCFILE" |
c6c6ce01 ER |
137 | } |
138 | ||
b64f015b ER |
139 | # generate code to mount /dev on tmpfs and create initial nodes |
140 | # can be called multiple times. /dev is cleaned up (umounted) automatically at | |
141 | # the end of script. | |
142 | mount_dev() { | |
143 | if [ "$INITRDFS" = "initramfs" ]; then | |
144 | # initramfs is read-write filesystem, no need for tmpfs | |
145 | return | |
146 | fi | |
147 | ||
148 | # we already generated tmpfs code; return | |
149 | if is_yes "$dev_mounted"; then | |
150 | return | |
151 | fi | |
152 | ||
153 | dev_mounted=yes | |
154 | ||
155 | busybox_applet mount mknod mkdir | |
156 | add_linuxrc <<-EOF | |
157 | : 'Creating /dev' | |
158 | mount -o mode=0755 -t tmpfs none /dev | |
159 | mknod /dev/console c 5 1 | |
160 | mknod /dev/null c 1 3 | |
161 | mknod /dev/zero c 1 5 | |
8aa2b6bd | 162 | mknod /dev/random c 1 8 |
067fb1da | 163 | mknod /dev/snapshot c 10 231 |
8b7ac737 | 164 | mknod /dev/urandom c 1 9 |
b64f015b ER |
165 | mkdir /dev/pts |
166 | mkdir /dev/shm | |
167 | EOF | |
168 | } | |
169 | ||
170 | # generate code to mount /proc on initrd | |
171 | # can be called multiple times | |
172 | mount_proc() { | |
173 | if is_yes "$proc_mounted"; then | |
174 | return | |
175 | fi | |
176 | ||
177 | proc_mounted=yes | |
2bface63 ER |
178 | if [ "$INITRDFS" = "initramfs" ]; then |
179 | # /proc is mounted with initramfs 2.6.22.14 kernel | |
180 | # XXX: remove when it is clear why proc was already mounted | |
181 | echo "[ -f /proc/cmdline ] || mount -t proc none /proc" | add_linuxrc | |
182 | else | |
183 | echo "mount -t proc none /proc" | add_linuxrc | |
184 | fi | |
b64f015b ER |
185 | } |
186 | ||
187 | # generate code to mount /sys on initrd | |
188 | # can be called multiple times | |
189 | mount_sys() { | |
190 | if is_yes "$sys_mounted"; then | |
191 | return | |
192 | fi | |
193 | ||
194 | sys_mounted=yes | |
195 | echo "mount -t sysfs none /sys" | add_linuxrc | |
196 | } | |
197 | ||
198 | # generate code to mount /tmp on initrd | |
199 | # can be called multiple times | |
200 | mount_tmp() { | |
201 | if [ "$INITRDFS" = "initramfs" ]; then | |
202 | # initramfs is read-write filesystem, no need for tmpfs | |
203 | return | |
204 | fi | |
205 | ||
206 | if is_yes "$tmp_mounted"; then | |
207 | return | |
208 | fi | |
209 | ||
210 | tmp_mounted=yes | |
211 | echo "mount -t tmpfs none /tmp" | add_linuxrc | |
212 | } | |
213 | ||
214 | # unmount all mountpoints mounted by geninitrd | |
215 | umount_all() { | |
ec49b7e6 ER |
216 | |
217 | add_linuxrc <<-'EOF' | |
903f21ea | 218 | : Last shell before umounting all and giving control over to real init. |
ec49b7e6 ER |
219 | debugshell |
220 | EOF | |
5b70f84d | 221 | |
b64f015b ER |
222 | if is_yes "$dev_mounted"; then |
223 | echo 'umount /dev' | add_linuxrc | |
224 | dev_mounted=no | |
225 | fi | |
b64f015b ER |
226 | if is_yes "$sys_mounted"; then |
227 | echo 'umount /sys' | add_linuxrc | |
228 | sys_mounted=no | |
229 | fi | |
230 | if is_yes "$tmp_mounted"; then | |
231 | echo 'umount /tmp' | add_linuxrc | |
232 | tmp_mounted=no | |
233 | fi | |
3c42f0a4 ER |
234 | if is_yes "$proc_mounted"; then |
235 | echo 'umount /proc' | add_linuxrc | |
236 | proc_mounted=no | |
237 | fi | |
b64f015b ER |
238 | } |
239 | ||
240 | ||
9299682f ER |
241 | # Checks if busybox has support for APPLET(s) |
242 | # Exits from geninitrd if the support is not present. | |
243 | # | |
244 | # NB! XXX do not output to STDOUT, it will appear in initrd images in some cases! | |
245 | busybox_applet() { | |
30495bbf | 246 | local err=0 applet |
9299682f ER |
247 | |
248 | if [ -z "$busybox_functions" ]; then | |
684d5d2a | 249 | local tmp=$($busybox 2>&1) |
9b1373fb ER |
250 | |
251 | # BusyBox v1.1.3 says applet not found if it's not called 'busybox'. | |
252 | if [[ "$tmp" = *applet\ not\ found* ]]; then | |
253 | local t=$(mktemp -d) | |
684d5d2a | 254 | ln -s $busybox $t/busybox |
9b1373fb ER |
255 | local tmp=$($t/busybox 2>&1) |
256 | rm -rf $t | |
257 | fi | |
258 | ||
259 | busybox_functions=$(echo "$tmp" | \ | |
9299682f ER |
260 | sed -ne '/Currently defined functions:/,$p' | \ |
261 | xargs | sed -e 's,.*Currently defined functions: ,,' | |
262 | ) | |
263 | fi | |
264 | for applet in $*; do | |
265 | local have | |
266 | # try cache | |
267 | eval have='$'busybox_have_$applet | |
268 | if [ -z "$have" ]; then | |
269 | have=$(echo "$busybox_functions" | egrep -c "( |^)$applet(,|$)") | |
270 | if [ "$have" = 0 ]; then | |
f4010f7f | 271 | warn "This setup requires busybox-initrd compiled with applet '$applet' support" |
9299682f ER |
272 | err=1 |
273 | fi | |
274 | eval busybox_have_$applet=$have | |
275 | fi | |
276 | done | |
277 | if [ $err = 1 ]; then | |
00eaa938 | 278 | die "Aborted" |
9299682f ER |
279 | fi |
280 | } | |
281 | ||
755e5cdc ER |
282 | # Extract the .config file from a kernel image |
283 | # uses extract-ikconfig from kernel sources (scripts/extract-ikconfig) | |
284 | ikconfig() { | |
61d20b49 | 285 | local kofile=$(modinfo -k $kernel -n configs 2> /dev/null) |
755e5cdc ER |
286 | if [ -n "$kofile" ]; then |
287 | /lib/geninitrd/extract-ikconfig $kofile | |
7283bf35 | 288 | return |
25cb53df | 289 | fi |
7283bf35 ER |
290 | |
291 | # see if config available as separate file | |
292 | if [ -f /boot/config-$kernel ]; then | |
293 | cat /boot/config-$kernel | |
294 | return | |
295 | fi | |
296 | ||
297 | # finally try vmlinuz itself | |
298 | /lib/geninitrd/extract-ikconfig /boot/vmlinuz-$kernel | |
25cb53df ER |
299 | } |
300 | ||
7ffba534 ER |
301 | # Finds module dependencies |
302 | # | |
7ffba534 ER |
303 | # @param $module |
304 | # | |
046c68c9 | 305 | # Outputs full path to module and it's dependencies |
7ffba534 | 306 | find_depmod() { |
fe280785 | 307 | local module="$1" |
046c68c9 ER |
308 | local skiperrors=0 |
309 | ||
310 | # if module is prefixed with dash, we should ignore errors if the module | |
311 | # can't be found. | |
312 | if [ ${module#-} != $module ]; then | |
313 | skiperrors=1 | |
314 | module=${module#-} | |
315 | fi | |
316 | ||
046c68c9 ER |
317 | # This works when user has module-init-tools installed even on 2.4 kernels |
318 | local modprobe | |
319 | modprobe=$(modprobe --set-version $kernel --show-depends $module --ignore-install 2>&1) | |
320 | ||
321 | if [ $? != 0 ]; then | |
322 | if [ $skiperrors = 1 ]; then | |
0c7e9123 | 323 | return 0 |
7cac5014 | 324 | fi |
046c68c9 | 325 | echo >&2 "$modprobe" |
7cac5014 | 326 | |
7ffba534 | 327 | if ! is_no "$EXIT_IF_MISSING"; then |
046c68c9 | 328 | die "$module: module not found for $kernel kernel" |
7ffba534 | 329 | fi |
046c68c9 ER |
330 | |
331 | warn "$module: module not found for $kernel kernel" | |
332 | warn "If $module isn't compiled in kernel then this initrd may not start your system." | |
7ffba534 ER |
333 | fi |
334 | ||
046c68c9 | 335 | echo "$modprobe" | \ |
c25765ed | 336 | while read insmod modpath options; do |
59a68735 | 337 | [ "$insmod" = "insmod" ] && echo $modpath |
7ffba534 | 338 | done |
0c7e9123 | 339 | return 0 |
7ffba534 ER |
340 | } |
341 | ||
4a60c01f AM |
342 | find_firmware() { |
343 | local module="$1" | |
601127ea ER |
344 | |
345 | # no firmware support in 2.4 kernels | |
346 | if [ "$kernel_version_long" -lt "002005048" ]; then | |
347 | return | |
348 | fi | |
349 | echo -n $(NEW_MODINFO=1 modinfo -k $kernel -F firmware $module 2>/dev/null | xargs) | |
4a60c01f AM |
350 | } |
351 | ||
046c68c9 ER |
352 | # @param $module |
353 | find_module() { | |
354 | local mod depmod module=$1 | |
ac085800 | 355 | |
046c68c9 | 356 | depmod=$(find_depmod $module) || exit 1 |
fe280785 | 357 | for mod in $depmod; do |
7ffba534 | 358 | mod=${mod#/lib/modules/$kernel/} |
fe280785 ER |
359 | |
360 | # add each module only once | |
361 | local m have=0 | |
362 | for m in $MODULES; do | |
363 | [ $m = $mod ] && have=1 | |
364 | done | |
365 | if [ $have = 0 ]; then | |
c2eb7d4f | 366 | MODULES="$MODULES $mod" |
fe280785 | 367 | fi |
10c3df06 | 368 | done |
bb529f94 JK |
369 | } |
370 | ||
034fdd5d ER |
371 | # install a file to temporary mount image. |
372 | # it will operate recursively (copying directories) | |
373 | # and will symlink destinations if source is symlink. | |
bb529f94 | 374 | inst() { |
17e97aec | 375 | if [ $# -lt 2 ]; then |
a9ace64a | 376 | die 'Usage: inst <file> [<file>] <destination>' |
c31050f3 | 377 | fi |
17e97aec ER |
378 | |
379 | local src i=0 c=$(($# - 1)) | |
380 | while [ $i -lt $c ]; do | |
381 | src="$src $1" | |
382 | i=$((i + 1)) | |
383 | shift | |
384 | done | |
385 | local dest=$1 | |
386 | set -- $src | |
ce40ba21 | 387 | local parentDir=$(dirname $DESTDIR$dest) |
242f2358 | 388 | [ ! -d "$parentDir" ] && (debug "+ mkdir -p $parentDir"; mkdir -p $parentDir) |
d8056591 | 389 | debug "+ cp $* $DESTDIR$dest" |
9b557a09 | 390 | cp -HR "$@" "$DESTDIR$dest" |
bb529f94 JK |
391 | } |
392 | ||
dab92b1d | 393 | inst_d() { |
17e97aec | 394 | if [ $# = 0 ]; then |
a9ace64a | 395 | die 'Usage: inst_d <destination> <destination>' |
034fdd5d ER |
396 | fi |
397 | for dir in "$@"; do | |
9b557a09 | 398 | install -d "$DESTDIR$dir" |
034fdd5d ER |
399 | done |
400 | } | |
401 | ||
209061e3 ER |
402 | # install executable and it's shared libraries |
403 | inst_exec() { | |
17e97aec | 404 | if [ $# -lt 2 ]; then |
3601c2fa | 405 | die "Invalid params ($@), Usage: inst_exec <file>[, <file>] <destination>" |
17e97aec | 406 | fi |
209061e3 ER |
407 | local src i=0 c=$(($# - 1)) |
408 | while [ $i -lt $c ]; do | |
409 | src="$src $1" | |
410 | i=$((i + 1)) | |
411 | shift | |
412 | done | |
9b532fe6 | 413 | local dest=$1 |
209061e3 ER |
414 | set -- $src |
415 | ||
9b532fe6 ER |
416 | inst "$@" $dest |
417 | ||
5265962d | 418 | local lib libs=$(ldd "$@" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u) |
9b532fe6 | 419 | for lib in $libs; do |
9b557a09 | 420 | if [ ! -f "$DESTDIR/$_lib/${lib##*/}" ]; then |
7efbe841 ER |
421 | inst_d /$_lib |
422 | inst_exec $lib /$_lib | |
17e97aec | 423 | fi |
209061e3 | 424 | done |
c7ade647 ER |
425 | |
426 | # hack for uclibc linked binaries requiring this fixed path | |
427 | # XXX: shouldn't rpath be used here instead so th | |
428 | if [ -f $DESTDIR/$_lib/libc.so.0 ]; then | |
429 | local lib=$DESTDIR/$_lib/libc.so.0 | |
430 | lib=$(ldd "$lib" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u) | |
431 | local libdir=$(cd $(dirname "$lib"); pwd) | |
432 | if [ ! -e $DESTDIR$libdir ]; then | |
433 | libdir=$(dirname "$libdir") | |
434 | inst_d $libdir | |
435 | debug "+ ln -s /$_lib $DESTDIR$libdir" | |
436 | ln -s /$_lib $DESTDIR$libdir | |
437 | fi | |
438 | fi | |
209061e3 ER |
439 | } |
440 | ||
82474db9 ER |
441 | # output modules.conf / modprobe.conf |
442 | modprobe_conf() { | |
443 | echo "$modprobe_conf_cache" | |
444 | } | |
445 | ||
446 | # | |
447 | # defaults to modprobe -c if not told otherwise, this means include statements | |
448 | # work from there. | |
449 | cache_modprobe_conf() { | |
707f5e60 | 450 | if [ "$kernel_version" -lt "002005" ]; then |
82474db9 ER |
451 | modulefile=/etc/modules.conf |
452 | if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then | |
453 | modulefile=/etc/conf.modules | |
454 | fi | |
455 | fi | |
456 | ||
457 | if [ -n "$modulefile" ]; then | |
d8056591 | 458 | debug "Using $modulefile for modules config" |
82474db9 ER |
459 | modprobe_conf_cache=$(cat $modulefile) |
460 | else | |
d8056591 | 461 | debug "Using modprobe -c to get modules config" |
520412e9 | 462 | modprobe_conf_cache=$(modprobe -c --set-version $kernel) |
82474db9 ER |
463 | fi |
464 | } | |
465 | ||
bc0d6f2d | 466 | # find modules for $devpath |
d8056591 | 467 | find_modules_for_devpath() { |
bc0d6f2d ER |
468 | local devpath="$1" |
469 | if [ -z "$devpath" ]; then | |
d8056591 | 470 | die "No argument passed to find_modules_for_devpath() - is your /etc/fstab correct?" |
f6536797 | 471 | fi |
ac085800 | 472 | |
c2eb7d4f ER |
473 | if [[ "$devpath" = /dev/dm-* ]]; then |
474 | # /dev/dm-3 -> /dev/mapper/sil_ahbgadcbchfc3 | |
d2cb46cf | 475 | devpath=$(dm_node "$devpath") |
c2eb7d4f ER |
476 | fi |
477 | ||
336018a5 | 478 | if [ -L "$devpath" ] && ! is_lvm "$devpath" && ! is_luks "$devpath"; then |
c2eb7d4f ER |
479 | # sanitize things like: |
480 | # /dev/block/104:2 -> /dev/cciss/c0d0p2 | |
481 | devpath=$(readlink -f "$devpath") | |
482 | fi | |
483 | ||
d8056591 | 484 | debug "Finding modules for device path $devpath" |
bc0d6f2d | 485 | |
3ef3d717 ER |
486 | if is_luks "$devpath"; then |
487 | find_modules_luks "$devpath" | |
488 | return | |
489 | fi | |
490 | ||
74d45ce1 ER |
491 | if is_nfs "$devpath"; then |
492 | find_modules_nfs "$devpath" | |
d9179777 ER |
493 | return |
494 | fi | |
495 | ||
e16414d9 | 496 | if is_md "$devpath"; then |
df738638 | 497 | find_modules_md "$devpath" |
d9179777 ER |
498 | return |
499 | fi | |
500 | ||
c3b54060 | 501 | if is_multipath "$devpath"; then |
07137fe3 | 502 | if find_modules_multipath "$devpath"; then |
9baf4f3f | 503 | return |
9baf4f3f | 504 | fi |
ecd7bf46 | 505 | |
9baf4f3f ER |
506 | # fallback |
507 | fi | |
508 | ||
36523626 | 509 | if is_dmraid "$devpath"; then |
c083ae23 ER |
510 | if find_modules_dmraid "$devpath"; then |
511 | return | |
512 | fi | |
513 | # fallback | |
514 | fi | |
515 | ||
35043b20 ER |
516 | if is_scsi "$devpath"; then |
517 | find_modules_scsi "$devpath" | |
d9179777 ER |
518 | return |
519 | fi | |
520 | ||
30ca4815 | 521 | if is_ide "$devpath"; then |
bc0d6f2d | 522 | find_modules_ide "$devpath" |
d9179777 ER |
523 | return |
524 | fi | |
525 | ||
07b09cf9 | 526 | if [[ "$devpath" == /dev/rd/* ]]; then |
046c68c9 | 527 | find_module "DAC960" |
c3313cd6 | 528 | rootdev_add=/dev/rd/ |
d9179777 ER |
529 | return |
530 | fi | |
531 | ||
07b09cf9 | 532 | if [[ "$devpath" == /dev/ida/* ]]; then |
046c68c9 | 533 | find_module "cpqarray" |
c3313cd6 | 534 | rootdev_add=/dev/ida/ |
d9179777 ER |
535 | return |
536 | fi | |
537 | ||
08651ad1 | 538 | if [[ "$devpath" == /dev/cciss/* ]]; then |
046c68c9 | 539 | find_module "cciss" |
c3313cd6 | 540 | rootdev_add=/dev/cciss/ |
d9179777 ER |
541 | return |
542 | fi | |
543 | ||
07b09cf9 | 544 | if [[ "$devpath" == /dev/ataraid/* ]]; then |
ac085800 | 545 | find_modules_ide |
046c68c9 | 546 | find_module "ataraid" |
82474db9 | 547 | ataraidmodules=$(modprobe_conf | awk '/ataraid_hostadapter/ && ! /^[\t ]*#/ { print $3; }') |
9ae446b9 | 548 | if [ -n "$ataraidmodules" ]; then |
7c38b114 | 549 | # FIXME: think about modules compiled in kernel |
82474db9 | 550 | die "ataraid_hostadapter alias not defined in modprobe.conf! Please set it and run $PROGRAM again." |
7c38b114 AF |
551 | fi |
552 | for n in $ataraidmodules; do | |
046c68c9 | 553 | find_module "$n" |
7c38b114 | 554 | done |
c3313cd6 | 555 | rootdev_add=/dev/ataraid/ |
d9179777 ER |
556 | return |
557 | fi | |
558 | ||
7c38b114 | 559 | # check to see if we need to set up a loopback filesystem |
07b09cf9 | 560 | if [[ "$devpath" == /dev/loop* ]]; then |
00eaa938 | 561 | die "Sorry, root on loop device isn't supported." |
7c38b114 AF |
562 | # TODO: rewrite for bsp and make nfs ready |
563 | if [ ! -x /sbin/losetup ]; then | |
00eaa938 | 564 | die "losetup is missing" |
7c38b114 | 565 | fi |
bc0d6f2d | 566 | key="^# $(echo $devpath | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):" |
82474db9 | 567 | if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`"; then |
42820142 | 568 | die "The root filesystem is on a $devpath, but there is no magic entry in $fstab for this device. Consult the $PROGRAM man page for more information" |
7c38b114 AF |
569 | fi |
570 | ||
571 | line="`awk '/'$key'/ { print $0; }' $fstab`" | |
572 | loopDev="$(echo $line | awk '{print $3}')" | |
573 | loopFs="$(echo $line | awk '{print $4}')" | |
574 | loopFile="$(echo $line | awk '{print $5}')" | |
575 | ||
576 | BASICMODULES="$BASICMODULES -loop" | |
046c68c9 | 577 | find_module "-$loopFs" |
7c38b114 | 578 | BASICMODULES="$BASICMODULES -${loopFs}" |
d9179777 ER |
579 | return |
580 | fi | |
581 | ||
67aa84bd ER |
582 | if is_lvm "$devpath"; then |
583 | find_modules_lvm "$devpath" | |
d9179777 | 584 | return |
7c38b114 AF |
585 | fi |
586 | } | |
587 | ||
b64f015b | 588 | firmware_install_module() { |
c3667d07 | 589 | local module="$1" |
c6c6ce01 | 590 | local firmware_files="$2" |
9ed6e1db | 591 | |
94769f11 | 592 | debug "Adding Firmwares ($firmware_files) to initrd for module $module" |
9ed6e1db | 593 | # firmware not yet installed |
9b557a09 | 594 | if [ ! -f "$DESTDIR/lib/firmware/firmware.sh" ]; then |
034fdd5d | 595 | inst_d /lib/firmware |
9b557a09 | 596 | cat << 'EOF' >> "$DESTDIR/lib/firmware/firmware.sh" |
9ed6e1db | 597 | #!/bin/sh -e |
8639f99a AM |
598 | echo 1 > /sys$DEVPATH/loading |
599 | cat "/lib/firmware/$FIRMWARE" > /sys$DEVPATH/data | |
600 | echo 0 > /sys$DEVPATH/loading | |
601 | exit 0 | |
9ed6e1db | 602 | EOF |
9b557a09 | 603 | chmod 755 "$DESTDIR/lib/firmware/firmware.sh" |
9ed6e1db AM |
604 | fi |
605 | ||
606 | for firmware in $firmware_files; do | |
80b23733 | 607 | if [ -f "/lib/firmware/$kernel/$firmware" ]; then |
486d26a5 | 608 | FIRMWAREDIR=${firmware%/*} |
4a9920e7 | 609 | [ "$FIRMWAREDIR" != "$firmware" ] && inst_d /lib/firmware/$FIRMWAREDIR |
80b23733 ER |
610 | inst /lib/firmware/$kernel/$firmware /lib/firmware/$firmware |
611 | elif [ -f "/lib/firmware/$firmware" ]; then | |
c661b23c | 612 | FIRMWAREDIR=${firmware%/*} |
613 | [ "$FIRMWAREDIR" != "$firmware" ] && inst_d /lib/firmware/$FIRMWAREDIR | |
80b23733 | 614 | inst /lib/firmware/$firmware /lib/firmware/$firmware |
c661b23c | 615 | else |
6652b089 | 616 | warn "Possible missing firmware file /lib/firmware/$firmware or /lib/firmware/$kernel/$firmware for module $module." |
4a60c01f | 617 | fi |
9ed6e1db AM |
618 | done |
619 | ||
b64f015b | 620 | mount_sys |
c6c6ce01 | 621 | echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" | add_linuxrc |
9ed6e1db AM |
622 | } |
623 | ||
2a5bcca9 | 624 | modules_install() { |
c3667d07 ER |
625 | local modules="$1" |
626 | local mod | |
2a5bcca9 AM |
627 | |
628 | for mod in $modules; do | |
fe280785 | 629 | MODULEDIR=${mod%/*} |
4a9920e7 | 630 | inst_d "/lib/modules/$kernel/$MODULEDIR" |
9b557a09 ER |
631 | cp -a "/lib/modules/$kernel/$mod" "$DESTDIR/lib/modules/$kernel/$mod" |
632 | gunzip "$DESTDIR/lib/modules/$kernel/$mod" 2> /dev/null | |
2a5bcca9 AM |
633 | done |
634 | } | |
635 | ||
636 | modules_add_linuxrc() { | |
fe280785 | 637 | local mod modpath |
ac085800 | 638 | |
fe280785 ER |
639 | for mod in "$@"; do |
640 | # module path without optional compression | |
641 | modpath=${mod%.gz} | |
642 | ||
643 | # name of the module | |
8011a76c ER |
644 | local module=${modpath##*/}; module=${module%$modext} |
645 | local options=$(modprobe_conf | awk -vmodule="$module" '{ if ($1 == "options" && $2 == module) { for(i=3;i<=NF;i++) printf("%s ",$i); }}' | xargs) | |
646 | local genericname=$(echo $module | tr - _) | |
647 | local usleep=$(eval echo \$MODULE_${genericname}_USLEEP) | |
648 | local firmware=$(eval echo \$MODULE_${genericname}_FIRMWARE) | |
2a5bcca9 | 649 | |
3e1d0df9 AM |
650 | if [ "$module" = "scsi_mod" -a "$kernel_version_long" -ge "002006030" ]; then |
651 | options="scan=sync $options" | |
652 | fi | |
653 | ||
ac085800 | 654 | if [ -n "$verbose" ]; then |
8a47b72c ER |
655 | s="" |
656 | if [ "$options" ]; then | |
657 | s="$s with options [$options]" | |
658 | fi | |
8011a76c ER |
659 | if [ "$usleep" ]; then |
660 | s="$s and $usleep usleep" | |
8a47b72c | 661 | fi |
4a60c01f | 662 | debug "Loading module [$module]$s" |
ac085800 ER |
663 | fi |
664 | ||
8011a76c ER |
665 | if [ -n "$firmware" ]; then |
666 | firmware_install_module "$module" "$firmware" | |
4a60c01f | 667 | else |
06e481d7 ER |
668 | for file in $(find_firmware "$module"); do |
669 | firmware_install_module "$module" "$file" | |
670 | done | |
9ed6e1db | 671 | fi |
4a60c01f | 672 | |
18ece493 | 673 | echo "insmod /lib/modules/$kernel/$modpath $options" | add_linuxrc |
8011a76c ER |
674 | if [ -n "$usleep" ]; then |
675 | echo "usleep $usleep" | add_linuxrc | |
8e598759 | 676 | fi |
e00dcfcb | 677 | if [ "$module" = "scsi_wait_scan" ]; then |
d29583e0 ER |
678 | if [ "$(busybox_applet rmmod 2>/dev/null; echo $?)" = 0 ]; then |
679 | echo "rmmod scsi_wait_scan" | add_linuxrc | |
680 | fi | |
e00dcfcb AM |
681 | fi |
682 | ||
2a5bcca9 AM |
683 | done |
684 | } | |
685 | ||
82b2dba2 ER |
686 | # Generates /dev nodes based on /proc/partitions information. |
687 | # Needs /proc mounted. | |
688 | # Can be called multiple times. | |
689 | initrd_gen_devices() { | |
690 | if is_yes "$proc_partitions"; then | |
691 | return | |
692 | fi | |
693 | proc_partitions=yes | |
335cd101 | 694 | |
82b2dba2 ER |
695 | mount_dev |
696 | add_linuxrc <<-'EOF' | |
697 | : 'Making device nodes' | |
698 | cat /proc/partitions | ( | |
8d4aba01 | 699 | # ignore first two lines: header, empty line |
82b2dba2 | 700 | read b; read b |
2cc3ae8b | 701 | |
82b2dba2 ER |
702 | while read major minor blocks dev rest; do |
703 | node=/dev/$dev | |
704 | mkdir -p ${node%/*} | |
8d4aba01 | 705 | [ -e $node ] || mknod $node b $major $minor |
82b2dba2 ER |
706 | done |
707 | ) | |
708 | EOF | |
709 | } | |
bb529f94 | 710 | |
82b2dba2 | 711 | |
4828c787 | 712 | initrd_gen_setrootdev() { |
5a761721 | 713 | debug "Adding rootfs finding based on kernel cmdline root= option support." |
71b2d771 | 714 | busybox_applet ls |
82b2dba2 | 715 | add_linuxrc <<-'EOF' |
dcdf6b49 ER |
716 | if [ "${ROOT##/dev/}" != "${ROOT}" ]; then |
717 | rootnr="$(busybox awk -v rootnode="${ROOT##/dev/}" '$4 == rootnode { print 256 * $1 + $2 }' /proc/partitions)" | |
71b2d771 AM |
718 | # fallback to ls |
719 | if [ -z "$rootnr" ]; then | |
5efa9a2e | 720 | rootnr="$(busybox ls -lL ${ROOT} | busybox awk '{if (/^b/) { print 256 * $3 + $4; }}')" |
71b2d771 | 721 | fi |
82b2dba2 ER |
722 | if [ -n "$rootnr" ]; then |
723 | echo "$rootnr" > /proc/sys/kernel/real-root-dev | |
724 | fi | |
725 | fi | |
b64f015b | 726 | EOF |
1606e343 AM |
727 | } |
728 | ||
4671d086 ER |
729 | initrd_gen_initramfs_switchroot() { |
730 | inst_d /newroot | |
731 | if [ "$rootdev" = "/dev/nfs" ]; then | |
732 | echo "rootfs on NFS root=/dev/nfs" | |
733 | else | |
734 | [ ! -e "$DESTDIR/$rootdev" ] && inst $rootdev $rootdev | |
735 | fi | |
5845b321 ER |
736 | |
737 | # parse 'root=xxx' kernel commandline | |
4671d086 ER |
738 | # We support passing root as hda3 /dev/hda3 0303 0x0303 and 303 |
739 | add_linuxrc <<-'EOF' | |
740 | device=/dev/no_partition_found | |
741 | eval "$(busybox awk -v c="$ROOT" ' | |
742 | BEGIN { | |
743 | num_pattern_short = "[0-9a-f][0-9a-f][0-9a-f]"; | |
744 | num_pattern = "[0-9a-f]" num_pattern_short; | |
745 | dev_pattern = "[hms][a-z][a-z]([0-9])+"; | |
746 | partition = "no_partition_found"; | |
747 | min = -1; maj = -1; | |
748 | ||
749 | sub("^0x", "", c); | |
750 | if (c ~ "^" num_pattern_short "$") sub("^", "0", c); | |
751 | if (c ~ "^" num_pattern "$") { | |
752 | maj = sprintf("%s",substr(c,1,2)); | |
753 | min = sprintf("%s",substr(c,3)); | |
754 | } | |
755 | if (c ~ "^\/dev\/" dev_pattern "$") sub("^/dev/","", c); | |
756 | if (c ~ "^" dev_pattern "$") partition = c; | |
757 | } | |
758 | ||
759 | $4 ~ partition { maj = $1; min = $2; } | |
760 | $1 ~ maj && $2 ~ min { partition = $4; } | |
761 | ||
762 | END { | |
fa3a5452 ER |
763 | if (maj >= 0 && min >= 0) { |
764 | printf("device=/dev/%s; maj=%s; min=%s;\n", partition, maj, min); | |
765 | } | |
4671d086 ER |
766 | } |
767 | ' /proc/partitions)" | |
768 | if [ "$device" != '/dev/no_partition_found' -a ! -b $device ]; then | |
769 | mknod $device b $maj $min | |
770 | fi | |
771 | EOF | |
772 | ||
773 | add_linuxrc <<-EOF | |
774 | rootdev=$rootdev | |
775 | rootfs=$rootFs | |
776 | EOF | |
777 | ||
778 | add_linuxrc <<-'EOF' | |
779 | if [ "$device" = '/dev/no_partition_found' ]; then | |
780 | device=$rootdev | |
781 | fi | |
782 | ||
aebd3ecb | 783 | [ -n "$ROOTFSFLAGS" ] && ROOTFSFLAGS="-o $ROOTFSFLAGS" |
41d3645c | 784 | |
aebd3ecb | 785 | mount -t $rootfs -r $device $ROOTFSFLAGS /newroot || echo "Mount of rootfs failed." |
4671d086 ER |
786 | init="$(echo "$CMDLINE" | busybox awk '/init=\// { gsub(/.*init=/,NIL,$0); gsub(/ .*/,NIL,$0); print }')" |
787 | if [ -z "$init" -o ! -x "/newroot$init" ]; then | |
788 | init=/sbin/init | |
789 | fi | |
790 | EOF | |
791 | ||
792 | umount_all | |
793 | busybox_applet switch_root | |
794 | add_linuxrc <<-'EOF' | |
f8ea6a63 | 795 | exec switch_root /newroot $init ${1:+"$@"} |
4671d086 ER |
796 | |
797 | echo "Error! initramfs should not reach this place." | |
798 | echo "It probably means you've got old version of busybox, with broken" | |
799 | echo "initramfs support. Trying to boot anyway, but won't promise anything." | |
800 | ||
f8ea6a63 | 801 | exec chroot /newroot $init ${1:+"$@"} |
4671d086 ER |
802 | |
803 | echo "Failed to chroot!" | |
b61aa273 | 804 | debugshell |
4671d086 ER |
805 | EOF |
806 | # we need /init being real file, not symlink, otherwise the initramfs will | |
807 | # not be ran by pid 1 which is required for switch_root | |
808 | mv $DESTDIR/linuxrc $DESTDIR/init | |
809 | ln -s init $DESTDIR/linuxrc | |
810 | } | |
811 | ||
c552503c ER |
812 | # find if $symbol exists in System.map $mapfile |
813 | sym_exists() { | |
814 | local mapfile="$1" | |
815 | local symbol="$2" | |
816 | if [ ! -f $mapfile ]; then | |
817 | # missing mapfile (not a pld kernel?) | |
818 | return 1 | |
819 | fi | |
820 | ||
821 | awk -vc=1 -vsymbol="$symbol" '$2 == "T" && $3 == symbol {c = 0} END {exit c}' $mapfile | |
822 | } | |
823 | ||
824 | # find best compressor (or forced one) for initrd | |
825 | find_compressor() { | |
826 | local mode="$1" | |
1b89c904 AM |
827 | # fastest initrd decompression speed is first |
828 | local compressors='lzo gzip xz lzma bzip2' | |
c552503c ER |
829 | |
830 | # a specified one, take it | |
55aaf2fe AM |
831 | if ! is_yes "$mode"; then |
832 | compressors="$mode" | |
c552503c ER |
833 | fi |
834 | ||
835 | debug "finding compressor: $compressors (via $mode)" | |
836 | # check for compressor validity | |
837 | local c prog map=/boot/System.map-$kernel | |
838 | for c in $compressors; do | |
839 | case $c in | |
66d146a5 | 840 | xz) |
29f7d622 AM |
841 | sym=unxz |
842 | prog=/usr/bin/xz | |
843 | ;; | |
844 | lzma) | |
c552503c | 845 | sym=unlzma |
66d146a5 | 846 | prog=/usr/bin/xz |
c552503c ER |
847 | ;; |
848 | bzip2) | |
849 | sym=bzip2 | |
c42692d4 | 850 | prog=/usr/bin/bzip2 |
c552503c ER |
851 | ;; |
852 | gzip) | |
853 | sym=gunzip | |
c42692d4 | 854 | prog=/bin/gzip |
c552503c | 855 | ;; |
8a6f083b AM |
856 | lzo) |
857 | sym=unlzo | |
858 | prog=/usr/bin/lzop | |
859 | ;; | |
5fcc3d04 AM |
860 | none|no) |
861 | # any existing sym will work | |
862 | sym=initrd_load | |
863 | prog=/bin/cat | |
864 | ;; | |
c552503c ER |
865 | *) |
866 | die "Unknown compressor $c" | |
867 | ;; | |
868 | esac | |
c454c66e | 869 | if sym_exists $map $sym && [ -x $prog ]; then |
c552503c ER |
870 | echo $c |
871 | return | |
872 | fi | |
873 | done | |
874 | ||
875 | debug "using gzip for compressor (fallback)" | |
876 | echo gzip | |
877 | } | |
878 | ||
97f51b6e ER |
879 | # compresses kernel image image |
880 | # in function so we could retry with other compressor on failure | |
881 | compress_image() { | |
882 | local compressor="$1" IMAGE="$2" target="$3" tmp | |
883 | tmp=$(mktemp "$target".XXXXXX) || die "mktemp failed" | |
884 | ||
885 | case "$compressor" in | |
886 | xz) | |
887 | # don't use -9 here since kernel won't understand it | |
888 | xz --format=xz --check=crc32 --lzma2=preset=6e,dict=1MiB < "$IMAGE" > "$tmp" || return $? | |
889 | ;; | |
890 | lzma) | |
891 | xz --format=lzma -9 < "$IMAGE" > "$tmp" || return $? | |
892 | ;; | |
893 | bzip2) | |
894 | bzip2 -9 < "$IMAGE" > "$tmp" || return $? | |
895 | ;; | |
896 | gzip) | |
897 | gzip -9 < "$IMAGE" > "$tmp" || return $? | |
898 | ;; | |
8a6f083b AM |
899 | lzo) |
900 | lzop -9 < "$IMAGE" > "$tmp" || return $? | |
901 | ;; | |
5fcc3d04 AM |
902 | none|no) |
903 | cat < "$IMAGE" > "$tmp" || return $? | |
904 | ;; | |
97f51b6e ER |
905 | esac |
906 | ||
907 | mv -f "$tmp" "$target" | |
908 | } | |
909 | ||
82b2dba2 ER |
910 | if [ -r /etc/sysconfig/geninitrd ]; then |
911 | . /etc/sysconfig/geninitrd | |
912 | fi | |
2cc3ae8b | 913 | |
c215292c AG |
914 | if [ ! -f /proc/mounts ]; then |
915 | warn "/proc filesystem not mounted, may cause wrong results or failure." | |
916 | fi | |
917 | ||
cefcd7bb | 918 | geninitrd_load_mods ide luks multipath dmraid lvm md blkid udev tuxonice suspend fbsplash condecor bootsplash uvesafb nfs sata scsi usbkbd |
c124d0cf | 919 | |
82b2dba2 ER |
920 | while [ $# -gt 0 ]; do |
921 | case $1 in | |
922 | --fstab=*) | |
923 | fstab=${1#--fstab=} | |
924 | ;; | |
925 | --fstab) | |
926 | fstab=$2 | |
927 | shift | |
928 | ;; | |
929 | --modules-conf=*) | |
930 | modulefile=${1#--modules-conf=} | |
931 | ;; | |
932 | --modules-conf) | |
933 | modulefile=$2 | |
934 | shift | |
935 | ;; | |
82b2dba2 ER |
936 | --with-bootsplash) |
937 | BOOT_SPLASH=yes | |
938 | ;; | |
939 | --without-bootsplash) | |
940 | BOOT_SPLASH=no | |
941 | ;; | |
942 | --with-fbsplash) | |
943 | FB_SPLASH=yes | |
944 | ;; | |
945 | --without-fbsplash) | |
946 | FB_SPLASH=no | |
947 | ;; | |
6c69a2d4 ER |
948 | --with-fbcondecor) |
949 | FB_CON_DECOR=yes | |
950 | ;; | |
951 | --without-fbcondecor) | |
952 | FB_CON_DECOR=no | |
953 | ;; | |
82b2dba2 ER |
954 | --with-suspend) |
955 | USE_SUSPEND=yes | |
956 | ;; | |
957 | --without-suspend) | |
958 | USE_SUSPEND=no | |
959 | ;; | |
960 | --with-suspend2 | --with-tuxonice) | |
961 | USE_TUXONICE=yes | |
962 | ;; | |
963 | --without-suspend2 | --without-tuxonice) | |
964 | USE_TUXONICE=no | |
965 | ;; | |
80b1ed79 | 966 | --lvmversion=*) |
7308edee | 967 | LVMTOOLSVERSION=${1#--lvmversion=} |
82b2dba2 | 968 | ;; |
80b1ed79 | 969 | --lvmtoolsversion=*) |
970 | LVMTOOLSVERSION=${1#--lvmtoolsversion=} | |
971 | ;; | |
82b2dba2 ER |
972 | --lvmtoolsversion|--lvmversion) |
973 | LVMTOOLSVERSION=$2 | |
974 | shift | |
975 | ;; | |
976 | --without-udev) | |
977 | USE_UDEV=no | |
978 | ;; | |
979 | --with-udev) | |
980 | USE_UDEV=yes | |
981 | ;; | |
982 | --without-dmraid) | |
983 | USE_DMRAID=no | |
984 | ;; | |
985 | --without-multipath) | |
ecd7bf46 ER |
986 | USE_MULTIPATH=no |
987 | ;; | |
988 | --with-multipath=*) | |
989 | USE_MULTIPATH=${1#--with-multipath=} | |
82b2dba2 | 990 | ;; |
af075488 | 991 | --without-blkid) |
992 | USE_BLKID=no | |
289fbc9b | 993 | ;; |
3ef3d717 ER |
994 | --without-luks) |
995 | USE_LUKS=no | |
996 | ;; | |
82b2dba2 ER |
997 | --with=*) |
998 | BASICMODULES="$BASICMODULES ${1#--with=}" | |
999 | ;; | |
1000 | --with) | |
1001 | BASICMODULES="$BASICMODULES $2" | |
1002 | shift | |
1003 | ;; | |
1004 | --version) | |
1005 | echo "$PROGRAM: version $VERSION" | |
1006 | exit 0 | |
1007 | ;; | |
1008 | -v) | |
1009 | verbose=-v | |
1010 | ;; | |
c552503c ER |
1011 | --compress) |
1012 | COMPRESS=$2 | |
1013 | ;; | |
1014 | --compress=*) | |
1015 | COMPRESS="${1#--compress=}" | |
1016 | ;; | |
82b2dba2 ER |
1017 | --nocompress) |
1018 | COMPRESS=no | |
1019 | ;; | |
1020 | --ifneeded) | |
1021 | ifneeded=1 | |
1022 | ;; | |
1023 | -f) | |
1024 | force=1 | |
1025 | ;; | |
1026 | --preload=*) | |
1027 | PREMODS="$PREMODS ${1#--preload=}" | |
1028 | ;; | |
1029 | --preload) | |
1030 | PREMODS="$PREMODS $2" | |
1031 | shift | |
1032 | ;; | |
2965cab9 | 1033 | --fs=* | --fs) |
bbbc32d0 | 1034 | die "--fs option is obsoleted. Use --initrdfs instead" |
82b2dba2 ER |
1035 | ;; |
1036 | --initrdfs=*) | |
1037 | INITRDFS=${1#--initrdfs=} | |
1038 | ;; | |
1039 | --initrdfs) | |
1040 | INITRDFS=$2 | |
1041 | shift | |
1042 | ;; | |
1043 | --image-version) | |
1044 | img_vers=yes | |
1045 | ;; | |
1046 | --ide-only-root) | |
1047 | ide_only_root="yes" | |
1048 | ;; | |
1049 | *) | |
1050 | if [ -z "$target" ]; then | |
1051 | target="$1" | |
1052 | elif [ -z "$kernel" ]; then | |
1053 | kernel="$1" | |
1054 | else | |
7f6e5359 | 1055 | usage |
36523626 | 1056 | exit 1 |
82b2dba2 ER |
1057 | fi |
1058 | ;; | |
1059 | esac | |
f5db170b | 1060 | |
82b2dba2 ER |
1061 | shift |
1062 | done | |
1063 | ||
1064 | if [ -z "$target" -o -z "$kernel" ]; then | |
7f6e5359 | 1065 | usage |
36523626 | 1066 | exit 1 |
82b2dba2 ER |
1067 | fi |
1068 | ||
93a38d1a ER |
1069 | # main() |
1070 | if [ "$(id -u)" != 0 ]; then | |
1071 | die "You need to be root to generate initrd" | |
1072 | fi | |
1073 | ||
a48c38c7 | 1074 | if [ -d /lib64 -a -d /usr/lib64 ]; then |
c124d0cf ER |
1075 | _lib=lib64 |
1076 | else | |
1077 | _lib=lib | |
130aadc1 ER |
1078 | fi |
1079 | ||
c124d0cf | 1080 | initrd_dir=/usr/$_lib/initrd |
e4487862 AM |
1081 | kernel_version=$(echo "$kernel" | awk -F. '{gsub(/[_-].*/, "", $0); print sprintf("%03d%03d",$1,$2)}') |
1082 | kernel_version_long=$(echo "$kernel" | awk -F. '{gsub(/[_-].*/, "", $0); print sprintf("%03d%03d%03d",$1,$2,$3)}') | |
82b2dba2 | 1083 | |
96fbf9e6 | 1084 | debug "# $GENINITRD_RCSID (geninitrd)" |
c124d0cf ER |
1085 | debug "Using _lib: $_lib" |
1086 | debug "Using initrd_dir: $initrd_dir" | |
1087 | ||
6ec4aea6 | 1088 | busybox=$(find_tool $initrd_dir/busybox $initrd_dir/initrd-busybox /bin/initrd-busybox) || die "Couldn't find busybox suitable for initrd" |
c124d0cf ER |
1089 | |
1090 | # we setup mods after parsing command line args | |
1091 | geninitrd_setup_mods | |
1092 | ||
1093 | if [ ! -f /boot/vmlinuz-"$kernel" ]; then | |
1094 | warn "/boot/vmlinuz-$kernel doesn't exist, is your /boot mounted?" | |
1095 | fi | |
1096 | ||
82b2dba2 | 1097 | if [ -z "$INITRDFS" ]; then |
bbbc32d0 ER |
1098 | if [ -n "$FS" ]; then |
1099 | # FS= can came only via /etc/sysconfig/geninitrd likely? | |
1100 | die "FS configuration option is obsoleted. Use INITRDFS instead" | |
f5db170b | 1101 | fi |
f5db170b | 1102 | |
bbbc32d0 ER |
1103 | # default value |
1104 | if [ "$kernel_version" -ge "002005" ]; then | |
1105 | INITRDFS="initramfs" | |
1106 | else | |
1107 | INITRDFS="rom" | |
1108 | fi | |
82b2dba2 | 1109 | fi |
966c32cc | 1110 | |
82b2dba2 | 1111 | case "$INITRDFS" in |
4877276e ER |
1112 | ext2) |
1113 | [ -x /sbin/mke2fs ] || die "/sbin/mke2fs is missing" | |
1114 | ;; | |
1115 | rom|romfs) | |
1116 | [ -x /sbin/genromfs ] || die "/sbin/genromfs is missing" | |
1117 | ;; | |
1118 | cram|cramfs) | |
1119 | [ -x /sbin/mkcramfs ] || die "/sbin/mkcramfs is missing" | |
1120 | ;; | |
1121 | initramfs) | |
1122 | [ -x /bin/cpio ] || die "/bin/cpio is missing" | |
1123 | [ -x /usr/bin/find ] || die "/usr/bin/find is missing" | |
1124 | ;; | |
1125 | *) | |
1126 | die "Filesystem $INITRDFS on initrd is not supported" | |
1127 | ;; | |
82b2dba2 | 1128 | esac |
f5db170b | 1129 | |
9323ada3 | 1130 | if [ -L "$target" ]; then |
1131 | target=$(readlink -f "$target") | |
1132 | fi | |
1133 | ||
82b2dba2 ER |
1134 | if [ -n "$img_vers" ]; then |
1135 | target="$target-$kernel" | |
1136 | fi | |
8bd582f1 | 1137 | |
82b2dba2 ER |
1138 | if [ -z "$force" -a -f "$target" ]; then |
1139 | die "$target already exists." | |
1140 | fi | |
c6c6ce01 | 1141 | |
82b2dba2 ER |
1142 | if [ ! -d "/lib/modules/$kernel" ]; then |
1143 | die "/lib/modules/$kernel is not a directory." | |
1144 | fi | |
2ffb1734 | 1145 | |
bbbc32d0 ER |
1146 | if [ "$kernel_version" -ge "002005" ]; then |
1147 | modext=".ko" | |
1148 | fi | |
1149 | ||
82b2dba2 ER |
1150 | cache_modprobe_conf |
1151 | ||
1152 | for n in $PREMODS; do | |
046c68c9 | 1153 | find_module "$n" |
82b2dba2 ER |
1154 | done |
1155 | ||
ed30e3a8 | 1156 | if [ "$FBMODULE" ]; then |
046c68c9 | 1157 | find_module "$FBMODULE" |
ed30e3a8 ER |
1158 | fi |
1159 | ||
cefcd7bb AM |
1160 | # autodetect USB keyboards |
1161 | find_modules_usbkbd | |
1162 | ||
82b2dba2 | 1163 | # allow forcing loading SCSI and/or IDE modules |
35043b20 | 1164 | # XXX: where ADDSCSI cames from? drop? |
82b2dba2 ER |
1165 | if is_yes "$ADDSCSI"; then |
1166 | find_modules_scsi | |
1167 | fi | |
1168 | ||
3f53322c | 1169 | # autodetect SATA modules |
1170 | find_modules_sata | |
e2d6ca6c | 1171 | |
35043b20 | 1172 | # XXX: where ADDIDE cames from? drop? |
82b2dba2 ER |
1173 | if is_yes "$ADDIDE"; then |
1174 | find_modules_ide | |
1175 | fi | |
1176 | ||
4c16dac4 ER |
1177 | if is_yes "$USE_SUSPEND"; then |
1178 | find_modules_suspend | |
1179 | fi | |
1180 | ||
82b2dba2 ER |
1181 | find_root "$fstab" || exit |
1182 | debug "Using $rootdev as device for rootfs" | |
1183 | ||
d8056591 | 1184 | find_modules_for_devpath "$rootdev" |
ac085800 | 1185 | |
ecd7bf46 ER |
1186 | # if USE_MULTIPATH is path to device, scan that too |
1187 | # this is to bootstrap multipath setup into initrd. | |
1188 | if ! is_no "$USE_MULTIPATH" && ! is_yes "$USE_MULTIPATH"; then | |
1189 | find_modules_multipath $USE_MULTIPATH | |
1190 | fi | |
1191 | ||
046c68c9 | 1192 | find_module "-$rootFs" |
ac085800 | 1193 | |
82b2dba2 | 1194 | for n in $BASICMODULES; do |
046c68c9 | 1195 | find_module "$n" |
82b2dba2 | 1196 | done |
4e9eb79c | 1197 | |
82b2dba2 | 1198 | if is_yes "$USE_TUXONICE"; then |
046c68c9 | 1199 | find_module "-lzf" |
82b2dba2 | 1200 | fi |
33d24e12 | 1201 | |
bf6d9c64 | 1202 | find_modules_fbsplash |
2968c9dd | 1203 | |
82b2dba2 ER |
1204 | if [ -n "$ifneeded" -a -z "$MODULES" ]; then |
1205 | debug "No modules are needed -- not building initrd image." | |
1206 | exit 0 | |
1207 | fi | |
c6c6ce01 | 1208 | |
6fc274fb | 1209 | debug "Building initrd..." |
d8056591 | 1210 | DESTDIR=$(mktemp -d -t initrd.XXXXXX) || die "mktemp failed" |
9b557a09 | 1211 | RCFILE="$DESTDIR/linuxrc" |
82b2dba2 ER |
1212 | > "$RCFILE" |
1213 | chmod a+rx "$RCFILE" | |
9b557a09 | 1214 | ln -s linuxrc $DESTDIR/init |
e8d178ff | 1215 | |
82b2dba2 ER |
1216 | # create dirs that we really need |
1217 | inst_d /{lib,bin,etc,dev{,/pts,/shm},loopfs,var,proc,sys} | |
e8d178ff | 1218 | |
82b2dba2 | 1219 | modules_install "$MODULES" |
b64f015b | 1220 | |
82b2dba2 ER |
1221 | # mknod'ing the devices instead of copying them works both with and |
1222 | # without devfs... | |
9b557a09 ER |
1223 | mknod "$DESTDIR/dev/console" c 5 1 |
1224 | mknod "$DESTDIR/dev/null" c 1 3 | |
1225 | mknod "$DESTDIR/dev/zero" c 1 5 | |
8aa2b6bd | 1226 | mknod "$DESTDIR/dev/random" c 1 8 |
94dce45c | 1227 | mknod "$DESTDIR/dev/urandom" c 1 9 |
be2f3ecc | 1228 | |
6ec4aea6 JR |
1229 | inst_exec $busybox /bin/busybox |
1230 | ln -s busybox $DESTDIR/bin/sh | |
1231 | # for older busyboxes who had /bin/initrd-busybox as EXEPATH | |
1232 | ln -s busybox $DESTDIR/bin/initrd-busybox | |
2ffb1734 | 1233 | |
82b2dba2 ER |
1234 | add_linuxrc <<EOF |
1235 | #!/bin/sh | |
1236 | # initrd generated by: | |
96fbf9e6 | 1237 | # $GENINITRD_RCSID |
c6c6ce01 | 1238 | |
82b2dba2 ER |
1239 | EOF |
1240 | mount_proc | |
1241 | add_linuxrc <<-'EOF' | |
3a1d3d88 | 1242 | read CMDLINE < /proc/cmdline; export CMDLINE |
c6c6ce01 | 1243 | |
82b2dba2 ER |
1244 | for arg in $CMDLINE; do |
1245 | if [ "${arg}" = "debuginitrd" ]; then | |
1246 | DEBUGINITRD=yes | |
1247 | fi | |
1248 | if [ "${arg##debuginitrd=}" != "${arg}" ]; then | |
1249 | DEBUGINITRD=${arg##debuginitrd=} | |
1250 | fi | |
fc787d45 | 1251 | if [ "${arg##root=}" != "${arg}" ]; then |
1252 | ROOT=${arg##root=} | |
1253 | fi | |
41d3645c AM |
1254 | if [ "${arg##rootfsflags=}" != "${arg}" ]; then |
1255 | ROOTFSFLAGS=${arg##rootfsflags=} | |
1256 | fi | |
82b2dba2 | 1257 | done |
c6c6ce01 | 1258 | |
82b2dba2 ER |
1259 | # make debugshell() invoke subshell if $DEBUGINITRD=sh |
1260 | if [ "$DEBUGINITRD" = "sh" ]; then | |
1261 | debugshell() { | |
e2405b29 AM |
1262 | EOF |
1263 | if is_yes "$RUN_SULOGIN_ON_ERR"; then | |
1264 | add_linuxrc <<-'EOF' | |
1265 | echo "debug shell disabled by /etc/sysconfig/system:RUN_SULOGIN_ON_ERR setting" | |
1266 | EOF | |
1267 | else | |
1268 | add_linuxrc <<-'EOF' | |
1269 | sh | |
1270 | EOF | |
1271 | fi | |
1272 | add_linuxrc <<-'EOF' | |
82b2dba2 ER |
1273 | } |
1274 | else | |
1275 | debugshell() { | |
1276 | : | |
1277 | } | |
1278 | fi | |
e934d044 | 1279 | |
82b2dba2 ER |
1280 | if [ "$DEBUGINITRD" ]; then |
1281 | set -x | |
cff3058d | 1282 | fi |
82b2dba2 | 1283 | EOF |
2df2e995 | 1284 | |
fe280785 | 1285 | modules_add_linuxrc $MODULES |
82b2dba2 ER |
1286 | |
1287 | # TODO: rewrite for busybox | |
1288 | #if [ -n "$loopDev" ]; then | |
1289 | # if [ ! -d /initrd ]; then | |
1290 | # mkdir /initrd | |
1291 | # fi | |
1292 | # | |
9b557a09 ER |
1293 | # cp -a "$loopDev" "$DESTDIR/dev" |
1294 | # cp -a "$rootdev" "$DESTDIR/dev" | |
82b2dba2 ER |
1295 | # echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE" |
1296 | # echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE" | |
1297 | # echo "echo Setting up loopback device $rootdev" >> $RCFILE | |
1298 | # echo "losetup $rootdev /loopfs$loopFile" >> "$RCFILE" | |
1299 | #fi | |
2b1a3707 | 1300 | |
1606e343 AM |
1301 | if is_yes "$USE_UDEV"; then |
1302 | initrd_gen_udev | |
1303 | fi | |
1304 | ||
3dd50160 ER |
1305 | find_modules_uvesafb |
1306 | initrd_gen_uvesafb | |
43258faf | 1307 | |
1b481849 ER |
1308 | initrd_gen_luks |
1309 | initrd_gen_dmraid | |
1310 | initrd_gen_multipath | |
1311 | initrd_gen_blkid | |
289fbc9b | 1312 | |
69b1e935 | 1313 | if is_yes "$have_nfs"; then |
2df2e995 | 1314 | initrd_gen_nfs |
1b481849 ER |
1315 | else |
1316 | initrd_gen_md | |
1317 | initrd_gen_lvm | |
1318 | initrd_gen_luks | |
4828c787 | 1319 | initrd_gen_setrootdev |
7c38b114 AF |
1320 | fi |
1321 | ||
1b481849 ER |
1322 | initrd_gen_tuxonice |
1323 | initrd_gen_suspend | |
4c16dac4 | 1324 | |
5101a385 | 1325 | # additional devs always needed |
9b557a09 | 1326 | [ ! -e "$DESTDIR/$rootdev_add" ] && inst $rootdev_add /dev |
5101a385 | 1327 | |
1b481849 | 1328 | initrd_gen_stop_udevd |
6fadace4 | 1329 | initrd_gen_stop_uvesafb |
82de999f | 1330 | |
f8f9e56d | 1331 | if [ "$INITRDFS" = "initramfs" ]; then |
4671d086 | 1332 | initrd_gen_initramfs_switchroot |
b64f015b | 1333 | else |
b64f015b | 1334 | umount_all |
f8f9e56d AM |
1335 | fi |
1336 | ||
1b481849 ER |
1337 | initrd_gen_fbsplash |
1338 | initrd_gen_fbcondecor | |
6c69a2d4 | 1339 | |
d8056591 | 1340 | IMAGE=$(mktemp -t initrd.img-XXXXXX) || die "mktemp failed" |
51dc1fe6 | 1341 | |
5c7359e3 ER |
1342 | IMAGESIZE=$(du -ks $DESTDIR | awk '{print int(($1+1023+512)/1024)*1024}') |
1343 | debug "image size: $IMAGESIZE KiB ($DESTDIR)" | |
1344 | ||
51dc1fe6 | 1345 | debug "Creating $INITRDFS image $IMAGE" |
2ad94d8a | 1346 | case "$INITRDFS" in |
4877276e | 1347 | ext2) |
4877276e | 1348 | dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null |
c091a192 ER |
1349 | # NOTE: ext2 label is max 16 chars |
1350 | mke2fs -q -F -b 1024 -m 0 -L "PLD/$kernel" "$IMAGE" 2>/dev/null 1>&2 | |
4877276e ER |
1351 | tune2fs -i 0 "$IMAGE" >/dev/null 2>&1 |
1352 | ||
1353 | local tmpmnt=$(mktemp -d -t initrd.mnt-XXXXXX) | |
1354 | debug "Mounting ext2 image $IMAGE to $tmpmnt" | |
5c7359e3 | 1355 | mount -o loop -t ext2 "$IMAGE" "$tmpmnt" || die "mount failed, check dmesg(1)" |
4877276e ER |
1356 | # We don't need this directory, so let's save space |
1357 | rm -rf "$tmpmnt"/lost+found | |
1358 | ||
1359 | debug "Copy recursively $DESTDIR -> $tmpmnt" | |
1360 | cp -a $DESTDIR/* $tmpmnt | |
1361 | umount "$IMAGE" | |
1362 | rmdir "$tmpmnt" | |
1363 | ||
1364 | ;; | |
1365 | rom|romfs) | |
c091a192 | 1366 | genromfs -f "$IMAGE" -d "$DESTDIR" -V "PLD Linux/$kernel (geninitrd/$VERSION)" |
4877276e ER |
1367 | ;; |
1368 | cram|cramfs) | |
1369 | mkcramfs "$DESTDIR" "$IMAGE" | |
1370 | ;; | |
1371 | initramfs) | |
1372 | (cd $DESTDIR; find . | cpio --quiet -H newc -o > "$IMAGE") | |
1373 | ;; | |
1374 | *) | |
93a38d1a | 1375 | die "Filesystem $INITRDFS not supported by $PROGRAM" |
c31050f3 | 1376 | esac |
bb529f94 | 1377 | |
755e5cdc | 1378 | CONFIG_BLK_DEV_RAM_SIZE=$(ikconfig | awk -F= '/^CONFIG_BLK_DEV_RAM_SIZE/{print $2}') |
25cb53df ER |
1379 | if [ -z "$CONFIG_BLK_DEV_RAM_SIZE" ]; then |
1380 | CONFIG_BLK_DEV_RAM_SIZE=4096 | |
1381 | fi | |
e3bc44e2 | 1382 | |
e3bc44e2 ER |
1383 | if [ "$IMAGESIZE" -gt $CONFIG_BLK_DEV_RAM_SIZE ]; then |
1384 | warn "Your image size is larger than $CONFIG_BLK_DEV_RAM_SIZE, Be sure to boot kernel with ramdisk_size=$IMAGESIZE!" | |
1385 | fi | |
1386 | ||
c552503c | 1387 | if ! is_no "$COMPRESS"; then |
c552503c ER |
1388 | compressor=$(find_compressor "$COMPRESS") |
1389 | debug "Compressing $target with $compressor" | |
1390 | ||
1391 | # TODO: the image name (specified from kernel.spec) already contains | |
1392 | # extension, which is .gz most of the time. | |
97f51b6e ER |
1393 | compress_image "$compressor" "$IMAGE" "$target" || { |
1394 | if [ $compressor != gzip ]; then | |
1395 | warn "Could not compress with $compressor, retrying with gzip" | |
1396 | compress_image gzip "$IMAGE" "$target" || die "compress failed with gzip" $? | |
1397 | else | |
1398 | die "Could not compress image with $compressor" | |
1399 | fi | |
1400 | } | |
bb529f94 | 1401 | else |
7d2fc5eb | 1402 | cp -a "$IMAGE" "$target" |
bb529f94 | 1403 | fi |
5b71959c | 1404 | |
7992356a | 1405 | # XXX. check if bootsplash can output data to tmp dir not directly to initramfs image. |
1b481849 | 1406 | initrd_gen_bootsplash "$target" |
5b71959c | 1407 | |
d8056591 | 1408 | rm -rf "$DESTDIR" "$IMAGE" |
ac085800 ER |
1409 | |
1410 | # vim:ts=4:sw=4:noet:fdm=marker |