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