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