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