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