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