]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut.sh
Generate reproducible initramfs images
[thirdparty/dracut.git] / dracut.sh
1 #!/bin/bash
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4 #
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
8 #
9
10 # Copyright 2005-2013 Red Hat, Inc. All rights reserved.
11 #
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.
16 #
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.
21 #
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/>.
24 #
25
26 # store for logging
27 dracut_args=( "$@" )
28
29 set -o pipefail
30
31 usage() {
32 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
33 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
34 . $dracutbasedir/dracut-version.sh
35 fi
36
37 # 80x25 linebreak here ^
38 cat << EOF
39 Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
40
41 Version: $DRACUT_VERSION
42
43 Creates initial ramdisk images for preloading modules
44
45 -h, --help Display all options
46
47 If a [LIST] has multiple arguments, then you have to put these in quotes.
48
49 For example:
50
51 # dracut --add-drivers "module1 module2" ...
52
53 EOF
54 }
55
56 long_usage() {
57 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
58 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
59 . $dracutbasedir/dracut-version.sh
60 fi
61
62 # 80x25 linebreak here ^
63 cat << EOF
64 Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
65
66 Version: $DRACUT_VERSION
67
68 Creates initial ramdisk images for preloading modules
69
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 --rebuild Append arguments to those of existing image and rebuild
74 -m, --modules [LIST] Specify a space-separated list of dracut modules to
75 call when building the initramfs. Modules are located
76 in /usr/lib/dracut/modules.d.
77 -o, --omit [LIST] Omit a space-separated list of dracut modules.
78 --force-add [LIST] Force to add a space-separated list of dracut modules
79 to the default set of modules, when -H is specified.
80 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
81 exclusively include in the initramfs.
82 --add-drivers [LIST] Specify a space-separated list of kernel
83 modules to add to the initramfs.
84 --omit-drivers [LIST] Specify a space-separated list of kernel
85 modules not to add to the initramfs.
86 --filesystems [LIST] Specify a space-separated list of kernel filesystem
87 modules to exclusively include in the generic
88 initramfs.
89 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
90 modules
91 --fwdir [DIR] Specify additional directories, where to look for
92 firmwares, separated by :
93 --kernel-only Only install kernel drivers and firmware files
94 --no-kernel Do not install kernel drivers and firmware files
95 --print-cmdline Print the kernel command line for the given disk layout
96 --early-microcode Combine early microcode with ramdisk
97 --no-early-microcode Do not combine early microcode with ramdisk
98 --kernel-cmdline [PARAMETERS] Specify default kernel command line parameters
99 --strip Strip binaries in the initramfs
100 --nostrip Do not strip binaries in the initramfs
101 --prelink Prelink binaries in the initramfs
102 --noprelink Do not prelink binaries in the initramfs
103 --hardlink Hardlink files in the initramfs
104 --nohardlink Do not hardlink files in the initramfs
105 --prefix [DIR] Prefix initramfs files with [DIR]
106 --noprefix Do not prefix initramfs files
107 --mdadmconf Include local /etc/mdadm.conf
108 --nomdadmconf Do not include local /etc/mdadm.conf
109 --lvmconf Include local /etc/lvm/lvm.conf
110 --nolvmconf Do not include local /etc/lvm/lvm.conf
111 --fscks [LIST] Add a space-separated list of fsck helpers.
112 --nofscks Inhibit installation of any fsck helpers.
113 --ro-mnt Mount / and /usr read-only by default.
114 -h, --help This message
115 --debug Output debug information of the build process
116 --profile Output profile information of the build process
117 -L, --stdlog [0-6] Specify logging level (to standard error)
118 0 - suppress any messages
119 1 - only fatal errors
120 2 - all errors
121 3 - warnings
122 4 - info
123 5 - debug info (here starts lots of output)
124 6 - trace info (and even more)
125 -v, --verbose Increase verbosity level
126 -q, --quiet Decrease verbosity level
127 -c, --conf [FILE] Specify configuration file to use.
128 Default: /etc/dracut.conf
129 --confdir [DIR] Specify configuration directory to use *.conf files
130 from. Default: /etc/dracut.conf.d
131 --tmpdir [DIR] Temporary directory to be used instead of default
132 /var/tmp.
133 -l, --local Local mode. Use modules from the current working
134 directory instead of the system-wide installed in
135 /usr/lib/dracut/modules.d.
136 Useful when running dracut from a git checkout.
137 -H, --hostonly Host-Only mode: Install only what is needed for
138 booting the local host instead of a generic host.
139 -N, --no-hostonly Disables Host-Only mode
140 --hostonly-cmdline Store kernel command line arguments needed
141 in the initramfs
142 --no-hostonly-cmdline Do not store kernel command line arguments needed
143 in the initramfs
144 --persistent-policy [POLICY]
145 Use [POLICY] to address disks and partitions.
146 POLICY can be any directory name found in /dev/disk.
147 E.g. "by-uuid", "by-label"
148 --fstab Use /etc/fstab to determine the root device.
149 --add-fstab [FILE] Add file to the initramfs fstab
150 --mount "[DEV] [MP] [FSTYPE] [FSOPTS]"
151 Mount device [DEV] on mountpoint [MP] with filesystem
152 [FSTYPE] and options [FSOPTS] in the initramfs
153 --add-device "[DEV]" Bring up [DEV] in initramfs
154 -i, --include [SOURCE] [TARGET]
155 Include the files in the SOURCE directory into the
156 Target directory in the final initramfs.
157 If SOURCE is a file, it will be installed to TARGET
158 in the final initramfs.
159 -I, --install [LIST] Install the space separated list of files into the
160 initramfs.
161 --install-optional [LIST] Install the space separated list of files into the
162 initramfs, if they exist.
163 --gzip Compress the generated initramfs using gzip.
164 This will be done by default, unless another
165 compression option or --no-compress is passed.
166 --bzip2 Compress the generated initramfs using bzip2.
167 Make sure your kernel has bzip2 decompression support
168 compiled in, otherwise you will not be able to boot.
169 --lzma Compress the generated initramfs using lzma.
170 Make sure your kernel has lzma support compiled in,
171 otherwise you will not be able to boot.
172 --xz Compress the generated initramfs using xz.
173 Make sure that your kernel has xz support compiled
174 in, otherwise you will not be able to boot.
175 --lzo Compress the generated initramfs using lzop.
176 Make sure that your kernel has lzo support compiled
177 in, otherwise you will not be able to boot.
178 --lz4 Compress the generated initramfs using lz4.
179 Make sure that your kernel has lz4 support compiled
180 in, otherwise you will not be able to boot.
181 --compress [COMPRESSION] Compress the generated initramfs with the
182 passed compression program. Make sure your kernel
183 knows how to decompress the generated initramfs,
184 otherwise you will not be able to boot.
185 --no-compress Do not compress the generated initramfs. This will
186 override any other compression options.
187 --list-modules List all available dracut modules.
188 -M, --show-modules Print included module's name to standard output during
189 build.
190 --keep Keep the temporary initramfs for debugging purposes
191 --printsize Print out the module install size
192 --sshkey [SSHKEY] Add ssh key to initramfs (use with ssh-client module)
193 --logfile [FILE] Logfile to use (overrides configuration setting)
194 --reproducible Create reproducible images
195
196 If [LIST] has multiple arguments, then you have to put these in quotes.
197
198 For example:
199
200 # dracut --add-drivers "module1 module2" ...
201
202 EOF
203 }
204
205 # function push()
206 # push values to a stack
207 # $1 = stack variable
208 # $2.. values
209 # example:
210 # push stack 1 2 "3 4"
211 push() {
212 local _i
213 local __stack=$1; shift
214 for _i in "$@"; do
215 eval ${__stack}'[${#'${__stack}'[@]}]="$_i"'
216 done
217 }
218
219 # function pop()
220 # pops the last value from a stack
221 # assigns value to second argument variable
222 # or echo to stdout, if no second argument
223 # $1 = stack variable
224 # $2 = optional variable to store the value
225 # example:
226 # pop stack val
227 # val=$(pop stack)
228 pop() {
229 local __stack=$1; shift
230 local __resultvar=$1
231 local _value;
232 # check for empty stack
233 eval '[[ ${#'${__stack}'[@]} -eq 0 ]] && return 1'
234
235 eval _value='${'${__stack}'[${#'${__stack}'[@]}-1]}'
236
237 if [[ "$__resultvar" ]]; then
238 eval $__resultvar="'$_value'"
239 else
240 printf "%s" "$_value"
241 fi
242 eval unset ${__stack}'[${#'${__stack}'[@]}-1]'
243 return 0
244 }
245
246 # Little helper function for reading args from the commandline.
247 # it automatically handles -a b and -a=b variants, and returns 1 if
248 # we need to shift $3.
249 read_arg() {
250 # $1 = arg name
251 # $2 = arg value
252 # $3 = arg parameter
253 local rematch='^[^=]*=(.*)$'
254 if [[ $2 =~ $rematch ]]; then
255 read "$1" <<< "${BASH_REMATCH[1]}"
256 else
257 read "$1" <<< "$3"
258 # There is no way to shift our callers args, so
259 # return 1 to indicate they should do it instead.
260 return 1
261 fi
262 }
263
264 dropindirs_sort()
265 {
266 local suffix=$1; shift
267 local -a files
268 local f d
269
270 for d in "$@"; do
271 for i in "$d/"*"$suffix"; do
272 if [[ -e "$i" ]]; then
273 printf "%s\n" "${i##*/}"
274 fi
275 done
276 done | sort -Vu | {
277 readarray -t files
278
279 for f in "${files[@]}"; do
280 for d in "$@"; do
281 if [[ -e "$d/$f" ]]; then
282 printf "%s\n" "$d/$f"
283 continue 2
284 fi
285 done
286 done
287 }
288 }
289
290 rearrange_params()
291 {
292 # Workaround -i, --include taking 2 arguments
293 set -- "${@/--include/++include}"
294
295 # This prevents any long argument ending with "-i"
296 # -i, like --opt-i but I think we can just prevent that
297 set -- "${@/%-i/++include}"
298
299 TEMP=$(unset POSIXLY_CORRECT; getopt \
300 -o "a:m:o:d:I:k:c:L:fvqlHhMN" \
301 --long kver: \
302 --long add: \
303 --long force-add: \
304 --long add-drivers: \
305 --long omit-drivers: \
306 --long modules: \
307 --long omit: \
308 --long drivers: \
309 --long filesystems: \
310 --long install: \
311 --long install-optional: \
312 --long fwdir: \
313 --long libdirs: \
314 --long fscks: \
315 --long add-fstab: \
316 --long mount: \
317 --long device: \
318 --long add-device: \
319 --long nofscks: \
320 --long ro-mnt \
321 --long kmoddir: \
322 --long conf: \
323 --long confdir: \
324 --long tmpdir: \
325 --long stdlog: \
326 --long compress: \
327 --long prefix: \
328 --long rebuild: \
329 --long force \
330 --long kernel-only \
331 --long no-kernel \
332 --long print-cmdline \
333 --long kernel-cmdline: \
334 --long strip \
335 --long nostrip \
336 --long prelink \
337 --long noprelink \
338 --long hardlink \
339 --long nohardlink \
340 --long noprefix \
341 --long mdadmconf \
342 --long nomdadmconf \
343 --long lvmconf \
344 --long nolvmconf \
345 --long debug \
346 --long profile \
347 --long sshkey: \
348 --long logfile: \
349 --long verbose \
350 --long quiet \
351 --long local \
352 --long hostonly \
353 --long host-only \
354 --long no-hostonly \
355 --long no-host-only \
356 --long hostonly-cmdline \
357 --long no-hostonly-cmdline \
358 --long persistent-policy: \
359 --long fstab \
360 --long help \
361 --long bzip2 \
362 --long lzma \
363 --long xz \
364 --long lzo \
365 --long lz4 \
366 --long no-compress \
367 --long gzip \
368 --long list-modules \
369 --long show-modules \
370 --long keep \
371 --long printsize \
372 --long regenerate-all \
373 --long noimageifnotneeded \
374 --long early-microcode \
375 --long no-early-microcode \
376 --long reproducible \
377 -- "$@")
378
379 if (( $? != 0 )); then
380 usage
381 exit 1
382 fi
383 }
384
385 verbosity_mod_l=0
386 unset kernel
387 unset outfile
388
389 rearrange_params "$@"
390 eval set -- "$TEMP"
391
392 # parse command line args to check if '--rebuild' option is present
393 unset append_args_l
394 unset rebuild_file
395 while :
396 do
397 if [ "$1" == "--" ]; then
398 shift; break
399 fi
400 if [ "$1" == "--rebuild" ]; then
401 append_args_l="yes"
402 rebuild_file=$2
403 if [ ! -e $rebuild_file ]; then
404 echo "Image file '$rebuild_file', for rebuild, does not exist!"
405 exit 1
406 fi
407 abs_rebuild_file=$(readlink -f "$rebuild_file") && rebuild_file="$abs_rebuild_file"
408 shift; continue
409 fi
410 shift
411 done
412
413 # get output file name and kernel version from command line arguments
414 while (($# > 0)); do
415 case ${1%%=*} in
416 ++include)
417 shift 2;;
418 *)
419 if ! [[ ${outfile+x} ]]; then
420 outfile=$1
421 elif ! [[ ${kernel+x} ]]; then
422 kernel=$1
423 else
424 printf "\nUnknown arguments: %s\n\n" "$*" >&2
425 usage; exit 1;
426 fi
427 ;;
428 esac
429 shift
430 done
431
432 # extract input image file provided with rebuild option to get previous parameters, if any
433 if [[ $append_args_l == "yes" ]]; then
434 unset rebuild_param
435
436 # determine resultant file
437 if ! [[ $outfile ]]; then
438 outfile=$rebuild_file
439 fi
440
441 if ! rebuild_param=$(lsinitrd $rebuild_file '*lib/dracut/build-parameter.txt'); then
442 echo "Image '$rebuild_file' has no rebuild information stored"
443 exit 1
444 fi
445
446 # prepend previous parameters to current command line args
447 if [[ $rebuild_param ]]; then
448 TEMP="$rebuild_param $TEMP"
449 eval set -- "$TEMP"
450 rearrange_params "$@"
451 fi
452
453 # clean the temporarily used scratch-pad directory
454 rm -rf $scratch_dir
455 fi
456
457 unset PARMS_TO_STORE
458 PARMS_TO_STORE=""
459
460 eval set -- "$TEMP"
461
462 while :; do
463 if [ $1 != "--" ] && [ $1 != "--rebuild" ]; then
464 PARMS_TO_STORE+=" $1";
465 fi
466 case $1 in
467 --kver) kernel="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
468 -a|--add) push add_dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
469 --force-add) push force_add_dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
470 --add-drivers) push add_drivers_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
471 --omit-drivers) push omit_drivers_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
472 -m|--modules) push dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
473 -o|--omit) push omit_dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
474 -d|--drivers) push drivers_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
475 --filesystems) push filesystems_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
476 -I|--install) push install_items_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
477 --install-optional)
478 push install_optional_items_l \
479 "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
480 --fwdir) push fw_dir_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
481 --libdirs) push libdirs_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
482 --fscks) push fscks_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
483 --add-fstab) push add_fstab_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
484 --mount) push fstab_lines "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
485 --add-device|--device)
486 push add_device_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
487 --kernel-cmdline) push kernel_cmdline_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
488 --nofscks) nofscks_l="yes";;
489 --ro-mnt) ro_mnt_l="yes";;
490 -k|--kmoddir) drivers_dir_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
491 -c|--conf) conffile="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
492 --confdir) confdir="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
493 --tmpdir) tmpdir_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
494 -L|--stdlog) stdloglvl_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
495 --compress) compress_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
496 --prefix) prefix_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
497 --rebuild) if [ $rebuild_file == $outfile ]; then
498 force=yes
499 fi
500 shift
501 ;;
502 -f|--force) force=yes;;
503 --kernel-only) kernel_only="yes"; no_kernel="no";;
504 --no-kernel) kernel_only="no"; no_kernel="yes";;
505 --print-cmdline)
506 print_cmdline="yes"; hostonly_l="yes"; kernel_only="yes"; no_kernel="yes";;
507 --early-microcode)
508 early_microcode_l="yes";;
509 --no-early-microcode)
510 early_microcode_l="no";;
511 --strip) do_strip_l="yes";;
512 --nostrip) do_strip_l="no";;
513 --prelink) do_prelink_l="yes";;
514 --noprelink) do_prelink_l="no";;
515 --hardlink) do_hardlink_l="yes";;
516 --nohardlink) do_hardlink_l="no";;
517 --noprefix) prefix_l="/";;
518 --mdadmconf) mdadmconf_l="yes";;
519 --nomdadmconf) mdadmconf_l="no";;
520 --lvmconf) lvmconf_l="yes";;
521 --nolvmconf) lvmconf_l="no";;
522 --debug) debug="yes";;
523 --profile) profile="yes";;
524 --sshkey) sshkey="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
525 --logfile) logfile_l="$2"; shift;;
526 -v|--verbose) ((verbosity_mod_l++));;
527 -q|--quiet) ((verbosity_mod_l--));;
528 -l|--local)
529 allowlocal="yes"
530 [[ -f "$(readlink -f "${0%/*}")/dracut-functions.sh" ]] \
531 && dracutbasedir="$(readlink -f "${0%/*}")"
532 ;;
533 -H|--hostonly|--host-only)
534 hostonly_l="yes" ;;
535 -N|--no-hostonly|--no-host-only)
536 hostonly_l="no" ;;
537 --hostonly-cmdline)
538 hostonly_cmdline_l="yes" ;;
539 --no-hostonly-cmdline)
540 hostonly_cmdline_l="no" ;;
541 --persistent-policy)
542 persistent_policy_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
543 --fstab) use_fstab_l="yes" ;;
544 -h|--help) long_usage; exit 1 ;;
545 -i|--include) push include_src "$2"; PARMS_TO_STORE+=" '$2'";
546 shift;;
547 --bzip2) compress_l="bzip2";;
548 --lzma) compress_l="lzma";;
549 --xz) compress_l="xz";;
550 --lzo) compress_l="lzo";;
551 --lz4) compress_l="lz4";;
552 --no-compress) _no_compress_l="cat";;
553 --gzip) compress_l="gzip";;
554 --list-modules) do_list="yes";;
555 -M|--show-modules)
556 show_modules_l="yes"
557 ;;
558 --keep) keep="yes";;
559 --printsize) printsize="yes";;
560 --regenerate-all) regenerate_all="yes";;
561 --noimageifnotneeded) noimageifnotneeded="yes";;
562 --reproducible) reproducible_l="yes";;
563 --) shift; break;;
564
565 *) # should not even reach this point
566 printf "\n!Unknown option: '%s'\n\n" "$1" >&2; usage; exit 1;;
567 esac
568 shift
569 done
570
571 # getopt cannot handle multiple arguments, so just handle "-I,--include"
572 # the old fashioned way
573
574 while (($# > 0)); do
575 if [ "${1%%=*}" == "++include" ]; then
576 push include_src "$2"
577 push include_target "$3"
578 PARMS_TO_STORE+=" --include '$2' '$3'"
579 shift 2
580 fi
581 shift
582 done
583
584 if [[ $regenerate_all == "yes" ]]; then
585 ret=0
586 if [[ $kernel ]]; then
587 printf -- "--regenerate-all cannot be called with a kernel version\n" >&2
588 exit 1
589 fi
590
591 if [[ $outfile ]]; then
592 printf -- "--regenerate-all cannot be called with a image file\n" >&2
593 exit 1
594 fi
595
596 ((len=${#dracut_args[@]}))
597 for ((i=0; i < len; i++)); do
598 [[ ${dracut_args[$i]} == "--regenerate-all" ]] && \
599 unset dracut_args[$i]
600 done
601
602 cd /lib/modules
603 for i in *; do
604 [[ -f $i/modules.dep ]] || [[ -f $i/modules.dep.bin ]] || continue
605 dracut --kver="$i" "${dracut_args[@]}"
606 ((ret+=$?))
607 done
608 exit $ret
609 fi
610
611 if ! [[ $kernel ]]; then
612 kernel=$(uname -r)
613 fi
614
615 if [[ $kernel ]]; then
616 if ! [[ -d /lib/modules/$kernel ]] && [[ $no_kernel != yes ]]; then
617 printf -- "Kernel version $kernel has no module directory /lib/modules/$kernel\n" >&2
618 fi
619 fi
620
621 if ! [[ $outfile ]]; then
622 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
623
624 if [[ $MACHINE_ID ]] && ( [[ -d /boot/${MACHINE_ID} ]] || [[ -L /boot/${MACHINE_ID} ]] ); then
625 outfile="/boot/${MACHINE_ID}/$kernel/initrd"
626 else
627 outfile="/boot/initramfs-$kernel.img"
628 fi
629 fi
630
631 unset LC_MESSAGES
632 unset LC_CTYPE
633 export LC_ALL=C
634 export LANG=C
635 unset LD_LIBRARY_PATH
636 unset LD_PRELOAD
637 unset GREP_OPTIONS
638
639 export DRACUT_LOG_LEVEL=warning
640 [[ $debug ]] && {
641 export DRACUT_LOG_LEVEL=debug
642 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
643 set -x
644 }
645
646 [[ $profile ]] && {
647 export PS4='+ $(date "+%s.%N") ${BASH_SOURCE}@${LINENO}: ';
648 set -x
649 debug=yes
650 }
651
652 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
653
654 # if we were not passed a config file, try the default one
655 if [[ ! -f $conffile ]]; then
656 if [[ $allowlocal ]]; then
657 conffile="$dracutbasedir/dracut.conf"
658 else
659 conffile="/etc/dracut.conf"
660 fi
661 fi
662
663 if [[ ! -d $confdir ]]; then
664 if [[ $allowlocal ]]; then
665 confdir="$dracutbasedir/dracut.conf.d"
666 else
667 confdir="/etc/dracut.conf.d"
668 fi
669 fi
670
671 # source our config file
672 [[ -f $conffile ]] && . "$conffile"
673
674 # source our config dir
675 for f in $(dropindirs_sort ".conf" "$confdir" "$dracutbasedir/dracut.conf.d"); do
676 [[ -e $f ]] && . "$f"
677 done
678
679 DRACUT_PATH=${DRACUT_PATH:-/usr/sbin /sbin /usr/bin /bin}
680
681 for i in $DRACUT_PATH; do
682 rl=$i
683 if [ -L "$i" ]; then
684 rl=$(readlink -f $i)
685 fi
686 if [[ "$NPATH" != *:$rl* ]] ; then
687 NPATH+=":$rl"
688 fi
689 done
690 export PATH="${NPATH#:}"
691 unset NPATH
692
693 # these optins add to the stuff in the config file
694 if (( ${#add_dracutmodules_l[@]} )); then
695 while pop add_dracutmodules_l val; do
696 add_dracutmodules+=" $val "
697 done
698 fi
699
700 if (( ${#force_add_dracutmodules_l[@]} )); then
701 while pop force_add_dracutmodules_l val; do
702 force_add_dracutmodules+=" $val "
703 done
704 fi
705
706 if (( ${#fscks_l[@]} )); then
707 while pop fscks_l val; do
708 fscks+=" $val "
709 done
710 fi
711
712 if (( ${#add_fstab_l[@]} )); then
713 while pop add_fstab_l val; do
714 add_fstab+=" $val "
715 done
716 fi
717
718 if (( ${#fstab_lines_l[@]} )); then
719 while pop fstab_lines_l val; do
720 push fstab_lines $val
721 done
722 fi
723
724 if (( ${#install_items_l[@]} )); then
725 while pop install_items_l val; do
726 install_items+=" $val "
727 done
728 fi
729
730 if (( ${#install_optional_items_l[@]} )); then
731 while pop install_optional_items_l val; do
732 install_optional_items+=" $val "
733 done
734 fi
735
736 # these options override the stuff in the config file
737 if (( ${#dracutmodules_l[@]} )); then
738 dracutmodules=''
739 while pop dracutmodules_l val; do
740 dracutmodules+="$val "
741 done
742 fi
743
744 if (( ${#omit_dracutmodules_l[@]} )); then
745 omit_dracutmodules=''
746 while pop omit_dracutmodules_l val; do
747 omit_dracutmodules+="$val "
748 done
749 fi
750
751 if (( ${#filesystems_l[@]} )); then
752 filesystems=''
753 while pop filesystems_l val; do
754 filesystems+="$val "
755 done
756 fi
757
758 if (( ${#fw_dir_l[@]} )); then
759 fw_dir=''
760 while pop fw_dir_l val; do
761 fw_dir+="$val "
762 done
763 fi
764
765 if (( ${#libdirs_l[@]} )); then
766 libdirs=''
767 while pop libdirs_l val; do
768 libdirs+="$val "
769 done
770 fi
771
772 [[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
773 [[ ! $stdloglvl ]] && stdloglvl=4
774 stdloglvl=$((stdloglvl + verbosity_mod_l))
775 ((stdloglvl > 6)) && stdloglvl=6
776 ((stdloglvl < 0)) && stdloglvl=0
777
778 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
779 [[ $do_strip_l ]] && do_strip=$do_strip_l
780 [[ $do_strip ]] || do_strip=yes
781 [[ $do_prelink_l ]] && do_prelink=$do_prelink_l
782 [[ $do_prelink ]] || do_prelink=yes
783 [[ $do_hardlink_l ]] && do_hardlink=$do_hardlink_l
784 [[ $do_hardlink ]] || do_hardlink=yes
785 [[ $prefix_l ]] && prefix=$prefix_l
786 [[ $prefix = "/" ]] && unset prefix
787 [[ $hostonly_l ]] && hostonly=$hostonly_l
788 [[ $hostonly_cmdline_l ]] && hostonly_cmdline=$hostonly_cmdline_l
789 [[ "$hostonly" == "yes" ]] && ! [[ $hostonly_cmdline ]] && hostonly_cmdline="yes"
790 [[ $persistent_policy_l ]] && persistent_policy=$persistent_policy_l
791 [[ $use_fstab_l ]] && use_fstab=$use_fstab_l
792 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
793 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
794 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
795 [[ $fw_dir ]] || fw_dir="/lib/firmware/updates /lib/firmware"
796 [[ $tmpdir_l ]] && tmpdir="$tmpdir_l"
797 [[ $tmpdir ]] || tmpdir=/var/tmp
798 [[ $INITRD_COMPRESS ]] && compress=$INITRD_COMPRESS
799 [[ $compress_l ]] && compress=$compress_l
800 [[ $show_modules_l ]] && show_modules=$show_modules_l
801 [[ $nofscks_l ]] && nofscks="yes"
802 [[ $ro_mnt_l ]] && ro_mnt="yes"
803 [[ $early_microcode_l ]] && early_microcode=$early_microcode_l
804 [[ $early_microcode ]] || early_microcode=no
805 [[ $logfile_l ]] && logfile="$logfile_l"
806 [[ $reproducible_l ]] && reproducible="$reproducible_l"
807
808 # eliminate IFS hackery when messing with fw_dir
809 fw_dir=${fw_dir//:/ }
810
811 # handle compression options.
812 [[ $compress ]] || compress="gzip"
813 case $compress in
814 bzip2) compress="bzip2 -9";;
815 lzma) compress="lzma -9 -T0";;
816 xz) compress="xz --check=crc32 --lzma2=dict=1MiB -T0";;
817 gzip) compress="gzip -n -9 --rsyncable"; command -v pigz > /dev/null 2>&1 && compress="pigz -9 -n -T -R";;
818 lzo) compress="lzop -9";;
819 lz4) compress="lz4 -l -9";;
820 esac
821 if [[ $_no_compress_l = "cat" ]]; then
822 compress="cat"
823 fi
824
825 [[ $hostonly = yes ]] && hostonly="-h"
826 [[ $hostonly != "-h" ]] && unset hostonly
827
828 [[ $reproducible == yes ]] && DRACUT_REPRODUCIBLE=1
829
830 readonly TMPDIR="$tmpdir"
831 readonly initdir="$(mktemp --tmpdir="$TMPDIR/" -d -t initramfs.XXXXXX)"
832 [ -d "$initdir" ] || {
833 printf "%s\n" "dracut: mktemp --tmpdir='$TMPDIR/' -d -t initramfs.XXXXXX failed." >&2
834 exit 1
835 }
836
837 if [[ $early_microcode = yes ]] || ( [[ $acpi_override = yes ]] && [[ -d $acpi_table_dir ]] ); then
838 readonly early_cpio_dir="$(mktemp --tmpdir="$TMPDIR/" -d -t early_cpio.XXXXXX)"
839 [ -d "$early_cpio_dir" ] || {
840 printf "%s\n" "dracut: mktemp --tmpdir='$TMPDIR/' -d -t early_cpio.XXXXXX failed." >&2
841 exit 1
842 }
843 fi
844 # clean up after ourselves no matter how we die.
845 trap '
846 ret=$?;
847 [[ $keep ]] && echo "Not removing $initdir." >&2 || { [[ $initdir ]] && rm -rf -- "$initdir"; };
848 [[ $keep ]] && echo "Not removing $early_cpio_dir." >&2 || { [[ $early_cpio_dir ]] && rm -Rf -- "$early_cpio_dir"; };
849 [[ $_dlogdir ]] && rm -Rf -- "$_dlogdir";
850 exit $ret;
851 ' EXIT
852
853 # clean up after ourselves no matter how we die.
854 trap 'exit 1;' SIGINT
855
856 export DRACUT_KERNEL_LAZY="1"
857 export DRACUT_RESOLVE_LAZY="1"
858
859 if [[ $print_cmdline ]]; then
860 stdloglvl=0
861 sysloglvl=0
862 fileloglvl=0
863 kmsgloglvl=0
864 fi
865
866 if [[ -f $dracutbasedir/dracut-functions.sh ]]; then
867 . $dracutbasedir/dracut-functions.sh
868 else
869 printf "%s\n" "dracut: Cannot find $dracutbasedir/dracut-functions.sh." >&2
870 printf "%s\n" "dracut: Are you running from a git checkout?" >&2
871 printf "%s\n" "dracut: Try passing -l as an argument to $0" >&2
872 exit 1
873 fi
874
875 if ! [[ $print_cmdline ]]; then
876 inst /bin/sh
877 if ! $DRACUT_INSTALL ${initdir:+-D "$initdir"} -R "$initdir/bin/sh" &>/dev/null; then
878 unset DRACUT_RESOLVE_LAZY
879 export DRACUT_RESOLVE_DEPS=1
880 fi
881 rm -fr -- ${initdir}/*
882 fi
883
884 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
885 . $dracutbasedir/dracut-version.sh
886 fi
887
888 # Verify bash version, current minimum is 3.1
889 if (( BASH_VERSINFO[0] < 4 )); then
890 dfatal 'You need at least Bash 4 to use dracut, sorry.'
891 exit 1
892 fi
893
894 dracutfunctions=$dracutbasedir/dracut-functions.sh
895 export dracutfunctions
896
897 if (( ${#drivers_l[@]} )); then
898 drivers=''
899 while pop drivers_l val; do
900 drivers+="$val "
901 done
902 fi
903 drivers=${drivers/-/_}
904
905 if (( ${#add_drivers_l[@]} )); then
906 while pop add_drivers_l val; do
907 add_drivers+=" $val "
908 done
909 fi
910 add_drivers=${add_drivers/-/_}
911
912 if (( ${#omit_drivers_l[@]} )); then
913 while pop omit_drivers_l val; do
914 omit_drivers+=" $val "
915 done
916 fi
917 omit_drivers=${omit_drivers/-/_}
918
919 if (( ${#kernel_cmdline_l[@]} )); then
920 while pop kernel_cmdline_l val; do
921 kernel_cmdline+=" $val "
922 done
923 fi
924
925 omit_drivers_corrected=""
926 for d in $omit_drivers; do
927 [[ " $drivers $add_drivers " == *\ $d\ * ]] && continue
928 omit_drivers_corrected+="$d|"
929 done
930 omit_drivers="${omit_drivers_corrected%|}"
931 unset omit_drivers_corrected
932
933 # prepare args for logging
934 for ((i=0; i < ${#dracut_args[@]}; i++)); do
935 [[ "${dracut_args[$i]}" == *\ * ]] && \
936 dracut_args[$i]="\"${dracut_args[$i]}\""
937 #" keep vim happy
938 done
939
940 dinfo "Executing: $0 ${dracut_args[@]}"
941
942 [[ $do_list = yes ]] && {
943 for mod in $dracutbasedir/modules.d/*; do
944 [[ -d $mod ]] || continue;
945 [[ -e $mod/install || -e $mod/installkernel || \
946 -e $mod/module-setup.sh ]] || continue
947 printf "%s\n" "${mod##*/??}"
948 done
949 exit 0
950 }
951
952 # This is kinda legacy -- eventually it should go away.
953 case $dracutmodules in
954 ""|auto) dracutmodules="all" ;;
955 esac
956
957 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
958
959 if [[ -d $srcmods ]]; then
960 [[ -f $srcmods/modules.dep ]] || {
961 dwarn "$srcmods/modules.dep is missing. Did you run depmod?"
962 }
963 fi
964
965 if [[ ! $print_cmdline ]]; then
966 if [[ -f $outfile && ! $force ]]; then
967 dfatal "Will not override existing initramfs ($outfile) without --force"
968 exit 1
969 fi
970
971 outdir=${outfile%/*}
972 [[ $outdir ]] || outdir="/"
973
974 if [[ ! -d "$outdir" ]]; then
975 dfatal "Can't write to $outdir: Directory $outdir does not exist or is not accessible."
976 exit 1
977 elif [[ ! -w "$outdir" ]]; then
978 dfatal "No permission to write to $outdir."
979 exit 1
980 elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
981 dfatal "No permission to write $outfile."
982 exit 1
983 fi
984 fi
985
986 # Need to be able to have non-root users read stuff (rpcbind etc)
987 chmod 755 "$initdir"
988
989 if [[ $hostonly ]]; then
990 for i in /sys /proc /run /dev; do
991 if ! findmnt --target "$i" &>/dev/null; then
992 dwarning "Turning off host-only mode: '$i' is not mounted!"
993 unset hostonly
994 fi
995 done
996 fi
997
998 declare -A host_fs_types
999
1000 for line in "${fstab_lines[@]}"; do
1001 set -- $line
1002 #dev mp fs fsopts
1003 push host_devs "$1"
1004 host_fs_types["$1"]="$3"
1005 done
1006
1007 for f in $add_fstab; do
1008 [[ -e $f ]] || continue
1009 while read dev rest; do
1010 push host_devs "$dev"
1011 done < "$f"
1012 done
1013
1014 for dev in $add_device; do
1015 push host_devs "$dev"
1016 done
1017
1018 if (( ${#add_device_l[@]} )); then
1019 while pop add_device_l val; do
1020 add_device+=" $val "
1021 push host_devs "$val"
1022 done
1023 fi
1024
1025 if [[ $hostonly ]]; then
1026 # in hostonly mode, determine all devices, which have to be accessed
1027 # and examine them for filesystem types
1028
1029 for mp in \
1030 "/" \
1031 "/etc" \
1032 "/bin" \
1033 "/sbin" \
1034 "/lib" \
1035 "/lib64" \
1036 "/usr" \
1037 "/usr/bin" \
1038 "/usr/sbin" \
1039 "/usr/lib" \
1040 "/usr/lib64" \
1041 "/boot" \
1042 "/boot/efi" \
1043 ;
1044 do
1045 mp=$(readlink -f "$mp")
1046 mountpoint "$mp" >/dev/null 2>&1 || continue
1047 _dev=$(find_block_device "$mp")
1048 _bdev=$(readlink -f "/dev/block/$_dev")
1049 [[ -b $_bdev ]] && _dev=$_bdev
1050 push host_devs $_dev
1051 [[ "$mp" == "/" ]] && root_dev="$_dev"
1052 push host_devs "$_dev"
1053 done
1054
1055 if [[ -f /proc/swaps ]] && [[ -f /etc/fstab ]]; then
1056 while read dev type rest; do
1057 [[ -b $dev ]] || continue
1058 [[ "$type" == "partition" ]] || continue
1059
1060 while read _d _m _t _o _r; do
1061 [[ "$_d" == \#* ]] && continue
1062 [[ $_d ]] || continue
1063 [[ $_t != "swap" ]] && continue
1064 [[ $_m != "swap" ]] && [[ $_m != "none" ]] && continue
1065 [[ "$_o" == *noauto* ]] && continue
1066 _d=$(expand_persistent_dev "$_d")
1067 [[ "$_d" -ef "$dev" ]] || continue
1068
1069 if [[ -f /etc/crypttab ]]; then
1070 while read _mapper _a _p _o; do
1071 [[ $_mapper = \#* ]] && continue
1072 [[ "$_d" -ef /dev/mapper/"$_mapper" ]] || continue
1073 [[ "$_o" ]] || _o="$_p"
1074 # skip entries with password files
1075 [[ "$_p" == /* ]] && [[ -f $_p ]] && continue 2
1076 # skip mkswap swap
1077 [[ $_o == *swap* ]] && continue 2
1078 done < /etc/crypttab
1079 fi
1080
1081 push host_devs "$(readlink -f "$dev")"
1082 break
1083 done < /etc/fstab
1084 done < /proc/swaps
1085 fi
1086 # record all host modaliases
1087 declare -A host_modalias
1088 find /sys/devices/ -name uevent -print > "$initdir/.modalias"
1089 while read m; do
1090 while read line; do
1091 [[ "$line" != MODALIAS\=* ]] && continue
1092 modalias="${line##MODALIAS=}" && [[ $modalias ]] && host_modalias["$modalias"]=1
1093 break
1094 done < "$m"
1095 done < "$initdir/.modalias"
1096
1097 rm -f -- "$initdir/.modalias"
1098
1099 while read _k _s _v; do
1100 [ "$_k" != "name" -a "$_k" != "driver" ] && continue
1101 host_modalias["$_v"]=1
1102 done </proc/crypto
1103
1104 # check /proc/modules
1105 declare -A host_modules
1106 while read m rest; do
1107 host_modules["$m"]=1
1108 done </proc/modules
1109 fi
1110
1111 unset m
1112 unset rest
1113
1114 _get_fs_type() {
1115 [[ $1 ]] || return
1116 if [[ -b /dev/block/$1 ]]; then
1117 ID_FS_TYPE=$(get_fs_env "/dev/block/$1") && host_fs_types["$(readlink -f "/dev/block/$1")"]="$ID_FS_TYPE"
1118 return 1
1119 fi
1120 if [[ -b $1 ]]; then
1121 ID_FS_TYPE=$(get_fs_env "$1") && host_fs_types["$(readlink -f "$1")"]="$ID_FS_TYPE"
1122 return 1
1123 fi
1124 if fstype=$(find_dev_fstype "$1"); then
1125 host_fs_types["$1"]="$fstype"
1126 return 1
1127 fi
1128 return 1
1129 }
1130
1131 for dev in ${host_devs[@]}; do
1132 _get_fs_type "$dev"
1133 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
1134 done
1135
1136 for dev in "${!host_fs_types[@]}"; do
1137 [[ ${host_fs_types[$dev]} = "reiserfs" ]] || [[ ${host_fs_types[$dev]} = "xfs" ]] || continue
1138 rootopts=$(find_dev_fsopts "$dev")
1139 if [[ ${host_fs_types[$dev]} = "reiserfs" ]]; then
1140 journaldev=$(fs_get_option $rootopts "jdev")
1141 elif [[ ${host_fs_types[$dev]} = "xfs" ]]; then
1142 journaldev=$(fs_get_option $rootopts "logdev")
1143 fi
1144 if [[ $journaldev ]]; then
1145 dev="$(readlink -f "$dev")"
1146 push host_devs "$dev"
1147 _get_fs_type "$dev"
1148 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
1149 fi
1150 done
1151
1152 [[ -d $udevdir ]] \
1153 || udevdir="$(pkg-config udev --variable=udevdir 2>/dev/null)"
1154 if ! [[ -d "$udevdir" ]]; then
1155 [[ -d /lib/udev ]] && udevdir=/lib/udev
1156 [[ -d /usr/lib/udev ]] && udevdir=/usr/lib/udev
1157 fi
1158
1159 [[ -d $systemdutildir ]] \
1160 || systemdutildir=$(pkg-config systemd --variable=systemdutildir 2>/dev/null)
1161
1162 if ! [[ -d "$systemdutildir" ]]; then
1163 [[ -d /lib/systemd ]] && systemdutildir=/lib/systemd
1164 [[ -d /usr/lib/systemd ]] && systemdutildir=/usr/lib/systemd
1165 fi
1166
1167 [[ -d $systemdsystemunitdir ]] \
1168 || systemdsystemunitdir=$(pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null)
1169
1170 [[ -d "$systemdsystemunitdir" ]] || systemdsystemunitdir=${systemdutildir}/system
1171
1172 [[ -d $systemdsystemconfdir ]] \
1173 || systemdsystemconfdir=$(pkg-config systemd --variable=systemdsystemconfdir 2>/dev/null)
1174
1175 [[ -d "$systemdsystemconfdir" ]] || systemdsystemconfdir=/etc/systemd/system
1176
1177 export initdir dracutbasedir dracutmodules \
1178 fw_dir drivers_dir debug no_kernel kernel_only \
1179 omit_drivers mdadmconf lvmconf root_dev \
1180 use_fstab fstab_lines libdirs fscks nofscks ro_mnt \
1181 stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
1182 debug host_fs_types host_devs sshkey add_fstab \
1183 DRACUT_VERSION udevdir prefix filesystems drivers \
1184 systemdutildir systemdsystemunitdir systemdsystemconfdir \
1185 host_modalias host_modules hostonly_cmdline
1186
1187 mods_to_load=""
1188 # check all our modules to see if they should be sourced.
1189 # This builds a list of modules that we will install next.
1190 for_each_module_dir check_module
1191 for_each_module_dir check_mount
1192
1193 dracut_module_included "fips" && export DRACUT_FIPS_MODE=1
1194
1195 if [[ $print_cmdline ]]; then
1196 modules_loaded=" "
1197 # source our modules.
1198 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1199 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
1200 module_cmdline "$_d_mod"
1201 done
1202 unset moddir
1203 printf "\n"
1204 exit 0
1205 fi
1206
1207 # Create some directory structure first
1208 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
1209
1210 [[ -h /lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
1211 [[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
1212
1213 if [[ $prefix ]]; then
1214 for d in bin etc lib sbin tmp usr var $libdirs; do
1215 [[ "$d" == */* ]] && continue
1216 ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
1217 done
1218 fi
1219
1220 if [[ $kernel_only != yes ]]; then
1221 for d in usr/bin usr/sbin bin etc lib sbin tmp usr var $libdirs; do
1222 [[ -e "${initdir}${prefix}/$d" ]] && continue
1223 if [ -L "/$d" ]; then
1224 inst_symlink "/$d" "${prefix}/$d"
1225 else
1226 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1227 fi
1228 done
1229
1230 for d in dev proc sys sysroot root run; do
1231 if [ -L "/$d" ]; then
1232 inst_symlink "/$d"
1233 else
1234 mkdir -m 0755 -p "$initdir/$d"
1235 fi
1236 done
1237
1238 ln -sfn ../run "$initdir/var/run"
1239 ln -sfn ../run/lock "$initdir/var/lock"
1240 ln -sfn ../run/log "$initdir/var/log"
1241 else
1242 for d in lib "$libdir"; do
1243 [[ -e "${initdir}${prefix}/$d" ]] && continue
1244 if [ -h "/$d" ]; then
1245 inst "/$d" "${prefix}/$d"
1246 else
1247 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1248 fi
1249 done
1250 fi
1251
1252 if [[ $kernel_only != yes ]]; then
1253 mkdir -p "${initdir}/etc/cmdline.d"
1254 for _d in $hookdirs; do
1255 mkdir -m 0755 -p ${initdir}/lib/dracut/hooks/$_d
1256 done
1257 if [[ "$UID" = "0" ]]; then
1258 [ -c ${initdir}/dev/null ] || mknod ${initdir}/dev/null c 1 3
1259 [ -c ${initdir}/dev/kmsg ] || mknod ${initdir}/dev/kmsg c 1 11
1260 [ -c ${initdir}/dev/console ] || mknod ${initdir}/dev/console c 5 1
1261 fi
1262 fi
1263
1264 _isize=0 #initramfs size
1265 modules_loaded=" "
1266 # source our modules.
1267 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1268 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
1269 if [[ "$mods_to_load" == *\ $_d_mod\ * ]]; then
1270 if [[ $show_modules = yes ]]; then
1271 printf "%s\n" "$_d_mod"
1272 else
1273 dinfo "*** Including module: $_d_mod ***"
1274 fi
1275 if [[ $kernel_only == yes ]]; then
1276 module_installkernel "$_d_mod" || {
1277 dfatal "installkernel failed in module $_d_mod"
1278 exit 1
1279 }
1280 else
1281 module_install "$_d_mod"
1282 if [[ $no_kernel != yes ]]; then
1283 module_installkernel "$_d_mod" || {
1284 dfatal "installkernel failed in module $_d_mod"
1285 exit 1
1286 }
1287 fi
1288 fi
1289 mods_to_load=${mods_to_load// $_d_mod /}
1290 modules_loaded+="$_d_mod "
1291
1292 #print the module install size
1293 if [ -n "$printsize" ]; then
1294 _isize_new=$(du -sk ${initdir}|cut -f1)
1295 _isize_delta=$((_isize_new - _isize))
1296 printf "%s\n" "$_d_mod install size: ${_isize_delta}k"
1297 _isize=$_isize_new
1298 fi
1299 fi
1300 done
1301 unset moddir
1302
1303 for i in $modules_loaded; do
1304 mkdir -p $initdir/lib/dracut
1305 printf "%s\n" "$i" >> $initdir/lib/dracut/modules.txt
1306 done
1307
1308 dinfo "*** Including modules done ***"
1309
1310 ## final stuff that has to happen
1311 if [[ $no_kernel != yes ]]; then
1312
1313 if [[ $drivers ]]; then
1314 hostonly='' instmods $drivers
1315 fi
1316
1317 if [[ $add_drivers ]]; then
1318 hostonly='' instmods -c $add_drivers
1319 fi
1320 if [[ $filesystems ]]; then
1321 hostonly='' instmods -c $filesystems
1322 fi
1323
1324 dinfo "*** Installing kernel module dependencies and firmware ***"
1325 dracut_kernel_post
1326 dinfo "*** Installing kernel module dependencies and firmware done ***"
1327
1328 if [[ $noimageifnotneeded == yes ]] && [[ $hostonly ]]; then
1329 if [[ ! -f "$initdir/lib/dracut/need-initqueue" ]] && \
1330 [[ -f ${initdir}/lib/modules/$kernel/modules.dep && ! -s ${initdir}/lib/modules/$kernel/modules.dep ]]; then
1331 for i in ${initdir}/etc/cmdline.d/*.conf; do
1332 # We need no initramfs image and do not generate one.
1333 [[ $i == "${initdir}/etc/cmdline.d/*.conf" ]] && exit 0
1334 done
1335 fi
1336 fi
1337 fi
1338
1339 if [[ $kernel_only != yes ]]; then
1340 (( ${#install_items[@]} > 0 )) && inst_multiple ${install_items[@]}
1341 (( ${#install_optional_items[@]} > 0 )) && inst_multiple -o ${install_optional_items[@]}
1342
1343 [[ $kernel_cmdline ]] && printf "%s\n" "$kernel_cmdline" >> "${initdir}/etc/cmdline.d/01-default.conf"
1344
1345 while pop fstab_lines line; do
1346 line=($line)
1347 [ -z "${line[3]}" ] && line[3]="defaults"
1348 [ -z "${line[4]}" ] && line[4]="0"
1349 [ -z "${line[5]}" ] && line[5]="2"
1350 echo "${line[@]}" >> "${initdir}/etc/fstab"
1351 done
1352
1353 for f in $add_fstab; do
1354 cat "$f" >> "${initdir}/etc/fstab"
1355 done
1356
1357 if [ -d ${initdir}/$systemdutildir ]; then
1358 mkdir -p ${initdir}/etc/conf.d
1359 {
1360 printf "%s\n" "systemdutildir=\"$systemdutildir\""
1361 printf "%s\n" "systemdsystemunitdir=\"$systemdsystemunitdir\""
1362 printf "%s\n" "systemdsystemconfdir=\"$systemdsystemconfdir\""
1363 } > ${initdir}/etc/conf.d/systemd.conf
1364 fi
1365
1366 if [[ $DRACUT_RESOLVE_LAZY ]] && [[ $DRACUT_INSTALL ]]; then
1367 dinfo "*** Resolving executable dependencies ***"
1368 find "$initdir" -type f \
1369 '(' -perm -0100 -or -perm -0010 -or -perm -0001 ')' \
1370 -not -path '*.ko' -print0 \
1371 | xargs -r -0 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -R ${DRACUT_FIPS_MODE:+-H} --
1372 dinfo "*** Resolving executable dependencies done***"
1373 fi
1374
1375 # libpthread workaround: pthread_cancel wants to dlopen libgcc_s.so
1376 for _dir in $libdirs; do
1377 for _f in "$_dir/libpthread.so"*; do
1378 [[ -e "$_f" ]] || continue
1379 inst_libdir_file "libgcc_s.so*"
1380 break 2
1381 done
1382 done
1383 fi
1384
1385 while pop include_src src && pop include_target tgt; do
1386 if [[ $src && $tgt ]]; then
1387 if [[ -f $src ]]; then
1388 inst $src $tgt
1389 else
1390 ddebug "Including directory: $src"
1391 mkdir -p "${initdir}/${tgt}"
1392 # check for preexisting symlinks, so we can cope with the
1393 # symlinks to $prefix
1394 for i in "$src"/*; do
1395 [[ -e "$i" || -h "$i" ]] || continue
1396 s=${initdir}/${tgt}/${i#$src/}
1397 if [[ -d "$i" ]]; then
1398 if ! [[ -e "$s" ]]; then
1399 mkdir -m 0755 -p "$s"
1400 chmod --reference="$i" "$s"
1401 fi
1402 cp --reflink=auto --sparse=auto -fa -t "$s" "$i"/*
1403 else
1404 cp --reflink=auto --sparse=auto -fa -t "$s" "$i"
1405 fi
1406 done
1407 fi
1408 fi
1409 done
1410
1411 if [[ $kernel_only != yes ]]; then
1412 # make sure that library links are correct and up to date
1413 for f in /etc/ld.so.conf /etc/ld.so.conf.d/*; do
1414 [[ -f $f ]] && inst_simple "$f"
1415 done
1416 if ! ldconfig -r "$initdir"; then
1417 if [[ $UID = 0 ]]; then
1418 derror "ldconfig exited ungracefully"
1419 else
1420 derror "ldconfig might need uid=0 (root) for chroot()"
1421 fi
1422 fi
1423 fi
1424
1425 PRELINK_BIN="$(command -v prelink)"
1426 if [[ $UID = 0 ]] && [[ $PRELINK_BIN ]]; then
1427 if [[ $DRACUT_FIPS_MODE ]]; then
1428 dinfo "*** Installing prelink files ***"
1429 inst_multiple -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf /etc/prelink.cache
1430 elif [[ $do_prelink == yes ]]; then
1431 dinfo "*** Pre-linking files ***"
1432 inst_multiple -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf
1433 chroot "$initdir" "$PRELINK_BIN" -a
1434 rm -f -- "$initdir/$PRELINK_BIN"
1435 rm -fr -- "$initdir"/etc/prelink.*
1436 dinfo "*** Pre-linking files done ***"
1437 fi
1438 fi
1439
1440 if [[ $do_hardlink = yes ]] && command -v hardlink >/dev/null; then
1441 dinfo "*** Hardlinking files ***"
1442 hardlink "$initdir" 2>&1
1443 dinfo "*** Hardlinking files done ***"
1444 fi
1445
1446 # strip binaries
1447 if [[ $do_strip = yes ]] ; then
1448 for p in strip xargs find; do
1449 if ! type -P $p >/dev/null; then
1450 dinfo "Could not find '$p'. Not stripping the initramfs."
1451 do_strip=no
1452 fi
1453 done
1454 fi
1455
1456 if [[ $do_strip = yes ]] && ! [[ $DRACUT_FIPS_MODE ]]; then
1457 dinfo "*** Stripping files ***"
1458 find "$initdir" -type f \
1459 -executable -not -path '*/lib/modules/*.ko' -print0 \
1460 | xargs -r -0 strip -g 2>/dev/null
1461
1462 # strip kernel modules, but do not touch signed modules
1463 find "$initdir" -type f -path '*/lib/modules/*.ko' -print0 \
1464 | while read -r -d $'\0' f; do
1465 SIG=$(tail -c 28 "$f")
1466 [[ $SIG == '~Module signature appended~' ]] || { printf "%s\000" "$f"; }
1467 done | xargs -r -0 strip -g
1468
1469 dinfo "*** Stripping files done ***"
1470 fi
1471 if [[ $early_microcode = yes ]]; then
1472 dinfo "*** Generating early-microcode cpio image ***"
1473 ucode_dir=(amd-ucode intel-ucode)
1474 ucode_dest=(AuthenticAMD.bin GenuineIntel.bin)
1475 _dest_dir="$early_cpio_dir/d/kernel/x86/microcode"
1476 _dest_idx="0 1"
1477 mkdir -p $_dest_dir
1478 if [[ $hostonly ]]; then
1479 [[ $(get_cpu_vendor) == "AMD" ]] && _dest_idx="0"
1480 [[ $(get_cpu_vendor) == "Intel" ]] && _dest_idx="1"
1481 fi
1482 for idx in $_dest_idx; do
1483 _fw=${ucode_dir[$idx]}
1484 for _fwdir in $fw_dir; do
1485 if [[ -d $_fwdir && -d $_fwdir/$_fw ]]; then
1486 _src="*"
1487 dinfo "*** Constructing ${ucode_dest[$idx]} ****"
1488 if [[ $hostonly ]]; then
1489 _src=$(get_ucode_file)
1490 if ! [[ -r $_fwdir/$_fw/$_src ]];then
1491 break;
1492 fi
1493 fi
1494 cat $_fwdir/$_fw/$_src > $_dest_dir/${ucode_dest[$idx]}
1495 create_early_cpio="yes"
1496 fi
1497 done
1498 done
1499 fi
1500
1501 if [[ $acpi_override = yes ]] && [[ -d $acpi_table_dir ]]; then
1502 dinfo "*** Packaging ACPI tables to override BIOS provided ones ***"
1503 _dest_dir="$early_cpio_dir/d/kernel/firmware/acpi"
1504 mkdir -p $_dest_dir
1505 for table in $acpi_table_dir/*.aml; do
1506 dinfo " Adding ACPI table: $table"
1507 cp -a $table $_dest_dir
1508 create_early_cpio="yes"
1509 done
1510 fi
1511
1512 dinfo "*** Store current command line parameters ***"
1513 if ! ( echo $PARMS_TO_STORE > $initdir/lib/dracut/build-parameter.txt ); then
1514 dfatal "Could not store the current command line parameters"
1515 exit 1
1516 fi
1517
1518 rm -f -- "$outfile"
1519 dinfo "*** Creating image file ***"
1520
1521 if [[ $DRACUT_REPRODUCIBLE ]]; then
1522 find "$initdir" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
1523 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
1524
1525 [[ "$(cpio --help)" == *--reproducible* ]] && CPIO_REPRODUCIBLE=1
1526 fi
1527
1528 [[ "$UID" != 0 ]] && cpio_owner_root="-R 0:0"
1529
1530 if [[ $create_early_cpio = yes ]]; then
1531 echo 1 > "$early_cpio_dir/d/early_cpio"
1532
1533 if [[ $DRACUT_REPRODUCIBLE ]]; then
1534 find "$early_cpio_dir/d" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
1535 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
1536 fi
1537
1538 # The microcode blob is _before_ the initramfs blob, not after
1539 (
1540 cd "$early_cpio_dir/d"
1541 find . -print0 | sort -z \
1542 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null $cpio_owner_root -H newc -o --quiet > $outfile
1543 )
1544 fi
1545
1546 if ! (
1547 umask 077; cd "$initdir"
1548 find . -print0 | sort -z \
1549 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null $cpio_owner_root -H newc -o --quiet \
1550 | $compress >> "$outfile"
1551 ); then
1552 dfatal "dracut: creation of $outfile failed"
1553 exit 1
1554 fi
1555 dinfo "*** Creating image file done ***"
1556
1557 if (( maxloglvl >= 5 )); then
1558 if [[ $allowlocal ]]; then
1559 "$dracutbasedir/lsinitrd.sh" "$outfile"| ddebug
1560 else
1561 lsinitrd "$outfile"| ddebug
1562 fi
1563 fi
1564
1565 exit 0