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