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 --print-cmdline Print the kernel command line for the given disk layout
95 --early-microcode Combine early microcode with ramdisk
96 --no-early-microcode Do not combine early microcode with ramdisk
97 --kernel-cmdline [PARAMETERS] Specify default kernel command line parameters
98 --strip Strip binaries in the initramfs
99 --nostrip Do not strip binaries in the initramfs
100 --hardlink Hardlink files in the initramfs
101 --nohardlink Do not hardlink files in the initramfs
102 --prefix [DIR] Prefix initramfs files with [DIR]
103 --noprefix Do not prefix initramfs files
104 --mdadmconf Include local /etc/mdadm.conf
105 --nomdadmconf Do not include local /etc/mdadm.conf
106 --lvmconf Include local /etc/lvm/lvm.conf
107 --nolvmconf Do not include local /etc/lvm/lvm.conf
108 --fscks [LIST] Add a space-separated list of fsck helpers.
109 --nofscks Inhibit installation of any fsck helpers.
110 --ro-mnt Mount / and /usr read-only by default.
111 -h, --help This message
112 --debug Output debug information of the build process
113 --profile Output profile information of the build process
114 -L, --stdlog [0-6] Specify logging level (to standard error)
115 0 - suppress any messages
116 1 - only fatal errors
120 5 - debug info (here starts lots of output)
121 6 - trace info (and even more)
122 -v, --verbose Increase verbosity level
123 -q, --quiet Decrease verbosity level
124 -c, --conf [FILE] Specify configuration file to use.
125 Default: /etc/dracut.conf
126 --confdir [DIR] Specify configuration directory to use *.conf files
127 from. Default: /etc/dracut.conf.d
128 --tmpdir [DIR] Temporary directory to be used instead of default
130 -l, --local Local mode. Use modules from the current working
131 directory instead of the system-wide installed in
132 /usr/lib/dracut/modules.d.
133 Useful when running dracut from a git checkout.
134 -H, --hostonly Host-Only mode: Install only what is needed for
135 booting the local host instead of a generic host.
136 -N, --no-hostonly Disables Host-Only mode
137 --persistent-policy [POLICY]
138 Use [POLICY] to address disks and partitions.
139 POLICY can be any directory name found in /dev/disk.
140 E.g. "by-uuid", "by-label"
141 --fstab Use /etc/fstab to determine the root device.
142 --add-fstab [FILE] Add file to the initramfs fstab
143 --mount "[DEV] [MP] [FSTYPE] [FSOPTS]"
144 Mount device [DEV] on mountpoint [MP] with filesystem
145 [FSTYPE] and options [FSOPTS] in the initramfs
146 --add-device "[DEV]" Bring up [DEV] in initramfs
147 -i, --include [SOURCE] [TARGET]
148 Include the files in the SOURCE directory into the
149 Target directory in the final initramfs.
150 If SOURCE is a file, it will be installed to TARGET
151 in the final initramfs.
152 -I, --install [LIST] Install the space separated list of files into the
154 --gzip Compress the generated initramfs using gzip.
155 This will be done by default, unless another
156 compression option or --no-compress is passed.
157 --bzip2 Compress the generated initramfs using bzip2.
158 Make sure your kernel has bzip2 decompression support
159 compiled in, otherwise you will not be able to boot.
160 --lzma Compress the generated initramfs using lzma.
161 Make sure your kernel has lzma support compiled in,
162 otherwise you will not be able to boot.
163 --xz Compress the generated initramfs using xz.
164 Make sure that your kernel has xz support compiled
165 in, otherwise you will not be able to boot.
166 --compress [COMPRESSION] Compress the generated initramfs with the
167 passed compression program. Make sure your kernel
168 knows how to decompress the generated initramfs,
169 otherwise you will not be able to boot.
170 --no-compress Do not compress the generated initramfs. This will
171 override any other compression options.
172 --list-modules List all available dracut modules.
173 -M, --show-modules Print included module's name to standard output during
175 --keep Keep the temporary initramfs for debugging purposes
176 --printsize Print out the module install size
177 --sshkey [SSHKEY] Add ssh key to initramfs (use with ssh-client module)
179 If [LIST] has multiple arguments, then you have to put these in quotes.
183 # dracut --add-drivers "module1 module2" ...
189 # push values to a stack
190 # $1 = stack variable
193 # push stack 1 2 "3 4"
196 local __stack=$1; shift
198 eval ${__stack}'[${#'${__stack}'[@]}]="$_i"'
203 # pops the last value from a stack
204 # assigns value to second argument variable
205 # or echo to stdout, if no second argument
206 # $1 = stack variable
207 # $2 = optional variable to store the value
212 local __stack=$1; shift
215 # check for empty stack
216 eval '[[ ${#'${__stack}'[@]} -eq 0 ]] && return 1'
218 eval _value='${'${__stack}'[${#'${__stack}'[@]}-1]}'
220 if [[ "$__resultvar" ]]; then
221 eval $__resultvar="'$_value'"
223 printf "%s" "$_value"
225 eval unset ${__stack}'[${#'${__stack}'[@]}-1]'
229 # Little helper function for reading args from the commandline.
230 # it automatically handles -a b and -a=b variants, and returns 1 if
231 # we need to shift $3.
236 local rematch='^[^=]*=(.*)$'
237 if [[ $2 =~ $rematch ]]; then
238 read "$1" <<< "${BASH_REMATCH[1]}"
241 # There is no way to shift our callers args, so
242 # return 1 to indicate they should do it instead.
249 local suffix=$1; shift
254 for i in "$d/"*"$suffix"; do
255 if [[ -e "$i" ]]; then
256 printf "%s\n" "${i##*/}"
262 for f in "${files[@]}"; do
264 if [[ -e "$d/$f" ]]; then
265 printf "%s\n" "$d/$f"
277 # Workaround -i, --include taking 2 arguments
278 set -- "${@/--include/++include}"
280 # This prevents any long argument ending with "-i"
281 # -i, like --opt-i but I think we can just prevent that
282 set -- "${@/%-i/++include}"
284 TEMP=$(unset POSIXLY_CORRECT; getopt \
285 -o "a:m:o:d:I:k:c:L:fvqlHhMN" \
289 --long add-drivers: \
290 --long omit-drivers: \
294 --long filesystems: \
314 --long print-cmdline \
315 --long kernel-cmdline: \
334 --long no-host-only \
335 --long persistent-policy: \
343 --long list-modules \
344 --long show-modules \
347 --long regenerate-all \
348 --long noimageifnotneeded \
349 --long early-microcode \
350 --long no-early-microcode \
353 if (( $? != 0 )); then
362 --kver) kernel="$2"; shift;;
363 -a|--add) push add_dracutmodules_l "$2"; shift;;
364 --force-add) push force_add_dracutmodules_l "$2"; shift;;
365 --add-drivers) push add_drivers_l "$2"; shift;;
366 --omit-drivers) push omit_drivers_l "$2"; shift;;
367 -m|--modules) push dracutmodules_l "$2"; shift;;
368 -o|--omit) push omit_dracutmodules_l "$2"; shift;;
369 -d|--drivers) push drivers_l "$2"; shift;;
370 --filesystems) push filesystems_l "$2"; shift;;
371 -I|--install) push install_items_l "$2"; shift;;
372 --fwdir) push fw_dir_l "$2"; shift;;
373 --libdirs) push libdirs_l "$2"; shift;;
374 --fscks) push fscks_l "$2"; shift;;
375 --add-fstab) push add_fstab_l "$2"; shift;;
376 --mount) push fstab_lines "$2"; shift;;
377 --add-device|--device)
378 push add_device_l "$2"; shift;;
379 --kernel-cmdline) push kernel_cmdline_l "$2"; shift;;
380 --nofscks) nofscks_l="yes";;
381 --ro-mnt) ro_mnt_l="yes";;
382 -k|--kmoddir) drivers_dir_l="$2"; shift;;
383 -c|--conf) conffile="$2"; shift;;
384 --confdir) confdir="$2"; shift;;
385 --tmpdir) tmpdir_l="$2"; shift;;
386 -L|--stdlog) stdloglvl_l="$2"; shift;;
387 --compress) compress_l="$2"; shift;;
388 --prefix) prefix_l="$2"; shift;;
389 -f|--force) force=yes;;
390 --kernel-only) kernel_only="yes"; no_kernel="no";;
391 --no-kernel) kernel_only="no"; no_kernel="yes";;
392 --print-cmdline) print_cmdline="yes"; hostonly_l="yes"; kernel_only="yes"; no_kernel="yes";;
393 --early-microcode) early_microcode_l="yes";;
394 --no-early-microcode) early_microcode_l="no";;
395 --strip) do_strip_l="yes";;
396 --nostrip) do_strip_l="no";;
397 --hardlink) do_hardlink_l="yes";;
398 --nohardlink) do_hardlink_l="no";;
399 --noprefix) prefix_l="/";;
400 --mdadmconf) mdadmconf_l="yes";;
401 --nomdadmconf) mdadmconf_l="no";;
402 --lvmconf) lvmconf_l="yes";;
403 --nolvmconf) lvmconf_l="no";;
404 --debug) debug="yes";;
405 --profile) profile="yes";;
406 --sshkey) sshkey="$2"; shift;;
407 -v|--verbose) ((verbosity_mod_l++));;
408 -q|--quiet) ((verbosity_mod_l--));;
411 [[ -f "$(readlink -f "${0%/*}")/dracut-functions.sh" ]] \
412 && dracutbasedir="$(readlink -f "${0%/*}")"
414 -H|--hostonly|--host-only)
416 -N|--no-hostonly|--no-host-only)
419 persistent_policy_l="$2"; shift;;
420 --fstab) use_fstab_l="yes" ;;
421 -h|--help) long_usage; exit 1 ;;
422 -i|--include) push include_src "$2"
424 --bzip2) compress_l="bzip2";;
425 --lzma) compress_l="lzma";;
426 --xz) compress_l="xz";;
427 --no-compress) _no_compress_l="cat";;
428 --gzip) compress_l="gzip";;
429 --list-modules) do_list="yes";;
434 --printsize) printsize="yes";;
435 --regenerate-all) regenerate_all="yes";;
436 --noimageifnotneeded) noimageifnotneeded="yes";;
440 *) # should not even reach this point
441 printf "\n!Unknown option: '%s'\n\n" "$1" >&2; usage; exit 1;;
446 # getopt cannot handle multiple arguments, so just handle "-I,--include"
447 # the old fashioned way
451 ++include) push include_src "$2"
452 push include_target "$3"
455 if ! [[ ${outfile+x} ]]; then
457 elif ! [[ ${kernel+x} ]]; then
460 printf "\nUnknown arguments: %s\n\n" "$*" >&2
468 if [[ $regenerate_all == "yes" ]]; then
470 if [[ $kernel ]]; then
471 printf -- "--regenerate-all cannot be called with a kernel version\n" >&2
475 if [[ $outfile ]]; then
476 printf -- "--regenerate-all cannot be called with a image file\n" >&2
480 ((len=${#dracut_args[@]}))
481 for ((i=0; i < len; i++)); do
482 [[ ${dracut_args[$i]} == "--regenerate-all" ]] && \
483 unset dracut_args[$i]
488 [[ -f $i/modules.builtin ]] || continue
489 dracut --kver="$i" "${dracut_args[@]}"
493 elif [[ $kernel ]]; then
494 if ! [[ -d /lib/modules/$kernel ]] && [[ $no_kernel != yes ]]; then
495 printf -- "Kernel version $kernel has no modules in /lib/modules/$kernel\n" >&2
500 if ! [[ $kernel ]]; then
504 if ! [[ $outfile ]]; then
505 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
507 if [[ $MACHINE_ID ]] && ( [[ -d /boot/${MACHINE_ID} ]] || [[ -L /boot/${MACHINE_ID} ]] ); then
508 outfile="/boot/${MACHINE_ID}/$kernel/initrd"
510 outfile="/boot/initramfs-$kernel.img"
514 for i in /usr/sbin /sbin /usr/bin /bin; do
519 if [[ "$NPATH" != "*:$rl*" ]] ; then
523 export PATH="${NPATH#:}"
529 unset LD_LIBRARY_PATH
532 export DRACUT_LOG_LEVEL=warning
534 export DRACUT_LOG_LEVEL=debug
535 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
540 export PS4='+ $(date "+%s.%N") ${BASH_SOURCE}@${LINENO}: ';
545 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
547 # if we were not passed a config file, try the default one
548 if [[ ! -f $conffile ]]; then
549 if [[ $allowlocal ]]; then
550 conffile="$dracutbasedir/dracut.conf"
552 conffile="/etc/dracut.conf"
556 if [[ ! -d $confdir ]]; then
557 if [[ $allowlocal ]]; then
558 confdir="$dracutbasedir/dracut.conf.d"
560 confdir="/etc/dracut.conf.d"
564 # source our config file
565 [[ -f $conffile ]] && . "$conffile"
567 # source our config dir
568 for f in $(dropindirs_sort ".conf" "$confdir" "$dracutbasedir/dracut.conf.d"); do
569 [[ -e $f ]] && . "$f"
572 # these optins add to the stuff in the config file
573 if (( ${#add_dracutmodules_l[@]} )); then
574 while pop add_dracutmodules_l val; do
575 add_dracutmodules+=" $val "
579 if (( ${#force_add_dracutmodules_l[@]} )); then
580 while pop force_add_dracutmodules_l val; do
581 force_add_dracutmodules+=" $val "
585 if (( ${#fscks_l[@]} )); then
586 while pop fscks_l val; do
591 if (( ${#add_fstab_l[@]} )); then
592 while pop add_fstab_l val; do
597 if (( ${#fstab_lines_l[@]} )); then
598 while pop fstab_lines_l val; do
599 push fstab_lines $val
603 if (( ${#install_items_l[@]} )); then
604 while pop install_items_l val; do
605 install_items+=" $val "
609 # these options override the stuff in the config file
610 if (( ${#dracutmodules_l[@]} )); then
612 while pop dracutmodules_l val; do
613 dracutmodules+="$val "
617 if (( ${#omit_dracutmodules_l[@]} )); then
618 omit_dracutmodules=''
619 while pop omit_dracutmodules_l val; do
620 omit_dracutmodules+="$val "
624 if (( ${#filesystems_l[@]} )); then
626 while pop filesystems_l val; do
631 if (( ${#fw_dir_l[@]} )); then
633 while pop fw_dir_l val; do
638 if (( ${#libdirs_l[@]} )); then
640 while pop libdirs_l val; do
645 [[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
646 [[ ! $stdloglvl ]] && stdloglvl=4
647 stdloglvl=$((stdloglvl + verbosity_mod_l))
648 ((stdloglvl > 6)) && stdloglvl=6
649 ((stdloglvl < 0)) && stdloglvl=0
651 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
652 [[ $do_strip_l ]] && do_strip=$do_strip_l
653 [[ $do_strip ]] || do_strip=yes
654 [[ $do_hardlink_l ]] && do_hardlink=$do_hardlink_l
655 [[ $do_hardlink ]] || do_hardlink=yes
656 [[ $prefix_l ]] && prefix=$prefix_l
657 [[ $prefix = "/" ]] && unset prefix
658 [[ $hostonly_l ]] && hostonly=$hostonly_l
659 [[ $persistent_policy_l ]] && persistent_policy=$persistent_policy_l
660 [[ $use_fstab_l ]] && use_fstab=$use_fstab_l
661 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
662 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
663 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
664 [[ $fw_dir ]] || fw_dir="/lib/firmware/updates /lib/firmware"
665 [[ $tmpdir_l ]] && tmpdir="$tmpdir_l"
666 [[ $tmpdir ]] || tmpdir=/var/tmp
667 [[ $compress_l ]] && compress=$compress_l
668 [[ $show_modules_l ]] && show_modules=$show_modules_l
669 [[ $nofscks_l ]] && nofscks="yes"
670 [[ $ro_mnt_l ]] && ro_mnt="yes"
671 [[ $early_microcode_l ]] && early_microcode=$early_microcode_l
672 [[ $early_microcode ]] || early_microcode=no
673 # eliminate IFS hackery when messing with fw_dir
674 fw_dir=${fw_dir//:/ }
676 # handle compression options.
677 [[ $compress ]] || compress="gzip"
679 bzip2) compress="bzip2 -9";;
680 lzma) compress="lzma -9";;
681 xz) compress="xz --check=crc32 --lzma2=dict=1MiB";;
682 gzip) compress="gzip -9"; command -v pigz > /dev/null 2>&1 && compress="pigz -9";;
684 if [[ $_no_compress_l = "cat" ]]; then
688 [[ $hostonly = yes ]] && hostonly="-h"
689 [[ $hostonly != "-h" ]] && unset hostonly
691 readonly TMPDIR="$tmpdir"
692 readonly initdir="$(mktemp --tmpdir="$TMPDIR/" -d -t initramfs.XXXXXX)"
693 [ -d "$initdir" ] || {
694 printf "%s\n" "dracut: mktemp --tmpdir=\"$TMPDIR/\" -d -t initramfs.XXXXXX failed." >&2
698 if [[ $early_microcode = yes ]]; then
699 readonly microcode_dir="$(mktemp --tmpdir="$TMPDIR/" -d -t early_microcode.XXXXXX)"
700 [ -d "$microcode_dir" ] || {
701 printf "%s\n" "dracut: mktemp --tmpdir=\"$TMPDIR/\" -d -t early_microcode.XXXXXX failed." >&2
705 # clean up after ourselves no matter how we die.
708 [[ $outfile ]] && [[ -f $outfile.$$ ]] && rm -f -- "$outfile.$$";
709 [[ $keep ]] && echo "Not removing $initdir." >&2 || { [[ $initdir ]] && rm -rf -- "$initdir"; };
710 [[ $keep ]] && echo "Not removing $microcode_dir." >&2 || { [[ $microcode_dir ]] && rm -Rf -- "$microcode_dir"; };
711 [[ $_dlogdir ]] && rm -Rf -- "$_dlogdir";
715 # clean up after ourselves no matter how we die.
716 trap 'exit 1;' SIGINT
718 export DRACUT_KERNEL_LAZY="1"
719 export DRACUT_RESOLVE_LAZY="1"
721 if [[ -f $dracutbasedir/dracut-functions.sh ]]; then
722 . $dracutbasedir/dracut-functions.sh
724 printf "%s\n" "dracut: Cannot find $dracutbasedir/dracut-functions.sh." >&2
725 printf "%s\n" "dracut: Are you running from a git checkout?" >&2
726 printf "%s\n" "dracut: Try passing -l as an argument to $0" >&2
730 if ! [[ $print_cmdline ]]; then
732 if ! $DRACUT_INSTALL ${initdir+-D "$initdir"} -R "$initdir/bin/sh" &>/dev/null; then
733 unset DRACUT_RESOLVE_LAZY
734 export DRACUT_RESOLVE_DEPS=1
736 rm -fr -- ${initdir}/*
739 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
740 . $dracutbasedir/dracut-version.sh
743 # Verify bash version, current minimum is 3.1
744 if (( BASH_VERSINFO[0] < 4 )); then
745 dfatal 'You need at least Bash 4 to use dracut, sorry.'
749 dracutfunctions=$dracutbasedir/dracut-functions.sh
750 export dracutfunctions
752 if (( ${#drivers_l[@]} )); then
754 while pop drivers_l val; do
758 drivers=${drivers/-/_}
760 if (( ${#add_drivers_l[@]} )); then
761 while pop add_drivers_l val; do
762 add_drivers+=" $val "
765 add_drivers=${add_drivers/-/_}
767 if (( ${#omit_drivers_l[@]} )); then
768 while pop omit_drivers_l val; do
769 omit_drivers+=" $val "
772 omit_drivers=${omit_drivers/-/_}
774 if (( ${#kernel_cmdline_l[@]} )); then
775 while pop kernel_cmdline_l val; do
776 kernel_cmdline+=" $val "
780 omit_drivers_corrected=""
781 for d in $omit_drivers; do
782 [[ " $drivers $add_drivers " == *\ $d\ * ]] && continue
783 omit_drivers_corrected+="$d|"
785 omit_drivers="${omit_drivers_corrected%|}"
786 unset omit_drivers_corrected
788 # prepare args for logging
789 for ((i=0; i < ${#dracut_args[@]}; i++)); do
790 [[ "${dracut_args[$i]}" == *\ * ]] && \
791 dracut_args[$i]="\"${dracut_args[$i]}\""
794 dinfo "Executing: $0 ${dracut_args[@]}"
796 [[ $do_list = yes ]] && {
797 for mod in $dracutbasedir/modules.d/*; do
798 [[ -d $mod ]] || continue;
799 [[ -e $mod/install || -e $mod/installkernel || \
800 -e $mod/module-setup.sh ]] || continue
801 printf "%s\n" "${mod##*/??}"
806 # This is kinda legacy -- eventually it should go away.
807 case $dracutmodules in
808 ""|auto) dracutmodules="all" ;;
811 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
813 if [[ -d $srcmods ]]; then
814 [[ -f $srcmods/modules.dep ]] || {
815 dwarn "$srcmods/modules.dep is missing. Did you run depmod?"
819 if [[ -f $outfile && ! $force && ! $print_cmdline ]]; then
820 dfatal "Will not override existing initramfs ($outfile) without --force"
825 [[ $outdir ]] || outdir="/"
827 if [[ ! -d "$outdir" ]]; then
828 dfatal "Can't write to $outdir: Directory $outdir does not exist or is not accessible."
830 elif [[ ! -w "$outdir" ]]; then
831 dfatal "No permission to write to $outdir."
833 elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
834 dfatal "No permission to write $outfile."
838 # Need to be able to have non-root users read stuff (rpcbind etc)
841 if [[ $hostonly ]]; then
842 for i in /sys /proc /run /dev; do
843 if ! findmnt --target "$i" &>/dev/null; then
844 dwarning "Turning off host-only mode: '$i' is not mounted!"
850 declare -A host_fs_types
852 for line in "${fstab_lines[@]}"; do
856 host_fs_types["$1"]="$3"
859 for f in $add_fstab; do
860 [[ -e $f ]] || continue
861 while read dev rest; do
862 push host_devs "$dev"
866 for dev in $add_device; do
867 push host_devs "$dev"
870 if (( ${#add_device_l[@]} )); then
871 while pop add_device_l val; do
873 push host_devs "$val"
877 if [[ $hostonly ]]; then
878 # in hostonly mode, determine all devices, which have to be accessed
879 # and examine them for filesystem types
895 mp=$(readlink -f "$mp")
896 mountpoint "$mp" >/dev/null 2>&1 || continue
897 _dev=$(find_block_device "$mp")
898 _bdev=$(readlink -f "/dev/block/$_dev")
899 [[ -b $_bdev ]] && _dev=$_bdev
901 [[ "$_mp" == "/" ]] && root_dev="$_dev"
902 push host_devs "$_dev"
905 if [[ -f /proc/swaps ]] && [[ -f /etc/fstab ]]; then
906 while read dev type rest; do
907 [[ -b $dev ]] || continue
908 [[ "$type" == "partition" ]] || continue
910 while read _d _m _t _o _r; do
911 [[ "$_d" == \#* ]] && continue
912 [[ $_d ]] || continue
913 [[ $_t != "swap" ]] && continue
914 [[ $_m != "swap" ]] && [[ $_m != "none" ]] && continue
915 [[ "$_o" == *noauto* ]] && continue
916 [[ "$_d" == UUID\=* ]] && _d="/dev/disk/by-uuid/${_d#UUID=}"
917 [[ "$_d" == LABEL\=* ]] && _d="/dev/disk/by-label/$_d#LABEL=}"
918 [[ "$_d" -ef "$dev" ]] || continue
920 if [[ -f /etc/crypttab ]]; then
921 while read _mapper _a _p _o; do
922 [[ $_mapper = \#* ]] && continue
923 [[ "$_d" -ef /dev/mapper/"$_mapper" ]] || continue
924 [[ "$_o" ]] || _o="$_p"
926 [[ $_o == *swap* ]] && continue 2
930 push host_devs "$(readlink -f "$dev")"
939 if [[ -b /dev/block/$1 ]] && ID_FS_TYPE=$(get_fs_env "/dev/block/$1"); then
940 host_fs_types["$(readlink -f "/dev/block/$1")"]="$ID_FS_TYPE"
943 if [[ -b $1 ]] && ID_FS_TYPE=$(get_fs_env "$1"); then
944 host_fs_types["$(readlink -f "$1")"]="$ID_FS_TYPE"
947 if fstype=$(find_dev_fstype "$1"); then
948 host_fs_types["$1"]="$fstype"
954 for dev in ${host_devs[@]}; do
956 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
959 for dev in "${!host_fs_types[@]}"; do
960 [[ ${host_fs_types[$dev]} = "reiserfs" ]] || [[ ${host_fs_types[$dev]} = "xfs" ]] || continue
961 rootopts=$(find_dev_fsopts "$dev")
962 if [[ ${host_fs_types[$dev]} = "reiserfs" ]]; then
963 journaldev=$(fs_get_option $rootopts "jdev")
964 elif [[ ${host_fs_types[$dev]} = "xfs" ]]; then
965 journaldev=$(fs_get_option $rootopts "logdev")
967 if [[ $journaldev ]]; then
968 dev="$(readlink -f "$dev")"
969 push host_devs "$dev"
971 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
976 || udevdir="$(pkg-config udev --variable=udevdir 2>/dev/null)"
977 if ! [[ -d "$udevdir" ]]; then
978 [[ -d /lib/udev ]] && udevdir=/lib/udev
979 [[ -d /usr/lib/udev ]] && udevdir=/usr/lib/udev
982 [[ -d $systemdutildir ]] \
983 || systemdutildir=$(pkg-config systemd --variable=systemdutildir 2>/dev/null)
985 if ! [[ -d "$systemdutildir" ]]; then
986 [[ -d /lib/systemd ]] && systemdutildir=/lib/systemd
987 [[ -d /usr/lib/systemd ]] && systemdutildir=/usr/lib/systemd
990 [[ -d $systemdsystemunitdir ]] \
991 || systemdsystemunitdir=$(pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null)
993 [[ -d "$systemdsystemunitdir" ]] || systemdsystemunitdir=${systemdutildir}/system
995 [[ -d $systemdsystemconfdir ]] \
996 || systemdsystemconfdir=$(pkg-config systemd --variable=systemdsystemconfdir 2>/dev/null)
998 [[ -d "$systemdsystemconfdir" ]] || systemdsystemconfdir=/etc/systemd/system
1000 export initdir dracutbasedir dracutmodules \
1001 fw_dir drivers_dir debug no_kernel kernel_only \
1002 omit_drivers mdadmconf lvmconf root_dev \
1003 use_fstab fstab_lines libdirs fscks nofscks ro_mnt \
1004 stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
1005 debug host_fs_types host_devs sshkey add_fstab \
1006 DRACUT_VERSION udevdir prefix filesystems drivers \
1007 systemdutildir systemdsystemunitdir systemdsystemconfdir
1010 # check all our modules to see if they should be sourced.
1011 # This builds a list of modules that we will install next.
1012 for_each_module_dir check_module
1013 for_each_module_dir check_mount
1015 dracut_module_included "fips" && export DRACUT_FIPS_MODE=1
1017 if [[ $print_cmdline ]]; then
1019 # source our modules.
1020 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1021 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
1022 module_cmdline "$_d_mod"
1029 # Create some directory structure first
1030 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
1032 [[ -h /lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
1033 [[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
1035 if [[ $prefix ]]; then
1036 for d in bin etc lib sbin tmp usr var $libdirs; do
1037 [[ "$d" == */* ]] && continue
1038 ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
1042 if [[ $kernel_only != yes ]]; then
1043 for d in usr/bin usr/sbin bin etc lib sbin tmp usr var $libdirs; do
1044 [[ -e "${initdir}${prefix}/$d" ]] && continue
1045 if [ -L "/$d" ]; then
1046 inst_symlink "/$d" "${prefix}/$d"
1048 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1052 for d in dev proc sys sysroot root run run/lock run/initramfs; do
1053 if [ -L "/$d" ]; then
1056 mkdir -m 0755 -p "$initdir/$d"
1060 ln -sfn ../run "$initdir/var/run"
1061 ln -sfn ../run/lock "$initdir/var/lock"
1062 ln -sfn ../run/log "$initdir/var/log"
1064 for d in lib "$libdir"; do
1065 [[ -e "${initdir}${prefix}/$d" ]] && continue
1066 if [ -h "/$d" ]; then
1067 inst "/$d" "${prefix}/$d"
1069 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1074 if [[ $kernel_only != yes ]]; then
1075 mkdir -p "${initdir}/etc/cmdline.d"
1076 for _d in $hookdirs; do
1077 mkdir -m 0755 -p ${initdir}/lib/dracut/hooks/$_d
1079 if [[ "$UID" = "0" ]]; then
1080 [ -c ${initdir}/dev/null ] || mknod ${initdir}/dev/null c 1 3
1081 [ -c ${initdir}/dev/kmsg ] || mknod ${initdir}/dev/kmsg c 1 11
1082 [ -c ${initdir}/dev/console ] || mknod ${initdir}/dev/console c 5 1
1086 _isize=0 #initramfs size
1088 # source our modules.
1089 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1090 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
1091 if [[ "$mods_to_load" == *\ $_d_mod\ * ]]; then
1092 if [[ $show_modules = yes ]]; then
1093 printf "%s\n" "$_d_mod"
1095 dinfo "*** Including module: $_d_mod ***"
1097 if [[ $kernel_only == yes ]]; then
1098 module_installkernel "$_d_mod" || {
1099 dfatal "installkernel failed in module $_d_mod"
1103 module_install "$_d_mod"
1104 if [[ $no_kernel != yes ]]; then
1105 module_installkernel "$_d_mod" || {
1106 dfatal "installkernel failed in module $_d_mod"
1111 mods_to_load=${mods_to_load// $_d_mod /}
1112 modules_loaded+="$_d_mod "
1114 #print the module install size
1115 if [ -n "$printsize" ]; then
1116 _isize_new=$(du -sk ${initdir}|cut -f1)
1117 _isize_delta=$((_isize_new - _isize))
1118 printf "%s\n" "$_d_mod install size: ${_isize_delta}k"
1125 for i in $modules_loaded; do
1126 mkdir -p $initdir/lib/dracut
1127 printf "%s\n" "$i" >> $initdir/lib/dracut/modules.txt
1130 dinfo "*** Including modules done ***"
1132 ## final stuff that has to happen
1133 if [[ $no_kernel != yes ]]; then
1135 if [[ $drivers ]]; then
1136 hostonly='' instmods $drivers
1139 if [[ $add_drivers ]]; then
1140 hostonly='' instmods -c $add_drivers
1142 if [[ $filesystems ]]; then
1143 hostonly='' instmods -c $filesystems
1146 dinfo "*** Installing kernel module dependencies and firmware ***"
1148 dinfo "*** Installing kernel module dependencies and firmware done ***"
1150 if [[ $noimageifnotneeded == yes ]] && [[ $hostonly ]]; then
1151 if [[ ! -f "$initdir/lib/dracut/need-initqueue" ]] && \
1152 [[ -f ${initdir}/lib/modules/$kernel/modules.dep && ! -s ${initdir}/lib/modules/$kernel/modules.dep ]]; then
1153 for i in ${initdir}/etc/cmdline.d/*.conf; do
1154 # We need no initramfs image and do not generate one.
1155 [[ $i == "${initdir}/etc/cmdline.d/*.conf" ]] && exit 0
1161 if [[ $kernel_only != yes ]]; then
1162 (( ${#install_items[@]} > 0 )) && inst_multiple ${install_items[@]}
1164 [[ $kernel_cmdline ]] && printf "%s\n" "$kernel_cmdline" >> "${initdir}/etc/cmdline.d/01-default.conf"
1166 while pop fstab_lines line; do
1167 printf "%s\n" "$line 0 0" >> "${initdir}/etc/fstab"
1170 for f in $add_fstab; do
1171 cat "$f" >> "${initdir}/etc/fstab"
1174 if [ -d ${initdir}/$systemdutildir ]; then
1175 mkdir -p ${initdir}/etc/conf.d
1177 printf "%s\n" "systemdutildir=\"$systemdutildir\""
1178 printf "%s\n" "systemdsystemunitdir=\"$systemdsystemunitdir\""
1179 printf "%s\n" "systemdsystemconfdir=\"$systemdsystemconfdir\""
1180 } > ${initdir}/etc/conf.d/systemd.conf
1183 if [[ $DRACUT_RESOLVE_LAZY ]] && [[ $DRACUT_INSTALL ]]; then
1184 dinfo "*** Resolving executable dependencies ***"
1185 find "$initdir" -type f \
1186 '(' -perm -0100 -or -perm -0010 -or -perm -0001 ')' \
1187 -not -path '*.ko' -print0 \
1188 | xargs -r -0 $DRACUT_INSTALL ${initdir+-D "$initdir"} -R ${DRACUT_FIPS_MODE+-H} --
1189 dinfo "*** Resolving executable dependencies done***"
1193 while pop include_src src && pop include_target tgt; do
1194 if [[ $src && $tgt ]]; then
1195 if [[ -f $src ]]; then
1198 ddebug "Including directory: $src"
1199 mkdir -p "${initdir}/${tgt}"
1200 # check for preexisting symlinks, so we can cope with the
1201 # symlinks to $prefix
1202 for i in "$src"/*; do
1203 [[ -e "$i" || -h "$i" ]] || continue
1204 s=${initdir}/${tgt}/${i#$src/}
1205 if [[ -d "$i" ]]; then
1206 if ! [[ -e "$s" ]]; then
1207 mkdir -m 0755 -p "$s"
1208 chmod --reference="$i" "$s"
1210 cp --reflink=auto --sparse=auto -fa -t "$s" "$i"/*
1212 cp --reflink=auto --sparse=auto -fa -t "$s" "$i"
1219 if [[ $kernel_only != yes ]]; then
1220 # make sure that library links are correct and up to date
1221 for f in /etc/ld.so.conf /etc/ld.so.conf.d/*; do
1222 [[ -f $f ]] && inst_simple "$f"
1224 if ! ldconfig -r "$initdir"; then
1225 if [[ $UID = 0 ]]; then
1226 derror "ldconfig exited ungracefully"
1228 derror "ldconfig might need uid=0 (root) for chroot()"
1233 PRELINK_BIN="$(command -v prelink)"
1234 if [[ $UID = 0 ]] && [[ $PRELINK_BIN ]]; then
1235 if [[ $DRACUT_FIPS_MODE ]]; then
1236 dinfo "*** Installing prelink files ***"
1237 inst_multiple -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf /etc/prelink.cache
1239 dinfo "*** Pre-linking files ***"
1240 inst_multiple -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf
1241 chroot "$initdir" "$PRELINK_BIN" -a
1242 rm -f -- "$initdir/$PRELINK_BIN"
1243 rm -fr -- "$initdir"/etc/prelink.*
1244 dinfo "*** Pre-linking files done ***"
1248 if [[ $do_hardlink = yes ]] && command -v hardlink >/dev/null; then
1249 dinfo "*** Hardlinking files ***"
1250 hardlink "$initdir" 2>&1
1251 dinfo "*** Hardlinking files done ***"
1255 if [[ $do_strip = yes ]] ; then
1256 for p in strip xargs find; do
1257 if ! type -P $p >/dev/null; then
1258 dinfo "Could not find '$p'. Not stripping the initramfs."
1264 if [[ $do_strip = yes ]] && ! [[ $DRACUT_FIPS_MODE ]]; then
1265 dinfo "*** Stripping files ***"
1266 find "$initdir" -type f \
1267 -executable -not -path '*/lib/modules/*.ko' -print0 \
1268 | xargs -r -0 strip -g 2>/dev/null
1270 # strip kernel modules, but do not touch signed modules
1271 find "$initdir" -type f -path '*/lib/modules/*.ko' -print0 \
1272 | while read -r -d $'\0' f; do
1273 SIG=$(tail -c 28 "$f")
1274 [[ $SIG == '~Module signature appended~' ]] || { printf "%s\000" "$f"; }
1275 done | xargs -r -0 strip -g
1277 dinfo "*** Stripping files done ***"
1279 if [[ $early_microcode = yes ]]; then
1280 dinfo "*** Generating early-microcode cpio image ***"
1281 ucode_dir=(amd-ucode intel-ucode)
1282 ucode_dest=(AuthenticAMD.bin GenuineIntel.bin)
1283 _dest_dir="$microcode_dir/d/kernel/x86/microcode"
1286 if [[ $hostonly ]]; then
1287 [[ $(get_cpu_vendor) == "AMD" ]] && _dest_idx="0"
1288 [[ $(get_cpu_vendor) == "Intel" ]] && _dest_idx="1"
1290 for idx in $_dest_idx; do
1291 _fw=${ucode_dir[$idx]}
1292 for _fwdir in $fw_dir; do
1293 if [[ -d $_fwdir && -d $_fwdir/$_fw ]]; then
1295 dinfo "*** Constructing ${ucode_dest[$idx]} ****"
1296 if [[ $hostonly ]]; then
1297 _src=$(get_ucode_file)
1299 cat $_fwdir/$_fw/$_src > $_dest_dir/${ucode_dest[$idx]}
1303 (cd "$microcode_dir/d"; find . -print0 | cpio --null -o -H newc --quiet >../ucode.cpio)
1307 dinfo "*** Creating image file ***"
1308 if [[ $early_microcode = yes ]]; then
1309 # The microcode blob is _before_ the initramfs blob, not after
1310 mv $microcode_dir/ucode.cpio $outfile.$$
1312 if ! ( umask 077; cd "$initdir"; find . -print0 | cpio --null -R 0:0 -H newc -o --quiet| \
1313 $compress >> "$outfile.$$"; ); then
1314 dfatal "dracut: creation of $outfile.$$ failed"
1317 mv -- "$outfile.$$" "$outfile"
1318 dinfo "*** Creating image file done ***"
1320 if (( maxloglvl >= 5 )); then
1321 if [[ $allowlocal ]]; then
1322 "$dracutbasedir/lsinitrd.sh" "$outfile"| ddebug
1324 lsinitrd "$outfile"| ddebug