TEST-16-DMSQUASH: add more debug parms
[dracut.git] / dracut.sh
blobd06d7d464a587f36c56dbed43d1e8dbc46aa8ce1
1 #!/bin/bash --norc
3 # Generator script for a dracut initramfs
4 # Tries to retain some degree of compatibility with the command line
5 # of the various mkinitrd implementations out there
8 # Copyright 2005-2013 Red Hat, Inc. All rights reserved.
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # store for logging
25 dracut_args=( "$@" )
27 set -o pipefail
29 usage() {
30 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
31 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
32 . $dracutbasedir/dracut-version.sh
35 # 80x25 linebreak here ^
36 cat << EOF
37 Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
39 Version: $DRACUT_VERSION
41 Creates initial ramdisk images for preloading modules
43 -h, --help Display all options
45 If a [LIST] has multiple arguments, then you have to put these in quotes.
47 For example:
49 # dracut --add-drivers "module1 module2" ...
51 EOF
54 long_usage() {
55 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
56 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
57 . $dracutbasedir/dracut-version.sh
60 # 80x25 linebreak here ^
61 cat << EOF
62 Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
64 Version: $DRACUT_VERSION
66 Creates initial ramdisk images for preloading modules
68 --kver [VERSION] Set kernel version to [VERSION].
69 -f, --force Overwrite existing initramfs file.
70 -a, --add [LIST] Add a space-separated list of dracut modules.
71 --rebuild Append arguments to those of existing image and rebuild
72 -m, --modules [LIST] Specify a space-separated list of dracut modules to
73 call when building the initramfs. Modules are located
74 in /usr/lib/dracut/modules.d.
75 -o, --omit [LIST] Omit a space-separated list of dracut modules.
76 --force-add [LIST] Force to add a space-separated list of dracut modules
77 to the default set of modules, when -H is specified.
78 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
79 exclusively include in the initramfs.
80 --add-drivers [LIST] Specify a space-separated list of kernel
81 modules to add to the initramfs.
82 --force-drivers [LIST] Specify a space-separated list of kernel
83 modules to add to the initramfs and make sure they
84 are tried to be loaded via modprobe same as passing
85 rd.driver.pre=DRIVER kernel parameter.
86 --omit-drivers [LIST] Specify a space-separated list of kernel
87 modules not to add to the initramfs.
88 --filesystems [LIST] Specify a space-separated list of kernel filesystem
89 modules to exclusively include in the generic
90 initramfs.
91 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
92 modules
93 --fwdir [DIR] Specify additional directories, where to look for
94 firmwares, separated by :
95 --kernel-only Only install kernel drivers and firmware files
96 --no-kernel Do not install kernel drivers and firmware files
97 --print-cmdline Print the kernel command line for the given disk layout
98 --early-microcode Combine early microcode with ramdisk
99 --no-early-microcode Do not combine early microcode with ramdisk
100 --kernel-cmdline [PARAMETERS] Specify default kernel command line parameters
101 --strip Strip binaries in the initramfs
102 --nostrip Do not strip binaries in the initramfs
103 --prelink Prelink binaries in the initramfs
104 --noprelink Do not prelink binaries in the initramfs
105 --hardlink Hardlink files in the initramfs
106 --nohardlink Do not hardlink files in the initramfs
107 --prefix [DIR] Prefix initramfs files with [DIR]
108 --noprefix Do not prefix initramfs files
109 --mdadmconf Include local /etc/mdadm.conf
110 --nomdadmconf Do not include local /etc/mdadm.conf
111 --lvmconf Include local /etc/lvm/lvm.conf
112 --nolvmconf Do not include local /etc/lvm/lvm.conf
113 --fscks [LIST] Add a space-separated list of fsck helpers.
114 --nofscks Inhibit installation of any fsck helpers.
115 --ro-mnt Mount / and /usr read-only by default.
116 -h, --help This message
117 --debug Output debug information of the build process
118 --profile Output profile information of the build process
119 -L, --stdlog [0-6] Specify logging level (to standard error)
120 0 - suppress any messages
121 1 - only fatal errors
122 2 - all errors
123 3 - warnings
124 4 - info
125 5 - debug info (here starts lots of output)
126 6 - trace info (and even more)
127 -v, --verbose Increase verbosity level
128 -q, --quiet Decrease verbosity level
129 -c, --conf [FILE] Specify configuration file to use.
130 Default: /etc/dracut.conf
131 --confdir [DIR] Specify configuration directory to use *.conf files
132 from. Default: /etc/dracut.conf.d
133 --tmpdir [DIR] Temporary directory to be used instead of default
134 /var/tmp.
135 -l, --local Local mode. Use modules from the current working
136 directory instead of the system-wide installed in
137 /usr/lib/dracut/modules.d.
138 Useful when running dracut from a git checkout.
139 -H, --hostonly Host-Only mode: Install only what is needed for
140 booting the local host instead of a generic host.
141 -N, --no-hostonly Disables Host-Only mode
142 --hostonly-cmdline Store kernel command line arguments needed
143 in the initramfs
144 --no-hostonly-cmdline Do not store kernel command line arguments needed
145 in the initramfs
146 --persistent-policy [POLICY]
147 Use [POLICY] to address disks and partitions.
148 POLICY can be any directory name found in /dev/disk.
149 E.g. "by-uuid", "by-label"
150 --fstab Use /etc/fstab to determine the root device.
151 --add-fstab [FILE] Add file to the initramfs fstab
152 --mount "[DEV] [MP] [FSTYPE] [FSOPTS]"
153 Mount device [DEV] on mountpoint [MP] with filesystem
154 [FSTYPE] and options [FSOPTS] in the initramfs
155 --add-device "[DEV]" Bring up [DEV] in initramfs
156 -i, --include [SOURCE] [TARGET]
157 Include the files in the SOURCE directory into the
158 Target directory in the final initramfs.
159 If SOURCE is a file, it will be installed to TARGET
160 in the final initramfs.
161 -I, --install [LIST] Install the space separated list of files into the
162 initramfs.
163 --install-optional [LIST] Install the space separated list of files into the
164 initramfs, if they exist.
165 --gzip Compress the generated initramfs using gzip.
166 This will be done by default, unless another
167 compression option or --no-compress is passed.
168 --bzip2 Compress the generated initramfs using bzip2.
169 Make sure your kernel has bzip2 decompression support
170 compiled in, otherwise you will not be able to boot.
171 --lzma Compress the generated initramfs using lzma.
172 Make sure your kernel has lzma support compiled in,
173 otherwise you will not be able to boot.
174 --xz Compress the generated initramfs using xz.
175 Make sure that your kernel has xz support compiled
176 in, otherwise you will not be able to boot.
177 --lzo Compress the generated initramfs using lzop.
178 Make sure that your kernel has lzo support compiled
179 in, otherwise you will not be able to boot.
180 --lz4 Compress the generated initramfs using lz4.
181 Make sure that your kernel has lz4 support compiled
182 in, otherwise you will not be able to boot.
183 --compress [COMPRESSION] Compress the generated initramfs with the
184 passed compression program. Make sure your kernel
185 knows how to decompress the generated initramfs,
186 otherwise you will not be able to boot.
187 --no-compress Do not compress the generated initramfs. This will
188 override any other compression options.
189 --list-modules List all available dracut modules.
190 -M, --show-modules Print included module's name to standard output during
191 build.
192 --keep Keep the temporary initramfs for debugging purposes
193 --printsize Print out the module install size
194 --sshkey [SSHKEY] Add ssh key to initramfs (use with ssh-client module)
195 --logfile [FILE] Logfile to use (overrides configuration setting)
196 --reproducible Create reproducible images
197 --loginstall [DIR] Log all files installed from the host to [DIR]
198 --uefi Create an UEFI executable with the kernel cmdline and
199 kernel combined
200 --uefi-stub [FILE] Use the UEFI stub [FILE] to create an UEFI executable
201 --kernel-image [FILE] location of the kernel image
203 If [LIST] has multiple arguments, then you have to put these in quotes.
205 For example:
207 # dracut --add-drivers "module1 module2" ...
212 # function push()
213 # push values to a stack
214 # $1 = stack variable
215 # $2.. values
216 # example:
217 # push stack 1 2 "3 4"
218 push() {
219 local _i
220 local __stack=$1; shift
221 for _i in "$@"; do
222 eval ${__stack}'[${#'${__stack}'[@]}]="$_i"'
223 done
226 # Fills up host_devs stack variable and makes sure there are no duplicates
227 push_host_devs() {
228 local _dev
229 for _dev in ${host_devs[@]}; do
230 [ "$_dev" = "$1" ] && return
231 done
232 push host_devs "$1"
236 # function pop()
237 # pops the last value from a stack
238 # assigns value to second argument variable
239 # or echo to stdout, if no second argument
240 # $1 = stack variable
241 # $2 = optional variable to store the value
242 # example:
243 # pop stack val
244 # val=$(pop stack)
245 pop() {
246 local __stack=$1; shift
247 local __resultvar=$1
248 local _value;
249 # check for empty stack
250 eval '[[ ${#'${__stack}'[@]} -eq 0 ]] && return 1'
252 eval _value='${'${__stack}'[${#'${__stack}'[@]}-1]}'
254 if [[ "$__resultvar" ]]; then
255 eval $__resultvar="'$_value'"
256 else
257 printf "%s" "$_value"
259 eval unset ${__stack}'[${#'${__stack}'[@]}-1]'
260 return 0
263 # Little helper function for reading args from the commandline.
264 # it automatically handles -a b and -a=b variants, and returns 1 if
265 # we need to shift $3.
266 read_arg() {
267 # $1 = arg name
268 # $2 = arg value
269 # $3 = arg parameter
270 local rematch='^[^=]*=(.*)$'
271 if [[ $2 =~ $rematch ]]; then
272 read "$1" <<< "${BASH_REMATCH[1]}"
273 else
274 read "$1" <<< "$3"
275 # There is no way to shift our callers args, so
276 # return 1 to indicate they should do it instead.
277 return 1
281 dropindirs_sort()
283 local suffix=$1; shift
284 local -a files
285 local f d
287 for d in "$@"; do
288 for i in "$d/"*"$suffix"; do
289 if [[ -e "$i" ]]; then
290 printf "%s\n" "${i##*/}"
292 done
293 done | sort -Vu | {
294 readarray -t files
296 for f in "${files[@]}"; do
297 for d in "$@"; do
298 if [[ -e "$d/$f" ]]; then
299 printf "%s\n" "$d/$f"
300 continue 2
302 done
303 done
307 rearrange_params()
309 # Workaround -i, --include taking 2 arguments
310 set -- "${@/--include/++include}"
312 # This prevents any long argument ending with "-i"
313 # -i, like --opt-i but I think we can just prevent that
314 set -- "${@/%-i/++include}"
316 TEMP=$(unset POSIXLY_CORRECT; getopt \
317 -o "a:m:o:d:I:k:c:L:fvqlHhMN" \
318 --long kver: \
319 --long add: \
320 --long force-add: \
321 --long add-drivers: \
322 --long force-drivers: \
323 --long omit-drivers: \
324 --long modules: \
325 --long omit: \
326 --long drivers: \
327 --long filesystems: \
328 --long install: \
329 --long install-optional: \
330 --long fwdir: \
331 --long libdirs: \
332 --long fscks: \
333 --long add-fstab: \
334 --long mount: \
335 --long device: \
336 --long add-device: \
337 --long nofscks: \
338 --long ro-mnt \
339 --long kmoddir: \
340 --long conf: \
341 --long confdir: \
342 --long tmpdir: \
343 --long stdlog: \
344 --long compress: \
345 --long prefix: \
346 --long rebuild: \
347 --long force \
348 --long kernel-only \
349 --long no-kernel \
350 --long print-cmdline \
351 --long kernel-cmdline: \
352 --long strip \
353 --long nostrip \
354 --long prelink \
355 --long noprelink \
356 --long hardlink \
357 --long nohardlink \
358 --long noprefix \
359 --long mdadmconf \
360 --long nomdadmconf \
361 --long lvmconf \
362 --long nolvmconf \
363 --long debug \
364 --long profile \
365 --long sshkey: \
366 --long logfile: \
367 --long verbose \
368 --long quiet \
369 --long local \
370 --long hostonly \
371 --long host-only \
372 --long no-hostonly \
373 --long no-host-only \
374 --long hostonly-cmdline \
375 --long no-hostonly-cmdline \
376 --long persistent-policy: \
377 --long fstab \
378 --long help \
379 --long bzip2 \
380 --long lzma \
381 --long xz \
382 --long lzo \
383 --long lz4 \
384 --long no-compress \
385 --long gzip \
386 --long list-modules \
387 --long show-modules \
388 --long keep \
389 --long printsize \
390 --long regenerate-all \
391 --long noimageifnotneeded \
392 --long early-microcode \
393 --long no-early-microcode \
394 --long reproducible \
395 --long loginstall: \
396 --long uefi \
397 --long uefi-stub: \
398 --long kernel-image: \
399 -- "$@")
401 if (( $? != 0 )); then
402 usage
403 exit 1
407 verbosity_mod_l=0
408 unset kernel
409 unset outfile
411 rearrange_params "$@"
412 eval set -- "$TEMP"
414 # parse command line args to check if '--rebuild' option is present
415 unset append_args_l
416 unset rebuild_file
417 while :
419 if [ "$1" == "--" ]; then
420 shift; break
422 if [ "$1" == "--rebuild" ]; then
423 append_args_l="yes"
424 rebuild_file=$2
425 if [ ! -e $rebuild_file ]; then
426 echo "Image file '$rebuild_file', for rebuild, does not exist!"
427 exit 1
429 abs_rebuild_file=$(readlink -f "$rebuild_file") && rebuild_file="$abs_rebuild_file"
430 shift; continue
432 shift
433 done
435 # get output file name and kernel version from command line arguments
436 while (($# > 0)); do
437 case ${1%%=*} in
438 ++include)
439 shift 2;;
441 if ! [[ ${outfile+x} ]]; then
442 outfile=$1
443 elif ! [[ ${kernel+x} ]]; then
444 kernel=$1
445 else
446 printf "\nUnknown arguments: %s\n\n" "$*" >&2
447 usage; exit 1;
450 esac
451 shift
452 done
454 # extract input image file provided with rebuild option to get previous parameters, if any
455 if [[ $append_args_l == "yes" ]]; then
456 unset rebuild_param
458 # determine resultant file
459 if ! [[ $outfile ]]; then
460 outfile=$rebuild_file
463 if ! rebuild_param=$(lsinitrd $rebuild_file '*lib/dracut/build-parameter.txt'); then
464 echo "Image '$rebuild_file' has no rebuild information stored"
465 exit 1
468 # prepend previous parameters to current command line args
469 if [[ $rebuild_param ]]; then
470 TEMP="$rebuild_param $TEMP"
471 eval set -- "$TEMP"
472 rearrange_params "$@"
475 # clean the temporarily used scratch-pad directory
476 rm -rf $scratch_dir
479 unset PARMS_TO_STORE
480 PARMS_TO_STORE=""
482 eval set -- "$TEMP"
484 while :; do
485 if [ $1 != "--" ] && [ $1 != "--rebuild" ]; then
486 PARMS_TO_STORE+=" $1";
488 case $1 in
489 --kver) kernel="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
490 -a|--add) push add_dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
491 --force-add) push force_add_dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
492 --add-drivers) push add_drivers_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
493 --force-drivers) push force_drivers_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
494 --omit-drivers) push omit_drivers_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
495 -m|--modules) push dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
496 -o|--omit) push omit_dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
497 -d|--drivers) push drivers_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
498 --filesystems) push filesystems_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
499 -I|--install) push install_items_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
500 --install-optional)
501 push install_optional_items_l \
502 "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
503 --fwdir) push fw_dir_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
504 --libdirs) push libdirs_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
505 --fscks) push fscks_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
506 --add-fstab) push add_fstab_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
507 --mount) push fstab_lines "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
508 --add-device|--device)
509 push add_device_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
510 --kernel-cmdline) push kernel_cmdline_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
511 --nofscks) nofscks_l="yes";;
512 --ro-mnt) ro_mnt_l="yes";;
513 -k|--kmoddir) drivers_dir_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
514 -c|--conf) conffile="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
515 --confdir) confdir="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
516 --tmpdir) tmpdir_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
517 -L|--stdlog) stdloglvl_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
518 --compress) compress_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
519 --prefix) prefix_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
520 --loginstall) loginstall_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
521 --rebuild) if [ $rebuild_file == $outfile ]; then
522 force=yes
524 shift
526 -f|--force) force=yes;;
527 --kernel-only) kernel_only="yes"; no_kernel="no";;
528 --no-kernel) kernel_only="no"; no_kernel="yes";;
529 --print-cmdline)
530 print_cmdline="yes"; hostonly_l="yes"; kernel_only="yes"; no_kernel="yes";;
531 --early-microcode)
532 early_microcode_l="yes";;
533 --no-early-microcode)
534 early_microcode_l="no";;
535 --strip) do_strip_l="yes";;
536 --nostrip) do_strip_l="no";;
537 --prelink) do_prelink_l="yes";;
538 --noprelink) do_prelink_l="no";;
539 --hardlink) do_hardlink_l="yes";;
540 --nohardlink) do_hardlink_l="no";;
541 --noprefix) prefix_l="/";;
542 --mdadmconf) mdadmconf_l="yes";;
543 --nomdadmconf) mdadmconf_l="no";;
544 --lvmconf) lvmconf_l="yes";;
545 --nolvmconf) lvmconf_l="no";;
546 --debug) debug="yes";;
547 --profile) profile="yes";;
548 --sshkey) sshkey="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
549 --logfile) logfile_l="$2"; shift;;
550 -v|--verbose) ((verbosity_mod_l++));;
551 -q|--quiet) ((verbosity_mod_l--));;
552 -l|--local)
553 allowlocal="yes"
554 [[ -f "$(readlink -f "${0%/*}")/dracut-functions.sh" ]] \
555 && dracutbasedir="$(readlink -f "${0%/*}")"
557 -H|--hostonly|--host-only)
558 hostonly_l="yes" ;;
559 -N|--no-hostonly|--no-host-only)
560 hostonly_l="no" ;;
561 --hostonly-cmdline)
562 hostonly_cmdline_l="yes" ;;
563 --no-hostonly-cmdline)
564 hostonly_cmdline_l="no" ;;
565 --persistent-policy)
566 persistent_policy_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
567 --fstab) use_fstab_l="yes" ;;
568 -h|--help) long_usage; exit 1 ;;
569 -i|--include) push include_src "$2"; PARMS_TO_STORE+=" '$2'";
570 shift;;
571 --bzip2) compress_l="bzip2";;
572 --lzma) compress_l="lzma";;
573 --xz) compress_l="xz";;
574 --lzo) compress_l="lzo";;
575 --lz4) compress_l="lz4";;
576 --no-compress) _no_compress_l="cat";;
577 --gzip) compress_l="gzip";;
578 --list-modules) do_list="yes";;
579 -M|--show-modules)
580 show_modules_l="yes"
582 --keep) keep="yes";;
583 --printsize) printsize="yes";;
584 --regenerate-all) regenerate_all="yes";;
585 --noimageifnotneeded) noimageifnotneeded="yes";;
586 --reproducible) reproducible_l="yes";;
587 --uefi) uefi="yes";;
588 --uefi-stub)
589 uefi_stub_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
590 --kernel-image)
591 kernel_image_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
592 --) shift; break;;
594 *) # should not even reach this point
595 printf "\n!Unknown option: '%s'\n\n" "$1" >&2; usage; exit 1;;
596 esac
597 shift
598 done
600 # getopt cannot handle multiple arguments, so just handle "-I,--include"
601 # the old fashioned way
603 while (($# > 0)); do
604 if [ "${1%%=*}" == "++include" ]; then
605 push include_src "$2"
606 push include_target "$3"
607 PARMS_TO_STORE+=" --include '$2' '$3'"
608 shift 2
610 shift
611 done
613 if [[ $regenerate_all == "yes" ]]; then
614 ret=0
615 if [[ $kernel ]]; then
616 printf -- "--regenerate-all cannot be called with a kernel version\n" >&2
617 exit 1
620 if [[ $outfile ]]; then
621 printf -- "--regenerate-all cannot be called with a image file\n" >&2
622 exit 1
625 ((len=${#dracut_args[@]}))
626 for ((i=0; i < len; i++)); do
627 [[ ${dracut_args[$i]} == "--regenerate-all" ]] && \
628 unset dracut_args[$i]
629 done
631 cd /lib/modules
632 for i in *; do
633 [[ -f $i/modules.dep ]] || [[ -f $i/modules.dep.bin ]] || continue
634 dracut --kver="$i" "${dracut_args[@]}"
635 ((ret+=$?))
636 done
637 exit $ret
640 if ! [[ $kernel ]]; then
641 kernel=$(uname -r)
644 if [[ $kernel ]]; then
645 if ! [[ -d /lib/modules/$kernel ]] && [[ $no_kernel != yes ]]; then
646 printf -- "Kernel version $kernel has no module directory /lib/modules/$kernel\n" >&2
650 if ! [[ $outfile ]]; then
651 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
653 if [[ $MACHINE_ID ]] && ( [[ -d /boot/${MACHINE_ID} ]] || [[ -L /boot/${MACHINE_ID} ]] ); then
654 outfile="/boot/${MACHINE_ID}/$kernel/initrd"
655 else
656 outfile="/boot/initramfs-$kernel.img"
660 unset LC_MESSAGES
661 unset LC_CTYPE
662 export LC_ALL=C
663 export LANG=C
664 unset LD_LIBRARY_PATH
665 unset LD_PRELOAD
666 unset GREP_OPTIONS
668 export DRACUT_LOG_LEVEL=warning
669 [[ $debug ]] && {
670 export DRACUT_LOG_LEVEL=debug
671 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
672 set -x
675 [[ $profile ]] && {
676 export PS4='+ $(date "+%s.%N") ${BASH_SOURCE}@${LINENO}: ';
677 set -x
678 debug=yes
681 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
683 # if we were not passed a config file, try the default one
684 if [[ ! -f $conffile ]]; then
685 if [[ $allowlocal ]]; then
686 conffile="$dracutbasedir/dracut.conf"
687 else
688 conffile="/etc/dracut.conf"
692 if [[ ! -d $confdir ]]; then
693 if [[ $allowlocal ]]; then
694 confdir="$dracutbasedir/dracut.conf.d"
695 else
696 confdir="/etc/dracut.conf.d"
700 # source our config file
701 [[ -f $conffile ]] && . "$conffile"
703 # source our config dir
704 for f in $(dropindirs_sort ".conf" "$confdir" "$dracutbasedir/dracut.conf.d"); do
705 [[ -e $f ]] && . "$f"
706 done
708 DRACUT_PATH=${DRACUT_PATH:-/sbin /bin /usr/sbin /usr/bin}
710 for i in $DRACUT_PATH; do
711 rl=$i
712 if [ -L "$i" ]; then
713 rl=$(readlink -f $i)
715 if [[ "$NPATH" != *:$rl* ]] ; then
716 NPATH+=":$rl"
718 done
719 export PATH="${NPATH#:}"
720 unset NPATH
722 # these optins add to the stuff in the config file
723 if (( ${#add_dracutmodules_l[@]} )); then
724 while pop add_dracutmodules_l val; do
725 add_dracutmodules+=" $val "
726 done
729 if (( ${#force_add_dracutmodules_l[@]} )); then
730 while pop force_add_dracutmodules_l val; do
731 force_add_dracutmodules+=" $val "
732 done
735 if (( ${#fscks_l[@]} )); then
736 while pop fscks_l val; do
737 fscks+=" $val "
738 done
741 if (( ${#add_fstab_l[@]} )); then
742 while pop add_fstab_l val; do
743 add_fstab+=" $val "
744 done
747 if (( ${#fstab_lines_l[@]} )); then
748 while pop fstab_lines_l val; do
749 push fstab_lines $val
750 done
753 if (( ${#install_items_l[@]} )); then
754 while pop install_items_l val; do
755 install_items+=" $val "
756 done
759 if (( ${#install_optional_items_l[@]} )); then
760 while pop install_optional_items_l val; do
761 install_optional_items+=" $val "
762 done
765 # these options override the stuff in the config file
766 if (( ${#dracutmodules_l[@]} )); then
767 dracutmodules=''
768 while pop dracutmodules_l val; do
769 dracutmodules+="$val "
770 done
773 if (( ${#omit_dracutmodules_l[@]} )); then
774 omit_dracutmodules=''
775 while pop omit_dracutmodules_l val; do
776 omit_dracutmodules+="$val "
777 done
780 if (( ${#filesystems_l[@]} )); then
781 filesystems=''
782 while pop filesystems_l val; do
783 filesystems+="$val "
784 done
787 if (( ${#fw_dir_l[@]} )); then
788 fw_dir=''
789 while pop fw_dir_l val; do
790 fw_dir+="$val "
791 done
794 if (( ${#libdirs_l[@]} )); then
795 libdirs=''
796 while pop libdirs_l val; do
797 libdirs+="$val "
798 done
801 [[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
802 [[ ! $stdloglvl ]] && stdloglvl=4
803 stdloglvl=$((stdloglvl + verbosity_mod_l))
804 ((stdloglvl > 6)) && stdloglvl=6
805 ((stdloglvl < 0)) && stdloglvl=0
807 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
808 [[ $do_strip_l ]] && do_strip=$do_strip_l
809 [[ $do_strip ]] || do_strip=yes
810 [[ $do_prelink_l ]] && do_prelink=$do_prelink_l
811 [[ $do_prelink ]] || do_prelink=yes
812 [[ $do_hardlink_l ]] && do_hardlink=$do_hardlink_l
813 [[ $do_hardlink ]] || do_hardlink=yes
814 [[ $prefix_l ]] && prefix=$prefix_l
815 [[ $prefix = "/" ]] && unset prefix
816 [[ $hostonly_l ]] && hostonly=$hostonly_l
817 [[ $hostonly_cmdline_l ]] && hostonly_cmdline=$hostonly_cmdline_l
818 [[ "$hostonly" == "yes" ]] && ! [[ $hostonly_cmdline ]] && hostonly_cmdline="yes"
819 [[ $persistent_policy_l ]] && persistent_policy=$persistent_policy_l
820 [[ $use_fstab_l ]] && use_fstab=$use_fstab_l
821 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
822 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
823 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
824 [[ $fw_dir ]] || fw_dir="/lib/firmware/updates /lib/firmware /lib/firmware/$kernel"
825 [[ $tmpdir_l ]] && tmpdir="$tmpdir_l"
826 [[ $tmpdir ]] || tmpdir=/var/tmp
827 [[ $INITRD_COMPRESS ]] && compress=$INITRD_COMPRESS
828 [[ $compress_l ]] && compress=$compress_l
829 [[ $show_modules_l ]] && show_modules=$show_modules_l
830 [[ $nofscks_l ]] && nofscks="yes"
831 [[ $ro_mnt_l ]] && ro_mnt="yes"
832 [[ $early_microcode_l ]] && early_microcode=$early_microcode_l
833 [[ $early_microcode ]] || early_microcode=no
834 [[ $logfile_l ]] && logfile="$logfile_l"
835 [[ $reproducible_l ]] && reproducible="$reproducible_l"
836 [[ $loginstall_l ]] && loginstall="$loginstall_l"
837 [[ $uefi_stub_l ]] && uefi_stub="$uefi_stub_l"
838 [[ $kernel_image_l ]] && kernel_image="$kernel_image_l"
840 # eliminate IFS hackery when messing with fw_dir
841 fw_dir=${fw_dir//:/ }
843 # check for logfile and try to create one if it doesn't exist
844 if [[ -n "$logfile" ]];then
845 if [[ ! -f "$logfile" ]];then
846 touch "$logfile"
847 if [ ! $? -eq 0 ] ;then
848 printf "%s\n" "dracut: touch $logfile failed." >&2
853 # handle compression options.
854 [[ $compress ]] || compress="gzip"
855 case $compress in
856 bzip2) compress="bzip2 -9";
857 command -v lbzip2 > /dev/null 2>&1 && compress="lbzip2 -9";;
858 lzma) compress="lzma -9 -T0";;
859 xz) compress="xz --check=crc32 --lzma2=dict=1MiB -T0";;
860 gzip) compress="gzip -n -9";
861 if command -v pigz > /dev/null 2>&1; then
862 compress="pigz -9 -n -T -R"
863 elif command -v gzip --help 2>&1 | grep -q rsyncable; then
864 compress="gzip -n -9 --rsyncable"
867 lzo) compress="lzop -9";;
868 lz4) compress="lz4 -l -9";;
869 esac
870 if [[ $_no_compress_l = "cat" ]]; then
871 compress="cat"
874 [[ $hostonly = yes ]] && hostonly="-h"
875 [[ $hostonly != "-h" ]] && unset hostonly
877 [[ $reproducible == yes ]] && DRACUT_REPRODUCIBLE=1
879 readonly TMPDIR="$tmpdir"
880 readonly initdir="$(mktemp --tmpdir="$TMPDIR/" -d -t initramfs.XXXXXX)"
881 [ -d "$initdir" ] || {
882 printf "%s\n" "dracut: mktemp --tmpdir='$TMPDIR/' -d -t initramfs.XXXXXX failed." >&2
883 exit 1
886 if [[ $early_microcode = yes ]] || ( [[ $acpi_override = yes ]] && [[ -d $acpi_table_dir ]] ); then
887 readonly early_cpio_dir="$(mktemp --tmpdir="$TMPDIR/" -d -t early_cpio.XXXXXX)"
888 [ -d "$early_cpio_dir" ] || {
889 printf "%s\n" "dracut: mktemp --tmpdir='$TMPDIR/' -d -t early_cpio.XXXXXX failed." >&2
890 exit 1
893 # clean up after ourselves no matter how we die.
894 trap '
895 ret=$?;
896 [[ $keep ]] && echo "Not removing $initdir." >&2 || { [[ $initdir ]] && rm -rf -- "$initdir"; };
897 [[ $keep ]] && echo "Not removing $early_cpio_dir." >&2 || { [[ $early_cpio_dir ]] && rm -Rf -- "$early_cpio_dir"; };
898 [[ $keep ]] && echo "Not removing $uefi_outdir." >&2 || { [[ $uefi_outdir ]] && rm -Rf -- "$uefi_outdir"; };
899 [[ $_dlogdir ]] && rm -Rf -- "$_dlogdir";
900 exit $ret;
901 ' EXIT
903 # clean up after ourselves no matter how we die.
904 trap 'exit 1;' SIGINT
906 export DRACUT_KERNEL_LAZY="1"
907 export DRACUT_RESOLVE_LAZY="1"
909 if [[ $print_cmdline ]]; then
910 stdloglvl=0
911 sysloglvl=0
912 fileloglvl=0
913 kmsgloglvl=0
916 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
917 . $dracutbasedir/dracut-version.sh
920 if [[ -f $dracutbasedir/dracut-functions.sh ]]; then
921 . $dracutbasedir/dracut-functions.sh
922 else
923 printf "%s\n" "dracut: Cannot find $dracutbasedir/dracut-functions.sh." >&2
924 printf "%s\n" "dracut: Are you running from a git checkout?" >&2
925 printf "%s\n" "dracut: Try passing -l as an argument to $0" >&2
926 exit 1
929 if ! [[ $print_cmdline ]]; then
930 inst /bin/sh
931 if ! $DRACUT_INSTALL ${initdir:+-D "$initdir"} -R "$initdir/bin/sh" &>/dev/null; then
932 unset DRACUT_RESOLVE_LAZY
933 export DRACUT_RESOLVE_DEPS=1
935 rm -fr -- ${initdir}/*
938 # Verify bash version, current minimum is 4
939 if (( BASH_VERSINFO[0] < 4 )); then
940 dfatal 'You need at least Bash 4 to use dracut, sorry.'
941 exit 1
944 dracutfunctions=$dracutbasedir/dracut-functions.sh
945 export dracutfunctions
947 if (( ${#drivers_l[@]} )); then
948 drivers=''
949 while pop drivers_l val; do
950 drivers+="$val "
951 done
953 drivers=${drivers/-/_}
955 if (( ${#add_drivers_l[@]} )); then
956 while pop add_drivers_l val; do
957 add_drivers+=" $val "
958 done
960 add_drivers=${add_drivers/-/_}
962 if (( ${#force_drivers_l[@]} )); then
963 while pop force_drivers_l val; do
964 force_drivers+=" $val "
965 done
967 force_drivers=${force_drivers/-/_}
969 if (( ${#omit_drivers_l[@]} )); then
970 while pop omit_drivers_l val; do
971 omit_drivers+=" $val "
972 done
974 omit_drivers=${omit_drivers/-/_}
976 if (( ${#kernel_cmdline_l[@]} )); then
977 while pop kernel_cmdline_l val; do
978 kernel_cmdline+=" $val "
979 done
982 omit_drivers_corrected=""
983 for d in $omit_drivers; do
984 [[ " $drivers $add_drivers " == *\ $d\ * ]] && continue
985 [[ " $drivers $force_drivers " == *\ $d\ * ]] && continue
986 omit_drivers_corrected+="$d|"
987 done
988 omit_drivers="${omit_drivers_corrected%|}"
989 unset omit_drivers_corrected
991 # prepare args for logging
992 for ((i=0; i < ${#dracut_args[@]}; i++)); do
993 [[ "${dracut_args[$i]}" == *\ * ]] && \
994 dracut_args[$i]="\"${dracut_args[$i]}\""
995 #" keep vim happy
996 done
998 dinfo "Executing: $0 ${dracut_args[@]}"
1000 [[ $do_list = yes ]] && {
1001 for mod in $dracutbasedir/modules.d/*; do
1002 [[ -d $mod ]] || continue;
1003 [[ -e $mod/install || -e $mod/installkernel || \
1004 -e $mod/module-setup.sh ]] || continue
1005 printf "%s\n" "${mod##*/??}"
1006 done
1007 exit 0
1010 # This is kinda legacy -- eventually it should go away.
1011 case $dracutmodules in
1012 ""|auto) dracutmodules="all" ;;
1013 esac
1015 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
1017 if [[ -d $srcmods ]]; then
1018 [[ -f $srcmods/modules.dep ]] || {
1019 dwarn "$srcmods/modules.dep is missing. Did you run depmod?"
1023 if [[ ! $print_cmdline ]]; then
1024 if [[ -f $outfile && ! $force ]]; then
1025 dfatal "Will not override existing initramfs ($outfile) without --force"
1026 exit 1
1029 outdir=${outfile%/*}
1030 [[ $outdir ]] || outdir="/"
1032 if [[ ! -d "$outdir" ]]; then
1033 dfatal "Can't write to $outdir: Directory $outdir does not exist or is not accessible."
1034 exit 1
1035 elif [[ ! -w "$outdir" ]]; then
1036 dfatal "No permission to write to $outdir."
1037 exit 1
1038 elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
1039 dfatal "No permission to write $outfile."
1040 exit 1
1043 if [[ $loginstall ]]; then
1044 if ! mkdir -p "$loginstall"; then
1045 dfatal "Could not create directory to log installed files to '$loginstall'."
1046 exit 1
1048 loginstall=$(readlink -f "$loginstall")
1051 if [[ $uefi = yes ]]; then
1052 if ! command -v objcopy &>/dev/null; then
1053 dfatal "Need 'objcopy' to create a UEFI executable"
1054 exit 1
1056 unset EFI_MACHINE_TYPE_NAME
1057 case $(arch) in
1058 x86_64)
1059 EFI_MACHINE_TYPE_NAME=x64;;
1060 ia32)
1061 EFI_MACHINE_TYPE_NAME=ia32;;
1063 dfatal "Architecture '$(arch)' not supported to create a UEFI executable"
1064 exit 1
1066 esac
1068 if ! [[ -s $uefi_stub ]]; then
1069 for uefi_stub in \
1070 "/lib/systemd/boot/efi/linux${EFI_MACHINE_TYPE_NAME}.efi.stub" \
1071 "/usr/lib/gummiboot/linux${EFI_MACHINE_TYPE_NAME}.efi.stub"; do
1072 [[ -s $uefi_stub ]] || continue
1073 break
1074 done
1076 if ! [[ -s $uefi_stub ]]; then
1077 dfatal "Can't find a uefi stub '$uefi_stub' to create a UEFI executable"
1078 exit 1
1081 if ! [[ $kernel_image ]]; then
1082 for kernel_image in "/lib/modules/$kernel/vmlinuz" "/boot/vmlinuz-$kernel"; do
1083 [[ -s "$kernel_image" ]] || continue
1084 break
1085 done
1087 if ! [[ -s $kernel_image ]]; then
1088 dfatal "Can't find a kernel image '$kernel_image' to create a UEFI executable"
1089 exit 1
1094 if [[ $acpi_override = yes ]] && ! check_kernel_config CONFIG_ACPI_INITRD_TABLE_OVERRIDE; then
1095 dwarn "Disabling ACPI override, because kernel does not support it. CONFIG_ACPI_INITRD_TABLE_OVERRIDE!=y"
1096 unset acpi_override
1099 if [[ $early_microcode = yes ]]; then
1100 if [[ $hostonly ]]; then
1101 [[ $(get_cpu_vendor) == "AMD" ]] \
1102 && ! check_kernel_config CONFIG_MICROCODE_AMD_EARLY \
1103 && unset early_microcode
1104 [[ $(get_cpu_vendor) == "Intel" ]] \
1105 && ! check_kernel_config CONFIG_MICROCODE_INTEL_EARLY \
1106 && unset early_microcode
1107 else
1108 ! check_kernel_config CONFIG_MICROCODE_AMD_EARLY \
1109 && ! check_kernel_config CONFIG_MICROCODE_INTEL_EARLY \
1110 && unset early_microcode
1112 [[ $early_microcode != yes ]] \
1113 && dwarn "Disabling early microcode, because kernel does not support it. CONFIG_MICROCODE_[AMD|INTEL]_EARLY!=y"
1116 # Need to be able to have non-root users read stuff (rpcbind etc)
1117 chmod 755 "$initdir"
1119 if [[ $hostonly ]]; then
1120 for i in /sys /proc /run /dev; do
1121 if ! findmnt --target "$i" &>/dev/null; then
1122 dwarning "Turning off host-only mode: '$i' is not mounted!"
1123 unset hostonly
1125 done
1128 declare -A host_fs_types
1130 for line in "${fstab_lines[@]}"; do
1131 set -- $line
1132 dev="$1"
1133 #dev mp fs fsopts
1134 case "$dev" in
1135 UUID=*)
1136 dev=$(blkid -l -t UUID=${dev#UUID=} -o device)
1138 LABEL=*)
1139 dev=$(blkid -l -t LABEL=${dev#LABEL=} -o device)
1141 PARTUUID=*)
1142 dev=$(blkid -l -t PARTUUID=${dev#PARTUUID=} -o device)
1144 PARTLABEL=*)
1145 dev=$(blkid -l -t PARTLABEL=${dev#PARTLABEL=} -o device)
1147 esac
1148 [ -z "$dev" ] && dwarn "Bad fstab entry $@" && continue
1149 push_host_devs "$dev"
1150 host_fs_types["$dev"]="$3"
1151 done
1153 for f in $add_fstab; do
1154 [[ -e $f ]] || continue
1155 while read dev rest || [ -n "$dev" ]; do
1156 push_host_devs "$dev"
1157 done < "$f"
1158 done
1160 for dev in $add_device; do
1161 push_host_devs "$dev"
1162 done
1164 if (( ${#add_device_l[@]} )); then
1165 while pop add_device_l val; do
1166 add_device+=" $val "
1167 push_host_devs "$val"
1168 done
1171 if [[ $hostonly ]]; then
1172 # in hostonly mode, determine all devices, which have to be accessed
1173 # and examine them for filesystem types
1175 for mp in \
1176 "/" \
1177 "/etc" \
1178 "/bin" \
1179 "/sbin" \
1180 "/lib" \
1181 "/lib64" \
1182 "/usr" \
1183 "/usr/bin" \
1184 "/usr/sbin" \
1185 "/usr/lib" \
1186 "/usr/lib64" \
1187 "/boot" \
1188 "/boot/efi" \
1191 mp=$(readlink -f "$mp")
1192 mountpoint "$mp" >/dev/null 2>&1 || continue
1193 _dev=$(find_block_device "$mp")
1194 _bdev=$(readlink -f "/dev/block/$_dev")
1195 [[ -b $_bdev ]] && _dev=$_bdev
1196 push_host_devs $_dev
1197 [[ "$mp" == "/" ]] && root_dev="$_dev"
1198 push_host_devs "$_dev"
1199 done
1201 if [[ -f /proc/swaps ]] && [[ -f /etc/fstab ]]; then
1202 while read dev type rest || [ -n "$dev" ]; do
1203 [[ -b $dev ]] || continue
1204 [[ "$type" == "partition" ]] || continue
1206 while read _d _m _t _o _r || [ -n "$_d" ]; do
1207 [[ "$_d" == \#* ]] && continue
1208 [[ $_d ]] || continue
1209 [[ $_t != "swap" ]] && continue
1210 [[ $_m != "swap" ]] && [[ $_m != "none" ]] && continue
1211 [[ "$_o" == *noauto* ]] && continue
1212 _d=$(expand_persistent_dev "$_d")
1213 [[ "$_d" -ef "$dev" ]] || continue
1215 if [[ -f /etc/crypttab ]]; then
1216 while read _mapper _a _p _o || [ -n "$_mapper" ]; do
1217 [[ $_mapper = \#* ]] && continue
1218 [[ "$_d" -ef /dev/mapper/"$_mapper" ]] || continue
1219 [[ "$_o" ]] || _o="$_p"
1220 # skip entries with password files
1221 [[ "$_p" == /* ]] && [[ -f $_p ]] && continue 2
1222 # skip mkswap swap
1223 [[ $_o == *swap* ]] && continue 2
1224 done < /etc/crypttab
1227 _dev="$(readlink -f "$dev")"
1228 push_host_devs "$_dev"
1229 push swap_devs "$_dev"
1230 break
1231 done < /etc/fstab
1232 done < /proc/swaps
1234 # record all host modaliases
1235 declare -A host_modalias
1236 find /sys/devices/ -name uevent -print > "$initdir/.modalias"
1237 while read m || [ -n "$m" ]; do
1238 while read line || [ -n "$line" ]; do
1239 [[ "$line" != MODALIAS\=* ]] && continue
1240 modalias="${line##MODALIAS=}" && [[ $modalias ]] && host_modalias["$modalias"]=1
1241 break
1242 done < "$m"
1243 done < "$initdir/.modalias"
1245 rm -f -- "$initdir/.modalias"
1247 while read _k _s _v || [ -n "$_k" ]; do
1248 [ "$_k" != "name" -a "$_k" != "driver" ] && continue
1249 host_modalias["$_v"]=1
1250 done </proc/crypto
1252 # check /proc/modules
1253 declare -A host_modules
1254 while read m rest || [ -n "$m" ]; do
1255 host_modules["$m"]=1
1256 done </proc/modules
1259 unset m
1260 unset rest
1262 _get_fs_type() {
1263 [[ $1 ]] || return
1264 if [[ -b /dev/block/$1 ]]; then
1265 ID_FS_TYPE=$(get_fs_env "/dev/block/$1") && host_fs_types["$(readlink -f "/dev/block/$1")"]="$ID_FS_TYPE"
1266 return 1
1268 if [[ -b $1 ]]; then
1269 ID_FS_TYPE=$(get_fs_env "$1") && host_fs_types["$(readlink -f "$1")"]="$ID_FS_TYPE"
1270 return 1
1272 if fstype=$(find_dev_fstype "$1"); then
1273 host_fs_types["$1"]="$fstype"
1274 return 1
1276 return 1
1279 for dev in ${host_devs[@]}; do
1280 _get_fs_type "$dev"
1281 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
1282 done
1284 for dev in "${!host_fs_types[@]}"; do
1285 [[ ${host_fs_types[$dev]} = "reiserfs" ]] || [[ ${host_fs_types[$dev]} = "xfs" ]] || continue
1286 rootopts=$(find_dev_fsopts "$dev")
1287 if [[ ${host_fs_types[$dev]} = "reiserfs" ]]; then
1288 journaldev=$(fs_get_option $rootopts "jdev")
1289 elif [[ ${host_fs_types[$dev]} = "xfs" ]]; then
1290 journaldev=$(fs_get_option $rootopts "logdev")
1292 if [[ $journaldev ]]; then
1293 dev="$(readlink -f "$dev")"
1294 push_host_devs "$dev"
1295 _get_fs_type "$dev"
1296 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
1298 done
1300 [[ -d $udevdir ]] \
1301 || udevdir="$(pkg-config udev --variable=udevdir 2>/dev/null)"
1302 if ! [[ -d "$udevdir" ]]; then
1303 [[ -d /lib/udev ]] && udevdir=/lib/udev
1304 [[ -d /usr/lib/udev ]] && udevdir=/usr/lib/udev
1307 [[ -d $systemdutildir ]] \
1308 || systemdutildir=$(pkg-config systemd --variable=systemdutildir 2>/dev/null)
1310 if ! [[ -d "$systemdutildir" ]]; then
1311 [[ -d /lib/systemd ]] && systemdutildir=/lib/systemd
1312 [[ -d /usr/lib/systemd ]] && systemdutildir=/usr/lib/systemd
1315 [[ -d $systemdsystemunitdir ]] \
1316 || systemdsystemunitdir=$(pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null)
1318 [[ -d "$systemdsystemunitdir" ]] || systemdsystemunitdir=${systemdutildir}/system
1320 [[ -d $systemdsystemconfdir ]] \
1321 || systemdsystemconfdir=$(pkg-config systemd --variable=systemdsystemconfdir 2>/dev/null)
1323 [[ -d "$systemdsystemconfdir" ]] || systemdsystemconfdir=/etc/systemd/system
1325 [[ -d $tmpfilesdir ]] \
1326 || tmpfilesdir=$(pkg-config systemd --variable=tmpfilesdir 2>/dev/null)
1328 if ! [[ -d "$tmpfilesdir" ]]; then
1329 [[ -f /lib/tmpfiles.d ]] && tmpfilesdir=/lib/tmpfiles.d
1330 [[ -f /usr/lib/tmpfiles.d ]] && tmpfilesdir=/usr/lib/tmpfiles.d
1333 export initdir dracutbasedir \
1334 dracutmodules force_add_dracutmodules add_dracutmodules omit_dracutmodules \
1335 mods_to_load \
1336 fw_dir drivers_dir debug no_kernel kernel_only \
1337 omit_drivers mdadmconf lvmconf root_dev \
1338 use_fstab fstab_lines libdirs fscks nofscks ro_mnt \
1339 stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
1340 debug host_fs_types host_devs swap_devs sshkey add_fstab \
1341 DRACUT_VERSION udevdir prefix filesystems drivers \
1342 systemdutildir systemdsystemunitdir systemdsystemconfdir \
1343 host_modalias host_modules hostonly_cmdline loginstall \
1344 tmpfilesdir
1346 mods_to_load=""
1347 # check all our modules to see if they should be sourced.
1348 # This builds a list of modules that we will install next.
1349 for_each_module_dir check_module
1350 for_each_module_dir check_mount
1352 dracut_module_included "fips" && export DRACUT_FIPS_MODE=1
1354 do_print_cmdline()
1356 local -A _mods_to_print
1357 for i in $modules_loaded $mods_to_load; do
1358 _mods_to_print[$i]=1
1359 done
1361 # source our modules.
1362 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1363 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
1364 [[ ${_mods_to_print[$_d_mod]} ]] || continue
1365 module_cmdline "$_d_mod"
1366 done
1367 unset moddir
1370 if [[ $print_cmdline ]]; then
1371 do_print_cmdline
1372 printf "\n"
1373 exit 0
1376 # Create some directory structure first
1377 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
1379 [[ -h /lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
1380 [[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
1382 if [[ $prefix ]]; then
1383 for d in bin etc lib sbin tmp usr var $libdirs; do
1384 [[ "$d" == */* ]] && continue
1385 ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
1386 done
1389 if [[ $kernel_only != yes ]]; then
1390 for d in usr/bin usr/sbin bin etc lib sbin tmp usr var var/tmp $libdirs; do
1391 [[ -e "${initdir}${prefix}/$d" ]] && continue
1392 if [ -L "/$d" ]; then
1393 inst_symlink "/$d" "${prefix}/$d"
1394 else
1395 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1397 done
1399 for d in dev proc sys sysroot root run; do
1400 if [ -L "/$d" ]; then
1401 inst_symlink "/$d"
1402 else
1403 mkdir -m 0755 -p "$initdir/$d"
1405 done
1407 ln -sfn ../run "$initdir/var/run"
1408 ln -sfn ../run/lock "$initdir/var/lock"
1409 else
1410 for d in lib "$libdir"; do
1411 [[ -e "${initdir}${prefix}/$d" ]] && continue
1412 if [ -h "/$d" ]; then
1413 inst "/$d" "${prefix}/$d"
1414 else
1415 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1417 done
1420 if [[ $kernel_only != yes ]]; then
1421 mkdir -p "${initdir}/etc/cmdline.d"
1422 for _d in $hookdirs; do
1423 mkdir -m 0755 -p ${initdir}/lib/dracut/hooks/$_d
1424 done
1425 if [[ "$UID" = "0" ]]; then
1426 [ -c ${initdir}/dev/null ] || mknod ${initdir}/dev/null c 1 3
1427 [ -c ${initdir}/dev/kmsg ] || mknod ${initdir}/dev/kmsg c 1 11
1428 [ -c ${initdir}/dev/console ] || mknod ${initdir}/dev/console c 5 1
1432 _isize=0 #initramfs size
1433 modules_loaded=" "
1434 # source our modules.
1435 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1436 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
1437 [[ "$mods_to_load" == *\ $_d_mod\ * ]] || continue
1438 if [[ $show_modules = yes ]]; then
1439 printf "%s\n" "$_d_mod"
1440 else
1441 dinfo "*** Including module: $_d_mod ***"
1443 if [[ $kernel_only == yes ]]; then
1444 module_installkernel "$_d_mod" || {
1445 dfatal "installkernel failed in module $_d_mod"
1446 exit 1
1448 else
1449 module_install "$_d_mod"
1450 if [[ $no_kernel != yes ]]; then
1451 module_installkernel "$_d_mod" || {
1452 dfatal "installkernel failed in module $_d_mod"
1453 exit 1
1457 mods_to_load=${mods_to_load// $_d_mod /}
1458 modules_loaded+="$_d_mod "
1460 #print the module install size
1461 if [ -n "$printsize" ]; then
1462 _isize_new=$(du -sk ${initdir}|cut -f1)
1463 _isize_delta=$((_isize_new - _isize))
1464 printf "%s\n" "$_d_mod install size: ${_isize_delta}k"
1465 _isize=$_isize_new
1467 done
1468 unset moddir
1470 for i in $modules_loaded; do
1471 mkdir -p $initdir/lib/dracut
1472 printf "%s\n" "$i" >> $initdir/lib/dracut/modules.txt
1473 done
1475 dinfo "*** Including modules done ***"
1477 ## final stuff that has to happen
1478 if [[ $no_kernel != yes ]]; then
1480 if [[ $drivers ]]; then
1481 hostonly='' instmods $drivers
1484 if [[ $add_drivers ]]; then
1485 hostonly='' instmods -c $add_drivers
1487 if [[ $force_drivers ]]; then
1488 hostonly='' instmods -c $force_drivers
1489 rm -f $initdir/etc/cmdline.d/20-force_driver.conf
1490 for mod in $force_drivers; do
1491 echo "rd.driver.pre=$mod" >>$initdir/etc/cmdline.d/20-force_drivers.conf
1492 done
1494 if [[ $filesystems ]]; then
1495 hostonly='' instmods -c $filesystems
1498 dinfo "*** Installing kernel module dependencies and firmware ***"
1499 dracut_kernel_post
1500 dinfo "*** Installing kernel module dependencies and firmware done ***"
1502 if [[ $noimageifnotneeded == yes ]] && [[ $hostonly ]]; then
1503 if [[ ! -f "$initdir/lib/dracut/need-initqueue" ]] && \
1504 [[ -f ${initdir}/lib/modules/$kernel/modules.dep && ! -s ${initdir}/lib/modules/$kernel/modules.dep ]]; then
1505 for i in ${initdir}/etc/cmdline.d/*.conf; do
1506 # We need no initramfs image and do not generate one.
1507 [[ $i == "${initdir}/etc/cmdline.d/*.conf" ]] && exit 0
1508 done
1513 if [[ $kernel_only != yes ]]; then
1514 (( ${#install_items[@]} > 0 )) && inst_multiple ${install_items[@]}
1515 (( ${#install_optional_items[@]} > 0 )) && inst_multiple -o ${install_optional_items[@]}
1517 [[ $kernel_cmdline ]] && printf "%s\n" "$kernel_cmdline" >> "${initdir}/etc/cmdline.d/01-default.conf"
1519 while pop fstab_lines line; do
1520 line=($line)
1521 [ -z "${line[3]}" ] && line[3]="defaults"
1522 [ -z "${line[4]}" ] && line[4]="0"
1523 [ -z "${line[5]}" ] && line[5]="2"
1524 strstr "${line[2]}" "nfs" && line[5]="0"
1525 echo "${line[@]}" >> "${initdir}/etc/fstab"
1526 done
1528 for f in $add_fstab; do
1529 cat "$f" >> "${initdir}/etc/fstab"
1530 done
1532 if [ -d ${initdir}/$systemdutildir ]; then
1533 mkdir -p ${initdir}/etc/conf.d
1535 printf "%s\n" "systemdutildir=\"$systemdutildir\""
1536 printf "%s\n" "systemdsystemunitdir=\"$systemdsystemunitdir\""
1537 printf "%s\n" "systemdsystemconfdir=\"$systemdsystemconfdir\""
1538 } > ${initdir}/etc/conf.d/systemd.conf
1541 if [[ $DRACUT_RESOLVE_LAZY ]] && [[ $DRACUT_INSTALL ]]; then
1542 dinfo "*** Resolving executable dependencies ***"
1543 find "$initdir" -type f -perm /0111 -not -path '*.ko' -print0 \
1544 | xargs -r -0 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -R ${DRACUT_FIPS_MODE:+-H} --
1545 dinfo "*** Resolving executable dependencies done***"
1548 # libpthread workaround: pthread_cancel wants to dlopen libgcc_s.so
1549 for _dir in $libdirs; do
1550 for _f in "$_dir/libpthread.so"*; do
1551 [[ -e "$_f" ]] || continue
1552 inst_libdir_file "libgcc_s.so*"
1553 break 2
1554 done
1555 done
1558 while pop include_src src && pop include_target target; do
1559 if [[ $src && $target ]]; then
1560 if [[ -f $src ]]; then
1561 inst $src $target
1562 else
1563 ddebug "Including directory: $src"
1564 destdir="${initdir}/${target}"
1565 mkdir -p "$destdir"
1566 # check for preexisting symlinks, so we can cope with the
1567 # symlinks to $prefix
1568 # Objectname is a file or a directory
1569 for objectname in "$src"/*; do
1570 [[ -e "$objectname" || -h "$objectname" ]] || continue
1571 if [[ -d "$objectname" ]]; then
1572 # objectname is a directory, let's compute the final directory name
1573 object_destdir=${destdir}/${objectname#$src/}
1574 if ! [[ -e "$object_destdir" ]]; then
1575 mkdir -m 0755 -p "$object_destdir"
1576 chmod --reference="$objectname" "$object_destdir"
1578 cp --reflink=auto --sparse=auto -fa -t "$object_destdir" "$objectname"/*
1579 else
1580 cp --reflink=auto --sparse=auto -fa -t "$destdir" "$objectname"
1582 done
1585 done
1587 if [[ $kernel_only != yes ]]; then
1588 # make sure that library links are correct and up to date
1589 for f in /etc/ld.so.conf /etc/ld.so.conf.d/*; do
1590 [[ -f $f ]] && inst_simple "$f"
1591 done
1592 if ! ldconfig -r "$initdir"; then
1593 if [[ $UID = 0 ]]; then
1594 derror "ldconfig exited ungracefully"
1595 else
1596 derror "ldconfig might need uid=0 (root) for chroot()"
1601 PRELINK_BIN="$(command -v prelink)"
1602 if [[ $UID = 0 ]] && [[ $PRELINK_BIN ]]; then
1603 if [[ $DRACUT_FIPS_MODE ]]; then
1604 dinfo "*** Installing prelink files ***"
1605 inst_multiple -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf /etc/prelink.cache
1606 elif [[ $do_prelink == yes ]]; then
1607 dinfo "*** Pre-linking files ***"
1608 inst_multiple -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf
1609 chroot "$initdir" "$PRELINK_BIN" -a
1610 rm -f -- "$initdir/$PRELINK_BIN"
1611 rm -fr -- "$initdir"/etc/prelink.*
1612 dinfo "*** Pre-linking files done ***"
1616 if [[ $do_hardlink = yes ]] && command -v hardlink >/dev/null; then
1617 dinfo "*** Hardlinking files ***"
1618 hardlink "$initdir" 2>&1
1619 dinfo "*** Hardlinking files done ***"
1622 # strip binaries
1623 if [[ $do_strip = yes ]] ; then
1624 for p in strip xargs find; do
1625 if ! type -P $p >/dev/null; then
1626 dinfo "Could not find '$p'. Not stripping the initramfs."
1627 do_strip=no
1629 done
1632 if [[ $do_strip = yes ]] && ! [[ $DRACUT_FIPS_MODE ]]; then
1633 dinfo "*** Stripping files ***"
1634 find "$initdir" -type f \
1635 -executable -not -path '*/lib/modules/*.ko' -print0 \
1636 | xargs -r -0 strip -g 2>/dev/null
1638 # strip kernel modules, but do not touch signed modules
1639 find "$initdir" -type f -path '*/lib/modules/*.ko' -print0 \
1640 | while read -r -d $'\0' f || [ -n "$f" ]; do
1641 SIG=$(tail -c 28 "$f")
1642 [[ $SIG == '~Module signature appended~' ]] || { printf "%s\000" "$f"; }
1643 done | xargs -r -0 strip -g
1645 dinfo "*** Stripping files done ***"
1647 if [[ $early_microcode = yes ]]; then
1648 dinfo "*** Generating early-microcode cpio image ***"
1649 ucode_dir=(amd-ucode intel-ucode)
1650 ucode_dest=(AuthenticAMD.bin GenuineIntel.bin)
1651 _dest_dir="$early_cpio_dir/d/kernel/x86/microcode"
1652 _dest_idx="0 1"
1653 mkdir -p $_dest_dir
1654 if [[ $hostonly ]]; then
1655 [[ $(get_cpu_vendor) == "AMD" ]] && _dest_idx="0"
1656 [[ $(get_cpu_vendor) == "Intel" ]] && _dest_idx="1"
1658 for idx in $_dest_idx; do
1659 _fw=${ucode_dir[$idx]}
1660 for _fwdir in $fw_dir; do
1661 if [[ -d $_fwdir && -d $_fwdir/$_fw ]]; then
1662 _src="*"
1663 dinfo "*** Constructing ${ucode_dest[$idx]} ****"
1664 if [[ $hostonly ]]; then
1665 _src=$(get_ucode_file)
1666 if ! [[ -r $_fwdir/$_fw/$_src ]];then
1667 break;
1670 cat $_fwdir/$_fw/$_src > $_dest_dir/${ucode_dest[$idx]}
1671 create_early_cpio="yes"
1673 done
1674 done
1677 if [[ $acpi_override = yes ]] && [[ -d $acpi_table_dir ]]; then
1678 dinfo "*** Packaging ACPI tables to override BIOS provided ones ***"
1679 _dest_dir="$early_cpio_dir/d/kernel/firmware/acpi"
1680 mkdir -p $_dest_dir
1681 for table in $acpi_table_dir/*.aml; do
1682 dinfo " Adding ACPI table: $table"
1683 cp -a $table $_dest_dir
1684 create_early_cpio="yes"
1685 done
1688 dinfo "*** Store current command line parameters ***"
1689 if ! ( echo $PARMS_TO_STORE > $initdir/lib/dracut/build-parameter.txt ); then
1690 dfatal "Could not store the current command line parameters"
1691 exit 1
1694 if [[ $hostonly_cmdline ]] ; then
1695 unset _stored_cmdline
1696 if [ -d $initdir/etc/cmdline.d ];then
1697 dinfo "Stored kernel commandline:"
1698 for conf in $initdir/etc/cmdline.d/*.conf ; do
1699 [ -e "$conf" ] || continue
1700 dinfo "$(< $conf)"
1701 _stored_cmdline=1
1702 done
1704 if ! [[ $_stored_cmdline ]]; then
1705 dinfo "No dracut internal kernel commandline stored in initrd"
1708 rm -f -- "$outfile"
1709 dinfo "*** Creating image file '$outfile' ***"
1711 if [[ $uefi = yes ]]; then
1712 uefi_outfile="$outfile"
1713 readonly uefi_outdir="$(mktemp --tmpdir="$TMPDIR/" -d -t initrd.XXXXXX)"
1714 # redirect initrd output
1715 outfile="$uefi_outdir/initrd"
1718 if [[ $DRACUT_REPRODUCIBLE ]]; then
1719 find "$initdir" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
1720 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
1722 [[ "$(cpio --help)" == *--reproducible* ]] && CPIO_REPRODUCIBLE=1
1725 [[ "$UID" != 0 ]] && cpio_owner_root="-R 0:0"
1727 if [[ $create_early_cpio = yes ]]; then
1728 echo 1 > "$early_cpio_dir/d/early_cpio"
1730 if [[ $DRACUT_REPRODUCIBLE ]]; then
1731 find "$early_cpio_dir/d" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
1732 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
1735 # The microcode blob is _before_ the initramfs blob, not after
1737 cd "$early_cpio_dir/d"
1738 find . -print0 | sort -z \
1739 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null $cpio_owner_root -H newc -o --quiet > $outfile
1743 if ! (
1744 umask 077; cd "$initdir"
1745 find . -print0 | sort -z \
1746 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null $cpio_owner_root -H newc -o --quiet \
1747 | $compress >> "$outfile"
1748 ); then
1749 dfatal "dracut: creation of $outfile failed"
1750 exit 1
1752 dinfo "*** Creating initrd image file '$outfile' done ***"
1754 if (( maxloglvl >= 5 )); then
1755 if [[ $allowlocal ]]; then
1756 "$dracutbasedir/lsinitrd.sh" "$outfile"| ddebug
1757 else
1758 lsinitrd "$outfile"| ddebug
1762 if [[ $uefi = yes ]]; then
1763 if [[ $kernel_cmdline ]]; then
1764 echo -n "$kernel_cmdline" > "$uefi_outdir/cmdline.txt"
1765 elif [[ $hostonly_cmdline = yes ]] && [ -d $initdir/etc/cmdline.d ];then
1766 for conf in $initdir/etc/cmdline.d/*.conf ; do
1767 [ -e "$conf" ] || continue
1768 printf "%s " "$(< $conf)" >> "$uefi_outdir/cmdline.txt"
1769 done
1770 else
1771 do_print_cmdline > "$uefi_outdir/cmdline.txt"
1773 echo -ne "\x00" >> "$uefi_outdir/cmdline.txt"
1775 dinfo "Using UEFI kernel cmdline:"
1776 dinfo $(< "$uefi_outdir/cmdline.txt")
1778 [[ -s /usr/lib/os-release ]] && uefi_osrelease="/usr/lib/os-release"
1779 [[ -s /etc/os-release ]] && uefi_osrelease="/etc/os-release"
1781 objcopy \
1782 ${uefi_osrelease:+--add-section .osrel=$uefi_osrelease --change-section-vma .osrel=0x20000} \
1783 --add-section .cmdline="$uefi_outdir/cmdline.txt" --change-section-vma .cmdline=0x30000 \
1784 --add-section .linux="$kernel_image" --change-section-vma .linux=0x40000 \
1785 --add-section .initrd="$outfile" --change-section-vma .initrd=0x3000000 \
1786 "$uefi_stub" "$uefi_outfile"
1787 dinfo "*** Creating UEFI image file '$uefi_outfile' done ***"
1790 exit 0