]> git.pld-linux.org Git - projects/geninitrd.git/blob - geninitrd
- no modules are to be found from directories
[projects/geninitrd.git] / geninitrd
1 #!/bin/sh
2
3 # geninitrd
4 #
5 #       by PLD Linux Team
6 #
7 # based on mkinitrd from RedHat Linux
8 #
9 # TODO:
10 # - make udev start before insmods
11 # - make proper use of USE_UDEV - don't copy rules if USE_UDEV is off no!
12 #
13
14 RCSID='$Id$'
15 R=${RCSID#* * }; VERSION=${R%% *}
16 PROGRAM=${0##*/}
17
18 . /etc/rc.d/init.d/functions
19 . /etc/geninitrd/functions
20 . /etc/sysconfig/system
21
22 COMPRESS=yes
23 USERAIDSTART=yes
24 USEMDADMSTATIC=no
25 USEINSMODSTATIC=no
26 USE_SUSPEND=yes
27 USE_TUXONICE=no
28 # it should be safe to remove scsi_mod from here, but I'm not sure...
29 PRESCSIMODS="-scsi_mod unknown -sd_mod"
30 target=""
31 kernel=""
32 force=""
33 verbose=""
34 MODULES=""
35 img_vers=""
36 fstab=/etc/fstab
37 insmod=insmod
38 modext=.o
39 rootdev_nr=0
40 # device node for rootfs from fstab
41 rootdev=""
42 # requires bootsplash package to operate
43 BOOT_SPLASH=no
44 # requires splashutils package to operate
45 FB_SPLASH=no
46 # requires splashutils package to operate
47 FB_CON_DECOR=no
48
49 # internal variables
50 # is /dev on tmpfs
51 dev_mounted=no
52 # is /proc mounted
53 proc_mounted=no
54 # is /sys mounted
55 sys_mounted=no
56 # is /tmp mounted on tmpfs
57 tmp_mounted=no
58
59 # are /dev nodes already created from /proc/devices info?
60 proc_partitions=no
61
62 # whether v86d should be installed
63 need_v86d=0
64
65 # resume device
66 resume_dev=""
67
68 # if we should init NFS at boot
69 have_nfs=no
70 # if we should init md (softraid) at boot
71 have_md=no
72
73 usage() {
74         uname_r=$(uname -r)
75         echo "usage: $PROGRAM [--version] [-v] [-f] [--ifneeded] [--preload <module>]"
76         echo "       [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]"
77         echo "       [--initrdfs=rom|initramfs|ext2|cram] [--modules-conf=<modules.conf>]"
78         echo "       [--with-raidstart] [--without-raidstart] [--with-insmod-static]"
79         echo "       [--with-bootsplash] [--without-bootsplash]"
80         echo "       [--with-fbsplash] [--without-fbsplash]"
81         echo "       [--with-fbcondecor] [--without-fbcondecor]"
82         echo "       [--lvmtoolsversion=1|2] [--with-udev] [--without-udev]"
83         echo "       [--with-suspend] [--without-suspend]"
84         echo "       [--with-tuxonice] [--without-tuxonice]"
85         echo "       [--without-dmraid] [--without-multipath]"
86         echo "       [--without-blkid] [--without-luks]"
87         echo "       <initrd-image> <kernel-version>"
88         echo ""
89         echo "example:"
90         echo "  $PROGRAM -f --initrdfs=rom /boot/initrd-$uname_r.gz $uname_r"
91         exit 0
92 }
93
94 msg() {
95         echo "$PROGRAM: $*"
96 }
97
98 warn() {
99         msg "WARNING: $*" >&2
100 }
101
102 debug() {
103         [ -n "$verbose" ] && msg "$*" >&2
104 }
105
106 # aborts program abnormally
107 die() {
108         local rc=${2:-1}
109         msg "ERROR: $1" 2>&2
110         exit $rc
111 }
112
113 # append text to /linuxrc
114 # takes STDIN as input
115 add_linuxrc() {
116         cat >> "$RCFILE"
117 }
118
119 # generate code to mount /dev on tmpfs and create initial nodes
120 # can be called multiple times. /dev is cleaned up (umounted) automatically at
121 # the end of script.
122 mount_dev() {
123     if [ "$INITRDFS" = "initramfs" ]; then
124                 # initramfs is read-write filesystem, no need for tmpfs
125                 return
126         fi
127
128         # we already generated tmpfs code; return
129         if is_yes "$dev_mounted"; then
130                 return
131         fi
132
133         dev_mounted=yes
134
135         busybox_applet mount mknod mkdir
136         add_linuxrc <<-EOF
137                 : 'Creating /dev'
138                 mount -o mode=0755 -t tmpfs none /dev
139                 mknod /dev/console c 5 1
140                 mknod /dev/null c 1 3
141                 mknod /dev/zero c 1 5
142                 mkdir /dev/pts
143                 mkdir /dev/shm
144         EOF
145 }
146
147 # generate code to mount /proc on initrd
148 # can be called multiple times
149 mount_proc() {
150         if is_yes "$proc_mounted"; then
151                 return
152         fi
153
154         proc_mounted=yes
155     if [ "$INITRDFS" = "initramfs" ]; then
156                 # /proc is mounted with initramfs 2.6.22.14 kernel
157                 # XXX: remove when it is clear why proc was already mounted
158                 echo "[ -f /proc/cmdline ] || mount -t proc none /proc" | add_linuxrc
159         else
160                 echo "mount -t proc none /proc" | add_linuxrc
161         fi
162 }
163
164 # generate code to mount /sys on initrd
165 # can be called multiple times
166 mount_sys() {
167         if is_yes "$sys_mounted"; then
168                 return
169         fi
170
171         sys_mounted=yes
172         echo "mount -t sysfs none /sys" | add_linuxrc
173 }
174
175 # generate code to mount /tmp on initrd
176 # can be called multiple times
177 mount_tmp() {
178     if [ "$INITRDFS" = "initramfs" ]; then
179                 # initramfs is read-write filesystem, no need for tmpfs
180                 return
181         fi
182
183         if is_yes "$tmp_mounted"; then
184                 return
185         fi
186
187         tmp_mounted=yes
188         echo "mount -t tmpfs none /tmp" | add_linuxrc
189 }
190
191 # unmount all mountpoints mounted by geninitrd
192 umount_all() {
193
194         add_linuxrc <<-'EOF'
195         : Last shell before umounting all and giving control over to real init.
196         debugshell
197         EOF
198
199         if is_yes "$dev_mounted"; then
200                 echo 'umount /dev' | add_linuxrc
201                 dev_mounted=no
202         fi
203         if is_yes "$proc_mounted"; then
204                 echo 'umount /proc' | add_linuxrc
205                 proc_mounted=no
206         fi
207         if is_yes "$sys_mounted"; then
208                 echo 'umount /sys' | add_linuxrc
209                 sys_mounted=no
210         fi
211         if is_yes "$tmp_mounted"; then
212                 echo 'umount /tmp' | add_linuxrc
213                 tmp_mounted=no
214         fi
215 }
216
217
218 # Checks if busybox has support for APPLET(s)
219 # Exits from geninitrd if the support is not present.
220 #
221 # NB! XXX do not output to STDOUT, it will appear in initrd images in some cases!
222 busybox_applet() {
223         local err=0
224
225         if [ -z "$busybox_functions" ]; then
226                 local tmp=$(/bin/initrd-busybox 2>&1)
227
228                 # BusyBox v1.1.3 says applet not found if it's not called 'busybox'.
229                 if [[ "$tmp" = *applet\ not\ found* ]]; then
230                         local t=$(mktemp -d)
231                         ln -s /bin/initrd-busybox $t/busybox
232                         local tmp=$($t/busybox 2>&1)
233                         rm -rf $t
234                 fi
235
236                 busybox_functions=$(echo "$tmp" | \
237                         sed -ne '/Currently defined functions:/,$p' | \
238                         xargs | sed -e 's,.*Currently defined functions: ,,'
239                 )
240         fi
241         for applet in $*; do
242                 local have
243                 # try cache
244                 eval have='$'busybox_have_$applet
245                 if [ -z "$have" ]; then
246                         have=$(echo "$busybox_functions" | egrep -c "( |^)$applet(,|$)")
247                         if [ "$have" = 0 ]; then
248                                 warn "This setup requires busybox-initrd compiled with applet '$applet' support"
249                                 err=1
250                         fi
251                         eval busybox_have_$applet=$have
252                 fi
253         done
254         if [ $err = 1 ]; then
255                 die "Aborted"
256         fi
257 }
258
259 # Finds module dependencies
260 #
261 # @param        $module
262 # @param        $mode   [silent]
263 #
264 # Outputs each dependant module full path including the module itself.
265 find_depmod() {
266         local module="$1"
267         local mode="$2"
268
269         # backwards compatible, process $mode
270         local modfile=$(modinfo -k $kernel -n $module 2>/dev/null)
271         if [ ! -f "$modfile" ]; then
272                 # module not found. try aliases!
273                 local alias aliases=$(modprobe_conf | awk -vm="$module" '$1 == "alias" && $2 == m { print $3 }')
274                 if [ "$aliases" ]; then
275                         for alias in $aliases; do
276                                 debug "Try $alias alias for $module"
277                                 find_depmod "$alias"
278                         done
279                         return
280                 fi
281
282                 if [ "$mode" != "silent" ]; then
283                         warn "$module: module not found for $kernel kernel"
284                 fi
285                 if ! is_no "$EXIT_IF_MISSING"; then
286                         exit 1
287                 else
288                         warn "If $module isn't compiled in kernel then this initrd may not start your system."
289                 fi
290         fi
291
292         # This works when user has module-init-tools installed even on 2.4 kernels
293         modprobe --set-version $kernel --show-depends $module --ignore-install | \
294         while read insmod modpath options; do
295                 [ "$insmod" = "insmod" ] && echo $modpath
296         done
297 }
298
299 find_firmware() {
300         local module="$1"
301         echo -n $(modinfo -k $kernel -F firmware $module 2>/dev/null | xargs)
302 }
303
304 findmodule() {
305         local skiperrors=""
306         local modName=$1
307
308         if [ ${modName#-} != $modName ]; then
309                 skiperrors=1
310                 modName=${modName#-}
311         fi
312
313         # what's that?
314         if [ "$modName" = "pluto" ]; then
315                 findmodule fc4
316                 findmodule soc
317         fi
318         if [ "$modName" = "fcal" ]; then
319                 findmodule fc4
320                 findmodule socal
321         fi
322
323         local mod depmod
324         if [ "$skiperrors" = 1 ]; then
325                 depmod=$(find_depmod $modName silent)
326         else
327                 depmod=$(find_depmod $modName)
328                 if [ $? != 0 ]; then
329                         exit 1
330                 fi
331         fi
332
333         for mod in $depmod; do
334                 mod=${mod#/lib/modules/$kernel/}
335
336                 # add each module only once
337                 local m have=0
338                 for m in $MODULES; do
339                         [ $m = $mod ] && have=1
340                 done
341                 if [ $have = 0 ]; then
342                         MODULES="$MODULES $mod"
343
344                         # if we are adding uvesafb, we need v86d as well
345                         if [ "$modName" = "uvesafb" ]; then
346                                 need_v86d=yes
347                         fi                              
348                 fi
349         done
350 }
351
352 # install a file to temporary mount image.
353 # it will operate recursively (copying directories)
354 # and will symlink destinations if source is symlink.
355 inst() {
356         if [ $# -lt 2 ]; then
357                 die 'Usage: inst <file> [<file>] <destination>'
358         fi
359
360         local src i=0 c=$(($# - 1))
361         while [ $i -lt $c ]; do
362                 src="$src $1"
363                 i=$((i + 1))
364                 shift
365         done
366         local dest=$1
367         set -- $src
368         parentDir=$(dirname $DESTDIR$dest)
369         [ ! -d "$parentDir" ] && (debug "+ mkdir -p $parentDir"; mkdir -p $parentDir)
370         debug "+ cp $* $DESTDIR$dest"
371         cp -HR "$@" "$DESTDIR$dest"
372 }
373
374 inst_d() {
375         if [ $# = 0 ]; then
376                 die 'Usage: inst_d <destination> <destination>'
377         fi
378         for dir in "$@"; do
379                 install -d "$DESTDIR$dir"
380         done
381 }
382
383 # install executable and it's shared libraries
384 inst_exec() {
385         if [ $# -lt 2 ]; then
386                 die 'Usage: inst_exec <file>[, <file>] <destination>'
387         fi
388         local src i=0 c=$(($# - 1))
389         while [ $i -lt $c ]; do
390                 src="$src $1"
391                 i=$((i + 1))
392                 shift
393         done
394         local dest=$1
395         set -- $src
396
397         inst "$@" $dest
398
399         local lib libs=$(ldd "$@" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
400         for lib in $libs; do
401                 if [ ! -f "$DESTDIR/$_lib/${lib##*/}" ]; then
402                         inst_d /$_lib
403                         inst_exec $lib /$_lib
404                 fi
405         done
406
407         # hack for uclibc linked binaries requiring this fixed path
408         # XXX: shouldn't rpath be used here instead so th
409         if [ -f $DESTDIR/$_lib/libc.so.0 ]; then
410                 local lib=$DESTDIR/$_lib/libc.so.0
411                 lib=$(ldd "$lib" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
412                 local libdir=$(cd $(dirname "$lib"); pwd)
413                 if [ ! -e $DESTDIR$libdir ]; then
414                         libdir=$(dirname "$libdir")
415                         inst_d $libdir
416                         debug "+ ln -s /$_lib $DESTDIR$libdir"
417                         ln -s /$_lib $DESTDIR$libdir
418                 fi
419         fi
420 }
421
422 # output modules.conf / modprobe.conf
423 modprobe_conf() {
424         echo "$modprobe_conf_cache"
425 }
426
427 #
428 # defaults to modprobe -c if not told otherwise, this means include statements
429 # work from there.
430 cache_modprobe_conf() {
431         if [ "$pack_version" -lt "002005" ]; then
432                 modulefile=/etc/modules.conf
433                 if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then
434                         modulefile=/etc/conf.modules
435                 fi
436         fi
437
438         if [ -n "$modulefile" ]; then
439                 debug "Using $modulefile for modules config"
440                 modprobe_conf_cache=$(cat $modulefile)
441         else
442                 debug "Using modprobe -c to get modules config"
443                 modprobe_conf_cache=$(modprobe -c --set-version $kernel)
444         fi
445 }
446
447 find_modules_md() {
448         local found raidlevel
449
450         if [ -f /etc/mdadm.conf ]; then
451                 debug "Finding RAID details using mdadm for rootdev=$1"
452                 eval `/sbin/mdadm -v --examine --scan --config=/etc/mdadm.conf | awk -v rootdev="$1" '
453                 BEGIN {
454                         found = "no";
455                         dev_list = "";
456                         raidlevel = ""
457                         rootdev_devfs = rootdev;
458                         if (rootdev ~ /\/dev\/md\/[0-9]/) {
459                                 gsub(/\/dev\/md\//,"/dev/md",rootdev_devfs);
460                         }
461                 }
462
463                 /^ARRAY/ {
464                         if (($2 == rootdev) || ($2 == rootdev_devfs)) {
465                                 raidlevel=$3;
466                                 gsub(/level=/,NUL,raidlevel);
467                                 if (raidlevel ~ /^raid([0-6]|10)/) {
468                                         gsub(/raid/,NUL,raidlevel);
469                                 };
470                                 found="yes";
471                                 getline x;
472                                 if (x ~ /devices=/) {
473                                         dev_list = x;
474                                         gsub(".*devices=", NUL, dev_list);
475                                         gsub(",", " ", dev_list);
476                                 }
477                         }
478                 }
479
480                 END {
481                         print "have_md=" found;
482                         print "raidlevel=" raidlevel;
483                         print "dev_list=\"" dev_list "\"";
484                 }'`
485         fi
486
487         if [ "$have_md" != "yes" -a -f /etc/raidtab ]; then
488                 die "raidtools are not longer supported. Please migrate to mdadm setup!"
489         fi
490
491         if is_yes "$have_md"; then
492                 case "$raidlevel" in
493                 [01]|10)
494                         findmodule "raid$raidlevel"
495                         ;;
496                 [456])
497                         findmodule "-raid$raidlevel"
498                         findmodule "-raid456"
499                         ;;
500                 linear)
501                         findmodule "linear"
502                         ;;
503                 *)
504                         warn "raid level $number (in mdadm config) not recognized"
505                         ;;
506                 esac
507         else
508                 die "RAID devices not found for \"$1\", check your configuration!"
509         fi
510
511         rootdev_nr=$(( $rootdev_nr + 1 ))
512         eval "rootdev${rootdev_nr}=\"$1\""
513         eval "dev_list${rootdev_nr}=\"${dev_list}\""
514
515         for device in $dev_list; do
516                 find_modules_for_devpath $device
517         done
518 }
519
520 find_modules_scsi() {
521         local n
522         for n in $PRESCSIMODS; do
523                 if [ "X$n" = "Xunknown" ]; then
524                         debug "Finding SCSI modules using scsi_hostadapter"
525                         local mod scsimodules=$(modprobe_conf | awk '/scsi_hostadapter/ && ! /^[\t ]*#/ { print $3; }')
526                         for mod in $scsimodules; do
527                                 # for now allow scsi modules to come from anywhere. There are some
528                                 # RAID controllers with drivers in block
529                                 findmodule "$mod"
530                         done
531                 else
532                         findmodule "$n"
533                 fi
534         done
535         findmodule "-scsi_wait_scan"
536 }
537
538 # return dependencies MAJOR:MINOR [MAJOR:MINOR] for DM_NAME
539 # TODO: patch `dmsetup export`
540 dm_deps() {
541         local dm_name="$1"
542         dmsetup deps $dm_name | sed -e 's/, /:/g;s/^.\+ dependencies[^:]: //;s/[()]//g;'
543 }
544
545 # export info from dmsetup
546 # param can be:
547 # - MAJOR:MINOR
548 # - /dev/dm-MINOR
549 # - /dev/mapper/DM_NAME
550 dm_export() {
551         local arg="$1"
552
553         case "$arg" in
554         *:*)
555                 local maj=${arg%:*} min=${arg#*:}
556                 dmsetup -j $maj -m $min export
557                 ;;
558         /dev/dm-*)
559                 local min=${arg#*dm-}
560                 local maj=$(awk '$2 == "device-mapper" {print $1}' /proc/devices)
561                 dm_export $maj:$min
562                 ;;
563         /dev/mapper/*)
564                 local dm_name=${arg#/dev/mapper/}
565                 dmsetup export $dm_name
566                 ;;
567         *)
568                 die "dm_export: unexpected $arg"
569                 ;;
570         esac
571 }
572
573 # find modules for $devpath
574 find_modules_for_devpath() {
575         local devpath="$1"
576         if [ -z "$devpath" ]; then
577                 die "No argument passed to find_modules_for_devpath() - is your /etc/fstab correct?"
578         fi
579
580         if [[ "$devpath" = /dev/dm-* ]]; then
581                 # /dev/dm-3 -> /dev/mapper/sil_ahbgadcbchfc3
582                 devpath=$(dm_longname "$devpath")
583         fi
584
585         if [ -L "$devpath" ] && ! is_lvm "$devpath"; then
586                 # sanitize things like:
587                 # /dev/block/104:2 -> /dev/cciss/c0d0p2
588                 devpath=$(readlink -f "$devpath")
589         fi
590
591         debug "Finding modules for device path $devpath"
592
593         if is_luks "$devpath"; then
594                 find_modules_luks "$devpath"
595                 return
596         fi
597
598         if is_yes "`echo "$devpath" | awk '/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:|\/dev\/nfs)/ { print "yes"; }'`"; then
599                 if [ ! -x /usr/bin/pcidev -a -z "$NFS_ETH_MODULES" ]; then
600                         die "root on NFS but /usr/bin/pcidev not found. Please install correct pci-database package and rerun $PROGRAM."
601                 fi
602                 if [ ! -f /proc/bus/pci/devices ]; then
603                         warn "Remember to add network card modules in /etc/sysconfig/geninitrd, example:"
604                         warn "BASICMODULES=\"e1000 ne2k-pci mii 8139too 3c59x\""
605                 else
606                         local m
607                         [ -z "$NFS_ETH_MODULES" ] && NFS_ETH_MODULES=$(/usr/bin/pcidev /m net | xargs)
608                         warn "NOTE: Network card(s) module(s) $NFS_ETH_MODULES is for this machine"
609                         for m in $NFS_ETH_MODULES; do
610                                 findmodule "$m"
611                         done
612                 fi
613                 findmodule "-ipv4"
614                 findmodule "nfs"
615                 have_nfs=yes
616                 warn "Remember to use \`root=/dev/ram0 init=/linuxrc' when starting kernel"
617                 warn "or you will have problems like init(xx) being child process of swapper(1)."
618                 return
619         fi
620
621         if [[ "$devpath" == /dev/md* ]]; then
622                 find_modules_md "$devpath"
623                 return
624         fi
625
626         if is_multipath "$devpath"; then
627                 if find_modules_multipath "$devpath"; then
628                         return
629                 fi
630                 # fallback
631         fi
632
633         if is_dmraid "$devpath"; then
634                 if find_modules_dmraid "$devpath"; then
635                         return
636                 fi
637                 # fallback
638         fi
639
640         if is_yes "$(echo "$devpath" | awk '/^\/dev\/(sd|scsi)/ { print "yes"; }')"; then
641                 find_modules_scsi
642                 return
643         fi
644
645         if is_ide "$devpath"; then
646                 find_modules_ide "$devpath"
647                 return
648         fi
649
650         if [[ "$devpath" == /dev/rd/* ]]; then
651                 findmodule "DAC960"
652                 rootdev_add=/dev/rd/
653                 return
654         fi
655
656         if [[ "$devpath" == /dev/ida/* ]]; then
657                 findmodule "cpqarray"
658                 rootdev_add=/dev/ida/
659                 return
660         fi
661
662         if [[ "$devpath" == /dev/cciss/* ]]; then
663                 findmodule "cciss"
664                 rootdev_add=/dev/cciss/
665                 return
666         fi
667
668         if [[ "$devpath" == /dev/ataraid/* ]]; then
669                 find_modules_ide
670                 findmodule "ataraid"
671                 ataraidmodules=$(modprobe_conf | awk '/ataraid_hostadapter/ && ! /^[\t ]*#/ { print $3; }')
672                 if [ -n "$ataraidmodules" ]; then
673                         # FIXME: think about modules compiled in kernel
674                         die "ataraid_hostadapter alias not defined in modprobe.conf! Please set it and run $PROGRAM again."
675                 fi
676                 for n in $ataraidmodules; do
677                         findmodule "$n"
678                 done
679                 rootdev_add=/dev/ataraid/
680                 return
681         fi
682
683         # check to see if we need to set up a loopback filesystem
684         if [[ "$devpath" == /dev/loop*  ]]; then
685                 die "Sorry, root on loop device isn't supported."
686                 # TODO: rewrite for bsp and make nfs ready
687                 if [ ! -x /sbin/losetup ]; then
688                         die "losetup is missing"
689                 fi
690                 key="^# $(echo $devpath | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
691                 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`"; then
692                         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"
693                 fi
694
695                 line="`awk '/'$key'/ { print $0; }' $fstab`"
696                 loopDev="$(echo $line | awk '{print $3}')"
697                 loopFs="$(echo $line | awk '{print $4}')"
698                 loopFile="$(echo $line | awk '{print $5}')"
699
700                 BASICMODULES="$BASICMODULES -loop"
701                 findmodule "-$loopFs"
702                 BASICMODULES="$BASICMODULES -${loopFs}"
703                 return
704         fi
705
706         if is_lvm "$devpath"; then
707                 find_modules_lvm "$devpath"
708                 return
709         fi
710 }
711
712 firmware_install_module() {
713         local module="$1"
714         local firmware_files="$2"
715
716         debug "Adding Firmwares ($firmware_files) to initrd for module $module"
717         # firmware not yet installed
718         if [ ! -f "$DESTDIR/lib/firmware/firmware.sh" ]; then
719                 inst_d /lib/firmware
720 cat << 'EOF' >> "$DESTDIR/lib/firmware/firmware.sh"
721 #!/bin/sh -e
722 echo 1 > /sys$DEVPATH/loading
723 cat "/lib/firmware/$FIRMWARE" > /sys$DEVPATH/data
724 echo 0 > /sys$DEVPATH/loading
725 exit 0
726 EOF
727                 chmod 755 "$DESTDIR/lib/firmware/firmware.sh"
728         fi
729
730         for firmware in $firmware_files; do
731                 if [ ! -f "/lib/firmware/$firmware" ]; then
732                                 die "firmware file /lib/firmware/$firmware doesn't exist."
733                 else
734                         FIRMWAREDIR=${firmware%/*}
735                         [ "$FIRMWAREDIR" != "$firmware" ] && inst_d /lib/firmware/$FIRMWAREDIR
736                         inst /lib/firmware/$firmware /lib/firmware/$firmware
737                 fi
738         done
739
740         mount_sys
741         echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" | add_linuxrc
742 }
743
744 modules_install() {
745         local modules="$1"
746         local mod
747
748         for mod in $modules; do
749                 MODULEDIR=${mod%/*}
750                 inst_d "/lib/modules/$kernel/$MODULEDIR"
751                 cp -a "/lib/modules/$kernel/$mod" "$DESTDIR/lib/modules/$kernel/$mod"
752                 gunzip "$DESTDIR/lib/modules/$kernel/$mod" 2> /dev/null
753         done
754 }
755
756 modules_add_linuxrc() {
757         local mod modpath
758
759         for mod in "$@"; do
760                 # module path without optional compression
761                 modpath=${mod%.gz}
762
763                 # name of the module
764                 module=${modpath##*/}
765                 module=${module%$modext}
766
767                 options=$(modprobe_conf | awk -vmodule="$module" '{ if ($1 == "options" && $2 == module) { for(i=3;i<=NF;i++) printf("%s ",$i); }}' | xargs)
768
769                 generic_module=$(echo $module | tr - _)
770                 sleep_var=$(eval echo \$MODULE_${generic_module}_USLEEP)
771                 firmware_var=$(eval echo \$MODULE_${generic_module}_FIRMWARE)
772
773                 if [ -n "$verbose" ]; then
774                         s=""
775                         if [ "$options" ]; then
776                                 s="$s with options [$options]"
777                         fi
778                         if [ "$sleep_var" ]; then
779                                 s="$s and $sleep_var usleep"
780                         fi
781                         debug "Loading module [$module]$s"
782                 fi
783
784                 if [ -n "$firmware_var" ]; then
785                         firmware_install_module "$module" "$firmware_var"
786                 else
787                         firmware_files=$(find_firmware "$module")
788                         if [ -n "$firmware_files" ]; then
789                                 for file in $firmware_files; do
790                                         firmware_install_module "$module" "$file"
791                                 done
792                         fi
793                 fi
794
795                 echo "$insmod /lib/modules/$kernel/$modpath $options" | add_linuxrc
796                 if [ -n "$sleep_var" ]; then
797                         echo "usleep $sleep_var" | add_linuxrc
798                 fi
799         done
800 }
801
802 find_modules_suspend() {
803         if [ ! -x /usr/${_lib}/suspend/resume -a ! -x /usr/sbin/resume ]; then
804                 die "/usr/${_lib}/suspend/resume is missing!"
805         fi
806         resume_dev="$(awk '/^resume device =/ { print $4 } ' /etc/suspend.conf)"
807
808         local vgvolumes=$VGVOLUMES
809         find_modules_for_devpath $resume_dev
810
811         if [ "$VGVOLUMES" != "$vgvolumes" ]; then
812                 # save our VG to be able to differenciate between rootfs VG
813                 SUSPENDVG=$VGVOLUMES
814                 VGVOLUMES=$vgvolumes
815                 debug "suspend device is on LVM"
816         fi
817 }
818
819 initrd_gen_suspend() {
820         mknod $DESTDIR/dev/snapshot c 10 231
821         mkdir -p $DESTDIR${resume_dev%/*}
822         inst $resume_dev $resume_dev
823
824         inst /etc/suspend.conf /etc/suspend.conf
825         if [ -x /usr/${_lib}/suspend/resume ]; then
826                 inst_exec /usr/${_lib}/suspend/resume /bin/resume
827         else
828                 inst_exec /usr/sbin/resume /bin/resume
829         fi
830
831         add_linuxrc <<-'EOF'
832         resume=no
833         for arg in $CMDLINE; do
834                 if [ "${arg##resume=}" != "${arg}" ]; then
835                         resume=yes
836                 fi
837         done
838         if [ "$resume" = "yes" ]; then
839                 resume
840         fi
841         EOF
842 }
843
844 initrd_gen_tuxonice() {
845         mount_sys
846         add_linuxrc <<-'EOF'
847                 resume=no
848                 for arg in $CMDLINE; do
849                         if [ "${arg##resume=}" != "${arg}" -o "${arg##resume2=}" != "${arg}" ]; then
850                                 resume=yes
851                         fi
852                 done
853                 if [ "$resume" = "yes" ]; then
854                         [ -e /proc/suspend2/do_resume ] && echo > /proc/suspend2/do_resume
855                         [ -e /sys/power/suspend2/do_resume ] && echo > /sys/power/suspend2/do_resume
856                         [ -e /sys/power/tuxonice/do_resume ] && echo > /sys/power/tuxonice/do_resume
857                 fi
858         EOF
859 }
860
861 initrd_gen_v86d() {
862         debug "initrd_gen_v86d"
863         mknod $DESTDIR/dev/mem c 1 1
864         mknod $DESTDIR/dev/tty1 c 4 1
865         inst_d /sbin
866         inst_exec /sbin/v86d /sbin
867 }
868
869 initrd_gen_udev() {
870         debug "Setting up udev..."
871         inst_d /sbin /etc/udev
872
873         if [ ! -x /sbin/initrd-udevd ]; then
874                 die "/sbin/initrd-udevd not present"
875         fi
876
877         inst_exec /sbin/initrd-udevd /sbin/udevd
878         inst /etc/udev/udev.conf /etc/udev/udev.conf
879
880         mount_dev
881         mount_sys
882         add_linuxrc <<-'EOF'
883                 : 'Starting udev'
884                 /sbin/udevd --daemon
885         EOF
886
887         inst_exec /sbin/initrd-udevadm /sbin/udevadm
888         add_linuxrc <<-'EOF'
889                 /sbin/udevadm trigger
890                 /sbin/udevadm settle
891         EOF
892
893 }
894
895 initrd_gen_stop_udevd() {
896         busybox_applet killall
897         add_linuxrc     <<-'EOF'
898                 killall udevd
899         EOF
900 }
901
902
903
904 initrd_gen_bootsplash() {
905         local target="$1"
906
907         debug "Generating bootsplash"
908
909         if [ ! -x /bin/splash.bin ]; then
910                 warn "Failed to execute /bin/splash.bin. Is bootsplash package installed?"
911                 return
912         fi
913
914         if [ -r /etc/sysconfig/bootsplash ]; then
915                 . /etc/sysconfig/bootsplash
916         fi
917
918         if [ -z "$THEME" ]; then
919                 warn "Please configure your /etc/sysconfig/bootsplash first."
920                 warn "Generating bootsplashes skipped."
921                 return
922         fi
923
924         if [ -z "$BOOT_SPLASH_RESOLUTIONS" ]; then
925                 warn "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash."
926                 warn "Not adding bootsplash to initrd."
927         fi
928
929         for res in $BOOT_SPLASH_RESOLUTIONS; do
930                 if [ -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" ]; then
931                         /bin/splash.bin -s -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" >> "$target" && \
932                         debug "Added $res $THEME theme to initrd."
933                 else
934                         warn "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped"
935                 fi
936         done
937 }
938
939 initrd_gen_fbsplash() {
940         debug "Generating fbsplash"
941
942         if [ ! -x /usr/bin/splash_geninitramfs -a ! -x /usr/sbin/splash_geninitramfs ]; then
943                 warn "Failed to find splash_geninitramfs. Is splashutils package installed?"
944                 return
945         fi
946         splash_geninitramfs_bin=/usr/sbin/splash_geninitramfs
947         [ -f /usr/bin/splash_geninitramfs ] && splash_geninitramfs_bin=/usr/bin/splash_geninitramfs
948
949         if [ -r /etc/sysconfig/fbsplash ]; then
950                 . /etc/sysconfig/fbsplash
951         fi
952
953         if [ -z "$SPLASH_THEME" ]; then
954                 warn "Please configure your /etc/sysconfig/fbsplash first."
955                 warn "Generating fbsplashes skipped."
956                 return
957         fi
958
959         if [ -z "$FB_SPLASH_RESOLUTIONS" ]; then
960                 warn "No FB_SPLASH_RESOLUTIONS specified in /etc/sysconfig/fbsplash."
961                 warn "Not adding fbsplash to initramfs."
962                 return
963         fi
964
965         for res in $FB_SPLASH_RESOLUTIONS; do
966                 if [ -f "/etc/splash/$SPLASH_THEME/$res.cfg" ]; then
967                         $splash_geninitramfs_bin -c $DESTDIR -r $res $SPLASH_THEME && \
968                         debug "Added $res $SPLASH_THEME theme to initramfs."
969                 else
970                         warn "/etc/splash/$SPLASH_THEME/$res.cfg doesn't exist, skipped"
971                 fi
972         done
973 }
974
975 initrd_gen_fbcondecor() {
976         debug "Generating fbcondecor"
977
978         if [ ! -x /usr/bin/splash_geninitramfs -a ! -x /usr/sbin/splash_geninitramfs ]; then
979                 warn "Failed to find splash_geninitramfs. Is splashutils package installed?"
980                 return
981         fi
982         splash_geninitramfs_bin=/usr/sbin/splash_geninitramfs
983         [ -f /usr/bin/splash_geninitramfs ] && splash_geninitramfs_bin=/usr/bin/splash_geninitramfs
984
985         if [ -r /etc/sysconfig/splash ]; then
986                 . /etc/sysconfig/splash
987         fi
988
989         if [ -z "$SPLASH_THEME" ]; then
990                 warn "Please configure your /etc/sysconfig/splash first."
991                 warn "Generating of splashes skipped."
992                 return
993         fi
994
995         if [ -z "$FB_SPLASH_RESOLUTIONS" ]; then
996                 warn "No FB_SPLASH_RESOLUTIONS specified in /etc/sysconfig/splash."
997                 warn "Not adding fbcondecor to initramfs."
998                 return
999         fi
1000
1001         for res in $FB_SPLASH_RESOLUTIONS; do
1002                 if [ -f "/etc/splash/$SPLASH_THEME/$res.cfg" ]; then
1003                         $splash_geninitramfs_bin -c $DESTDIR -r $res $SPLASH_THEME && \
1004                         debug "Added $res $SPLASH_THEME theme to initramfs."
1005                 else
1006                         warn "/etc/splash/$SPLASH_THEME/$res.cfg doesn't exist, skipped"
1007                 fi
1008         done
1009 }
1010
1011 # Generates /dev nodes based on /proc/partitions information.
1012 # Needs /proc mounted.
1013 # Can be called multiple times.
1014 initrd_gen_devices() {
1015         if is_yes "$proc_partitions"; then
1016                 return
1017         fi
1018         proc_partitions=yes
1019
1020         mount_dev
1021         add_linuxrc <<-'EOF'
1022                 : 'Making device nodes'
1023                 cat /proc/partitions | (
1024                         # ignore first two lines: header, empty line
1025                         read b; read b
1026
1027                         while read major minor blocks dev rest; do
1028                                 node=/dev/$dev
1029                                 mkdir -p ${node%/*}
1030                                 [ -e $node ] || mknod $node b $major $minor
1031                         done
1032                 )
1033         EOF
1034 }
1035
1036 initrd_gen_md() {
1037         debug "Setting up mdadm..."
1038
1039         if [ ! -x /sbin/mdadm -o ! -x /sbin/initrd-mdassemble ]; then
1040                 die "/sbin/mdadm or /sbin/initrd-mdassemble is missing!"
1041         fi
1042
1043         inst_exec /sbin/initrd-mdassemble /bin/mdassemble
1044
1045         # LVM on RAID case
1046         dev_list_extra=$(awk '/^DEVICE / { for (i=2; i<=NF; i++) { printf "%s ", $i; }; } ' /etc/mdadm.conf | xargs)
1047         if [ "$dev_list_extra" = "partitions" ]; then
1048                 # FIXME: handle this case (see man mdadm.conf)
1049                 echo "DEVICE partitions" >> "$DESTDIR/etc/mdadm.conf"
1050                 dev_list_extra=""
1051         else
1052                 for ex_dev in $dev_list_extra; do
1053                         echo "DEVICE $ex_dev" >> "$DESTDIR/etc/mdadm.conf"
1054                 done
1055         fi
1056         do_md0=1
1057         for nr in `seq 1 $rootdev_nr`; do
1058                 eval cr_rootdev="\$rootdev${nr}"
1059                 eval cr_dev_list="\$dev_list${nr}"
1060                 debug echo "Setting up array ($cr_rootdev = $cr_dev_list)"
1061
1062                 [ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
1063
1064                 echo "DEVICE $cr_dev_list" >> "$DESTDIR/etc/mdadm.conf"
1065                 cr_dev_list_md="$(echo "$cr_dev_list" | xargs | awk ' { gsub(/ +/,",",$0); print $0; }')"
1066                 cr_md_conf=$(/sbin/mdadm --detail --brief --config=/etc/mdadm.conf $cr_rootdev | awk ' { gsub(/spares=[0-9]+/, "", $0); print $0; }')
1067                 if [ -n "$cr_md_conf" ]; then
1068                         echo "$cr_md_conf" >> "$DESTDIR/etc/mdadm.conf"
1069                 else
1070                         echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$DESTDIR/etc/mdadm.conf"
1071                 fi
1072
1073                 for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
1074                         # mkdir in case of devfs name
1075                         inst_d $(dirname $f)
1076                         [ -e "$DESTDIR/$f" ] && continue
1077                         # this works fine with and without devfs
1078                         inst $f $f
1079                 done
1080         done
1081
1082         echo "mdassemble" | add_linuxrc
1083
1084         # needed to determine md-version
1085         if [ "$do_md0" -eq 1 ]; then
1086                 mknod $DESTDIR/dev/md0 b 9 0
1087         fi
1088 }
1089
1090 initrd_gen_nfs() {
1091         # use root=/dev/ram0 init=/linuxrc when starting kernel or you will
1092         # have problems like init(XX) being child process of swapper(1).
1093         debug "Adding rootfs on NFS support to initrd (dhcp)"
1094         mknod "$DESTDIR/dev/urandom" c 1 9
1095         mkdir "$DESTDIR/newroot"
1096         add_linuxrc <<-'EOF'
1097                 ifconfig lo 127.0.0.1 up
1098                 route add -net 127.0.0.0 mask 255.0.0.0 lo
1099                 ifconfig eth0 up
1100                 udhcpc -i eth0 -f -q -s /bin/setdhcp
1101
1102                 cd /newroot
1103                 pivot_root . initrd
1104                 [ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1
1105                 exec /usr/sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1
1106         EOF
1107
1108         cat <<-'EOF' > "$DESTDIR/bin/setdhcp"
1109                 #!/bin/sh
1110                 [ "$1" != "bound" ] && exit
1111                 [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
1112                 [ -n "$subnet" ] && NETMASK="netmask $subnet"
1113                 ifconfig $interface $ip $BROADCAST $NETMASK up
1114                 if [ -n "$router" ]; then
1115                         for r in $router; do
1116                                 route add default gw $r dev $interface
1117                         done
1118                 fi
1119
1120                 for o in $CMDLINE; do
1121                         case $o in
1122                         nfsroot=*)
1123                                 rootpath=${o#nfsroot=}
1124                                 ;;
1125                         esac
1126                 done
1127
1128                 if [ -n "$rootpath" ]; then
1129                         mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath /newroot
1130                 else
1131                         echo "Missing rootpath in what DHCP server sent to us. Failing..."
1132                         echo "All seen variables are listed below:"
1133                         set
1134                 fi
1135         EOF
1136
1137         chmod 755 "$DESTDIR/bin/setdhcp"
1138 }
1139
1140 initrd_gen_blkid()
1141 {
1142         debug "Adding BLKID support to initrd"
1143         inst_exec /sbin/initrd-blkid /bin/blkid
1144         initrd_gen_devices
1145         add_linuxrc <<-'EOF'
1146                 # if built with blkid change ROOT=LABEL=something into ROOT=/dev/somethingelse -
1147                 # parsed by blkid
1148                 if [ "${ROOT##LABEL=}" != "${ROOT}" -o "${ROOT##UUID=}" != "${ROOT}" ]; then
1149                         ROOT="$(/bin/blkid -t $ROOT -o device -l)"
1150                 fi
1151         EOF
1152 }
1153
1154 initrd_gen_setrootdev() {
1155         debug "Adding rootfs finding based on kernel cmdline root= option support."
1156         add_linuxrc <<-'EOF'
1157                 if [ "${ROOT##/dev/}" != "${ROOT}" ]; then
1158                         rootnr="$(busybox awk -v rootnode="${ROOT##/dev/}" '$4 == rootnode { print 256 * $1 + $2 }' /proc/partitions)"
1159                         if [ -n "$rootnr" ]; then
1160                                 echo "$rootnr" > /proc/sys/kernel/real-root-dev
1161                         fi
1162                 fi
1163         EOF
1164 }
1165
1166 initrd_gen_initramfs_switchroot() {
1167         inst_d /newroot
1168         if [ "$rootdev" = "/dev/nfs" ]; then
1169                 echo "rootfs on NFS root=/dev/nfs"
1170         else
1171                 [ ! -e "$DESTDIR/$rootdev" ] && inst $rootdev $rootdev
1172         fi
1173
1174         # parse 'root=xxx' kernel commandline
1175         # We support passing root as hda3 /dev/hda3 0303 0x0303 and 303
1176         add_linuxrc <<-'EOF'
1177                 device=/dev/no_partition_found
1178                 eval "$(busybox awk -v c="$ROOT" '
1179                         BEGIN {
1180                                 num_pattern_short = "[0-9a-f][0-9a-f][0-9a-f]";
1181                                 num_pattern = "[0-9a-f]" num_pattern_short;
1182                                 dev_pattern = "[hms][a-z][a-z]([0-9])+";
1183                                 partition = "no_partition_found";
1184                                 min = -1; maj = -1;
1185
1186                                 sub("^0x", "", c);
1187                                 if (c ~ "^" num_pattern_short "$") sub("^", "0", c);
1188                                 if (c ~ "^" num_pattern  "$") {
1189                                         maj = sprintf("%s",substr(c,1,2));
1190                                         min = sprintf("%s",substr(c,3));
1191                                 }
1192                                 if (c ~ "^\/dev\/" dev_pattern "$") sub("^/dev/","", c);
1193                                 if (c ~ "^" dev_pattern "$") partition = c;
1194                         }
1195
1196                         $4 ~ partition { maj = $1; min = $2; }
1197                         $1 ~ maj && $2 ~ min { partition = $4; }
1198
1199                         END {
1200                                 if (maj >= 0 && min >= 0) {
1201                                         printf("device=/dev/%s; maj=%s; min=%s;\n", partition, maj, min);
1202                                 }
1203                         }
1204                         ' /proc/partitions)"
1205                 if [ "$device" != '/dev/no_partition_found' -a ! -b $device ]; then
1206                         mknod $device b $maj $min
1207                 fi
1208         EOF
1209
1210         add_linuxrc <<-EOF
1211                 rootdev=$rootdev
1212                 rootfs=$rootFs
1213         EOF
1214
1215         add_linuxrc <<-'EOF'
1216                 if [ "$device" = '/dev/no_partition_found' ]; then
1217                         device=$rootdev
1218                 fi
1219
1220                 mount -t $rootfs -r $device /newroot
1221                 init="$(echo "$CMDLINE" | busybox awk '/init=\// { gsub(/.*init=/,NIL,$0); gsub(/ .*/,NIL,$0); print }')"
1222                 if [ -z "$init" -o ! -x "/newroot$init" ]; then
1223                         init=/sbin/init
1224                 fi
1225         EOF
1226
1227         umount_all
1228         busybox_applet switch_root
1229         add_linuxrc <<-'EOF'
1230                 exec switch_root /newroot $init
1231
1232                 echo "Error! initramfs should not reach this place."
1233                 echo "It probably means you've got old version of busybox, with broken"
1234                 echo "initramfs support. Trying to boot anyway, but won't promise anything."
1235
1236                 exec chroot /newroot $init
1237
1238                 echo "Failed to chroot!"
1239         EOF
1240         # we need /init being real file, not symlink, otherwise the initramfs will
1241         # not be ran by pid 1 which is required for switch_root
1242         mv $DESTDIR/linuxrc $DESTDIR/init
1243         ln -s init $DESTDIR/linuxrc
1244 }
1245
1246 # main()
1247 if [ "$(id -u)" != 0 ]; then
1248         die "You need to be root to generate initrd"
1249 fi
1250
1251 if [ -f /etc/udev/udev.conf -a -x /sbin/initrd-udevd ]; then
1252         USE_UDEV=yes
1253         . /etc/udev/udev.conf
1254 fi
1255
1256 # ide addon
1257 . /etc/geninitrd/mod-ide.sh
1258 # cryptsetup luks addon
1259 . /etc/geninitrd/mod-luks.sh
1260 # dm-multipath addon
1261 . /etc/geninitrd/mod-multipath.sh
1262 # dmraid addon
1263 . /etc/geninitrd/mod-dmraid.sh
1264 # LVM addon
1265 . /etc/geninitrd/mod-lvm.sh
1266
1267 if [ -x /sbin/initrd-blkid ]; then
1268         USE_BLKID=yes
1269 fi
1270
1271 if [ -r /etc/sysconfig/geninitrd ]; then
1272         . /etc/sysconfig/geninitrd
1273 fi
1274
1275 if [ ! -x /bin/initrd-busybox ]; then
1276         die "/bin/initrd-busybox is missing!"
1277 fi
1278
1279 # backwards compatible
1280 if [ "$USE_SUSPEND2" ]; then
1281         USE_TUXONICE=$USE_SUSPEND2
1282         warn "USE_SUSPEND2 is deprecated, use USE_TUXONICE now instead."
1283 fi
1284
1285 while [ $# -gt 0 ]; do
1286         case $1 in
1287         --fstab=*)
1288                 fstab=${1#--fstab=}
1289                 ;;
1290         --fstab)
1291                 fstab=$2
1292                 shift
1293                 ;;
1294         --modules-conf=*)
1295                 modulefile=${1#--modules-conf=}
1296                 ;;
1297         --modules-conf)
1298                 modulefile=$2
1299                 shift
1300                 ;;
1301         --use-raidstart|--with-raidstart)
1302                 USERAIDSTART=yes
1303                 ;;
1304         --without-raidstart)
1305                 USERAIDSTART=no
1306                 ;;
1307         --use-insmod-static|--with-insmod-static)
1308                 USEINSMODSTATIC=yes
1309                 ;;
1310         --without-insmod-static)
1311                 USEINSMODSTATIC=no
1312                 ;;
1313         --with-bootsplash)
1314                 BOOT_SPLASH=yes
1315                 ;;
1316         --without-bootsplash)
1317                 BOOT_SPLASH=no
1318                 ;;
1319         --with-fbsplash)
1320                 FB_SPLASH=yes
1321                 ;;
1322         --without-fbsplash)
1323                 FB_SPLASH=no
1324                 ;;
1325         --with-fbcondecor)
1326                 FB_CON_DECOR=yes
1327                 ;;
1328         --without-fbcondecor)
1329                 FB_CON_DECOR=no
1330                 ;;
1331         --with-suspend)
1332                 USE_SUSPEND=yes
1333                 ;;
1334         --without-suspend)
1335                 USE_SUSPEND=no
1336                 ;;
1337         --with-suspend2 | --with-tuxonice)
1338                 USE_TUXONICE=yes
1339                 ;;
1340         --without-suspend2 | --without-tuxonice)
1341                 USE_TUXONICE=no
1342                 ;;
1343         --lvmversion=*)
1344                 LVMTOOLSVERSION=${1#--lvmversion=}
1345                 ;;
1346         --lvmtoolsversion=*)
1347                 LVMTOOLSVERSION=${1#--lvmtoolsversion=}
1348                 ;;
1349         --lvmtoolsversion|--lvmversion)
1350                 LVMTOOLSVERSION=$2
1351                 shift
1352                 ;;
1353         --without-udev)
1354                 USE_UDEV=no
1355                 ;;
1356         --with-udev)
1357                 USE_UDEV=yes
1358                 ;;
1359         --without-dmraid)
1360                 USE_DMRAID=no
1361                 ;;
1362         --without-multipath)
1363                 USE_MULTPATH=no
1364                 ;;
1365         --without-blkid)
1366                 USE_BLKID=no
1367                 ;;
1368         --without-luks)
1369                 USE_LUKS=no
1370                 ;;
1371         --with=*)
1372                 BASICMODULES="$BASICMODULES ${1#--with=}"
1373                 ;;
1374         --with)
1375                 BASICMODULES="$BASICMODULES $2"
1376                 shift
1377                 ;;
1378         --version)
1379                 echo "$PROGRAM: version $VERSION"
1380                 exit 0
1381                 ;;
1382         -v)
1383                 verbose=-v
1384                 ;;
1385         --nocompress)
1386                 COMPRESS=no
1387                 ;;
1388         --ifneeded)
1389                 ifneeded=1
1390                 ;;
1391         -f)
1392                 force=1
1393                 ;;
1394         --preload=*)
1395                 PREMODS="$PREMODS ${1#--preload=}"
1396                 ;;
1397         --preload)
1398                 PREMODS="$PREMODS $2"
1399                 shift
1400                 ;;
1401         --fs=*)
1402                 warn "Warning: --fs option is obsoleted. Use --initrdfs instead"
1403                 INITRDFS=${1#--fs=}
1404                 ;;
1405         --fs)
1406                 warn "Warning: --fs option is obsoleted. Use --initrdfs instead"
1407                 INITRDFS=$2
1408                 shift
1409                 ;;
1410         --initrdfs=*)
1411                 INITRDFS=${1#--initrdfs=}
1412                 ;;
1413         --initrdfs)
1414                 INITRDFS=$2
1415                 shift
1416                 ;;
1417         --image-version)
1418                 img_vers=yes
1419                 ;;
1420         --ide-only-root)
1421                 ide_only_root="yes"
1422                 ;;
1423         *)
1424                 if [ -z "$target" ]; then
1425                         target="$1"
1426                 elif [ -z "$kernel" ]; then
1427                         kernel="$1"
1428                 else
1429                         usage >&2
1430                         exit 1
1431                 fi
1432                 ;;
1433         esac
1434
1435         shift
1436 done
1437
1438 if [ -z "$target" -o -z "$kernel" ]; then
1439         usage >&2
1440         exit 1
1441 fi
1442
1443 if [ ! -f /boot/vmlinuz-"$kernel" ]; then
1444         warn "/boot/vmlinuz-$kernel doesn't exist, is your /boot mounted?"
1445 fi
1446
1447 pack_version=$(echo "$kernel" | awk -F. '{print sprintf("%03d%03d",$1,$2)}')
1448 pack_version_long=$(echo "$kernel" | awk -F. '{print sprintf("%03d%03d%03d",$1,$2,$3)}')
1449
1450 if [ -z "$INITRDFS" ]; then
1451         if [ -z "$FS" ]; then
1452                 # default value
1453                 if [ "$pack_version" -ge "002005" ]; then
1454                         INITRDFS="initramfs"
1455                 else
1456                         INITRDFS="rom"
1457                 fi
1458         else
1459                 warn "Warning: FS configuration options is obsoleted. Use INITRDFS instead"
1460                 INITRDFS="$FS"
1461         fi
1462 fi
1463
1464 if [ "$pack_version" -lt "002006" ]; then
1465         USE_UDEV=no
1466         USE_DMRAID=no
1467 fi
1468
1469 if [ "$pack_version" -ge "002005" ]; then
1470         modext=".ko"
1471         insmod="insmod"
1472 fi
1473
1474 if is_yes "$USEINSMODSTATIC"; then
1475         insmod="insmod.static"
1476         INSMOD="/sbin/insmod.static"
1477         if [ "$pack_version" -lt "002005" -a -f /sbin/insmod.static.modutils ]; then
1478                 INSMOD="/sbin/insmod.static.modutils"
1479         fi
1480         if [ ! -f "$INSMOD" ]; then
1481                 die "insmod.static requested but /sbin/insmod.static not found!"
1482         fi
1483 fi
1484
1485 case "$INITRDFS" in
1486         ext2)
1487                 if [ ! -x /sbin/mke2fs ]; then
1488                         die "/sbin/mke2fs is missing"
1489                 fi
1490                 ;;
1491         rom|romfs)
1492                 if [ ! -x /sbin/genromfs ]; then
1493                         die "/sbin/genromfs is missing"
1494                 fi
1495                 ;;
1496         cram|cramfs)
1497                 if [ ! -x /sbin/mkcramfs ]; then
1498                         die "/sbin/mkcramfs is missing"
1499                 fi
1500                 ;;
1501         initramfs)
1502                 if [ ! -x /bin/cpio ]; then
1503                         die "/bin/cpio is missing"
1504                 fi
1505                 if [ ! -x /usr/bin/find ]; then
1506                         die "/usr/bin/find is missing"
1507                 fi
1508                 ;;
1509         *)
1510                 die "Filesystem $INITRDFS on initrd is not supported"
1511                 ;;
1512 esac
1513
1514 if [ -L "$target" ]; then
1515         target=$(readlink -f "$target")
1516 fi
1517
1518 if [ -n "$img_vers" ]; then
1519         target="$target-$kernel"
1520 fi
1521
1522 if [ -z "$force" -a -f "$target" ]; then
1523         die "$target already exists."
1524 fi
1525
1526 if [ ! -d "/lib/modules/$kernel" ]; then
1527         die "/lib/modules/$kernel is not a directory."
1528 fi
1529
1530 if [ "$INITRDFS" != "initramfs" ]; then
1531         if is_yes "$FB_SPLASH"; then
1532                 warn "Using fbsplash requires INITRDFS=initramfs; skipping fbsplash generation"
1533                 FB_SPLASH=no
1534         fi
1535         if is_yes "$FB_CON_DECOR"; then
1536                 warn "Using fbcondecor requires INITRDFS=initramfs; skipping fbcondecor generation"
1537                 FB_CON_DECOR=no
1538         fi
1539 fi
1540
1541 if is_yes "$USE_SUSPEND" && is_yes "$USE_TUXONICE"; then
1542         warn "Tuxonice and mainline suspend are exclusive!"
1543 fi
1544
1545 if is_yes "$FB_SPLASH"  && is_yes "$BOOT_SPLASH"; then
1546         warn "bootsplash and fbsplash are exclusive!"
1547 fi
1548
1549 if [ ! -f /proc/mounts ]; then
1550         warn "/proc filesystem not mounted, may cause wrong results or failure."
1551 fi
1552
1553 if [ -d /usr/lib64 ]; then
1554         _lib=lib64
1555 else
1556         _lib=lib
1557 fi
1558 debug "# $RCSID"
1559 debug "Using libdir: $_lib"
1560
1561 cache_modprobe_conf
1562
1563 for n in $PREMODS; do
1564         findmodule "$n"
1565 done
1566
1567 if [ "$FBMODULE" ]; then
1568         findmodule "$FBMODULE"
1569 fi
1570
1571 # allow forcing loading SCSI and/or IDE modules
1572 if is_yes "$ADDSCSI"; then
1573         find_modules_scsi
1574 fi
1575
1576 if is_yes "$ADDIDE"; then
1577         find_modules_ide
1578 fi
1579
1580 if is_yes "$USE_SUSPEND"; then
1581         find_modules_suspend
1582 fi
1583
1584 find_root "$fstab" || exit
1585 debug "Using $rootdev as device for rootfs"
1586
1587 find_modules_for_devpath "$rootdev"
1588
1589 findmodule "-$rootFs"
1590
1591 for n in $BASICMODULES; do
1592         findmodule "$n"
1593 done
1594
1595 if is_yes "$USE_TUXONICE"; then
1596         findmodule "-lzf"
1597 fi
1598
1599 if is_yes "$FB_SPLASH"; then
1600         findmodule "-evdev"
1601 fi
1602
1603 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
1604         debug "No modules are needed -- not building initrd image."
1605         exit 0
1606 fi
1607
1608 DESTDIR=$(mktemp -d -t initrd.XXXXXX) || die "mktemp failed"
1609 RCFILE="$DESTDIR/linuxrc"
1610 > "$RCFILE"
1611 chmod a+rx "$RCFILE"
1612 ln -s linuxrc $DESTDIR/init
1613
1614 # create dirs that we really need
1615 inst_d /{lib,bin,etc,dev{,/pts,/shm},loopfs,var,proc,sys}
1616
1617 modules_install "$MODULES"
1618
1619 # mknod'ing the devices instead of copying them works both with and
1620 # without devfs...
1621 mknod "$DESTDIR/dev/console" c 5 1
1622 mknod "$DESTDIR/dev/null" c 1 3
1623 mknod "$DESTDIR/dev/zero" c 1 5
1624
1625 inst_exec /bin/initrd-busybox /bin/initrd-busybox
1626 ln -s initrd-busybox $DESTDIR/bin/sh
1627 ln -s initrd-busybox $DESTDIR/bin/busybox # for older busyboxes who had /bin/busybox as EXEPATH
1628
1629 if is_yes "$USEINSMODSTATIC"; then
1630         inst_exec "$INSMOD" /bin/insmod.static
1631 fi
1632
1633 add_linuxrc <<EOF
1634 #!/bin/sh
1635 # initrd generated by:
1636 # $RCSID
1637
1638 EOF
1639 mount_proc
1640 add_linuxrc <<-'EOF'
1641         read CMDLINE < /proc/cmdline; export CMDLINE
1642
1643         for arg in $CMDLINE; do
1644                 if [ "${arg}" = "debuginitrd" ]; then
1645                         DEBUGINITRD=yes
1646                 fi
1647                 if [ "${arg##debuginitrd=}" != "${arg}" ]; then
1648                         DEBUGINITRD=${arg##debuginitrd=}
1649                 fi
1650                 if [ "${arg##root=}" != "${arg}" ]; then
1651                         ROOT=${arg##root=}
1652                 fi
1653         done
1654
1655         # make debugshell() invoke subshell if $DEBUGINITRD=sh
1656         if [ "$DEBUGINITRD" = "sh" ]; then
1657                 debugshell() {
1658 EOF
1659 if is_yes "$RUN_SULOGIN_ON_ERR"; then
1660 add_linuxrc <<-'EOF'
1661         echo "debug shell disabled by /etc/sysconfig/system:RUN_SULOGIN_ON_ERR setting"
1662 EOF
1663 else
1664 add_linuxrc <<-'EOF'
1665         sh
1666 EOF
1667 fi
1668 add_linuxrc <<-'EOF'
1669                 }
1670         else
1671                 debugshell() {
1672                         :
1673                 }
1674         fi
1675
1676         if [ "$DEBUGINITRD" ]; then
1677                 set -x
1678         fi
1679 EOF
1680
1681 modules_add_linuxrc $MODULES
1682
1683 # TODO: rewrite for busybox
1684 #if [ -n "$loopDev" ]; then
1685 #       if [ ! -d /initrd ]; then
1686 #               mkdir /initrd
1687 #       fi
1688 #
1689 #       cp -a "$loopDev" "$DESTDIR/dev"
1690 #       cp -a "$rootdev" "$DESTDIR/dev"
1691 #       echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
1692 #       echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
1693 #       echo "echo Setting up loopback device $rootdev" >> $RCFILE
1694 #       echo "losetup $rootdev /loopfs$loopFile" >> "$RCFILE"
1695 #fi
1696
1697 if is_yes "$USE_UDEV"; then
1698         initrd_gen_udev
1699 fi
1700
1701 if is_yes "$USE_TUXONICE"; then
1702         initrd_gen_tuxonice
1703 fi
1704
1705 if is_yes "$need_v86d"; then
1706         initrd_gen_v86d
1707 fi
1708
1709 if is_yes "$have_luks"; then
1710         initrd_gen_luks
1711 fi
1712
1713 if is_yes "$have_dmraid"; then
1714         initrd_gen_dmraid
1715 fi
1716
1717 if is_yes "$have_multipath"; then
1718         initrd_gen_multipath
1719 fi
1720
1721 if is_yes "$USE_BLKID"; then
1722         initrd_gen_blkid
1723 fi
1724
1725 if is_yes "$have_nfs"; then
1726         initrd_gen_nfs
1727 elif is_yes "$USERAIDSTART" && is_yes "$have_md"; then
1728         initrd_gen_md
1729         if is_yes "$have_lvm"; then
1730                 initrd_gen_lvm
1731         else
1732                 initrd_gen_setrootdev
1733         fi
1734 elif is_yes "$have_lvm"; then
1735         initrd_gen_lvm
1736 else
1737         initrd_gen_setrootdev
1738 fi
1739
1740 if is_yes "$USE_SUSPEND"; then
1741         initrd_gen_suspend
1742 fi
1743
1744 # additional devs always needed
1745 [ ! -e "$DESTDIR/$rootdev_add" ] && inst $rootdev_add /dev
1746
1747 if is_yes "$USE_UDEV"; then
1748         initrd_gen_stop_udevd
1749 fi
1750
1751 if [ "$INITRDFS" = "initramfs" ]; then
1752         initrd_gen_initramfs_switchroot
1753 else
1754         umount_all
1755 fi
1756
1757 if is_yes "$FB_SPLASH"; then
1758         initrd_gen_fbsplash
1759 fi
1760
1761 if is_yes "$FB_CON_DECOR"; then
1762         initrd_gen_fbcondecor
1763 fi
1764
1765 IMAGE=$(mktemp -t initrd.img-XXXXXX) || die "mktemp failed"
1766
1767 debug "Creating $INITRDFS image $IMAGE"
1768 case "$INITRDFS" in
1769         ext2)
1770                 IMAGESIZE=$(du -ks $DESTDIR | awk '{print int(($1+1023+512)/1024)*1024}')
1771                 debug   "ext2 image size: $IMAGESIZE ($DESTDIR)"
1772                 if [ "$IMAGESIZE" -gt 4096 ]; then
1773                         warn "Your image size is larger than 4096, Be sure to boot kernel with ramdisk_size=$IMAGESIZE!"
1774                 fi
1775
1776                 dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
1777                 mke2fs -q -F -b 1024 -m 0 "$IMAGE" 2>/dev/null 1>&2
1778                 tune2fs -i 0 "$IMAGE" >/dev/null 2>&1
1779
1780                 local tmpmnt=$(mktemp -d -t initrd.mnt-XXXXXX)
1781                 debug "Mounting ext2 image $IMAGE to $tmpmnt"
1782                 mount -o loop -t ext2 "$IMAGE" "$tmpmnt"
1783                 # We don't need this directory, so let's save space
1784                 rm -rf "$tmpmnt"/lost+found
1785
1786                 debug "Copy recursively $DESTDIR -> $tmpmnt"
1787                 cp -a $DESTDIR/* $tmpmnt
1788                 umount "$IMAGE"
1789                 rmdir "$tmpmnt"
1790
1791                 ;;
1792         rom|romfs)
1793                 genromfs -f "$IMAGE" -d "$DESTDIR" -V "PLD initrd for kernel $kernel"
1794                 IMAGESIZE=$(stat -c %s $IMAGE | awk '{print int((($1/1024)+1023)/1024)*1024}')
1795                 debug "Image size for romfs: ${IMAGESIZE}KiB ($IMAGE)"
1796                 if [ "$IMAGESIZE" -gt 4096 ]; then
1797                         warn "Your image size is larger than 4096, Be sure to boot kernel with ramdisk_size=$IMAGESIZE!"
1798                 fi
1799                 ;;
1800         cram|cramfs)
1801                 mkcramfs "$DESTDIR" "$IMAGE"
1802                 ;;
1803         initramfs)
1804                 (cd $DESTDIR; find . | cpio --quiet -H newc -o > "$IMAGE")
1805                 ;;
1806         *)
1807                 echo "Filesystem $INITRDFS not supported by $PROGRAM";
1808 esac
1809
1810 if is_yes "$COMPRESS"; then
1811         tmp=$(mktemp "$target".XXXXXX) || die "mktemp failed"
1812         debug "Compressing $target"
1813         gzip -9 < "$IMAGE" > "$tmp"
1814         mv -f "$tmp" "$target"
1815 else
1816         cp -a "$IMAGE" "$target"
1817 fi
1818
1819 size=$(stat -c %s $target | awk '{print int((($1/1024)+1023)/1024)*1024}')
1820 debug "Minimum RAM size that could be used for $target is: ramdisk_size=$size"
1821
1822 # XXX. check if bootsplash can output data to tmp dir not directly to initramfs image.
1823 if is_yes "$BOOT_SPLASH"; then
1824         initrd_gen_bootsplash "$target"
1825 fi
1826
1827 rm -rf "$DESTDIR" "$IMAGE"
1828
1829 # vim:ts=4:sw=4:noet:fdm=marker
This page took 0.163367 seconds and 4 git commands to generate.