]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut.sh
lsinitrd.sh: fixed version file extraction
[thirdparty/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.XXXXXXfailed." >&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 done
645 ddebug "Executing: $0 ${dracut_args[@]}"
646
647 [[ $do_list = yes ]] && {
648 for mod in $dracutbasedir/modules.d/*; do
649 [[ -d $mod ]] || continue;
650 [[ -e $mod/install || -e $mod/installkernel || \
651 -e $mod/module-setup.sh ]] || continue
652 echo ${mod##*/??}
653 done
654 exit 0
655 }
656
657 # This is kinda legacy -- eventually it should go away.
658 case $dracutmodules in
659 ""|auto) dracutmodules="all" ;;
660 esac
661
662 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
663
664 [[ -f $srcmods/modules.dep ]] || {
665 dfatal "$srcmods/modules.dep is missing. Did you run depmod?"
666 exit 1
667 }
668
669 if [[ -f $outfile && ! $force ]]; then
670 dfatal "Will not override existing initramfs ($outfile) without --force"
671 exit 1
672 fi
673
674 outdir=${outfile%/*}
675 [[ $outdir ]] || outdir="/"
676
677 if [[ ! -d "$outdir" ]]; then
678 dfatal "Can't write $outfile: Directory $outdir does not exist."
679 exit 1
680 elif [[ ! -w "$outdir" ]]; then
681 dfatal "No permission to write to $outdir."
682 exit 1
683 elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
684 dfatal "No permission to write $outfile."
685 exit 1
686 fi
687
688 # clean up after ourselves no matter how we die.
689 trap 'ret=$?;[[ $keep ]] && echo "Not removing $initdir." >&2 || rm -rf "$initdir";exit $ret;' EXIT
690 # clean up after ourselves no matter how we die.
691 trap 'exit 1;' SIGINT
692
693 # Need to be able to have non-root users read stuff (rpcbind etc)
694 chmod 755 "$initdir"
695
696 for line in "${fstab_lines[@]}"; do
697 set -- $line
698 #dev mp fs fsopts
699 push host_devs "$1"
700 push host_fs_types "$1|$3"
701 done
702
703 for f in $add_fstab; do
704 [ -e $f ] || continue
705 while read dev rest; do
706 push host_devs $dev
707 done < $f
708 done
709
710 if [[ $hostonly ]]; then
711 # in hostonly mode, determine all devices, which have to be accessed
712 # and examine them for filesystem types
713
714 push host_mp \
715 "/" \
716 "/etc" \
717 "/usr" \
718 "/usr/bin" \
719 "/usr/sbin" \
720 "/usr/lib" \
721 "/usr/lib64" \
722 "/boot"
723
724 for mp in "${host_mp[@]}"; do
725 mountpoint "$mp" >/dev/null 2>&1 || continue
726 push host_devs $(readlink -f "/dev/block/$(find_block_device "$mp")")
727 done
728 fi
729
730 _get_fs_type() (
731 [[ $1 ]] || return
732 if [[ -b $1 ]] && get_fs_env $1; then
733 echo "$(readlink -f $1)|$ID_FS_TYPE"
734 return 1
735 fi
736 if [[ -b /dev/block/$1 ]] && get_fs_env /dev/block/$1; then
737 echo "$(readlink -f /dev/block/$1)|$ID_FS_TYPE"
738 return 1
739 fi
740 if fstype=$(find_dev_fstype $1); then
741 echo "$1|$fstype"
742 return 1
743 fi
744 return 1
745 )
746
747 for dev in "${host_devs[@]}"; do
748 unset fs_type
749 for fstype in $(_get_fs_type $dev) \
750 $(check_block_and_slaves _get_fs_type $(get_maj_min $dev)); do
751 if ! strstr " ${host_fs_types[*]} " " $fstype ";then
752 push host_fs_types "$fstype"
753 fi
754 done
755 done
756
757 [[ -d $udevdir ]] \
758 || udevdir=$(pkg-config udev --variable=udevdir 2>/dev/null)
759 if ! [[ -d "$udevdir" ]]; then
760 [[ -d /lib/udev ]] && udevdir=/lib/udev
761 [[ -d /usr/lib/udev ]] && udevdir=/usr/lib/udev
762 fi
763
764 [[ -d $systemdutildir ]] \
765 || systemdutildir=$(pkg-config systemd --variable=systemdutildir 2>/dev/null)
766 [[ -d $systemdsystemunitdir ]] \
767 || systemdsystemunitdir=$(pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null)
768
769 if ! [[ -d "$systemdutildir" ]]; then
770 [[ -d /lib/systemd ]] && systemdutildir=/lib/systemd
771 [[ -d /usr/lib/systemd ]] && systemdutildir=/usr/lib/systemd
772 fi
773 [[ -d "$systemdsystemunitdir" ]] || systemdsystemunitdir=${systemdutildir}/system
774
775 export initdir dracutbasedir dracutmodules drivers \
776 fw_dir drivers_dir debug no_kernel kernel_only \
777 add_drivers omit_drivers mdadmconf lvmconf filesystems \
778 use_fstab fstab_lines libdirs fscks nofscks \
779 stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
780 debug host_fs_types host_devs sshkey add_fstab \
781 DRACUT_VERSION udevdir systemdutildir systemdsystemunitdir
782
783 # Create some directory structure first
784 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
785
786 [[ -h /lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
787 [[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
788
789 if [[ $prefix ]]; then
790 for d in bin etc lib sbin tmp usr var $libdirs; do
791 strstr "$d" "/" && continue
792 ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
793 done
794 fi
795
796 if [[ $kernel_only != yes ]]; then
797 for d in usr/bin usr/sbin bin etc lib sbin tmp usr var var/log var/run var/lock $libdirs; do
798 [[ -e "${initdir}${prefix}/$d" ]] && continue
799 if [ -L "/$d" ]; then
800 inst_symlink "/$d" "${prefix}/$d"
801 else
802 mkdir -m 0755 -p "${initdir}${prefix}/$d"
803 fi
804 done
805
806 for d in dev proc sys sysroot root run run/lock run/initramfs; do
807 if [ -L "/$d" ]; then
808 inst_symlink "/$d"
809 else
810 mkdir -m 0755 -p "$initdir/$d"
811 fi
812 done
813
814 ln -sfn /run "$initdir/var/run"
815 ln -sfn /run/lock "$initdir/var/lock"
816 else
817 for d in lib "$libdir"; do
818 [[ -e "${initdir}${prefix}/$d" ]] && continue
819 if [ -h "/$d" ]; then
820 inst "/$d" "${prefix}/$d"
821 else
822 mkdir -m 0755 -p "${initdir}${prefix}/$d"
823 fi
824 done
825 fi
826
827 if [[ $kernel_only != yes ]]; then
828 mkdir -p "${initdir}/etc/cmdline.d"
829 for _d in $hookdirs; do
830 mkdir -m 0755 -p ${initdir}/lib/dracut/hooks/$_d
831 done
832 if [[ "$UID" = "0" ]]; then
833 [ -c ${initdir}/dev/null ] || mknod ${initdir}/dev/null c 1 3
834 [ -c ${initdir}/dev/kmsg ] || mknod ${initdir}/dev/kmsg c 1 11
835 [ -c ${initdir}/dev/console ] || mknod ${initdir}/dev/console c 5 1
836 fi
837 fi
838
839 mods_to_load=""
840 # check all our modules to see if they should be sourced.
841 # This builds a list of modules that we will install next.
842 for_each_module_dir check_module
843 for_each_module_dir check_mount
844
845 strstr "$mods_to_load" "fips" && export DRACUT_FIPS_MODE=1
846
847 _isize=0 #initramfs size
848 modules_loaded=" "
849 # source our modules.
850 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
851 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
852 if strstr "$mods_to_load" " $_d_mod "; then
853 [[ $show_modules = yes ]] && echo "$_d_mod" || \
854 dinfo "*** Including module: $_d_mod ***"
855 if [[ $kernel_only = yes ]]; then
856 module_installkernel $_d_mod || {
857 dfatal "installkernel failed in module $_d_mod"
858 exit 1
859 }
860 else
861 module_install $_d_mod
862 if [[ $no_kernel != yes ]]; then
863 module_installkernel $_d_mod || {
864 dfatal "installkernel failed in module $_d_mod"
865 exit 1
866 }
867 fi
868 fi
869 mods_to_load=${mods_to_load// $_d_mod /}
870 modules_loaded+="$_d_mod "
871
872 #print the module install size
873 if [ -n "$printsize" ]; then
874 _isize_new=$(du -sk ${initdir}|cut -f1)
875 _isize_delta=$(($_isize_new - $_isize))
876 echo "$_d_mod install size: ${_isize_delta}k"
877 _isize=$_isize_new
878 fi
879 fi
880 done
881 unset moddir
882
883 for i in $modules_loaded; do
884 mkdir -p $initdir/lib/dracut
885 echo "$i" >> $initdir/lib/dracut/modules.txt
886 done
887
888 dinfo "*** Including modules done ***"
889
890 ## final stuff that has to happen
891 if [[ $no_kernel != yes ]]; then
892 dinfo "*** Installing kernel module dependencies and firmware ***"
893 dracut_kernel_post
894 dinfo "*** Installing kernel module dependencies and firmware done ***"
895 fi
896
897 if [[ $kernel_only != yes ]]; then
898 (( ${#install_items[@]} > 0 )) && dracut_install ${install_items[@]}
899
900 while pop fstab_lines line; do
901 echo "$line 0 0" >> "${initdir}/etc/fstab"
902 done
903
904 for f in $add_fstab; do
905 cat $f >> "${initdir}/etc/fstab"
906 done
907
908 if [[ $DRACUT_RESOLVE_LAZY ]] && [[ $DRACUT_INSTALL ]]; then
909 dinfo "*** Resolving executable dependencies ***"
910 find "$initdir" -type f \
911 '(' -perm -0100 -or -perm -0010 -or -perm -0001 ')' \
912 -not -path '*.ko' -print0 \
913 | xargs -r -0 $DRACUT_INSTALL ${initdir+-D "$initdir"} -R ${DRACUT_FIPS_MODE+-H}
914 dinfo "*** Resolving executable dependencies done***"
915 fi
916 fi
917
918 while pop include_src src && pop include_target tgt; do
919 if [[ $src && $tgt ]]; then
920 if [[ -f $src ]]; then
921 inst $src $tgt
922 else
923 ddebug "Including directory: $src"
924 mkdir -p "${initdir}/${tgt}"
925 # check for preexisting symlinks, so we can cope with the
926 # symlinks to $prefix
927 for i in "$src"/*; do
928 [[ -e "$i" || -h "$i" ]] || continue
929 s=${initdir}/${tgt}/${i#$src/}
930 if [[ -d "$i" ]]; then
931 if ! [[ -e "$s" ]]; then
932 mkdir -m 0755 -p "$s"
933 chmod --reference="$i" "$s"
934 fi
935 cp --reflink=auto --sparse=auto -pfLr -t "$s" "$i"/*
936 else
937 cp --reflink=auto --sparse=auto -pfLr -t "$s" "$i"
938 fi
939 done
940 fi
941 fi
942 done
943
944 if [[ $kernel_only != yes ]]; then
945 # make sure that library links are correct and up to date
946 for f in /etc/ld.so.conf /etc/ld.so.conf.d/*; do
947 [[ -f $f ]] && inst_simple "$f"
948 done
949 if ! ldconfig -r "$initdir"; then
950 if [[ $UID = 0 ]]; then
951 derror "ldconfig exited ungracefully"
952 else
953 derror "ldconfig might need uid=0 (root) for chroot()"
954 fi
955 fi
956 fi
957
958 if (($maxloglvl >= 5)); then
959 ddebug "Listing sizes of included files:"
960 du -c "$initdir" | sort -n | ddebug
961 fi
962
963 # strip binaries
964 if [[ $do_strip = yes ]] ; then
965 for p in strip xargs find; do
966 if ! type -P $p >/dev/null; then
967 derror "Could not find '$p'. You should run $0 with '--nostrip'."
968 do_strip=no
969 fi
970 done
971 fi
972
973 if strstr "$modules_loaded" " fips " && command -v prelink >/dev/null; then
974 dinfo "*** pre-unlinking files ***"
975 for dir in "$initdir/bin" \
976 "$initdir/sbin" \
977 "$initdir/usr/bin" \
978 "$initdir/usr/sbin"; do
979 [[ -L "$dir" ]] && continue
980 for i in "$dir"/*; do
981 [[ -L $i ]] && continue
982 [[ -x $i ]] && prelink -u $i &>/dev/null
983 done
984 done
985 dinfo "*** pre-unlinking files done ***"
986 fi
987
988 if [[ $do_strip = yes ]] ; then
989 dinfo "*** Stripping files ***"
990 find "$initdir" -type f \
991 '(' -perm -0100 -or -perm -0010 -or -perm -0001 \
992 -or -path '*/lib/modules/*.ko' ')' -print0 \
993 | xargs -r -0 strip -g 2>/dev/null
994 dinfo "*** Stripping files done ***"
995 fi
996
997 if [[ $do_hardlink = yes ]] ; then
998 type hardlink &>/dev/null && {
999 dinfo "*** hardlinking files ***"
1000 hardlink "$initdir" 2>&1
1001 dinfo "*** hardlinking files done ***"
1002 }
1003 fi
1004
1005 dinfo "*** Creating image file ***"
1006 if ! ( cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet| \
1007 $compress > "$outfile"; ); then
1008 dfatal "dracut: creation of $outfile failed"
1009 exit 1
1010 fi
1011 dinfo "*** Creating image file done ***"
1012
1013 dinfo "Wrote $outfile:"
1014 dinfo "$(ls -l "$outfile")"
1015
1016 exit 0