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