]> git.pld-linux.org Git - projects/geninitrd.git/blame - geninitrd
Cosmetics.
[projects/geninitrd.git] / geninitrd
CommitLineData
bb529f94
JK
1#!/bin/sh
2
3# geninitrd
4#
4e9eb79c 5# by Jacek Konieczny <jajcus@pld-linux.org>
bb529f94 6#
413878f8 7# based on mkinitrd from RedHat
c31050f3 8
28eec2b1 9RCSID='$Id$'
bb529f94
JK
10PATH=/sbin:$PATH
11export PATH
12
2b649ae0 13VERSION="`echo "$RCSID"|awk '{print $3}'`"
bb529f94
JK
14
15. /etc/rc.d/init.d/functions
16
17COMPRESS="yes"
2ad94d8a
AF
18# INITRDFS is set later (catch obsoleted FS option)
19#INITRDFS="rom"
6bd433bc 20USERAIDSTART="yes"
d3fa6ec7 21USEMDADMSTATIC="no"
b2e62ae1 22USEINSMODSTATIC="no"
4ef98747 23USE_SUSPEND="yes"
335cd101 24USE_SUSPEND2="yes"
7c38b114 25uselvm="no"
6fe19d5b 26usenfs="no"
10c3df06
MM
27# it should be safe to remove scsi_mod from here, but I'm not sure...
28PRESCSIMODS="-scsi_mod unknown -sd_mod"
64497ebb
AM
29PREIDEMODS="-ide-core unknown -ide-detect -ide-disk"
30PREIDEMODSOLD="-ide-probe -ide-probe-mod -ide-disk"
bb529f94
JK
31target=""
32kernel=""
33force=""
34verbose=""
35MODULES=""
36img_vers=""
f121024f 37modulefile=""
bb529f94 38fstab="/etc/fstab"
0868f49f
AF
39insmod="insmod"
40modext=".o"
4e9eb79c 41rootdev_nr=0
5b71959c
AM
42# default bootsplash is off, if it have to be on, install bootsplash package
43BOOT_SPLASH=no
bb529f94 44
b4d9d384
ER
45# is /dev on tmpfs. internal variable
46tmpfs_dev=
47
326d9c3a 48if [ -f /etc/udev/udev.conf -a -x /sbin/initrd-udev ]; then
1606e343 49 USE_UDEV="yes"
1606e343
AM
50 . /etc/udev/udev.conf
51fi
882472ae
ER
52
53if [ -x /sbin/dmraid-initrd ]; then
54 USE_DMRAID="yes"
55fi
ac085800 56
bb529f94 57usage () {
ac085800
ER
58 echo "usage: `basename $0` [--version] [-v] [-f] [--ifneeded] [--preload <module>]"
59 echo " [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]"
60 echo " [--initrdfs=rom|ext2|cram] [--modules-conf=<modules.conf>]"
61 echo " [--with-raidstart] [--without-raidstart] [--with-insmod-static]"
d7d5530e 62 echo " [--without-bootsplash] [--lvmtoolsversion=1|2] [--with-udev] [--without-udev]"
4ef98747 63 echo " [--without-suspend] [--without-suspend2] [--without-dmraid]"
ac085800
ER
64 echo " <initrd-image> <kernel-version>"
65 echo " (ex: `basename $0` /boot/initrd-2.2.5-15.img 2.2.5-15)"
7d2fc5eb 66 exit 1
bb529f94
JK
67}
68
94769f11
ER
69debug() {
70 [ -n "$verbose" ] && "$*"
71}
aa69da6e 72
10c3df06
MM
73find_depmod () {
74 typeset mods module f level depfile first
75
76 depfile=/lib/modules/$kernel/modules.dep
ac085800 77
b2e62ae1 78 if [ -f $depfile ] ; then
f6de9380
MM
79 : ok
80 else
94769f11 81 echo >&2 "Error: no $depfile ! Run depmod and rerun geninitrd."
c83257bc 82 exit 1
f6de9380 83 fi
ac085800 84
0868f49f 85 # prepend / if no path given, append $modext.gz if not given,
10c3df06 86 # quote /
3b00e899 87 origmodule="$2"
10c3df06 88 module=$(echo "$2" | \
ac085800
ER
89 awk '/\// {print;next} {print "/" $0}' | \
90 awk '/\./ {print;next} {print $0 "'$modext'.gz"}' |
91 awk '{gsub("/","\\/");print}')
10c3df06
MM
92 mods=$(awk '
93BEGIN { here = 0 }
94/'"$module"':(.*)/ { gsub(/:/," "); gsub(/\\/," "); print; here = 1; next }
95/:/ { here = 0 }
96/(.*)/ { gsub(/\\/," "); if (here) print }
4fd11db9
AF
97' $depfile | xargs)
98
0868f49f 99 # fallback to $modext
4fd11db9 100 if [ "$mods" = "" ] ; then
ac085800
ER
101 module=$(echo "$module" | \
102 awk '{gsub("\'$modext'\.gz$","\'$modext'",$0);print}')
103 fi
4fd11db9
AF
104 mods=$(awk '
105BEGIN { here = 0 }
106/'"$module"':(.*)/ { gsub(/:/," "); gsub(/\\/," "); print; here = 1; next }
107/:/ { here = 0 }
108/(.*)/ { gsub(/\\/," "); if (here) print }
10c3df06
MM
109' $depfile | xargs)
110
111 if [ "$mods" = "" ] ; then
112 if [ "$1" != silent ] ; then
94769f11 113 echo >&2 "$origmodule: module not found in $depfile"
10c3df06 114 fi
ac085800 115 if ! is_no "$EXIT_IF_MISSING" ; then
fd2dc249
AF
116 exit 1
117 else
94769f11 118 echo >&2 "If $origmodule isn't compiled in kernel then this initrd may not start your system."
fd2dc249 119 fi
10c3df06 120 fi
ac085800 121
10c3df06
MM
122 level=$3
123 if [ "$level" = "" ] ; then
124 level=0
125 fi
94769f11 126 level=$((level + 1))
10c3df06 127 if [ $level -gt 20 ] ; then
94769f11 128 echo >&2 "$origmodule: cycle in $depfile"
10c3df06
MM
129 exit 1
130 fi
ac085800 131
10c3df06
MM
132 first=
133 for f in $mods ; do
134 if [ "$first" = "" ] ; then
135 first=$f
413878f8 136 else
10c3df06 137 find_depmod $1 $f $level
9c9f7cdb 138 fi
aa69da6e 139 done
ac085800 140
10c3df06 141 echo $first
aa69da6e 142}
143
10c3df06
MM
144addmodule() {
145 fmPath=$1
146 skiperrors=$2
147
148 if [ ! -f "/lib/modules/$kernel/$fmPath" ]; then
149 if [ -n "$skiperrors" ]; then
150 return
151 fi
152
94769f11 153 echo >&2 "module $fmPath present in modules.dep, but not in filesystem (kernel = $kernel)"
10c3df06
MM
154 exit 1
155 fi
156
157 # only need to add each module once
158 # quote /
159 tmpFmPath=$(echo $fmPath | awk '{gsub(/\//,"\\/");print}')
160 if echo "$MODULES" | awk '/'"$tmpFmPath"'/ {exit 1}' ; then
161 MODULES="$MODULES $fmPath"
162 fi
81de8443
AF
163}
164
bb529f94 165findmodule() {
413878f8 166 skiperrors=""
167 modName=$1
10c3df06 168
94769f11 169 if [ ${modName#-} != ${modName} ]; then
413878f8 170 skiperrors=1
94769f11 171 modName=${modName#-}
413878f8 172 fi
bb529f94 173
10c3df06 174 # what's that?
413878f8 175 if [ "$modName" = "pluto" ]; then
176 findmodule fc4
177 findmodule soc
178 fi
179 if [ "$modName" = "fcal" ]; then
180 findmodule fc4
181 findmodule socal
182 fi
bb529f94 183
10c3df06 184 if [ -n "$skiperrors" ]; then
94769f11 185 allModulesToFind=$(find_depmod silent $modName)
10c3df06 186 else
94769f11
ER
187 allModulesToFind=$(find_depmod normal $modName)
188 if [ $? != 0 ]; then
10c3df06 189 exit 1
413878f8 190 fi
413878f8 191 fi
ac085800 192
94769f11
ER
193 for mod in $allModulesToFind; do
194 mod=$(echo $mod | awk '{sub(/^\/lib\/modules\/[^\/]*\//, ""); print}')
10c3df06
MM
195 addmodule $mod "$skiperrors"
196 done
bb529f94
JK
197}
198
199inst() {
413878f8 200 if [ "$#" != "2" ];then
201 echo "usage: inst <file> <destination>"
202 return
c31050f3 203 fi
94769f11 204 debug "$1 -> $2"
413878f8 205 cp "$1" "$2"
bb529f94
JK
206}
207
a2c2980f
AF
208get_label_ext2 () {
209 /sbin/e2label $1 2> /dev/null
210}
211
212get_uuid_ext2 () {
c31050f3 213 /sbin/tune2fs -l $1 2> /dev/null | awk -F: '/UUID:/ {gsub(" ",""); print $2}'
a2c2980f
AF
214}
215
216get_label_xfs () {
3b9c8219 217 /usr/sbin/xfs_db -x -p xfs_admin -c label -r "$1"|awk -F= '{sub("^\"","", $2); sub("\"$", "", $2); print $2}'
c31050f3 218
a2c2980f
AF
219}
220
221get_uuid_xfs () {
8e9fd24e 222 /usr/sbin/xfs_admin -u "$1" 2>/dev/null | awk -F= '{print $2}' | xargs
a2c2980f
AF
223}
224
c31050f3 225find_root() {
a2c2980f
AF
226 eval `awk '/^[\t ]*#/ {next} {if ( $2 == "/" ) {print "rootdev=\"" $1 "\"\nrootFs=\"" $3 "\""}}' $fstab`
227 case $rootdev in
228 LABEL=*)
4fd11db9
AF
229 if [ -x /sbin/findfs -a \( "$rootFs." = "ext2." -o "$rootFs." = "ext3." \) ] ; then
230 rootdev2="`/sbin/findfs $rootdev 2>/dev/null`"
231 if [ -n "$rootdev2" ] ; then
232 rootdev=$rootdev2
233 rootdev_found=1
a2c2980f 234 fi
4fd11db9
AF
235 fi
236 if [ "$rootdev_found." != "1." ] ; then
237 case $rootFs in
238 ext2|ext3)
239 if [ ! -x /sbin/e2label ] ; then
94769f11 240 echo >&2 "/sbin/e2label is missing"
4fd11db9
AF
241 exit 1
242 fi
243 get_label="get_label_ext2"
244 ;;
245 xfs)
246 if [ ! -x /usr/sbin/xfs_db ] ; then
94769f11 247 echo >&2 "/usr/sbin/xfs_db is missing"
4fd11db9
AF
248 exit 1
249 fi
250 get_label="get_label_xfs"
251 ;;
252 *)
94769f11 253 echo >&2 "LABEL on $rootFs in not supported by geninitrd"
4fd11db9
AF
254 exit 1
255 ;;
256 esac
257 if [ ! -r /proc/partitions ] ; then
94769f11 258 echo >&2 '/proc/partitions is not readable'
3b9c8219
AF
259 exit 1
260 fi
4fd11db9
AF
261 label=${rootdev#"LABEL="}
262 for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
263 if [ -r $dev ] && [ "$label" = "`$get_label $dev`" ] ; then
94769f11 264 debug "Using $dev as device for rootfs"
4fd11db9
AF
265 rootdev=$dev
266 rootdev_found=1
267 break
a2c2980f 268 fi
4fd11db9
AF
269 done
270 if [ "$rootdev_found." != "1." ] ; then
94769f11 271 echo >&2 "geninitrd can't find real device for LABEL=$label"
4fd11db9
AF
272 exit 1
273 fi
a2c2980f
AF
274 fi
275 ;;
276 UUID=*)
4fd11db9
AF
277 if [ -x /sbin/findfs -a \( "$rootFs." = "ext2." -o "$rootFs." = "ext3." \) ] ; then
278 rootdev2="`/sbin/findfs $rootdev 2>/dev/null`"
279 if [ -n "$rootdev2" ] ; then
280 rootdev=$rootdev2
281 rootdev_found=1
a2c2980f 282 fi
4fd11db9
AF
283 fi
284 if [ "$rootdev_found." != "1." ] ; then
285 case $rootFs in
286 ext2|ext3)
287 if [ ! -x /sbin/tune2fs ] ; then
94769f11 288 echo >&2 "/sbin/tune2fs is missing"
4fd11db9
AF
289 exit 1
290 fi
291 get_uuid="get_uuid_ext2"
292 ;;
293 xfs)
6deef977 294 if [ ! -x /usr/sbin/xfs_admin ] ; then
94769f11 295 echo >&2 "/usr/sbin/xfs_admin is missing"
4fd11db9
AF
296 exit 1
297 fi
01832c44 298 get_uuid="get_uuid_xfs"
4fd11db9
AF
299 ;;
300 *)
94769f11 301 echo >&2 "UUID on $rootFs in not supported by geninitrd"
4fd11db9
AF
302 exit 1
303 ;;
304 esac
305 if [ ! -r /proc/partitions ] ; then
94769f11 306 echo >&2 '/proc/partitions is not readable'
3b9c8219
AF
307 exit 1
308 fi
4fd11db9 309 uuid=${rootdev#"UUID="}
f6536797 310 for dev in $(awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions); do
4fd11db9 311 if [ -r $dev ] && [ "$uuid" = "`$get_uuid $dev`" ] ; then
94769f11 312 debug "Using $dev as device for rootfs"
4fd11db9
AF
313 rootdev=$dev
314 rootdev_found=1
315 break
a2c2980f 316 fi
4fd11db9
AF
317 done
318 if [ "$rootdev_found" != 1 ] ; then
94769f11 319 echo >&2 "geninitrd can't find real device for UUID=$uuid"
4fd11db9
AF
320 exit 1
321 fi
a2c2980f
AF
322 fi
323 ;;
324 esac
4e9eb79c 325 rootdev1=${rootdev}
a2c2980f 326}
bb529f94 327
7c38b114 328find_modules_softraid() {
ac085800 329 if [ -f /etc/mdadm.conf ]; then
94769f11 330 debug "Finding RAID details using mdadm for rootdev=$1"
ac085800
ER
331 eval `/sbin/mdadm -v --examine --scan --config=/etc/mdadm.conf | awk -v rootdev="$1" '
332 BEGIN {
333 found = "no";
334 dev_list = "";
335 raidlevel = ""
336 rootdev_devfs = rootdev;
337 if (rootdev ~ /\/dev\/md\/[0-9]/) {
338 gsub(/\/dev\/md\//,"/dev/md",rootdev_devfs);
339 }
514a668a 340 }
ac085800
ER
341
342 /^ARRAY/ {
343 if (($2 == rootdev) || ($2 == rootdev_devfs)) {
344 raidlevel=$3;
345 gsub(/level=/,NUL,raidlevel);
346 if (raidlevel ~ /^raid([0-6]|10)/) {
347 gsub(/raid/,NUL,raidlevel);
348 };
349 found="yes";
350 getline x;
351 if (x ~ /devices=/) {
352 dev_list = x;
353 gsub(".*devices=", NUL, dev_list);
354 gsub(",", " ", dev_list);
355 }
356 }
56946560 357 }
ac085800
ER
358
359 END {
360 print "raidfound=" found;
361 print "raidlevel=" raidlevel;
362 print "dev_list=\"" dev_list "\"";
363 }'`
364 fi
7c38b114 365
59846acb 366 if [ "$raidfound" != "yes" -a -f /etc/raidtab ]; then
94769f11 367 echo >&2 "ERROR: raidtools are not longer supported. Please migrate to mdadm setup!"
59846acb 368 exit 1
809cc4cc 369 fi
ac085800 370
7c38b114
AF
371 if is_yes "$raidfound" ; then
372 case "$raidlevel" in
dc9a3d8d 373 [01456]|10)
7c38b114
AF
374 findmodule "raid$raidlevel"
375 ;;
376 linear)
377 findmodule "linear"
378 ;;
379 *)
94769f11 380 echo >&2 "raid level $number (in mdadm config) not recognized"
7c38b114
AF
381 ;;
382 esac
88614cd1 383 else
94769f11 384 echo >&2 "ERROR: RAID devices not found for \"$1\", check your configuration!"
88614cd1 385 exit 1
7c38b114 386 fi
4e9eb79c
AM
387
388 rootdev_nr=$(( $rootdev_nr + 1 ))
389 eval "rootdev${rootdev_nr}=\"$1\""
390 eval "dev_list${rootdev_nr}=\"${dev_list}\""
ac085800 391
7c38b114
AF
392 for device in $dev_list; do
393 find_modules_for $device
394 done
395}
396
397find_modules_scsi() {
398 for n in $PRESCSIMODS; do
399 if [ "X$n" = "Xunknown" ] ; then
7c38b114 400 if [ -f "$modulefile" ]; then
d4a15d5f 401 scsimodules="`awk '/scsi_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
7c38b114
AF
402 for n in $scsimodules; do
403 # for now allow scsi modules to come from anywhere. There are some
404 # RAID controllers with drivers in block
405 findmodule "$n"
406 done
407 fi
408 else
409 findmodule "$n"
410 fi
411 done
412}
413
414find_modules_ide() {
ac085800
ER
415 typeset rootdev
416
417 rootdev="$(echo "$1" | awk ' { gsub(/\/dev\//,NUL); gsub(/[0-9].*/, NUL); print $0 } ')"
418 if [ "$pack_version_long" -lt "002004021" ]; then
94769f11 419 debug "Finding IDE modules for kernels <= 2.4.20"
ac085800
ER
420 for n in $PREIDEMODSOLD; do
421 findmodule "$n"
422 done
64497ebb 423 else
ac085800
ER
424 tryauto=1
425 for n in $PREIDEMODS; do
426 if [ "X$n" = "Xunknown" ] ; then
427 if [ -f "$modulefile" ]; then
94769f11 428 debug "Finding IDE modules using ide_hostadapter"
ac085800
ER
429 idemodules="`awk '/ide_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
430 for na in $idemodules; do
431 tryauto=0;
432 findmodule "$na"
433 done
434 fi
64497ebb 435
ac085800
ER
436 if [ "$tryauto" -eq 1 ]; then
437 # If tryauto {{{
438 if [ -r /usr/share/pci-database/ide.pci -a -r /proc/bus/pci/devices ]; then
94769f11 439 debug "Finding IDE modules using PCI ID database"
ac085800
ER
440 # Finding IDE modules using PCI ID database {{{
441 if is_yes "${ide_only_root}"; then
442 if [ -f /sys/block/${rootdev}/device/../../vendor -a -f /sys/block/${rootdev}/device/../../device ]; then
443 vendorid="$(awk ' { gsub(/0x/,NUL); print $0 } ' /sys/block/${rootdev}/device/../../vendor)"
444 deviceid="$(awk ' { gsub(/0x/,NUL); print $0 } ' /sys/block/${rootdev}/device/../../device)"
445 searchpciid="${vendorid}${deviceid}"
446 elif [ -f /proc/ide/${rootdev}/../config ]; then
447 searchpciid="$(awk ' /pci bus/ { print $7$9 } ' /proc/ide/${rootdev}/../config)"
448 fi
449 fi
450
451 if [ -z "${searchpciid}" ]; then
452 searchpciid="$(awk ' { print $2 } ' /proc/bus/pci/devices)"
453 fi
454
455 idemodules=""
456
457 for nb in $searchpciid; do
458 eval `awk -v pciid="$nb" '{
459 gsub("\t"," ");
460 gsub(" +", " ");
461 gsub("^ ","");
462 if (/^[\t ]*#/)
463 next;
464 compmod = $1 ""; # make sure comparison type will be string
465 # cause pci IDs are hexadecimal numeric
466 if (compmod == pciid) {
467 module=$2;
468 # min_kernel=$3; # now in ide.pci $3,$4 = vendor and device name
469 # max_kernel=$4; #
470 exit 0;
471 }
472 }
473
474 END {
475 print "module=" module "\nmin_kernel=" min_kernel "\nmax_kernel=\"" max_kernel "\"\n";
476 }' /usr/share/pci-database/ide.pci`
477 [ -n "$module" ] && idemodules="$idemodules $module"
478 done
479 if is_yes "$(awk ' /ide=reverse/ { print "yes" } ' /proc/cmdline)"; then
480 new_idemodules=""
481 for nc in idemodules; do
482 new_idemodules="$nc $new_idemodules"
483 done
484 idemodules="${new_idemodules}"
485 fi
486
487 if [ -z "$idemodules" ]; then
488 echo "WARNING: rootfs on IDE device but no related modules found, loading ide-generic."
489 idemodules="ide-generic"
490 fi
491
492 # }}}
493 for nd in $idemodules; do
494 findmodule "-$nd"
495 done
496 # }}}
497 # else tryauto {{{
498 else
499 [ -r /usr/share/pci-database/ide.pci ] || echo "WARNING: /usr/share/pci-database/ide.pci missing."
500 [ -r /proc/bus/pci/devices ] || echo "WARNING: /proc/bus/pci/devices missing."
501 echo "Automatic IDE modules finding not available."
502 fi
503 # }}}
504 fi
505 else
506 findmodule "$n"
5de40f1f 507 fi
ac085800 508 done
64497ebb 509 fi
7c38b114
AF
510}
511
f6536797
ER
512# resolve /dev/dm-0 to lvm2 node
513# which they got from /proc/partitions when rootfs is UUID=
514_lvm2_node_resolve() {
515 local node="$1"
516
517 # redirect output to tmpfile so we won't get subshell
518 local tmp ret
519 tmp=$(mktemp) || exit 1
520
521 lvdisplay -c 2>/dev/null|awk -F: '{print $1}' > $tmp
522 while read dev; do
523 count=$(ls -Ll $dev $node | awk '{print $5, $6}' | sort -u | wc -l)
524 if [ "$count" = 1 ]; then
525 ret="$dev"
526 break
527 fi
528 done < $tmp
529 rm -f $tmp
530
531 if [ -z "$ret" ]; then
532 ret=$node
533 fi
534 echo "$ret"
535}
536
a7fce633
ER
537# return true if node is lvm node
538_check_lvm() {
539 local node="$1"
540 if [ ! -e "$node" ]; then
94769f11 541 echo >&2 "WARNING: check_lvm(): node $node doesn't exist!"
a7fce633
ER
542 return 1
543 fi
544
545 # block-major-58 is lvm1
546 ls -lL "$node" 2> /dev/null | awk '{if (/^b/) { if ($5 == "58,") { exit 0; } else { exit 1; } } else { exit 1; }}'
547 rc=$?
548
549 if [ $rc = 0 ]; then
94769f11 550 debug "$node is LVM1 node"
a7fce633
ER
551 # is lvm1
552 return 0
553 fi
554
f6536797 555 node=$(_lvm2_node_resolve $node)
a7fce633
ER
556 /sbin/lvm lvdisplay "$node" > /dev/null 2>&1
557 rc=$?
558 if [ $rc -gt 127 ]; then
559 # lvdisplay terminated by signal! most likely it segfaulted.
560 echo >&2 "ERROR: Unexpected exit from 'lvdisplay $node': $rc - are your lvm tools broken?"
561 exit 1
562 fi
563
564 if [ $rc = 0 ]; then
94769f11 565 debug "$node is lvm2 node"
a7fce633 566 else
94769f11 567 debug "$node is not any lvm node"
a7fce633
ER
568 fi
569 return $rc
570}
571
7c38b114 572find_modules_for() {
ac085800
ER
573 if [ -z "$1" ]; then
574 echo "ERROR: no argument passed to find_modules_for() - is your /etc/fstab correct?" >&2
4e9eb79c 575 exit
f6536797 576 fi
ac085800 577
f6536797 578 if is_yes "`echo "$1" | awk '/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:|\/dev\/nfs)/ { print "yes"; }'`"; then
5c4cec75 579 if [ ! -x /usr/bin/pcidev -a -z "$NFS_ETH_MODULES" ] ; then
ac085800
ER
580 echo "ERROR: root on NFS but /usr/bin/pcidev not found." >&2
581 echo "Please install correct pci-database package and rerun $0." >&2
582 exit 1
6fe19d5b 583 fi
5c4cec75
AM
584 [ -z "$NFS_ETH_MODULES" ] && NFS_ETH_MODULES=$(/usr/bin/pcidev /m net | xargs)
585 for m in $NFS_ETH_MODULES; do
ac085800 586 findmodule "$m"
6fe19d5b
AM
587 done
588 findmodule "-ipv4"
ac085800 589 findmodule "nfs"
6fe19d5b 590 usenfs="yes"
d024196b 591 echo "Remember to use \`root=/dev/ram0 init=/linuxrc' when starting kernel" >&2
ac085800 592 echo "or you will have problems like init(xx) being child process of swapper(1)." >&2
4e9eb79c 593 elif is_yes "`echo "$1" | awk '/^\/dev\/md/ { print "yes"; }'`"; then
7c38b114
AF
594 find_modules_softraid "$1"
595 elif is_yes "$(echo "$1" | awk '/^\/dev\/(sd|scsi)/ { print "yes"; }')" ; then
596 find_modules_scsi
5d27006d 597 elif is_yes "`echo "$1" | awk '/^\/dev\/(hd|ide)/ { print "yes"; }'`" ; then
afa3d7a8 598 find_modules_ide "$1"
7c38b114
AF
599 elif is_yes "`echo "$1" | awk '/\/dev\/rd\// { print "yes"; }'`" ; then
600 findmodule "DAC960"
601 elif is_yes "`echo "$1" | awk '/\/dev\/ida\// { print "yes"; }'`" ; then
602 findmodule "cpqarray"
603 elif is_yes "`echo "$1" | awk '/\/dev\/cciss\// { print "yes"; }'`" ; then
604 findmodule "cciss"
605 elif is_yes "`echo "$1" | awk '/\/dev\/ataraid\// { print "yes"; }'`"; then
ac085800 606 find_modules_ide
7c38b114 607 findmodule "ataraid"
d4a15d5f 608 ataraidmodules="`awk '/ataraid_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
7c38b114
AF
609 if -n "$ataraidmodules" ; then
610 # FIXME: think about modules compiled in kernel
611 echo "ERROR: ataraid_hostadapter alias not defined in $modulefile !"
612 echo "Please set it and run $0 again."
613 exit 1
614 fi
615 for n in $ataraidmodules; do
616 findmodule "$n"
617 done
618 # check to see if we need to set up a loopback filesystem
38228bb7 619 elif is_yes "`echo "$1" | awk -F/ '{print($3);}' | awk '/loop/ { print "yes"; }'`" ; then
94769f11 620 echo >&2 "Sorry, root on loop device isn't supported."
7c38b114
AF
621 exit 1
622 # TODO: rewrite for bsp and make nfs ready
623 if [ ! -x /sbin/losetup ]; then
624 echo "losetup is missing"
625 exit 1
626 fi
627 key="^# $(echo $1 | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
628 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`" ; then
94769f11
ER
629 echo >&2 "The root filesystem is on a $1, but there is no magic entry in $fstab"
630 echo >&2 "for this device. Consult the geninitrd man page for more information"
7c38b114
AF
631 exit 1
632 fi
633
634 line="`awk '/'$key'/ { print $0; }' $fstab`"
635 loopDev="$(echo $line | awk '{print $3}')"
636 loopFs="$(echo $line | awk '{print $4}')"
637 loopFile="$(echo $line | awk '{print $5}')"
638
639 BASICMODULES="$BASICMODULES -loop"
640 findmodule "-$loopFs"
641 BASICMODULES="$BASICMODULES -${loopFs}"
a7fce633 642 elif _check_lvm "$1"; then
f6536797
ER
643 node=$(_lvm2_node_resolve "$1")
644
9f2d32ed
AM
645 if [ ! -f /sbin/initrd-lvm -o ! -x /sbin/lvdisplay -o ! -x /sbin/pvdisplay ] ; then
646 echo "ERROR: root on LVM but /sbin/initrd-lvm, /sbin/lvdisplay and /sbin/pvdisplay not found." >&2
cff3058d 647 echo "Please install lvm(2) and lvm(2)-initrd package and rerun $0." >&2
7c38b114
AF
648 exit 1
649 fi
ac085800
ER
650 if [ -z "$LVMTOOLSVERSION" ] ; then
651 LVMTOOLSVERSION=$(/sbin/initrd-lvm vgchange --version 2>/dev/null|head -n 1|awk '{gsub("vgchange: Logical Volume Manager ",NIL); gsub("LVM version: ",NIL); gsub(/\..*/,NIL); print $1}')
652 if [ -z "$LVMTOOLSVERSION" ] ; then
653 echo "ERROR: Can't determine LVM tools version. Please set LVMTOOLSVERSION" >&2
654 echo "and rerun $0." >&2
655 exit 1
656 fi
77bcfc68 657 fi
decd277c 658 if [ -z "$PVDEVICES" ] ; then
f6536797 659 VGVOLUME=$(/sbin/lvdisplay -c "$node" 2> /dev/null | awk -F":" ' { print $2 } ')
ac085800 660 PVDEVICES=$(/sbin/pvdisplay -c | awk -F":" -v vg="$VGVOLUME" ' BEGIN { devices="" } { if ($2 == vg) { devices = devices " " $1 } } END { print devices } ')
decd277c 661 fi
7c38b114
AF
662 if [ -n "$PVDEVICES" ] ; then
663 for device in $PVDEVICES; do
664 find_modules_for $device
665 done
666 else
ac085800 667 echo "ERROR: I wasn't able to find PV (via lvdisplay and pvdisplay)." >&2
b78e6497 668 echo "You can try to set PVDEVICES in /etc/sysconfig/geninitrd." >&2
7c38b114
AF
669 exit 1
670 fi
e0c502bb 671 if [ "$LVMTOOLSVERSION" = "2" ]; then
ac085800 672 findmodule "-dm-mod"
e0c502bb 673 elif [ "$LVMTOOLSVERSION" = "1" ]; then
ac085800
ER
674 findmodule "-lvm"
675 findmodule "-lvm-mod"
93d781d1 676 else
e0c502bb 677 echo "ERROR: LVM version $LVMTOOLSVERSION is not supported yet." >&2
cff3058d 678 exit 1
93d781d1 679 fi
94769f11 680 debug "LVM $LVMTOOLSVERSION enabled"
7c38b114
AF
681 uselvm="yes"
682 fi
683}
684
9ed6e1db
AM
685firmware_install_module_pre() {
686 module="$1"
687 linuxrc="$2"
688 firmware_files="$3"
689
94769f11 690 debug "Adding Firmwares ($firmware_files) to initrd for module $module"
f21ff30b
AM
691 mkdir -p $MNTIMAGE/proc
692 mkdir -p $MNTIMAGE/sys
9ed6e1db
AM
693 # firmware not yet installed
694 if [ ! -f "$MNTIMAGE/lib/firmware/firmware.sh" ]; then
695 mkdir -p $MNTIMAGE/lib/firmware
696cat << 'EOF' >> "$MNTIMAGE/lib/firmware/firmware.sh"
697#!/bin/sh -e
8639f99a
AM
698echo 1 > /sys$DEVPATH/loading
699cat "/lib/firmware/$FIRMWARE" > /sys$DEVPATH/data
700echo 0 > /sys$DEVPATH/loading
701exit 0
9ed6e1db 702EOF
94769f11 703 chmod 755 "$MNTIMAGE/lib/firmware/firmware.sh"
9ed6e1db
AM
704 fi
705
706 for firmware in $firmware_files; do
707 inst "/lib/firmware/$firmware" "$MNTIMAGE/lib/firmware/$firmware"
708 done
709
710 echo "mount -t proc none /proc" >> "$linuxrc"
711 echo "mount -t sysfs none /sys" >> "$linuxrc"
0867ab75 712 echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" >> "$linuxrc"
9ed6e1db
AM
713}
714
715firmware_install_module_post() {
716 module="$1"
717 linuxrc="$2"
718 firmware_files="$3"
719 echo "umount /sys" >> "$linuxrc"
720 echo "umount /proc" >> "$linuxrc"
721}
722
2a5bcca9
AM
723modules_install() {
724 modules="$1"
725
726 for mod in $modules; do
87d21026 727 MODULEDIR="`dirname "$mod"`"
ac085800 728 mkdir -p "$MNTIMAGE/lib/modules/$kernel/$MODULEDIR"
dd87738f 729 cp -a "/lib/modules/$kernel/$mod" "$MNTIMAGE/lib/modules/$kernel/$mod"
ac085800 730 gunzip "$MNTIMAGE/lib/modules/$kernel/$mod" 2> /dev/null
2a5bcca9
AM
731 done
732}
733
734modules_add_linuxrc() {
735 modules="$1"
736 linuxrc="$2"
ac085800 737
2a5bcca9 738 for mod in $modules; do
87d21026 739 MODULE2="`dirname "$mod"`"
ac085800
ER
740 NAME2=`basename "$mod" .gz`
741 MODULE2=$MODULE2/$NAME2
742 module="`echo $mod | awk -F/ '{ $0=$NF } /'$modext'.*$/ { gsub(/'$modext'.*/, NIL, $0); } { print $0; }'`"
743 options="`awk '{ if($1 == "options" && $2 == "'${module}'") { for(i=3;i<=NF;i++) printf("%s ",$i); }}' "$modulefile"`"
ffa1b198 744
9ed6e1db
AM
745 generic_module=$(echo "${module}" | awk ' { gsub("-", "_", $0) } { print $0; } ')
746 sleep_var="$(eval echo \$MODULE_${generic_module}_USLEEP)"
94769f11 747 firmware_var="$(eval echo \$MODULE_${generic_module}_FIRMWARE)"
2a5bcca9 748
ac085800
ER
749 if [ -n "$verbose" ]; then
750 echo -n "Loading module [$module] "
751 if [ -n "$options" ] ; then
752 echo -n "with options [$options]"
753 else
754 echo -n "without options"
755 fi
8e598759 756 if [ -n "$sleep_var" ]; then
ba0552ce 757 echo " and $sleep_var usleep."
8e598759
AM
758 else
759 echo "."
760 fi
ac085800
ER
761 fi
762
9ed6e1db
AM
763 if [ -n "$firmware_var" ]; then
764 firmware_install_module_pre "$module" "$linuxrc" "$firmware_var"
765 fi
ac085800 766 echo "$insmod /lib/modules/$kernel/$MODULE2 $options" >> "$linuxrc"
8e598759
AM
767 if [ -n "${sleep_var}" ]; then
768 echo "usleep $sleep_var" >> "$linuxrc"
769 fi
9ed6e1db
AM
770 if [ -n "$firmware_var" ]; then
771 firmware_install_module_post "$module" "$linuxrc" "$firmware_var"
772 fi
2a5bcca9
AM
773 done
774}
775
5dc785c7 776if [ -r /etc/sysconfig/geninitrd ] ; then
777 . /etc/sysconfig/geninitrd
778fi
779
5b71959c
AM
780if [ -r /etc/sysconfig/bootsplash ] ; then
781 . /etc/sysconfig/bootsplash
782fi
783
ac085800 784if [ ! -x /bin/initrd-busybox ] ; then
2baa76b4 785 echo "/bin/initrd-busybox is missing !"
fd2dc249 786 exit 1
5dc785c7 787fi
901afd79 788
bb529f94 789while [ $# -gt 0 ]; do
413878f8 790 case $1 in
6bd433bc
AF
791 --fstab=*)
792 fstab="`echo $1 | awk -F= '{print $2;}'`"
793 ;;
794 --fstab)
795 fstab="$2"
796 shift
413878f8 797 ;;
cd670d83
AF
798 --modules-conf=*)
799 modulefile="`echo $1 | awk -F= '{print $2;}'`"
800 ;;
57188d60 801 --modules-conf)
cd670d83
AF
802 modulefile="$2"
803 shift
804 ;;
6bd433bc
AF
805 --use-raidstart|--with-raidstart)
806 USERAIDSTART="yes"
807 ;;
808 --without-raidstart)
809 USERAIDSTART="no"
810 ;;
57227e0a
AF
811 --use-insmod-static|--with-insmod-static)
812 USEINSMODSTATIC="yes"
813 ;;
814 --without-insmod-static)
815 USEINSMODSTATIC="no"
816 ;;
5b71959c
AM
817 --without-bootsplash)
818 BOOT_SPLASH="no"
819 ;;
4ef98747
AM
820 --without-suspend)
821 USE_SUSPEND="no";
22b0c9f4 822 ;;
335cd101
JR
823 --without-suspend2)
824 USE_SUSPEND2="no";
2c52f208 825 ;;
e0c502bb
AM
826 --lvmtoolsversion=|--lvmversion=)
827 LVMTOOLSVERSION="`echo $1 | awk -F= '{print $2;}'`"
77bcfc68 828 ;;
e0c502bb
AM
829 --lvmtoolsversion|--lvmversion)
830 LVMTOOLSVERSION="$2"
77bcfc68
AF
831 shift
832 ;;
1606e343
AM
833 --without-udev)
834 USE_UDEV=
835 ;;
d7d5530e
ER
836 --with-udev)
837 USE_UDEV="yes"
838 ;;
882472ae
ER
839 --without-dmraid)
840 USE_DMRAID=
841 ;;
6bd433bc
AF
842 --with=*)
843 BASICMODULES="$BASICMODULES `echo $1 | awk -F= '{print $2;}'`"
844 ;;
845 --with)
846 BASICMODULES="$BASICMODULES $2"
847 shift
413878f8 848 ;;
bb529f94 849 --version)
413878f8 850 echo "geninitrd: version $VERSION"
851 exit 0
852 ;;
bb529f94 853 -v)
413878f8 854 verbose=-v
855 ;;
bb529f94 856 --nocompress)
413878f8 857 COMPRESS="no"
858 ;;
bb529f94 859 --ifneeded)
413878f8 860 ifneeded=1
861 ;;
bb529f94 862 -f)
413878f8 863 force=1
864 ;;
6bd433bc
AF
865 --preload=*)
866 PREMODS="$PREMODS `echo $1 | awk -F= '{print $2;}'`"
867 ;;
868 --preload)
72623c13 869 PREMODS="$PREMODS $2"
6bd433bc 870 shift
413878f8 871 ;;
2ad94d8a 872 --fs=*)
94769f11 873 echo >&2 "Warning: --fs option is obsoleted. Use --initrdfs instead"
2ad94d8a
AF
874 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
875 ;;
2ad94d8a 876 --fs)
94769f11 877 echo >&2 "Warning: --fs option is obsoleted. Use --initrdfs instead"
2ad94d8a
AF
878 INITRDFS="$2"
879 shift
cd670d83 880 ;;
6bd433bc
AF
881 --initrdfs=*)
882 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
883 ;;
fd2dc249 884 --initrdfs)
2ad94d8a 885 INITRDFS="$2"
413878f8 886 shift
887 ;;
bb529f94 888 --image-version)
413878f8 889 img_vers=yes
890 ;;
4f4e832d
AM
891 --ide-only-root)
892 ide_only_root="yes"
893 ;;
bb529f94 894 *)
413878f8 895 if [ -z "$target" ]; then
896 target="$1"
897 elif [ -z "$kernel" ]; then
898 kernel="$1"
899 else
900 usage
901 fi
902 ;;
903 esac
904
905 shift
bb529f94
JK
906done
907
06ac05d6 908if [ -z "$target" -o -z "$kernel" ]; then
413878f8 909 usage
06ac05d6
AF
910fi
911
0868f49f 912pack_version="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d",$1,$2)}'`"
64497ebb 913pack_version_long="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d%03d",$1,$2,$3)}'`"
0868f49f 914
48413b7f
AM
915if [ "x" = "x$INITRDFS" ] ; then
916 if [ "x" = "x$FS" ] ; then
917 # default value
f8f9e56d
AM
918 # XXX: initramfs blocked for now
919 if [ "1" = " 0" -a "$pack_version" -ge "002005" ] ; then
ac085800 920 INITRDFS="initramfs"
f8f9e56d 921 else
ac085800 922 INITRDFS="rom"
f8f9e56d 923 fi
48413b7f 924 else
94769f11 925 echo >&2 "Warning: FS configuration options is obsoleted. Use INITRDFS instead"
48413b7f
AM
926 INITRDFS="$FS"
927 fi
928fi
929
1606e343 930if [ "$pack_version" -lt "002006" ] ; then
882472ae
ER
931 USE_UDEV=
932 USE_DMRAID=
1606e343 933fi
ac085800 934
0868f49f
AF
935if [ "$pack_version" -ge "002005" ] ; then
936 modext=".ko"
0b6536f5 937 insmod="insmod"
0868f49f
AF
938fi
939
57227e0a
AF
940if is_yes "$USEINSMODSTATIC" ; then
941 insmod="insmod.static"
b2e62ae1 942 INSMOD="/sbin/insmod.static"
f7c18703
AM
943 if [ "$pack_version" -lt "002005" -a -f /sbin/insmod.static.modutils ] ; then
944 INSMOD="/sbin/insmod.static.modutils"
b2e62ae1
AF
945 fi
946 if [ ! -f "$INSMOD" ] ; then
947 echo "insmod.static requested but /sbin/insmod.static not found !" >&2
948 exit 1
949 fi
57227e0a
AF
950fi
951
2ad94d8a 952case "$INITRDFS" in
8b1e4ac7 953 ext2)
f5d71bb2 954 if [ ! -x /sbin/mke2fs ]; then
94769f11 955 echo >&2 "/sbin/mke2fs is missing"
8b1e4ac7
AF
956 exit 1
957 fi
958 ;;
959 rom)
960 if [ ! -x /sbin/genromfs ]; then
94769f11 961 echo >&2 "/sbin/genromfs is missing"
8b1e4ac7
AF
962 exit 1
963 fi
964 ;;
965 cram)
966 if [ ! -x /sbin/mkcramfs ]; then
94769f11 967 echo >&2 "/sbin/mkcramfs is missing"
8b1e4ac7
AF
968 exit 1
969 fi
c31050f3 970 ;;
48413b7f
AM
971 initramfs)
972 if [ ! -x /bin/cpio ]; then
94769f11 973 echo >&2 "/bin/cpio is missing"
48413b7f
AM
974 exit 1
975 fi
976 if [ ! -x /usr/bin/find ]; then
94769f11 977 echo >&2 "/usr/bin/find is missing"
48413b7f
AM
978 exit 1
979 fi
980 ;;
8b1e4ac7 981 *)
94769f11 982 echo >&2 "Filesystem $INITRDFS on initrd is not supported"
8b1e4ac7
AF
983 exit 1
984 ;;
985esac
bb529f94 986
bb529f94 987if [ -n "$img_vers" ]; then
413878f8 988 target="$target-$kernel"
bb529f94
JK
989fi
990
35164381 991if [ -z "$force" -a -f "$target" ]; then
94769f11 992 echo >&2 "$target already exists."
413878f8 993 exit 1
bb529f94
JK
994fi
995
35164381 996if [ ! -d "/lib/modules/$kernel" ]; then
94769f11 997 echo >&2 "/lib/modules/$kernel is not a directory."
413878f8 998 exit 1
bb529f94
JK
999fi
1000
2ce7656a
AM
1001if is_yes "$USE_SUSPEND"; then
1002 if is_yes "$USE_SUSPEND2"; then
94769f11 1003 echo >&2 "Suspend2 shouldn't be used in parallel with mainline suspend!."
22b0c9f4
AM
1004 exit 1
1005 fi
1006fi
1007
48762878 1008if [ ! -f /proc/mounts ]; then
94769f11 1009 echo >&2 "WARNING: /proc filesystem not mounted, may cause wrong results or failure."
48762878 1010fi
f121024f
AM
1011
1012if [ "$pack_version" -lt "002005" ]; then
1013 modulefile=/etc/modules.conf
1014 if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then
1015 modulefile=/etc/conf.modules
1016 fi
1017else
1018 modulefile=/etc/modprobe.conf
1019fi
ac085800 1020
bb529f94 1021for n in $PREMODS; do
35164381 1022 findmodule "$n"
bb529f94
JK
1023done
1024
7c38b114
AF
1025# allow forcing loading SCSI and/or IDE modules
1026if is_yes "$ADDSCSI" ; then
1027 find_modules_scsi
bb529f94
JK
1028fi
1029
7c38b114
AF
1030if is_yes "$ADDIDE" ; then
1031 find_modules_ide
cd670d83
AF
1032fi
1033
7c38b114 1034find_root
cd670d83 1035
4e1e40bd
AM
1036org_rootdev="$rootdev1"
1037
4e9eb79c 1038find_modules_for "$rootdev1"
cd670d83 1039
7c38b114 1040findmodule "-$rootFs"
bb529f94 1041
c31050f3 1042for n in $BASICMODULES; do
7d2fc5eb 1043 findmodule "$n"
bb529f94
JK
1044done
1045
335cd101
JR
1046if is_yes "$USE_SUSPEND2"; then
1047 findmodule "-lzf"
1048fi
1049
bb529f94 1050if [ -n "$ifneeded" -a -z "$MODULES" ]; then
94769f11 1051 debug "No modules are needed -- not building initrd image."
7d2fc5eb 1052 exit 0
bb529f94
JK
1053fi
1054
94769f11 1055debug "Using modules: $MODULES"
bb529f94 1056
35164381
SZ
1057MNTIMAGE="`mktemp -d /tmp/initrd.XXXXXX`"
1058IMAGE="`mktemp -u /tmp/initrd.img-XXXXXX`"
1059MNTPOINT="`mktemp -d /tmp/initrd.mnt-XXXXXX`"
1060RCFILE="$MNTIMAGE/linuxrc"
bb529f94 1061
35164381 1062if [ -f "$MNTIMAGE" ]; then
94769f11 1063 echo >&2 "$MNTIMAGE already exists. Remove it and try again"
7d2fc5eb 1064 exit 1
bb529f94
JK
1065fi
1066
35164381 1067if [ -f "$IMAGE" ]; then
94769f11 1068 echo >&2 "$IMAGE already exists. Remove it and try again"
7d2fc5eb 1069 exit 1
bb529f94
JK
1070fi
1071
73edc0c1 1072mkdir -p "$MNTPOINT"
bb529f94 1073
8b1e4ac7 1074
cff3058d 1075mkdir -p "$MNTIMAGE"/{lib,bin,etc,dev,loopfs,var}
8b1e4ac7 1076
bb529f94
JK
1077
1078# We don't need this directory, so let's save space
35164381 1079rm -rf "$MNTPOINT"/lost+found
bb529f94 1080
2a5bcca9 1081modules_install "$MODULES"
bb529f94
JK
1082
1083# mknod'ing the devices instead of copying them works both with and
1084# without devfs...
35164381 1085mknod "$MNTIMAGE/dev/console" c 5 1
eb6e37ff
MM
1086mknod "$MNTIMAGE/dev/null" c 1 3
1087mknod "$MNTIMAGE/dev/zero" c 1 5
bb529f94 1088
84670990 1089s="$RCFILE"
566ad08b 1090ln -s /linuxrc $MNTIMAGE/init
ef66f232 1091
5fe0ec4a
ER
1092inst /bin/initrd-busybox "$MNTIMAGE/bin/initrd-busybox"
1093ln -s initrd-busybox "$MNTIMAGE/bin/sh"
578e304a 1094ln -s initrd-busybox "$MNTIMAGE/bin/busybox" # for older busyboxes who had /bin/busybox as EXEPATH
ef66f232 1095
b2e62ae1
AF
1096if is_yes "$USEINSMODSTATIC" ; then
1097 inst "$INSMOD" $MNTIMAGE/bin/insmod.static
57227e0a
AF
1098fi
1099
84670990
MM
1100cat > "$s" <<EOF
1101#! /bin/sh
1102
d70364fa 1103set -x
84670990
MM
1104EOF
1105chmod 755 "$s"
c31050f3 1106
2a5bcca9 1107modules_add_linuxrc "$MODULES" "$s"
fd2dc249 1108
4e9eb79c 1109# TODO: rewrite for busybox
fd2dc249
AF
1110#if [ -n "$loopDev" ]; then
1111# if [ ! -d /initrd ]; then
1112# mkdir /initrd
1113# fi
1114#
1115# cp -a "$loopDev" "$MNTIMAGE/dev"
4e9eb79c 1116# cp -a "$rootdev1" "$MNTIMAGE/dev"
fd2dc249
AF
1117# echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
1118# echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
4e9eb79c
AM
1119# echo "echo Setting up loopback device $rootdev1" >> $RCFILE
1120# echo "losetup $rootdev1 /loopfs$loopFile" >> "$RCFILE"
fd2dc249 1121#fi
bb529f94 1122
22b0c9f4
AM
1123initrd_gen_suspend() {
1124 if [ ! -x /usr/sbin/resume ]; then
1125 echo "/usr/sbin/resume is missing !"
1126 exit 1
1127 fi
1128 mkdir -p $MNTIMAGE/etc
1129 mkdir -p $MNTIMAGE/dev
1130 resume_dev="$(awk '/^resume device =/ { print $4 } ' /etc/suspend.conf)"
1131 cp -HR /dev/snapshot $resume_dev $MNTIMAGE/dev
1132 inst /etc/suspend.conf $MNTIMAGE/etc/suspend.conf
1133 inst /usr/sbin/resume "$MNTIMAGE/bin/resume"
1134 echo "resume" >> "$s"
1135}
1136
9426259f 1137initrd_gen_suspend2() {
ac085800
ER
1138 mkdir -p $MNTIMAGE/sys
1139 mkdir -p $MNTIMAGE/proc
5a61a17c 1140cat << 'EOF' >> "$s"
2b1a3707 1141mount -t proc none /proc
5a61a17c 1142if [ "$(awk ' /resume2=/ { print "yes"; } ' /proc/cmdline)" = "yes" ]; then
2b1a3707 1143EOF
2b1a3707 1144cat << EOF >> "$s"
21c802bf 1145 echo > /proc/suspend2/do_resume
2b1a3707
AM
1146fi
1147umount /proc
1148EOF
2df2e995 1149}
2c52f208 1150
b4d9d384
ER
1151initrd_gen_tmpfs_dev() {
1152 tmpfs_dev=yes
1153 cat <<-EOF
1154: 'Creating /dev'
1155mount -o mode=0755 -t tmpfs none /dev
1156mknod /dev/console c 5 1
1157mknod /dev/null c 1 3
1158mknod /dev/zero c 1 5
1159mkdir /dev/pts
1160mkdir /dev/shm
1161EOF
1162}
1163
1606e343 1164initrd_gen_udev() {
94769f11 1165 debug "Setting up udev..."
1606e343 1166 mkdir -p $MNTIMAGE/sbin
d152a29d 1167 mkdir -p $MNTIMAGE/proc
1606e343 1168 mkdir -p $MNTIMAGE/etc/udev
29d03731 1169 mkdir -p $MNTIMAGE/sys
ac085800 1170
1606e343
AM
1171 inst /sbin/initrd-udev $MNTIMAGE/sbin/udev
1172 ln -s udev $MNTIMAGE/sbin/udevstart
1173 inst /etc/udev/udev.conf $MNTIMAGE/etc/udev/udev.conf
1174
1175 ln -s udev $MNTIMAGE/sbin/hotplug
1176
1177 if is_yes "$USE_UDEV"; then
b4d9d384 1178 initrd_gen_tmpfs_dev >> "$s"
882472ae 1179 cat >> "$s" <<-EOF
b161812e 1180mount -t proc none /proc
d8bf1484 1181mount -t sysfs none /sys
9bb6d02a 1182: 'Starting udev'
1606e343 1183/sbin/udevstart
9bb6d02a 1184echo -n /sbin/hotplug > /proc/sys/kernel/hotplug
b161812e 1185umount /proc
29d03731 1186umount /sys
1606e343
AM
1187EOF
1188 fi
1189}
1190
882472ae 1191initrd_gen_dmraid() {
ac085800
ER
1192 if [ ! -x /sbin/dmraid-initrd ] ; then
1193 echo "/sbin/dmraid-initrd is missing missing !"
1194 exit 1
1195 fi
1196
e5d02b56 1197 # AFAIR it just needs tmpfs on /dev.
ac085800
ER
1198 if [ ! -x $MNTIMAGE/sbin/udev ]; then
1199 echo "udev is needed on target initrd for dmraid to work!"
1200 exit 1
1201 fi
1202
1203 mkdir -p "$MNTIMAGE/sbin"
1204 inst /sbin/dmraid-initrd $MNTIMAGE/sbin/dmraid
9bb6d02a 1205 cat <<-EOF >> "$s"
882472ae
ER
1206 mount -t proc none /proc
1207 mount -t sysfs none /sys
1208 # 2 secs was enough for my system to initialize. but really this is udev issue?
1209 usleep 2000000
e5d02b56 1210 : 'Activating Device-Mapper RAID(s)'
882472ae
ER
1211 /sbin/dmraid -ay -i
1212 umount /sys
1213 umount /proc
1214EOF
1215}
1216
1217
2df2e995 1218initrd_gen_softraid() {
94769f11 1219 debug "Setting up mdadm..."
ac085800 1220
6cca3165 1221 if [ ! -x /sbin/mdadm -o ! -x /sbin/initrd-mdassemble ] ; then
ac085800 1222 echo "/sbin/mdadm or /sbin/initrd-mdassemble is missing !"
8c4ce35e 1223 exit 1
d3fa6ec7 1224 fi
994eb509 1225
6cca3165
AM
1226 inst /sbin/initrd-mdassemble "$MNTIMAGE/bin/mdassemble"
1227
4e9eb79c 1228 # LVM on RAID case
6cca3165
AM
1229 dev_list_extra=$(awk '/^DEVICE / { for (i=2; i<=NF; i++) { printf "%s ", $i; }; } ' /etc/mdadm.conf)
1230 for ex_dev in $dev_list_extra; do
1231 echo "DEVICE $ex_dev" >> "$MNTIMAGE/etc/mdadm.conf"
1232 done
ac085800 1233 do_md0=1
4e9eb79c 1234 for nr in `seq 1 $rootdev_nr`; do
ac085800
ER
1235 eval cr_rootdev="\$rootdev${nr}"
1236 eval cr_dev_list="\$dev_list${nr}"
94769f11 1237 debug echo "Setting up array ($cr_rootdev = $cr_dev_list)"
ac085800
ER
1238
1239 [ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
1240
1241 echo "DEVICE $cr_dev_list" >> "$MNTIMAGE/etc/mdadm.conf"
1242 cr_dev_list_md="$(echo "$cr_dev_list" | xargs | awk ' { gsub(/ +/,",",$0); print $0; }')"
1243 cr_md_conf=$(/sbin/mdadm --detail --brief --config=/etc/mdadm.conf $cr_rootdev)
1244 if [ -n "$cr_md_conf" ]; then
1245 echo "$cr_md_conf" >> "$MNTIMAGE/etc/mdadm.conf"
1246 else
1247 echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$MNTIMAGE/etc/mdadm.conf"
1248 fi
1249
1250 for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
1251 # mkdir in case of devfs name
87d21026 1252 mkdir -p $MNTIMAGE/`dirname $f`
ac085800 1253 [ -e "$MNTIMAGE/$f" ] && continue
94769f11 1254 debug echo "copying $f"
ac085800
ER
1255 # this works fine with and without devfs
1256 cp -HR $f $MNTIMAGE/$f
1257 done
ef66f232 1258 done
4e9eb79c 1259
6cca3165 1260 echo "mdassemble" >> "$s"
33d24e12 1261
4e9eb79c
AM
1262 # needed to determine md-version
1263 if [ "$do_md0" -eq 1 ] ; then
1264 mknod $MNTIMAGE/dev/md0 b 9 0
1265 fi
2df2e995 1266}
2968c9dd 1267
2df2e995 1268initrd_gen_nfs() {
5a61a17c 1269 # use root=/dev/ram0 init=/linuxrc when starting kernel or you will
51447ab3 1270 # have problems like init(XX) being child process of swapper(1).
94769f11 1271 debug "Adding rootfs on NFS support to initrd (dhcp)"
53623c90 1272 mknod "$MNTIMAGE/dev/urandom" c 1 8
5c4cec75 1273 mkdir "$MNTIMAGE/newroot"
7d400dbe 1274 mkdir "$MNTIMAGE/proc"
5c4cec75
AM
1275 echo "ifconfig lo 127.0.0.1 up" >> "$s"
1276 echo "route add -net 127.0.0.0 netmask 255.0.0.0 lo" >> "$s"
1277 echo "ifconfig eth0 0.0.0.0 up" >> "$s"
ac085800 1278 echo "udhcpc -i eth0 -f -q -s /bin/setdhcp" >> "$s"
5a61a17c 1279 cat << 'EOF' > "$MNTIMAGE/bin/setdhcp"
5c4cec75 1280#!/bin/sh
5a61a17c
ER
1281[ "$1" != "bound" ] && exit
1282[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
1283[ -n "$subnet" ] && NETMASK="netmask $subnet"
5c4cec75 1284set -x
5a61a17c 1285ifconfig $interface $ip $BROADCAST $NETMASK up
5c4cec75 1286set +x
5a61a17c 1287if [ -n "$router" ]; then
ac085800
ER
1288 for r in $router; do
1289 set -x
1290 route add default gw $r dev $interface
1291 set +x
1292 done
5c4cec75 1293fi
7d400dbe
ER
1294
1295mount -t proc none /proc
5a61a17c
ER
1296for o in $(cat /proc/cmdline); do
1297 case $o in
7d400dbe 1298 nfsroot=*)
5a61a17c 1299 rootpath=${o#nfsroot=}
7d400dbe
ER
1300 ;;
1301 esac
1302done
1303umount /proc
1304
5a61a17c 1305if [ -n "$rootpath" ]; then
ac085800
ER
1306 set -x
1307 mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath /newroot
1308 set +x
5c4cec75 1309else
ac085800
ER
1310 set +x
1311 echo "Missing rootpath in what DHCP server sent to us. Failing..."
1312 echo "All seen variables are listed below:"
1313 set
1314 set -x
5c4cec75
AM
1315fi
1316EOF
1317 chmod 755 "$MNTIMAGE/bin/setdhcp"
1318 echo "cd /newroot" >> "$s"
c52ba2fe 1319 echo "pivot_root . initrd" >> "$s"
d4a8e40e 1320 echo "[ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
c52ba2fe 1321 echo "exec /usr/sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
2df2e995 1322}
6fe19d5b 1323
2df2e995 1324initrd_gen_lvm() {
94769f11 1325 debug "Adding LVM support to initrd"
7c38b114 1326 inst /sbin/initrd-lvm $MNTIMAGE/bin/lvm
f34c76a3
AM
1327 mkdir -p $MNTIMAGE/etc
1328 mkdir -p $MNTIMAGE/tmp
1329 mkdir -p $MNTIMAGE/proc
4e1e40bd 1330 mkdir -p $MNTIMAGE/newroot
e8d178ff
ER
1331
1332 # always make /dev on tmpfs for LVM2
1333 if [ "$LVMTOOLSVERSION" = "2" ] && ! is_yes "$tmpfs_dev"; then
1334 initrd_gen_tmpfs_dev >> "$s"
1335 fi
1336
b4d9d384 1337 if ! is_yes "$tmpfs_dev"; then
afaefcb5
AM
1338 mkdir -p $MNTIMAGE/dev/mapper
1339 mknod $MNTIMAGE/dev/mapper/control c 10 63
b007399c 1340 for device in $PVDEVICES; do
13b29c8f
ER
1341 # if LVM on RAID then device might be copied already in gen_softraid
1342 [ -e "$MNTIMAGE/dev/$(basename $device)" ] && continue
7bdf23ec 1343 cp -HR $device $MNTIMAGE/dev/
b007399c 1344 done
afaefcb5 1345 fi
84670990 1346 echo "mount -t proc none /proc" >> "$s"
b78e6497 1347 echo "mount -t tmpfs none /tmp" >> "$s"
e0c502bb 1348 if [ "$LVMTOOLSVERSION" = "1" ] ; then
cff3058d 1349 echo "lvm vgscan -T" >> "$s"
90b5b05a 1350 echo "lvm vgchange -T -a y $VGVOLUME" >> "$s"
4e1e40bd 1351 echo "umount /tmp" >> "$s"
ac085800
ER
1352 # fail to umount
1353 echo "umount /dev" >> "$s"
1354 echo "umount /proc" >> "$s"
cff3058d 1355 else
f6536797 1356 org_rootdev=$(_lvm2_node_resolve "$org_rootdev")
ac085800
ER
1357 echo "cat /etc/lvm.conf > /tmp/lvm.conf" >> "$s"
1358 echo "global {" > "$MNTIMAGE/etc/lvm.conf"
f34c76a3
AM
1359 echo " locking_type = 0" >> "$MNTIMAGE/etc/lvm.conf"
1360 echo " locking_dir = \"/tmp\"" >> "$MNTIMAGE/etc/lvm.conf"
1361 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
ac085800 1362 echo "devices {" >> "$MNTIMAGE/etc/lvm.conf"
8fb0a4d2 1363 echo " sysfs_scan=0" >> "$MNTIMAGE/etc/lvm.conf"
f34c76a3 1364 if is_yes "$raidfound"; then
ac085800 1365 echo " md_component_detection = 1" >> "$MNTIMAGE/etc/lvm.conf"
f34c76a3
AM
1366 fi
1367 lvm dumpconfig | awk '/filter=/' >> "$MNTIMAGE/etc/lvm.conf"
1368 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
be2f3ecc 1369
c79b26a6 1370 cat <<-EOF >> "$s"
e8d178ff 1371 : 'Making device nodes'
dd87738f 1372 set +x
e8d178ff
ER
1373 (
1374 # ignore first two lines, header and empty line
1375 read a
1376 read a
1377 while read major minor blocks dev; do
94769f11 1378 mknod /dev/\$dev b \$major \$minor
e8d178ff
ER
1379 done
1380 ) < /proc/partitions
1381
dd87738f
ER
1382 # disable noise from lvm accessing devices that aren't ready.
1383 printk=\$(cat /proc/sys/kernel/printk)
e8d178ff 1384 echo 0 > /proc/sys/kernel/printk
dd87738f
ER
1385 set -x
1386
1387 : 'Scanning for Volume Groups'
e8d178ff 1388 LVM_SYSTEM_DIR=/tmp lvm vgscan --ignorelockingfailure 2>/dev/null
e8d178ff 1389
c79b26a6
ER
1390 : 'Activating Volume Groups'
1391 LVM_SYSTEM_DIR=/tmp lvm vgchange --ignorelockingfailure -a y $VGVOLUME 2>/dev/null
be2f3ecc 1392
dd87738f
ER
1393 echo "\$printk" > /proc/sys/kernel/printk
1394
91ed0242 1395 # Find out major/minor
e8d178ff 1396 majmin="\$(LVM_SYSTEM_DIR=/tmp lvm lvdisplay --ignorelockingfailure -c $org_rootdev 2>/dev/null)"
c79b26a6
ER
1397 majmin="\${majmin#*/}"
1398 majmin="\${majmin#*:*:*:*:*:*:*:*:*:*:*:*}"
1399 major="\${majmin%:*}"
1400 minor="\${majmin#*:}"
91ed0242 1401 # Pass it to kernel
c79b26a6
ER
1402 val=\$((256 * \$major + \$minor))
1403 echo \$val > /proc/sys/kernel/real-root-dev
1404 umount /tmp
1405 umount /proc
1406EOF
cff3058d 1407 fi
2df2e995
AM
1408}
1409
2b1a3707 1410initrd_gen_procdata() {
94769f11 1411 debug "Adding rootfs finding based on root= option support."
2b1a3707 1412 mkdir -p $MNTIMAGE/proc
5a61a17c 1413cat << 'EOF' >> "$s"
2b1a3707
AM
1414set +x
1415mount -t proc none /proc
5a61a17c
ER
1416root="$(busybox awk ' /root=\/dev\// { gsub(/.*root=\/dev\//,NIL,$0); gsub(/ .*/,NIL,$0); print $0; } ' /proc/cmdline)"
1417if [ -n "$root" ]; then
ac085800
ER
1418 rootnr="$(busybox awk -v root="$root" ' { if ($4 == root) { print 256*$1+$2; } } ' /proc/partitions)"
1419 if [ -n "$rootnr" ]; then
1420 echo "$rootnr" > /proc/sys/kernel/real-root-dev
1421 fi
2b1a3707 1422fi
9d949203 1423umount /proc
2b1a3707
AM
1424set -x
1425EOF
1426}
1427
2df2e995 1428# main generation
1606e343
AM
1429
1430if is_yes "$USE_UDEV"; then
1431 initrd_gen_udev
1432fi
1433
2ce7656a 1434if is_yes "$USE_SUSPEND"; then
22b0c9f4
AM
1435 initrd_gen_suspend
1436fi
1437
2ce7656a 1438if is_yes "$USE_SUSPEND2"; then
9426259f 1439 initrd_gen_suspend2
2df2e995
AM
1440fi
1441
882472ae
ER
1442if is_yes "$USE_DMRAID"; then
1443 initrd_gen_dmraid
1444fi
1445
2df2e995
AM
1446if is_yes "$usenfs" ; then
1447 initrd_gen_nfs
1448elif is_yes "$USERAIDSTART" && is_yes "$raidfound" ; then
1449 initrd_gen_softraid
1450 if is_yes "$uselvm" ; then
1451 initrd_gen_lvm
bfea009a
AM
1452 else
1453 initrd_gen_procdata
2df2e995
AM
1454 fi
1455elif is_yes "$uselvm" ; then
1456 initrd_gen_lvm
2b1a3707
AM
1457else
1458 initrd_gen_procdata
7c38b114
AF
1459fi
1460
f8f9e56d
AM
1461if [ "$INITRDFS" = "initramfs" ]; then
1462 mkdir -p $MNTIMAGE/newroot
1463 cp -HR $org_rootdev $MNTIMAGE/dev
1464 echo "mount -t $rootFs $org_rootdev /newroot" >> "$s"
1465 echo "switch_root /newroot /sbin/init" >> "$s"
1466 # we need real file, not symlink
1467 rm -f $MNTIMAGE/init
1468 cp -a $MNTIMAGE/linuxrc $MNTIMAGE/init
1469fi
1470
35164381 1471chmod +x "$RCFILE"
bb529f94 1472
35164381 1473(cd "$MNTIMAGE"; tar cf - .) | (cd "$MNTPOINT"; tar xf -)
bb529f94 1474
2ad94d8a 1475case "$INITRDFS" in
8b1e4ac7 1476 ext2)
3edb8220 1477 IMAGESIZE=$(du -ks $MNTPOINT | awk '{print int(($1+1023)/1024)*1024}')
94769f11 1478 debug "ext2 image size: $IMAGESIZE ($MNTPOINT)"
73edc0c1
ER
1479 if [ "$IMAGESIZE" -gt 4096 ]; then
1480 echo >&2 "$0: Your image size is larger than 4096, You should add ramdisk_size=$IMAGESIZE to your kernel commandline!"
1481 fi
1482
1483 dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
1484 mke2fs -q -F -b 1024 -m 0 "$IMAGE" 2>/dev/null 1>&2
1485 tune2fs -i 0 "$IMAGE" >/dev/null 2>&1
1486
1487 tmpmnt="`mktemp -d /tmp/initrd.mnte2-XXXXXX`"
1488 mount -o loop -t ext2 "$IMAGE" "$tmpmnt"
1489
1490 (cd "$MNTPOINT"; tar cf - .) | (cd "$tmpmnt"; tar xf -)
1491
35164381 1492 umount "$IMAGE"
8b1e4ac7
AF
1493 ;;
1494 rom)
1495 genromfs -f "$IMAGE" -d "$MNTPOINT" -V "PLD initrd for kernel $kernel"
1496 ;;
1497 cram)
1498 mkcramfs "$MNTPOINT" "$IMAGE"
8b1e4ac7 1499 ;;
48413b7f 1500 initramfs)
566ad08b 1501 (cd $MNTPOINT ; find . | cpio -H newc -o > "$IMAGE")
48413b7f 1502 ;;
8b1e4ac7 1503 *)
2ad94d8a 1504 echo "Filesystem $INITRDFS not supported by $0";
c31050f3 1505esac
bb529f94
JK
1506
1507if is_yes "$COMPRESS" ; then
7d2fc5eb 1508 gzip -9 < "$IMAGE" > "$target"
bb529f94 1509else
7d2fc5eb 1510 cp -a "$IMAGE" "$target"
bb529f94 1511fi
5b71959c
AM
1512
1513if is_yes "$BOOT_SPLASH"; then
1514 if [ ! -x /bin/splash.bin ]; then
94769f11 1515 echo >&2 "Failed to execute /bin/splash.bin. Is bootsplash package installed?"
5b71959c 1516 elif [ -z "$THEME" ]; then
94769f11
ER
1517 echo >&2 "Please configure your /etc/sysconfig/bootsplash first."
1518 echo >&2 "Generating bootsplashes skipped."
5b71959c 1519 else
ac085800
ER
1520 if [ -n "$BOOT_SPLASH_RESOLUTIONS" ]; then
1521 for res in $BOOT_SPLASH_RESOLUTIONS; do
1522 if [ -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" ]; then
1523 /bin/splash.bin -s -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" >> "$target" && \
94769f11 1524 debug "Added $res $THEME theme to initrd."
ac085800 1525 else
94769f11 1526 echo >&2 "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped"
ac085800
ER
1527 fi
1528 done
1529 else
94769f11
ER
1530 echo >&2 "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash."
1531 echo >&2 "Not adding bootsplash to initrd."
ac085800 1532 fi
5b71959c
AM
1533 fi
1534fi
1535
35164381 1536rm -rf "$MNTIMAGE" "$MNTPOINT" "$IMAGE"
ac085800
ER
1537
1538# vim:ts=4:sw=4:noet:fdm=marker
This page took 0.319499 seconds and 4 git commands to generate.