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