2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
5 # Generator script for a dracut initramfs
6 # Tries to retain some degree of compatibility with the command line
7 # of the various mkinitrd implementations out there
10 # Copyright 2005-2013 Red Hat, Inc. All rights reserved.
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program. If not, see <http://www.gnu.org/licenses/>.
32 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
33 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
34 . $dracutbasedir/dracut-version.sh
37 # 80x25 linebreak here ^
39 Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
41 Version: $DRACUT_VERSION
43 Creates initial ramdisk images for preloading modules
45 -h, --help Display all options
47 If a [LIST] has multiple arguments, then you have to put these in quotes.
51 # dracut --add-drivers "module1 module2" ...
57 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
58 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
59 . $dracutbasedir/dracut-version.sh
62 # 80x25 linebreak here ^
64 Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
66 Version: $DRACUT_VERSION
68 Creates initial ramdisk images for preloading modules
70 --kver [VERSION] Set kernel version to [VERSION].
71 -f, --force Overwrite existing initramfs file.
72 -a, --add [LIST] Add a space-separated list of dracut modules.
73 -m, --modules [LIST] Specify a space-separated list of dracut modules to
74 call when building the initramfs. Modules are located
75 in /usr/lib/dracut/modules.d.
76 -o, --omit [LIST] Omit a space-separated list of dracut modules.
77 --force-add [LIST] Force to add a space-separated list of dracut modules
78 to the default set of modules, when -H is specified.
79 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
80 exclusively include in the initramfs.
81 --add-drivers [LIST] Specify a space-separated list of kernel
82 modules to add to the initramfs.
83 --omit-drivers [LIST] Specify a space-separated list of kernel
84 modules not to add to the initramfs.
85 --filesystems [LIST] Specify a space-separated list of kernel filesystem
86 modules to exclusively include in the generic
88 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
90 --fwdir [DIR] Specify additional directories, where to look for
91 firmwares, separated by :
92 --kernel-only Only install kernel drivers and firmware files
93 --no-kernel Do not install kernel drivers and firmware files
94 --early-microcode Combine early microcode with ramdisk
95 --no-early-microcode Do not combine early microcode with ramdisk
96 --kernel-cmdline [PARAMETERS] Specify default kernel command line parameters
97 --strip Strip binaries in the initramfs
98 --nostrip Do not strip binaries in the initramfs
99 --hardlink Hardlink files in the initramfs
100 --nohardlink Do not hardlink files in the initramfs
101 --prefix [DIR] Prefix initramfs files with [DIR]
102 --noprefix Do not prefix initramfs files
103 --mdadmconf Include local /etc/mdadm.conf
104 --nomdadmconf Do not include local /etc/mdadm.conf
105 --lvmconf Include local /etc/lvm/lvm.conf
106 --nolvmconf Do not include local /etc/lvm/lvm.conf
107 --fscks [LIST] Add a space-separated list of fsck helpers.
108 --nofscks Inhibit installation of any fsck helpers.
109 --ro-mnt Mount / and /usr read-only by default.
110 -h, --help This message
111 --debug Output debug information of the build process
112 --profile Output profile information of the build process
113 -L, --stdlog [0-6] Specify logging level (to standard error)
114 0 - suppress any messages
115 1 - only fatal errors
119 5 - debug info (here starts lots of output)
120 6 - trace info (and even more)
121 -v, --verbose Increase verbosity level
122 -q, --quiet Decrease verbosity level
123 -c, --conf [FILE] Specify configuration file to use.
124 Default: /etc/dracut.conf
125 --confdir [DIR] Specify configuration directory to use *.conf files
126 from. Default: /etc/dracut.conf.d
127 --tmpdir [DIR] Temporary directory to be used instead of default
129 -l, --local Local mode. Use modules from the current working
130 directory instead of the system-wide installed in
131 /usr/lib/dracut/modules.d.
132 Useful when running dracut from a git checkout.
133 -H, --hostonly Host-Only mode: Install only what is needed for
134 booting the local host instead of a generic host.
135 -N, --no-hostonly Disables Host-Only mode
136 --fstab Use /etc/fstab to determine the root device.
137 --add-fstab [FILE] Add file to the initramfs fstab
138 --mount "[DEV] [MP] [FSTYPE] [FSOPTS]"
139 Mount device [DEV] on mountpoint [MP] with filesystem
140 [FSTYPE] and options [FSOPTS] in the initramfs
141 --add-device "[DEV]" Bring up [DEV] in initramfs
142 -i, --include [SOURCE] [TARGET]
143 Include the files in the SOURCE directory into the
144 Target directory in the final initramfs.
145 If SOURCE is a file, it will be installed to TARGET
146 in the final initramfs.
147 -I, --install [LIST] Install the space separated list of files into the
149 --gzip Compress the generated initramfs using gzip.
150 This will be done by default, unless another
151 compression option or --no-compress is passed.
152 --bzip2 Compress the generated initramfs using bzip2.
153 Make sure your kernel has bzip2 decompression support
154 compiled in, otherwise you will not be able to boot.
155 --lzma Compress the generated initramfs using lzma.
156 Make sure your kernel has lzma support compiled in,
157 otherwise you will not be able to boot.
158 --xz Compress the generated initramfs using xz.
159 Make sure that your kernel has xz support compiled
160 in, otherwise you will not be able to boot.
161 --compress [COMPRESSION] Compress the generated initramfs with the
162 passed compression program. Make sure your kernel
163 knows how to decompress the generated initramfs,
164 otherwise you will not be able to boot.
165 --no-compress Do not compress the generated initramfs. This will
166 override any other compression options.
167 --list-modules List all available dracut modules.
168 -M, --show-modules Print included module's name to standard output during
170 --keep Keep the temporary initramfs for debugging purposes
171 --printsize Print out the module install size
172 --sshkey [SSHKEY] Add ssh key to initramfs (use with ssh-client module)
174 If [LIST] has multiple arguments, then you have to put these in quotes.
178 # dracut --add-drivers "module1 module2" ...
184 # push values to a stack
185 # $1 = stack variable
188 # push stack 1 2 "3 4"
191 local __stack=$1; shift
193 eval ${__stack}'[${#'${__stack}'[@]}]="$_i"'
198 # pops the last value from a stack
199 # assigns value to second argument variable
200 # or echo to stdout, if no second argument
201 # $1 = stack variable
202 # $2 = optional variable to store the value
207 local __stack=$1; shift
210 # check for empty stack
211 eval '[[ ${#'${__stack}'[@]} -eq 0 ]] && return 1'
213 eval _value='${'${__stack}'[${#'${__stack}'[@]}-1]}'
215 if [[ "$__resultvar" ]]; then
216 eval $__resultvar="'$_value'"
218 printf "%s" "$_value"
220 eval unset ${__stack}'[${#'${__stack}'[@]}-1]'
224 # Little helper function for reading args from the commandline.
225 # it automatically handles -a b and -a=b variants, and returns 1 if
226 # we need to shift $3.
231 local rematch='^[^=]*=(.*)$'
232 if [[ $2 =~ $rematch ]]; then
233 read "$1" <<< "${BASH_REMATCH[1]}"
236 # There is no way to shift our callers args, so
237 # return 1 to indicate they should do it instead.
244 local suffix=$1; shift
248 readarray -t files < <(
250 for i in "$d/"*"$suffix"; do
251 if [[ -e "$i" ]]; then
252 printf "%s\n" "${i##*/}"
258 for f in "${files[@]}"; do
260 if [[ -e "$d/$f" ]]; then
261 printf "%s\n" "$d/$f"
272 # Workaround -i, --include taking 2 arguments
273 set -- "${@/--include/++include}"
275 # This prevents any long argument ending with "-i"
276 # -i, like --opt-i but I think we can just prevent that
277 set -- "${@/%-i/++include}"
279 TEMP=$(unset POSIXLY_CORRECT; getopt \
280 -o "a:m:o:d:I:k:c:L:fvqlHhMN" \
284 --long add-drivers: \
285 --long omit-drivers: \
289 --long filesystems: \
309 --long kernel-cmdline: \
328 --long no-host-only \
336 --long list-modules \
337 --long show-modules \
340 --long regenerate-all \
341 --long noimageifnotneeded \
344 if (( $? != 0 )); then
353 --kver) kernel="$2"; shift;;
354 -a|--add) push add_dracutmodules_l "$2"; shift;;
355 --force-add) push force_add_dracutmodules_l "$2"; shift;;
356 --add-drivers) push add_drivers_l "$2"; shift;;
357 --omit-drivers) push omit_drivers_l "$2"; shift;;
358 -m|--modules) push dracutmodules_l "$2"; shift;;
359 -o|--omit) push omit_dracutmodules_l "$2"; shift;;
360 -d|--drivers) push drivers_l "$2"; shift;;
361 --filesystems) push filesystems_l "$2"; shift;;
362 -I|--install) push install_items_l "$2"; shift;;
363 --fwdir) push fw_dir_l "$2"; shift;;
364 --libdirs) push libdirs_l "$2"; shift;;
365 --fscks) push fscks_l "$2"; shift;;
366 --add-fstab) push add_fstab_l "$2"; shift;;
367 --mount) push fstab_lines "$2"; shift;;
368 --add-device|--device)
369 push add_device_l "$2"; shift;;
370 --kernel-cmdline) push kernel_cmdline_l "$2"; shift;;
371 --nofscks) nofscks_l="yes";;
372 --ro-mnt) ro_mnt_l="yes";;
373 -k|--kmoddir) drivers_dir_l="$2"; shift;;
374 -c|--conf) conffile="$2"; shift;;
375 --confdir) confdir="$2"; shift;;
376 --tmpdir) tmpdir_l="$2"; shift;;
377 -L|--stdlog) stdloglvl_l="$2"; shift;;
378 --compress) compress_l="$2"; shift;;
379 --prefix) prefix_l="$2"; shift;;
380 -f|--force) force=yes;;
381 --kernel-only) kernel_only="yes"; no_kernel="no";;
382 --no-kernel) kernel_only="no"; no_kernel="yes";;
383 --early-microcode) early_microcode="yes";;
384 --no-early-microcode) early_microcode="no";;
385 --strip) do_strip_l="yes";;
386 --nostrip) do_strip_l="no";;
387 --hardlink) do_hardlink_l="yes";;
388 --nohardlink) do_hardlink_l="no";;
389 --noprefix) prefix_l="/";;
390 --mdadmconf) mdadmconf_l="yes";;
391 --nomdadmconf) mdadmconf_l="no";;
392 --lvmconf) lvmconf_l="yes";;
393 --nolvmconf) lvmconf_l="no";;
394 --debug) debug="yes";;
395 --profile) profile="yes";;
396 --sshkey) sshkey="$2"; shift;;
397 -v|--verbose) ((verbosity_mod_l++));;
398 -q|--quiet) ((verbosity_mod_l--));;
401 [[ -f "$(readlink -f "${0%/*}")/dracut-functions.sh" ]] \
402 && dracutbasedir="$(readlink -f "${0%/*}")"
404 -H|--hostonly|--host-only)
406 -N|--no-hostonly|--no-host-only)
408 --fstab) use_fstab_l="yes" ;;
409 -h|--help) long_usage; exit 1 ;;
410 -i|--include) push include_src "$2"
412 --bzip2) compress_l="bzip2";;
413 --lzma) compress_l="lzma";;
414 --xz) compress_l="xz";;
415 --no-compress) _no_compress_l="cat";;
416 --gzip) compress_l="gzip";;
417 --list-modules) do_list="yes";;
422 --printsize) printsize="yes";;
423 --regenerate-all) regenerate_all="yes";;
424 --noimageifnotneeded) noimageifnotneeded="yes";;
428 *) # should not even reach this point
429 printf "\n!Unknown option: '%s'\n\n" "$1" >&2; usage; exit 1;;
434 # getopt cannot handle multiple arguments, so just handle "-I,--include"
435 # the old fashioned way
439 ++include) push include_src "$2"
440 push include_target "$3"
443 if ! [[ ${outfile+x} ]]; then
445 elif ! [[ ${kernel+x} ]]; then
448 printf "\nUnknown arguments: %s\n\n" "$*" >&2
456 if [[ $regenerate_all == "yes" ]]; then
458 if [[ $kernel ]]; then
459 printf -- "--regenerate-all cannot be called with a kernel version\n" >&2
463 if [[ $outfile ]]; then
464 printf -- "--regenerate-all cannot be called with a image file\n" >&2
468 ((len=${#dracut_args[@]}))
469 for ((i=0; i < len; i++)); do
470 [[ ${dracut_args[$i]} == "--regenerate-all" ]] && \
471 unset dracut_args[$i]
476 [[ -f $i/modules.builtin ]] || continue
477 dracut --kver="$i" "${dracut_args[@]}"
483 if ! [[ $kernel ]]; then
487 if ! [[ $outfile ]]; then
488 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
490 if [[ $MACHINE_ID ]] && ( [[ -d /boot/${MACHINE_ID} ]] || [[ -L /boot/${MACHINE_ID} ]] ); then
491 outfile="/boot/${MACHINE_ID}/$kernel/initrd"
493 outfile="/boot/initramfs-$kernel.img"
497 for i in /usr/sbin /sbin /usr/bin /bin; do
502 if [[ "$NPATH" != "*:$rl*" ]] ; then
506 export PATH="${NPATH#:}"
512 unset LD_LIBRARY_PATH
515 export DRACUT_LOG_LEVEL=warning
517 export DRACUT_LOG_LEVEL=debug
518 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
523 export PS4='+ $(date "+%s.%N") ${BASH_SOURCE}@${LINENO}: ';
528 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
530 # if we were not passed a config file, try the default one
531 if [[ ! -f $conffile ]]; then
532 if [[ $allowlocal ]]; then
533 conffile="$dracutbasedir/dracut.conf"
535 conffile="/etc/dracut.conf"
539 if [[ ! -d $confdir ]]; then
540 if [[ $allowlocal ]]; then
541 confdir="$dracutbasedir/dracut.conf.d"
543 confdir="/etc/dracut.conf.d"
547 # source our config file
548 [[ -f $conffile ]] && . "$conffile"
550 # source our config dir
551 for f in $(dropindirs_sort ".conf" "$confdir" "$dracutbasedir/dracut.conf.d"); do
552 [[ -e $f ]] && . "$f"
555 # these optins add to the stuff in the config file
556 if (( ${#add_dracutmodules_l[@]} )); then
557 while pop add_dracutmodules_l val; do
558 add_dracutmodules+=" $val "
562 if (( ${#force_add_dracutmodules_l[@]} )); then
563 while pop force_add_dracutmodules_l val; do
564 force_add_dracutmodules+=" $val "
568 if (( ${#fscks_l[@]} )); then
569 while pop fscks_l val; do
574 if (( ${#add_fstab_l[@]} )); then
575 while pop add_fstab_l val; do
580 if (( ${#fstab_lines_l[@]} )); then
581 while pop fstab_lines_l val; do
582 push fstab_lines $val
586 if (( ${#install_items_l[@]} )); then
587 while pop install_items_l val; do
588 install_items+=" $val "
592 # these options override the stuff in the config file
593 if (( ${#dracutmodules_l[@]} )); then
595 while pop dracutmodules_l val; do
596 dracutmodules+="$val "
600 if (( ${#omit_dracutmodules_l[@]} )); then
601 omit_dracutmodules=''
602 while pop omit_dracutmodules_l val; do
603 omit_dracutmodules+="$val "
607 if (( ${#filesystems_l[@]} )); then
609 while pop filesystems_l val; do
614 if (( ${#fw_dir_l[@]} )); then
616 while pop fw_dir_l val; do
621 if (( ${#libdirs_l[@]} )); then
623 while pop libdirs_l val; do
628 [[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
629 [[ ! $stdloglvl ]] && stdloglvl=4
630 stdloglvl=$((stdloglvl + verbosity_mod_l))
631 ((stdloglvl > 6)) && stdloglvl=6
632 ((stdloglvl < 0)) && stdloglvl=0
634 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
635 [[ $do_strip_l ]] && do_strip=$do_strip_l
636 [[ $do_strip ]] || do_strip=yes
637 [[ $do_hardlink_l ]] && do_hardlink=$do_hardlink_l
638 [[ $do_hardlink ]] || do_hardlink=yes
639 [[ $prefix_l ]] && prefix=$prefix_l
640 [[ $prefix = "/" ]] && unset prefix
641 [[ $hostonly_l ]] && hostonly=$hostonly_l
642 [[ $use_fstab_l ]] && use_fstab=$use_fstab_l
643 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
644 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
645 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
646 [[ $fw_dir ]] || fw_dir="/lib/firmware/updates /lib/firmware"
647 [[ $tmpdir_l ]] && tmpdir="$tmpdir_l"
648 [[ $tmpdir ]] || tmpdir=/var/tmp
649 [[ $compress_l ]] && compress=$compress_l
650 [[ $show_modules_l ]] && show_modules=$show_modules_l
651 [[ $nofscks_l ]] && nofscks="yes"
652 [[ $ro_mnt_l ]] && ro_mnt="yes"
653 # eliminate IFS hackery when messing with fw_dir
654 fw_dir=${fw_dir//:/ }
656 # handle compression options.
657 [[ $compress ]] || compress="gzip"
659 bzip2) compress="bzip2 -9";;
660 lzma) compress="lzma -9";;
661 xz) compress="xz --check=crc32 --lzma2=dict=1MiB";;
662 gzip) compress="gzip -9"; command -v pigz > /dev/null 2>&1 && compress="pigz -9";;
664 if [[ $_no_compress_l = "cat" ]]; then
668 [[ $hostonly = yes ]] && hostonly="-h"
669 [[ $hostonly != "-h" ]] && unset hostonly
671 readonly TMPDIR="$tmpdir"
672 readonly initdir="$(mktemp --tmpdir="$TMPDIR/" -d -t initramfs.XXXXXX)"
673 [ -d "$initdir" ] || {
674 printf "%s\n" "dracut: mktemp --tmpdir=\"$TMPDIR/\" -d -t initramfs.XXXXXX failed." >&2
678 if [[ $early_microcode = yes ]]; then
679 readonly microcode_dir="$(mktemp --tmpdir="$TMPDIR/" -d -t early_microcode.XXXXXX)"
680 [ -d "$microcode_dir" ] || {
681 printf "%s\n" "dracut: mktemp --tmpdir=\"$TMPDIR/\" -d -t early_microcode.XXXXXX failed." >&2
685 # clean up after ourselves no matter how we die.
686 trap 'ret=$?;[[ $outfile ]] && [[ -f $outfile.$$ ]] && rm -f -- "$outfile.$$";[[ $keep ]] && echo "Not removing $initdir." >&2 || { [[ $initdir ]] && rm -rf -- "$initdir"; [[ $microcode_dir ]] && rm -Rf -- "$microcode_dir"; exit $ret; };' EXIT
687 # clean up after ourselves no matter how we die.
688 trap 'exit 1;' SIGINT
690 export DRACUT_KERNEL_LAZY="1"
691 export DRACUT_RESOLVE_LAZY="1"
693 if [[ -f $dracutbasedir/dracut-functions.sh ]]; then
694 . $dracutbasedir/dracut-functions.sh
696 printf "%s\n" "dracut: Cannot find $dracutbasedir/dracut-functions.sh." >&2
697 printf "%s\n" "dracut: Are you running from a git checkout?" >&2
698 printf "%s\n" "dracut: Try passing -l as an argument to $0" >&2
703 if ! $DRACUT_INSTALL ${initdir+-D "$initdir"} -R "$initdir/bin/sh" &>/dev/null; then
704 unset DRACUT_RESOLVE_LAZY
705 export DRACUT_RESOLVE_DEPS=1
707 rm -fr -- ${initdir}/*
709 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
710 . $dracutbasedir/dracut-version.sh
713 # Verify bash version, current minimum is 3.1
714 if (( BASH_VERSINFO[0] < 4 )); then
715 dfatal 'You need at least Bash 4 to use dracut, sorry.'
719 dracutfunctions=$dracutbasedir/dracut-functions.sh
720 export dracutfunctions
722 if (( ${#drivers_l[@]} )); then
724 while pop drivers_l val; do
728 drivers=${drivers/-/_}
730 if (( ${#add_drivers_l[@]} )); then
731 while pop add_drivers_l val; do
732 add_drivers+=" $val "
735 add_drivers=${add_drivers/-/_}
737 if (( ${#omit_drivers_l[@]} )); then
738 while pop omit_drivers_l val; do
739 omit_drivers+=" $val "
742 omit_drivers=${omit_drivers/-/_}
744 if (( ${#kernel_cmdline_l[@]} )); then
745 while pop kernel_cmdline_l val; do
746 kernel_cmdline+=" $val "
750 omit_drivers_corrected=""
751 for d in $omit_drivers; do
752 [[ " $drivers $add_drivers " == *\ $d\ * ]] && continue
753 omit_drivers_corrected+="$d|"
755 omit_drivers="${omit_drivers_corrected%|}"
756 unset omit_drivers_corrected
758 # prepare args for logging
759 for ((i=0; i < ${#dracut_args[@]}; i++)); do
760 [[ "${dracut_args[$i]}" == *\ * ]] && \
761 dracut_args[$i]="\"${dracut_args[$i]}\""
764 ddebug "Executing: $0 ${dracut_args[@]}"
766 [[ $do_list = yes ]] && {
767 for mod in $dracutbasedir/modules.d/*; do
768 [[ -d $mod ]] || continue;
769 [[ -e $mod/install || -e $mod/installkernel || \
770 -e $mod/module-setup.sh ]] || continue
771 printf "%s\n" "${mod##*/??}"
776 # This is kinda legacy -- eventually it should go away.
777 case $dracutmodules in
778 ""|auto) dracutmodules="all" ;;
781 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
783 if [[ -d $srcmods ]]; then
784 [[ -f $srcmods/modules.dep ]] || {
785 dwarn "$srcmods/modules.dep is missing. Did you run depmod?"
789 if [[ -f $outfile && ! $force ]]; then
790 dfatal "Will not override existing initramfs ($outfile) without --force"
795 [[ $outdir ]] || outdir="/"
797 if [[ ! -d "$outdir" ]]; then
798 dfatal "Can't write to $outdir: Directory $outdir does not exist or is not accessible."
800 elif [[ ! -w "$outdir" ]]; then
801 dfatal "No permission to write to $outdir."
803 elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
804 dfatal "No permission to write $outfile."
808 # Need to be able to have non-root users read stuff (rpcbind etc)
811 if [[ $hostonly ]]; then
812 for i in /sys /proc /run /dev; do
813 if ! findmnt --target "$i" &>/dev/null; then
814 dwarning "Turning off host-only mode: '$i' is not mounted!"
818 if ! [[ -d /run/udev/data ]]; then
819 dwarning "Turning off host-only mode: udev database not found!"
824 declare -A host_fs_types
826 for line in "${fstab_lines[@]}"; do
830 host_fs_types["$1"]="$3"
833 for f in $add_fstab; do
834 [[ -e $f ]] || continue
835 while read dev rest; do
836 push host_devs "$dev"
840 for dev in $add_device; do
841 push host_devs "$dev"
844 if (( ${#add_device_l[@]} )); then
845 while pop add_device_l val; do
847 push host_devs "$val"
851 if [[ $hostonly ]]; then
852 # in hostonly mode, determine all devices, which have to be accessed
853 # and examine them for filesystem types
865 mountpoint "$mp" >/dev/null 2>&1 || continue
866 _dev="$(readlink -f "/dev/block/$(find_block_device "$mp")")"
867 [[ "$_mp" == "/" ]] && root_dev="$_dev"
868 push host_devs "$_dev"
871 while read dev type rest; do
872 [[ -b $dev ]] || continue
873 [[ "$type" == "partition" ]] || continue
874 while read _d _m _t _o _r; do
875 [[ "$_d" == \#* ]] && continue
876 [[ $_d ]] || continue
877 [[ $_t != "swap" ]] || [[ $_m != "swap" ]] && continue
878 [[ "$_o" == *noauto* ]] && continue
879 [[ "$_d" == UUID\=* ]] && _d="/dev/disk/by-uuid/${_d#UUID=}"
880 [[ "$_d" == LABEL\=* ]] && _d="/dev/disk/by-label/$_d#LABEL=}"
881 [[ "$_d" -ef "$dev" ]] || continue
883 while read _mapper _a _p _o; do
884 [[ $_mapper = \#* ]] && continue
885 [[ "$_d" -ef /dev/mapper/"$_mapper" ]] || continue
886 [[ "$_o" ]] || _o="$_p"
888 [[ $_o == *swap* ]] && continue 2
891 push host_devs "$(readlink -f "$dev")"
899 if [[ -b /dev/block/$1 ]] && ID_FS_TYPE=$(get_fs_env "/dev/block/$1"); then
900 printf "%s\n" "$(readlink -f "/dev/block/$1")" "$ID_FS_TYPE"
903 if [[ -b $1 ]] && ID_FS_TYPE=$(get_fs_env "$1"); then
904 printf "%s\n" "$(readlink -f "$1")" "$ID_FS_TYPE"
907 if fstype=$(find_dev_fstype "$1"); then
908 printf "%s\n" "$1" "$fstype"
914 for dev in "${host_devs[@]}"; do
917 host_fs_types["$key"]="$val"
920 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
925 || udevdir="$(pkg-config udev --variable=udevdir 2>/dev/null)"
926 if ! [[ -d "$udevdir" ]]; then
927 [[ -d /lib/udev ]] && udevdir=/lib/udev
928 [[ -d /usr/lib/udev ]] && udevdir=/usr/lib/udev
931 [[ -d $systemdutildir ]] \
932 || systemdutildir=$(pkg-config systemd --variable=systemdutildir 2>/dev/null)
934 if ! [[ -d "$systemdutildir" ]]; then
935 [[ -d /lib/systemd ]] && systemdutildir=/lib/systemd
936 [[ -d /usr/lib/systemd ]] && systemdutildir=/usr/lib/systemd
939 [[ -d $systemdsystemunitdir ]] \
940 || systemdsystemunitdir=$(pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null)
942 [[ -d "$systemdsystemunitdir" ]] || systemdsystemunitdir=${systemdutildir}/system
944 [[ -d $systemdsystemconfdir ]] \
945 || systemdsystemconfdir=$(pkg-config systemd --variable=systemdsystemconfdir 2>/dev/null)
947 [[ -d "$systemdsystemconfdir" ]] || systemdsystemconfdir=/etc/systemd/system
949 export initdir dracutbasedir dracutmodules \
950 fw_dir drivers_dir debug no_kernel kernel_only \
951 omit_drivers mdadmconf lvmconf root_dev \
952 use_fstab fstab_lines libdirs fscks nofscks ro_mnt \
953 stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
954 debug host_fs_types host_devs sshkey add_fstab \
955 DRACUT_VERSION udevdir prefix filesystems drivers \
956 systemdutildir systemdsystemunitdir systemdsystemconfdir
958 # Create some directory structure first
959 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
961 [[ -h /lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
962 [[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
964 if [[ $prefix ]]; then
965 for d in bin etc lib sbin tmp usr var $libdirs; do
966 [[ "$d" == */* ]] && continue
967 ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
971 if [[ $kernel_only != yes ]]; then
972 for d in usr/bin usr/sbin bin etc lib sbin tmp usr var $libdirs; do
973 [[ -e "${initdir}${prefix}/$d" ]] && continue
974 if [ -L "/$d" ]; then
975 inst_symlink "/$d" "${prefix}/$d"
977 mkdir -m 0755 -p "${initdir}${prefix}/$d"
981 for d in dev proc sys sysroot root run run/lock run/initramfs; do
982 if [ -L "/$d" ]; then
985 mkdir -m 0755 -p "$initdir/$d"
989 ln -sfn ../run "$initdir/var/run"
990 ln -sfn ../run/lock "$initdir/var/lock"
991 ln -sfn ../run/log "$initdir/var/log"
993 for d in lib "$libdir"; do
994 [[ -e "${initdir}${prefix}/$d" ]] && continue
995 if [ -h "/$d" ]; then
996 inst "/$d" "${prefix}/$d"
998 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1003 if [[ $kernel_only != yes ]]; then
1004 mkdir -p "${initdir}/etc/cmdline.d"
1005 for _d in $hookdirs; do
1006 mkdir -m 0755 -p ${initdir}/lib/dracut/hooks/$_d
1008 if [[ "$UID" = "0" ]]; then
1009 [ -c ${initdir}/dev/null ] || mknod ${initdir}/dev/null c 1 3
1010 [ -c ${initdir}/dev/kmsg ] || mknod ${initdir}/dev/kmsg c 1 11
1011 [ -c ${initdir}/dev/console ] || mknod ${initdir}/dev/console c 5 1
1016 # check all our modules to see if they should be sourced.
1017 # This builds a list of modules that we will install next.
1018 for_each_module_dir check_module
1019 for_each_module_dir check_mount
1021 [[ "$mods_to_load " == *01fips\ * ]] && export DRACUT_FIPS_MODE=1
1023 _isize=0 #initramfs size
1025 # source our modules.
1026 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1027 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
1028 if [[ "$mods_to_load" == *\ $_d_mod\ * ]]; then
1029 if [[ $show_modules = yes ]]; then
1030 printf "%s\n" "$_d_mod"
1032 dinfo "*** Including module: $_d_mod ***"
1034 if [[ $kernel_only == yes ]]; then
1035 module_installkernel "$_d_mod" || {
1036 dfatal "installkernel failed in module $_d_mod"
1040 module_install "$_d_mod"
1041 if [[ $no_kernel != yes ]]; then
1042 module_installkernel "$_d_mod" || {
1043 dfatal "installkernel failed in module $_d_mod"
1048 mods_to_load=${mods_to_load// $_d_mod /}
1049 modules_loaded+="$_d_mod "
1051 #print the module install size
1052 if [ -n "$printsize" ]; then
1053 _isize_new=$(du -sk ${initdir}|cut -f1)
1054 _isize_delta=$((_isize_new - _isize))
1055 printf "%s\n" "$_d_mod install size: ${_isize_delta}k"
1062 for i in $modules_loaded; do
1063 mkdir -p $initdir/lib/dracut
1064 printf "%s\n" "$i" >> $initdir/lib/dracut/modules.txt
1067 dinfo "*** Including modules done ***"
1069 ## final stuff that has to happen
1070 if [[ $no_kernel != yes ]]; then
1072 if [[ $drivers ]]; then
1073 hostonly='' instmods $drivers
1076 if [[ $add_drivers ]]; then
1077 hostonly='' instmods -c $add_drivers
1079 if [[ $filesystems ]]; then
1080 hostonly='' instmods -c $filesystems
1083 dinfo "*** Installing kernel module dependencies and firmware ***"
1085 dinfo "*** Installing kernel module dependencies and firmware done ***"
1087 if [[ $noimageifnotneeded == yes ]] && [[ $hostonly ]]; then
1088 if [[ ! -f "$initdir/lib/dracut/need-initqueue" ]] && \
1089 [[ -f ${initdir}/lib/modules/$kernel/modules.dep && ! -s ${initdir}/lib/modules/$kernel/modules.dep ]]; then
1090 for i in ${initdir}/etc/cmdline.d/*.conf; do
1091 # We need no initramfs image and do not generate one.
1092 [[ $i == "${initdir}/etc/cmdline.d/*.conf" ]] && exit 0
1098 if [[ $kernel_only != yes ]]; then
1099 (( ${#install_items[@]} > 0 )) && dracut_install ${install_items[@]}
1101 [[ $kernel_cmdline ]] && printf "%s\n" "$kernel_cmdline" >> "${initdir}/etc/cmdline.d/01-default.conf"
1103 while pop fstab_lines line; do
1104 printf "%s\n" "$line 0 0" >> "${initdir}/etc/fstab"
1107 for f in $add_fstab; do
1108 cat "$f" >> "${initdir}/etc/fstab"
1111 if [ -d ${initdir}/$systemdutildir ]; then
1112 mkdir -p ${initdir}/etc/conf.d
1114 printf "%s\n" "systemdutildir=\"$systemdutildir\""
1115 printf "%s\n" "systemdsystemunitdir=\"$systemdsystemunitdir\""
1116 printf "%s\n" "systemdsystemconfdir=\"$systemdsystemconfdir\""
1117 } > ${initdir}/etc/conf.d/systemd.conf
1120 if [[ $DRACUT_RESOLVE_LAZY ]] && [[ $DRACUT_INSTALL ]]; then
1121 dinfo "*** Resolving executable dependencies ***"
1122 find "$initdir" -type f \
1123 '(' -perm -0100 -or -perm -0010 -or -perm -0001 ')' \
1124 -not -path '*.ko' -print0 \
1125 | xargs -r -0 $DRACUT_INSTALL ${initdir+-D "$initdir"} -R ${DRACUT_FIPS_MODE+-H} --
1126 dinfo "*** Resolving executable dependencies done***"
1130 while pop include_src src && pop include_target tgt; do
1131 if [[ $src && $tgt ]]; then
1132 if [[ -f $src ]]; then
1135 ddebug "Including directory: $src"
1136 mkdir -p "${initdir}/${tgt}"
1137 # check for preexisting symlinks, so we can cope with the
1138 # symlinks to $prefix
1139 for i in "$src"/*; do
1140 [[ -e "$i" || -h "$i" ]] || continue
1141 s=${initdir}/${tgt}/${i#$src/}
1142 if [[ -d "$i" ]]; then
1143 if ! [[ -e "$s" ]]; then
1144 mkdir -m 0755 -p "$s"
1145 chmod --reference="$i" "$s"
1147 cp --reflink=auto --sparse=auto -fa -t "$s" "$i"/*
1149 cp --reflink=auto --sparse=auto -fa -t "$s" "$i"
1156 if [[ $kernel_only != yes ]]; then
1157 # make sure that library links are correct and up to date
1158 for f in /etc/ld.so.conf /etc/ld.so.conf.d/*; do
1159 [[ -f $f ]] && inst_simple "$f"
1161 if ! ldconfig -r "$initdir"; then
1162 if [[ $UID = 0 ]]; then
1163 derror "ldconfig exited ungracefully"
1165 derror "ldconfig might need uid=0 (root) for chroot()"
1170 if (( maxloglvl >= 5 )); then
1171 ddebug "Listing sizes of included files:"
1172 du -c "$initdir" | sort -n | ddebug
1175 PRELINK_BIN="$(command -v prelink)"
1176 if [[ $UID = 0 ]] && [[ $PRELINK_BIN ]]; then
1177 if [[ $DRACUT_FIPS_MODE ]]; then
1178 dinfo "*** Installing prelink files ***"
1179 dracut_install -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf /etc/prelink.cache
1181 dinfo "*** Pre-linking files ***"
1182 dracut_install -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf
1183 chroot "$initdir" "$PRELINK_BIN" -a
1184 rm -f -- "$initdir/$PRELINK_BIN"
1185 rm -fr -- "$initdir"/etc/prelink.*
1186 dinfo "*** Pre-linking files done ***"
1190 if [[ $do_hardlink = yes ]] && command -v hardlink >/dev/null; then
1191 dinfo "*** Hardlinking files ***"
1192 hardlink "$initdir" 2>&1
1193 dinfo "*** Hardlinking files done ***"
1197 if [[ $do_strip = yes ]] ; then
1198 for p in strip xargs find; do
1199 if ! type -P $p >/dev/null; then
1200 dinfo "Could not find '$p'. Not stripping the initramfs."
1206 if [[ $do_strip = yes ]] ; then
1207 dinfo "*** Stripping files ***"
1208 if [[ $DRACUT_FIPS_MODE ]]; then
1209 find "$initdir" -type f \
1210 -executable -not -path '*/lib/modules/*.ko' -print0 \
1211 | while read -r -d $'\0' f; do
1212 if ! [[ -e "${f%/*}/.${f##*/}.hmac" ]] \
1213 && ! [[ -e "/lib/hmaccalc/${f##*/}.hmac" ]] \
1214 && ! [[ -e "/lib64/hmaccalc/${f##*/}.hmac" ]] \
1215 && ! [[ -e "/lib/fipscheck/${f##*/}.hmac" ]] \
1216 && ! [[ -e "/lib64/fipscheck/${f##*/}.hmac" ]]; then
1217 printf "%s\000" "$f";
1219 done | xargs -r -0 strip -g 2>/dev/null
1221 find "$initdir" -type f \
1222 -executable -not -path '*/lib/modules/*.ko' -print0 \
1223 | xargs -r -0 strip -g 2>/dev/null
1226 # strip kernel modules, but do not touch signed modules
1227 find "$initdir" -type f -path '*/lib/modules/*.ko' -print0 \
1228 | while read -r -d $'\0' f; do
1229 SIG=$(tail -c 28 "$f")
1230 [[ $SIG == '~Module signature appended~' ]] || { printf "%s\000" "$f"; }
1231 done | xargs -r -0 strip -g
1233 dinfo "*** Stripping files done ***"
1235 if [[ $early_microcode = yes ]]; then
1236 dinfo "*** Generating early-microcode cpio image ***"
1237 ucode_dir=(amd-ucode intel-ucode)
1238 ucode_dest=(AuthenticAMD.bin GenuineIntel.bin)
1239 _dest_dir="$microcode_dir/d/kernel/x86/microcode"
1242 if [[ $hostonly ]]; then
1243 [[ $(get_cpu_vendor) == "AMD" ]] && _dest_idx="0"
1244 [[ $(get_cpu_vendor) == "Intel" ]] && _dest_idx="1"
1246 for idx in $_dest_idx; do
1247 _fw=${ucode_dir[$idx]}
1248 for _fwdir in $fw_dir; do
1249 if [[ -d $_fwdir && -d $_fwdir/$_fw ]]; then
1251 dinfo "*** Constructing ${ucode_dest[$idx]} ****"
1252 if [[ $hostonly ]]; then
1253 _src=$(get_ucode_file)
1255 cat $_fwdir/$_fw/$_src > $_dest_dir/${ucode_dest[$idx]}
1259 (cd "$microcode_dir/d"; find . | cpio -o -H newc --quiet >../ucode.cpio)
1262 dinfo "*** Creating image file ***"
1263 if [[ $early_microcode = yes ]]; then
1264 # The microcode blob is _before_ the initramfs blob, not after
1265 mv $microcode_dir/ucode.cpio $outfile.$$
1267 if ! ( umask 077; cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet| \
1268 $compress >> "$outfile.$$"; ); then
1269 dfatal "dracut: creation of $outfile.$$ failed"
1272 mv -- "$outfile.$$" "$outfile"
1273 dinfo "*** Creating image file done ***"
1275 dinfo "Wrote $outfile:"
1276 dinfo "$(ls -l "$outfile")"