dracut.sh: output unknown argument
[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 usage() {
30     [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
31     if [[ -f $dracutbasedir/dracut-version.sh ]]; then
32         . $dracutbasedir/dracut-version.sh
33     fi
34
35 #                                                       80x25 linebreak here ^
36     cat << EOF
37 Usage: $0 [OPTION]... <initramfs> <kernel-version>
38
39 Version: $DRACUT_VERSION
40
41 Creates initial ramdisk images for preloading modules
42
43   -f, --force           Overwrite existing initramfs file.
44   -m, --modules [LIST]  Specify a space-separated list of dracut modules to
45                          call when building the initramfs. Modules are located
46                          in /usr/lib/dracut/modules.d.
47   -o, --omit [LIST]     Omit a space-separated list of dracut modules.
48   -a, --add [LIST]      Add a space-separated list of dracut modules.
49   -d, --drivers [LIST]  Specify a space-separated list of kernel modules to
50                         exclusively include in the initramfs.
51   --add-drivers [LIST] Specify a space-separated list of kernel
52                         modules to add to the initramfs.
53   --omit-drivers [LIST] Specify a space-separated list of kernel
54                         modules not to add to the initramfs.
55   --filesystems [LIST]  Specify a space-separated list of kernel filesystem
56                         modules to exclusively include in the generic
57                         initramfs.
58   -k, --kmoddir [DIR]   Specify the directory, where to look for kernel
59                         modules
60   --fwdir [DIR]         Specify additional directories, where to look for
61                         firmwares, separated by :
62   --kernel-only         Only install kernel drivers and firmware files
63   --no-kernel           Do not install kernel drivers and firmware files
64   --strip               Strip binaries in the initramfs
65   --nostrip             Do not strip binaries in the initramfs (default)
66   --prefix [DIR]        Prefix initramfs files with [DIR]
67   --noprefix            Do not prefix initramfs files (default)
68   --mdadmconf           Include local /etc/mdadm.conf
69   --nomdadmconf         Do not include local /etc/mdadm.conf
70   --lvmconf             Include local /etc/lvm/lvm.conf
71   --nolvmconf           Do not include local /etc/lvm/lvm.conf
72   --fscks [LIST]        Add a space-separated list of fsck helpers.
73   --nofscks             Inhibit installation of any fsck helpers.
74   -h, --help            This message
75   --debug               Output debug information of the build process
76   --profile             Output profile information of the build process
77   -L, --stdlog [0-6]    Specify logging level (to standard error)
78                          0 - suppress any messages
79                          1 - only fatal errors
80                          2 - all errors
81                          3 - warnings
82                          4 - info (default)
83                          5 - debug info (here starts lots of output)
84                          6 - trace info (and even more)
85   -v, --verbose         Increase verbosity level (default is info(4))
86   -q, --quiet           Decrease verbosity level (default is info(4))
87   -c, --conf [FILE]     Specify configuration file to use.
88                          Default: /etc/dracut.conf
89   --confdir [DIR]       Specify configuration directory to use *.conf files
90                          from. Default: /etc/dracut.conf.d
91   --tmpdir [DIR]        Temporary directory to be used instead of default
92                          /var/tmp.
93   -l, --local           Local mode. Use modules from the current working
94                          directory instead of the system-wide installed in
95                          /usr/lib/dracut/modules.d.
96                          Useful when running dracut from a git checkout.
97   -H, --hostonly        Host-Only mode: Install only what is needed for
98                          booting the local host instead of a generic host.
99   --no-hostonly         Disables Host-Only mode
100   --fstab               Use /etc/fstab to determine the root device.
101   --add-fstab [FILE]    Add file to the initramfs fstab
102   --mount "[DEV] [MP] [FSTYPE] [FSOPTS]"
103                         Mount device [DEV] on mountpoint [MP] with filesystem
104                         [FSTYPE] and options [FSOPTS] in the initramfs
105   -i, --include [SOURCE] [TARGET]
106                         Include the files in the SOURCE directory into the
107                          Target directory in the final initramfs.
108                         If SOURCE is a file, it will be installed to TARGET
109                          in the final initramfs.
110   -I, --install [LIST]  Install the space separated list of files into the
111                          initramfs.
112   --gzip                Compress the generated initramfs using gzip.
113                          This will be done by default, unless another
114                          compression option or --no-compress is passed.
115   --bzip2               Compress the generated initramfs using bzip2.
116                          Make sure your kernel has bzip2 decompression support
117                          compiled in, otherwise you will not be able to boot.
118   --lzma                Compress the generated initramfs using lzma.
119                          Make sure your kernel has lzma support compiled in,
120                          otherwise you will not be able to boot.
121   --xz                  Compress the generated initramfs using xz.
122                          Make sure that your kernel has xz support compiled
123                          in, otherwise you will not be able to boot.
124   --compress [COMPRESSION] Compress the generated initramfs with the
125                          passed compression program.  Make sure your kernel
126                          knows how to decompress the generated initramfs,
127                          otherwise you will not be able to boot.
128   --no-compress         Do not compress the generated initramfs.  This will
129                          override any other compression options.
130   --list-modules        List all available dracut modules.
131   -M, --show-modules    Print included module's name to standard output during
132                          build.
133   --keep                Keep the temporary initramfs for debugging purposes
134   --printsize           Print out the module install size
135   --sshkey [SSHKEY]     Add ssh key to initramfs (use with ssh-client module)
136
137 If [LIST] has multiple arguments, then you have to put these in quotes.
138 For example:
139 # dracut --add-drivers "module1 module2"  ...
140 EOF
141 }
142
143 # function push()
144 # push values to a stack
145 # $1 = stack variable
146 # $2.. values
147 # example:
148 # push stack 1 2 "3 4"
149 push() {
150     local __stack=$1; shift
151     for i in "$@"; do
152         eval ${__stack}'[${#'${__stack}'[@]}]="$i"'
153     done
154 }
155
156 # function pop()
157 # pops the last value from a stack
158 # assigns value to second argument variable
159 # or echo to stdout, if no second argument
160 # $1 = stack variable
161 # $2 = optional variable to store the value
162 # example:
163 # pop stack val
164 # val=$(pop stack)
165 pop() {
166     local __stack=$1; shift
167     local __resultvar=$1
168     local myresult;
169     # check for empty stack
170     eval '[[ ${#'${__stack}'[@]} -eq 0 ]] && return 1'
171
172     eval myresult='${'${__stack}'[${#'${__stack}'[@]}-1]}'
173
174     if [[ "$__resultvar" ]]; then
175         eval $__resultvar="'$myresult'"
176     else
177         echo "$myresult"
178     fi
179     eval unset ${__stack}'[${#'${__stack}'[@]}-1]'
180     return 0
181 }
182
183 # Little helper function for reading args from the commandline.
184 # it automatically handles -a b and -a=b variants, and returns 1 if
185 # we need to shift $3.
186 read_arg() {
187     # $1 = arg name
188     # $2 = arg value
189     # $3 = arg parameter
190     local rematch='^[^=]*=(.*)$'
191     if [[ $2 =~ $rematch ]]; then
192         read "$1" <<< "${BASH_REMATCH[1]}"
193     else
194         read "$1" <<< "$3"
195         # There is no way to shift our callers args, so
196         # return 1 to indicate they should do it instead.
197         return 1
198     fi
199 }
200
201 # Little helper function for reading args from the commandline to a stack.
202 # it automatically handles -a b and -a=b variants, and returns 1 if
203 # we need to shift $3.
204 push_arg() {
205     # $1 = arg name
206     # $2 = arg value
207     # $3 = arg parameter
208     local rematch='^[^=]*=(.*)$'
209     if [[ $2 =~ $rematch ]]; then
210         push "$1" "${BASH_REMATCH[1]}"
211     else
212         push "$1" "$3"
213         # There is no way to shift our callers args, so
214         # return 1 to indicate they should do it instead.
215         return 1
216     fi
217 }
218
219 verbosity_mod_l=0
220
221 while (($# > 0)); do
222     case ${1%%=*} in
223         -a|--add)      push_arg add_dracutmodules_l  "$@" || shift;;
224         --force-add)   push_arg force_add_dracutmodules_l  "$@" || shift;;
225         --add-drivers) push_arg add_drivers_l        "$@" || shift;;
226         --omit-drivers) push_arg omit_drivers_l      "$@" || shift;;
227         -m|--modules)  push_arg dracutmodules_l      "$@" || shift;;
228         -o|--omit)     push_arg omit_dracutmodules_l "$@" || shift;;
229         -d|--drivers)  push_arg drivers_l            "$@" || shift;;
230         --filesystems) push_arg filesystems_l        "$@" || shift;;
231         -I|--install)  push_arg install_items_l      "$@" || shift;;
232         --fwdir)       push_arg fw_dir_l             "$@" || shift;;
233         --libdirs)     push_arg libdirs_l            "$@" || shift;;
234         --fscks)       push_arg fscks_l              "$@" || shift;;
235         --add-fstab)   push_arg add_fstab_l          "$@" || shift;;
236         --mount)       push_arg fstab_lines          "$@" || shift;;
237         --nofscks)     nofscks_l="yes";;
238         -k|--kmoddir)  read_arg drivers_dir_l        "$@" || shift;;
239         -c|--conf)     read_arg conffile             "$@" || shift;;
240         --confdir)     read_arg confdir              "$@" || shift;;
241         --tmpdir)      read_arg tmpdir_l             "$@" || shift;;
242         -L|--stdlog)   read_arg stdloglvl_l          "$@" || shift;;
243         --compress)    read_arg compress_l           "$@" || shift;;
244         --prefix)      read_arg prefix_l             "$@" || shift;;
245         -f|--force)    force=yes;;
246         --kernel-only) kernel_only="yes"; no_kernel="no";;
247         --no-kernel)   kernel_only="no"; no_kernel="yes";;
248         --strip)       do_strip_l="yes";;
249         --nostrip)     do_strip_l="no";;
250         --noprefix)    prefix_l="/";;
251         --mdadmconf)   mdadmconf_l="yes";;
252         --nomdadmconf) mdadmconf_l="no";;
253         --lvmconf)     lvmconf_l="yes";;
254         --nolvmconf)   lvmconf_l="no";;
255         --debug)       debug="yes";;
256         --profile)     profile="yes";;
257         --sshkey)      read_arg sshkey   "$@" || shift;;
258         -v|--verbose)  ((verbosity_mod_l++));;
259         -q|--quiet)    ((verbosity_mod_l--));;
260         -l|--local)
261                        allowlocal="yes"
262                        [[ -f "$(readlink -f ${0%/*})/dracut-functions.sh" ]] \
263                            && dracutbasedir="$(readlink -f ${0%/*})"
264                        ;;
265         -H|--hostonly) hostonly_l="yes" ;;
266         --no-hostonly) hostonly_l="no" ;;
267         --fstab)       use_fstab_l="yes" ;;
268         -h|--help)     usage; exit 1 ;;
269         -i|--include)  push include_src "$2"
270                        push include_target "$3"
271                        shift 2;;
272         --bzip2)       compress_l="bzip2";;
273         --lzma)        compress_l="lzma";;
274         --xz)          compress_l="xz";;
275         --no-compress) _no_compress_l="cat";;
276         --gzip)        compress_l="gzip";;
277         --list-modules)
278             do_list="yes";
279             ;;
280         -M|--show-modules)
281                        show_modules_l="yes"
282                        ;;
283         --keep)        keep="yes";;
284         --printsize)   printsize="yes";;
285         -*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage; exit 1;;
286         *)
287             if ! [[ ${outfile+x} ]]; then
288                 outfile=$1
289             elif ! [[ ${kernel+x} ]]; then
290                 kernel=$1
291             else
292                 echo "Unknown argument: $1"
293                 usage; exit 1;
294             fi
295             ;;
296     esac
297     shift
298 done
299 if ! [[ $kernel ]]; then
300     kernel=$(uname -r)
301 fi
302 [[ $outfile ]] || outfile="/boot/initramfs-$kernel.img"
303
304 for i in /usr/sbin /sbin /usr/bin /bin; do
305     rl=$i
306     if [ -L "$i" ]; then
307         rl=$(readlink -f $i)
308     fi
309     NPATH+=":$rl"
310 done
311 export PATH="${NPATH#:}"
312 unset NPATH
313 unset LD_LIBRARY_PATH
314 unset GREP_OPTIONS
315
316 [[ $debug ]] && {
317     export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
318     set -x
319 }
320
321 [[ $profile ]] && {
322     export PS4='+ $(date "+%s.%N") ${BASH_SOURCE}@${LINENO}: ';
323     set -x
324     debug=yes
325 }
326
327 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
328
329 # if we were not passed a config file, try the default one
330 if [[ ! -f $conffile ]]; then
331     [[ $allowlocal ]] && conffile="$dracutbasedir/dracut.conf" || \
332         conffile="/etc/dracut.conf"
333 fi
334
335 if [[ ! -d $confdir ]]; then
336     [[ $allowlocal ]] && confdir="$dracutbasedir/dracut.conf.d" || \
337         confdir="/etc/dracut.conf.d"
338 fi
339
340 # source our config file
341 [[ -f $conffile ]] && . "$conffile"
342
343 # source our config dir
344 if [[ $confdir && -d $confdir ]]; then
345     for f in "$confdir"/*.conf; do
346         [[ -e $f ]] && . "$f"
347     done
348 fi
349
350 # these optins add to the stuff in the config file
351 if (( ${#add_dracutmodules_l[@]} )); then
352     while pop add_dracutmodules_l val; do
353         add_dracutmodules+=" $val "
354     done
355 fi
356
357 if (( ${#force_add_dracutmodules_l[@]} )); then
358     while pop force_add_dracutmodules_l val; do
359         force_add_dracutmodules+=" $val "
360     done
361 fi
362
363 if (( ${#fscks_l[@]} )); then
364     while pop fscks_l val; do
365         fscks+=" $val "
366     done
367 fi
368
369 if (( ${#add_fstab_l[@]} )); then
370     while pop add_fstab_l val; do
371         add_fstab+=" $val "
372     done
373 fi
374
375 if (( ${#fstab_lines_l[@]} )); then
376     while pop fstab_lines_l val; do
377         push fstab_lines $val
378     done
379 fi
380
381 if (( ${#install_items_l[@]} )); then
382     while pop install_items_l val; do
383         install_items+=" $val "
384     done
385 fi
386
387 # these options override the stuff in the config file
388 if (( ${#dracutmodules_l[@]} )); then
389     dracutmodules=''
390     while pop dracutmodules_l val; do
391         dracutmodules+="$val "
392     done
393 fi
394
395 if (( ${#omit_dracutmodules_l[@]} )); then
396     omit_dracutmodules=''
397     while pop omit_dracutmodules_l val; do
398         omit_dracutmodules+="$val "
399     done
400 fi
401
402 if (( ${#filesystems_l[@]} )); then
403     filesystems=''
404     while pop filesystems_l val; do
405         filesystems+="$val "
406     done
407 fi
408
409 if (( ${#fw_dir_l[@]} )); then
410     fw_dir=''
411     while pop fw_dir_l val; do
412         fw_dir+="$val "
413     done
414 fi
415
416 if (( ${#libdirs_l[@]} )); then
417     libdirs=''
418     while pop libdirs_l val; do
419         libdirs+="$val "
420     done
421 fi
422
423 [[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
424 [[ ! $stdloglvl ]] && stdloglvl=4
425 stdloglvl=$((stdloglvl + verbosity_mod_l))
426 ((stdloglvl > 6)) && stdloglvl=6
427 ((stdloglvl < 0)) && stdloglvl=0
428
429 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
430 [[ $do_strip_l ]] && do_strip=$do_strip_l
431 [[ $prefix_l ]] && prefix=$prefix_l
432 [[ $prefix = "/" ]] && unset prefix
433 [[ $hostonly_l ]] && hostonly=$hostonly_l
434 [[ $use_fstab_l ]] && use_fstab=$use_fstab_l
435 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
436 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
437 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
438 [[ $fw_dir ]] || fw_dir="/lib/firmware/updates /lib/firmware"
439 [[ $tmpdir_l ]] && tmpdir="$tmpdir_l"
440 [[ $tmpdir ]] || tmpdir=/var/tmp
441 [[ $do_strip ]] || do_strip=no
442 [[ $compress_l ]] && compress=$compress_l
443 [[ $show_modules_l ]] && show_modules=$show_modules_l
444 [[ $nofscks_l ]] && nofscks="yes"
445 # eliminate IFS hackery when messing with fw_dir
446 fw_dir=${fw_dir//:/ }
447
448 # handle compression options.
449 [[ $compress ]] || compress="gzip"
450 case $compress in
451     bzip2) compress="bzip2 -9";;
452     lzma)  compress="lzma -9";;
453     xz)    compress="xz --check=crc32 --lzma2=dict=1MiB";;
454     gzip)  command -v pigz > /dev/null 2>&1 && compress="pigz -9" || \
455                                          compress="gzip -9";;
456 esac
457 if [[ $_no_compress_l = "cat" ]]; then
458     compress="cat"
459 fi
460
461 [[ $hostonly = yes ]] && hostonly="-h"
462 [[ $hostonly != "-h" ]] && unset hostonly
463
464 if [[ -f $dracutbasedir/dracut-functions.sh ]]; then
465     . $dracutbasedir/dracut-functions.sh
466 else
467     echo "Cannot find $dracutbasedir/dracut-functions.sh." >&2
468     echo "Are you running from a git checkout?" >&2
469     echo "Try passing -l as an argument to $0" >&2
470     exit 1
471 fi
472
473 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
474     . $dracutbasedir/dracut-version.sh
475 fi
476
477 # Verify bash version, curret minimum is 3.1
478 if (( ${BASH_VERSINFO[0]} < 3 ||
479     ( ${BASH_VERSINFO[0]} == 3 && ${BASH_VERSINFO[1]} < 1 ) )); then
480     dfatal 'You need at least Bash 3.1 to use dracut, sorry.'
481     exit 1
482 fi
483
484 dracutfunctions=$dracutbasedir/dracut-functions.sh
485 export dracutfunctions
486
487 if (( ${#drivers_l[@]} )); then
488     drivers=''
489     while pop drivers_l val; do
490         drivers+="$val "
491     done
492 fi
493 drivers=${drivers/-/_}
494
495 if (( ${#add_drivers_l[@]} )); then
496     while pop add_drivers_l val; do
497         add_drivers+=" $val "
498     done
499 fi
500 add_drivers=${add_drivers/-/_}
501
502 if (( ${#omit_drivers_l[@]} )); then
503     while pop omit_drivers_l val; do
504         omit_drivers+=" $val "
505     done
506 fi
507 omit_drivers=${omit_drivers/-/_}
508
509 omit_drivers_corrected=""
510 for d in $omit_drivers; do
511     strstr " $drivers $add_drivers " " $d " && continue
512     omit_drivers_corrected+="$d|"
513 done
514 omit_drivers="${omit_drivers_corrected%|}"
515 unset omit_drivers_corrected
516
517
518 ddebug "Executing $0 $dracut_args"
519
520 [[ $do_list = yes ]] && {
521     for mod in $dracutbasedir/modules.d/*; do
522         [[ -d $mod ]] || continue;
523         [[ -e $mod/install || -e $mod/installkernel || \
524             -e $mod/module-setup.sh ]] || continue
525         echo ${mod##*/??}
526     done
527     exit 0
528 }
529
530 # Detect lib paths
531 if ! [[ $libdirs ]] ; then
532     if strstr "$(ldd /bin/sh)" "/lib64/" &>/dev/null \
533         && [[ -d /lib64 ]]; then
534         libdirs+=" /lib64"
535         [[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64"
536     else
537         libdirs+=" /lib"
538         [[ -d /usr/lib ]] && libdirs+=" /usr/lib"
539     fi
540 fi
541
542 # This is kinda legacy -- eventually it should go away.
543 case $dracutmodules in
544     ""|auto) dracutmodules="all" ;;
545 esac
546
547 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
548
549 srcmods="/lib/modules/$kernel/"
550 [[ $drivers_dir ]] && {
551     if vercmp $(modprobe --version | cut -d' ' -f3) lt 3.7; then
552         dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
553         exit 1
554     fi
555     srcmods="$drivers_dir"
556 }
557 export srcmods
558
559 [[ -f $srcmods/modules.dep ]] || {
560     dfatal "$srcmods/modules.dep is missing. Did you run depmod?"
561     exit 1
562 }
563
564 if [[ -f $outfile && ! $force ]]; then
565     dfatal "Will not override existing initramfs ($outfile) without --force"
566     exit 1
567 fi
568
569 outdir=${outfile%/*}
570 [[ $outdir ]] || outdir="/"
571
572 if [[ ! -d "$outdir" ]]; then
573     dfatal "Can't write $outfile: Directory $outdir does not exist."
574     exit 1
575 elif [[ ! -w "$outdir" ]]; then
576     dfatal "No permission to write $outdir."
577     exit 1
578 elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
579     dfatal "No permission to write $outfile."
580     exit 1
581 fi
582
583 readonly TMPDIR="$tmpdir"
584 readonly initdir=$(mktemp --tmpdir="$TMPDIR/" -d -t initramfs.XXXXXX)
585 [ -d "$initdir" ] || {
586     dfatal "mktemp failed."
587     exit 1
588 }
589
590 # clean up after ourselves no matter how we die.
591 trap 'ret=$?;[[ $keep ]] && echo "Not removing $initdir." >&2 || rm -rf "$initdir";exit $ret;' EXIT
592 # clean up after ourselves no matter how we die.
593 trap 'exit 1;' SIGINT
594
595 # Need to be able to have non-root users read stuff (rpcbind etc)
596 chmod 755 "$initdir"
597
598 for line in "${fstab_lines[@]}"; do
599     set -- $line
600     #dev mp fs fsopts
601     push host_devs "$1"
602     push host_fs_types "$1|$3"
603 done
604
605 for f in $add_fstab; do
606     [ -e $f ] || continue
607     while read dev rest; do
608         push host_devs $dev
609     done < $f
610 done
611
612 if [[ $hostonly ]]; then
613     # in hostonly mode, determine all devices, which have to be accessed
614     # and examine them for filesystem types
615
616     push host_mp \
617         "/" \
618         "/etc" \
619         "/usr" \
620         "/usr/bin" \
621         "/usr/sbin" \
622         "/usr/lib" \
623         "/usr/lib64" \
624         "/boot"
625
626     for mp in "${host_mp[@]}"; do
627         mountpoint "$mp" >/dev/null 2>&1 || continue
628         push host_devs $(readlink -f "/dev/block/$(find_block_device "$mp")")
629     done
630 fi
631
632 _get_fs_type() (
633     [[ $1 ]] || return
634     if [[ -b $1 ]] && get_fs_env $1; then
635         echo "$(readlink -f $1)|$ID_FS_TYPE"
636         return 1
637     fi
638     if [[ -b /dev/block/$1 ]] && get_fs_env /dev/block/$1; then
639         echo "$(readlink -f /dev/block/$1)|$ID_FS_TYPE"
640         return 1
641     fi
642     if fstype=$(find_dev_fstype $1); then
643         echo "$1|$fstype"
644         return 1
645     fi
646     return 1
647 )
648
649 for dev in "${host_devs[@]}"; do
650     unset fs_type
651     for fstype in $(_get_fs_type $dev) \
652         $(check_block_and_slaves _get_fs_type $(get_maj_min $dev)); do
653         if ! strstr " ${host_fs_types[*]} " " $fstype ";then
654             push host_fs_types "$fstype"
655         fi
656     done
657 done
658
659 [[ -d $udevdir ]] \
660     || udevdir=$(pkg-config udev --variable=udevdir 2>/dev/null)
661 if ! [[ -d "$udevdir" ]]; then
662     [[ -d /lib/udev ]] && udevdir=/lib/udev
663     [[ -d /usr/lib/udev ]] && udevdir=/usr/lib/udev
664 fi
665
666 [[ -d $systemdutildir ]] \
667     || systemdutildir=$(pkg-config systemd --variable=systemdutildir 2>/dev/null)
668 [[ -d $systemdsystemunitdir ]] \
669     || systemdsystemunitdir=$(pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null)
670
671 if ! [[ -d "$systemdutildir" ]]; then
672     [[ -d /lib/systemd ]] && systemdutildir=/lib/systemd
673     [[ -d /usr/lib/systemd ]] && systemdutildir=/usr/lib/systemd
674 fi
675 [[ -d "$systemdsystemunitdir" ]] || systemdsystemunitdir=${systemdutildir}/system
676
677 export initdir dracutbasedir dracutmodules drivers \
678     fw_dir drivers_dir debug no_kernel kernel_only \
679     add_drivers omit_drivers mdadmconf lvmconf filesystems \
680     use_fstab fstab_lines libdirs fscks nofscks \
681     stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
682     debug host_fs_types host_devs sshkey add_fstab \
683     DRACUT_VERSION udevdir systemdutildir systemdsystemunitdir
684
685 # Create some directory structure first
686 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
687
688 [[ -h /lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
689 [[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
690
691 if [[ $prefix ]]; then
692     for d in bin etc lib sbin tmp usr var $libdirs; do
693         strstr "$d" "/" && continue
694         ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
695     done
696 fi
697
698 if [[ $kernel_only != yes ]]; then
699     for d in usr/bin usr/sbin bin etc lib sbin tmp usr var var/log var/run var/lock $libdirs; do
700         [[ -e "${initdir}${prefix}/$d" ]] && continue
701         if [ -L "/$d" ]; then
702             inst_symlink "/$d" "${prefix}/$d"
703         else
704             mkdir -m 0755 -p "${initdir}${prefix}/$d"
705         fi
706     done
707
708     for d in dev proc sys sysroot root run run/lock run/initramfs; do
709         if [ -L "/$d" ]; then
710             inst_symlink "/$d"
711         else
712             mkdir -m 0755 -p "$initdir/$d"
713         fi
714     done
715
716     ln -sfn /run "$initdir/var/run"
717     ln -sfn /run/lock "$initdir/var/lock"
718 else
719     for d in lib "$libdir"; do
720         [[ -e "${initdir}${prefix}/$d" ]] && continue
721         if [ -h "/$d" ]; then
722             inst "/$d" "${prefix}/$d"
723         else
724             mkdir -m 0755 -p "${initdir}${prefix}/$d"
725         fi
726     done
727 fi
728
729 if [[ $kernel_only != yes ]]; then
730     mkdir -p "${initdir}/etc/cmdline.d"
731     for _d in $hookdirs; do
732         mkdir -m 0755 -p ${initdir}/lib/dracut/hooks/$_d
733     done
734     if [[ "$UID" = "0" ]]; then
735         for i in /dev/kmsg /dev/null /dev/console; do
736             [ -e $i ] || continue
737             cp -a $i $initdir/dev
738         done
739     fi
740 fi
741
742 mkdir -p "$initdir/.kernelmodseen"
743
744 mods_to_load=""
745 # check all our modules to see if they should be sourced.
746 # This builds a list of modules that we will install next.
747 for_each_module_dir check_module
748 for_each_module_dir check_mount
749
750 _isize=0 #initramfs size
751 modules_loaded=" "
752 # source our modules.
753 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
754     _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
755     if strstr "$mods_to_load" " $_d_mod "; then
756         [[ $show_modules = yes ]] && echo "$_d_mod" || \
757             dinfo "*** Including module: $_d_mod ***"
758         if [[ $kernel_only = yes ]]; then
759             module_installkernel $_d_mod || {
760                 dfatal "installkernel failed in module $_d_mod"
761                 exit 1
762             }
763         else
764             module_install $_d_mod
765             if [[ $no_kernel != yes ]]; then
766                 module_installkernel $_d_mod || {
767                     dfatal "installkernel failed in module $_d_mod"
768                     exit 1
769                 }
770             fi
771         fi
772         mods_to_load=${mods_to_load// $_d_mod /}
773         modules_loaded+="$_d_mod "
774
775         #print the module install size
776         if [ -n "$printsize" ]; then
777             _isize_new=$(du -sk ${initdir}|cut -f1)
778             _isize_delta=$(($_isize_new - $_isize))
779             echo "$_d_mod install size: ${_isize_delta}k"
780             _isize=$_isize_new
781         fi
782     fi
783 done
784 unset moddir
785
786 for i in $modules_loaded; do
787     mkdir -p $initdir/lib/dracut
788     echo "$i" >> $initdir/lib/dracut/modules.txt
789 done
790
791 dinfo "*** Including modules done ***"
792
793 ## final stuff that has to happen
794
795 # generate module dependencies for the initrd
796 if [[ -d $initdir/lib/modules/$kernel ]] && \
797     ! depmod -a -b "$initdir" $kernel; then
798     dfatal "\"depmod -a $kernel\" failed."
799     exit 1
800 fi
801
802 while pop include_src src && pop include_target tgt; do
803     if [[ $src && $tgt ]]; then
804         if [[ -f $src ]]; then
805             inst $src $tgt
806         else
807             ddebug "Including directory: $src"
808             mkdir -p "${initdir}/${tgt}"
809             # check for preexisting symlinks, so we can cope with the
810             # symlinks to $prefix
811             for i in "$src"/*; do
812                 [[ -e "$i" || -h "$i" ]] || continue
813                 s=${initdir}/${tgt}/${i#$src/}
814                 if [[ -d "$i" ]]; then
815                     if ! [[ -e "$s" ]]; then
816                         mkdir -m 0755 -p "$s"
817                         chmod --reference="$i" "$s"
818                     fi
819                     cp -a -t "$s" "$i"/*
820                 else
821                     cp -a -t "$s" "$i"
822                 fi
823             done
824         fi
825     fi
826 done
827
828 if [[ $kernel_only != yes ]]; then
829     for item in $install_items; do
830         dracut_install "$item"
831     done
832     unset item
833
834     while pop fstab_lines line; do
835         echo "$line 0 0" >> "${initdir}/etc/fstab"
836     done
837
838     for f in $add_fstab; do
839         cat $f >> "${initdir}/etc/fstab"
840     done
841
842     # make sure that library links are correct and up to date
843     for f in /etc/ld.so.conf /etc/ld.so.conf.d/*; do
844         [[ -f $f ]] && inst_simple "$f"
845     done
846     if ! ldconfig -r "$initdir"; then
847         if [[ $UID = 0 ]]; then
848             derror "ldconfig exited ungracefully"
849         else
850             derror "ldconfig might need uid=0 (root) for chroot()"
851         fi
852     fi
853 fi
854
855 rm -fr "$initdir/.kernelmodseen"
856
857
858 if (($maxloglvl >= 5)); then
859     ddebug "Listing sizes of included files:"
860     du -c "$initdir" | sort -n | ddebug
861 fi
862
863 # strip binaries
864 if [[ $do_strip = yes ]] ; then
865     for p in strip grep find; do
866         if ! type -P $p >/dev/null; then
867             derror "Could not find '$p'. You should run $0 with '--nostrip'."
868             do_strip=no
869         fi
870     done
871 fi
872
873 if [[ $do_strip = yes ]] ; then
874     for f in $(find "$initdir" -type f \
875         \( -perm -0100 -or -perm -0010 -or -perm -0001 \
876            -or -path '*/lib/modules/*.ko' \) ); do
877         dinfo "Stripping $f"
878         strip -g "$f" 2>/dev/null|| :
879     done
880 fi
881
882 type hardlink &>/dev/null && {
883     hardlink "$initdir" 2>&1
884 }
885
886 if strstr "$modules_loaded" " fips " && command -v prelink >/dev/null; then
887     for dir in "$initdir/bin" \
888        "$initdir/sbin" \
889        "$initdir/usr/bin" \
890        "$initdir/usr/sbin"; do
891         [[ -L "$dir" ]] && continue
892         for i in "$dir"/*; do
893             [[ -x $i ]] && prelink -u $i &>/dev/null
894         done
895     done
896 fi
897
898 if ! ( cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet| \
899     $compress > "$outfile"; ); then
900     dfatal "dracut: creation of $outfile failed"
901     exit 1
902 fi
903
904 dinfo "Wrote $outfile:"
905 dinfo "$(ls -l "$outfile")"
906
907 exit 0