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