]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut.sh
Merge pull request #30 from vtolstov/master
[thirdparty/dracut.git] / dracut.sh
1 #!/bin/bash
2 #
3 # Generator script for a dracut initramfs
4 # Tries to retain some degree of compatibility with the command line
5 # of the various mkinitrd implementations out there
6 #
7
8 # Copyright 2005-2013 Red Hat, Inc. All rights reserved.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #
23
24 # store for logging
25 dracut_args=( "$@" )
26
27 set -o pipefail
28
29 usage() {
30 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
31 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
32 . $dracutbasedir/dracut-version.sh
33 fi
34
35 # 80x25 linebreak here ^
36 cat << EOF
37 Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
38
39 Version: $DRACUT_VERSION
40
41 Creates initial ramdisk images for preloading modules
42
43 -h, --help Display all options
44
45 If a [LIST] has multiple arguments, then you have to put these in quotes.
46
47 For example:
48
49 # dracut --add-drivers "module1 module2" ...
50
51 EOF
52 }
53
54 long_usage() {
55 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
56 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
57 . $dracutbasedir/dracut-version.sh
58 fi
59
60 # 80x25 linebreak here ^
61 cat << EOF
62 Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
63
64 Version: $DRACUT_VERSION
65
66 Creates initial ramdisk images for preloading modules
67
68 --kver [VERSION] Set kernel version to [VERSION].
69 -f, --force Overwrite existing initramfs file.
70 -a, --add [LIST] Add a space-separated list of dracut modules.
71 --rebuild Append arguments to those of existing image and rebuild
72 -m, --modules [LIST] Specify a space-separated list of dracut modules to
73 call when building the initramfs. Modules are located
74 in /usr/lib/dracut/modules.d.
75 -o, --omit [LIST] Omit a space-separated list of dracut modules.
76 --force-add [LIST] Force to add a space-separated list of dracut modules
77 to the default set of modules, when -H is specified.
78 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
79 exclusively include in the initramfs.
80 --add-drivers [LIST] Specify a space-separated list of kernel
81 modules to add to the initramfs.
82 --force-drivers [LIST] Specify a space-separated list of kernel
83 modules to add to the initramfs and make sure they
84 are tried to be loaded via modprobe same as passing
85 rd.driver.pre=DRIVER kernel parameter.
86 --omit-drivers [LIST] Specify a space-separated list of kernel
87 modules not to add to the initramfs.
88 --filesystems [LIST] Specify a space-separated list of kernel filesystem
89 modules to exclusively include in the generic
90 initramfs.
91 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
92 modules
93 --fwdir [DIR] Specify additional directories, where to look for
94 firmwares, separated by :
95 --kernel-only Only install kernel drivers and firmware files
96 --no-kernel Do not install kernel drivers and firmware files
97 --print-cmdline Print the kernel command line for the given disk layout
98 --early-microcode Combine early microcode with ramdisk
99 --no-early-microcode Do not combine early microcode with ramdisk
100 --kernel-cmdline [PARAMETERS] Specify default kernel command line parameters
101 --strip Strip binaries in the initramfs
102 --nostrip Do not strip binaries in the initramfs
103 --prelink Prelink binaries in the initramfs
104 --noprelink Do not prelink binaries in the initramfs
105 --hardlink Hardlink files in the initramfs
106 --nohardlink Do not hardlink files in the initramfs
107 --prefix [DIR] Prefix initramfs files with [DIR]
108 --noprefix Do not prefix initramfs files
109 --mdadmconf Include local /etc/mdadm.conf
110 --nomdadmconf Do not include local /etc/mdadm.conf
111 --lvmconf Include local /etc/lvm/lvm.conf
112 --nolvmconf Do not include local /etc/lvm/lvm.conf
113 --fscks [LIST] Add a space-separated list of fsck helpers.
114 --nofscks Inhibit installation of any fsck helpers.
115 --ro-mnt Mount / and /usr read-only by default.
116 -h, --help This message
117 --debug Output debug information of the build process
118 --profile Output profile information of the build process
119 -L, --stdlog [0-6] Specify logging level (to standard error)
120 0 - suppress any messages
121 1 - only fatal errors
122 2 - all errors
123 3 - warnings
124 4 - info
125 5 - debug info (here starts lots of output)
126 6 - trace info (and even more)
127 -v, --verbose Increase verbosity level
128 -q, --quiet Decrease verbosity level
129 -c, --conf [FILE] Specify configuration file to use.
130 Default: /etc/dracut.conf
131 --confdir [DIR] Specify configuration directory to use *.conf files
132 from. Default: /etc/dracut.conf.d
133 --tmpdir [DIR] Temporary directory to be used instead of default
134 /var/tmp.
135 -l, --local Local mode. Use modules from the current working
136 directory instead of the system-wide installed in
137 /usr/lib/dracut/modules.d.
138 Useful when running dracut from a git checkout.
139 -H, --hostonly Host-Only mode: Install only what is needed for
140 booting the local host instead of a generic host.
141 -N, --no-hostonly Disables Host-Only mode
142 --hostonly-cmdline Store kernel command line arguments needed
143 in the initramfs
144 --no-hostonly-cmdline Do not store kernel command line arguments needed
145 in the initramfs
146 --persistent-policy [POLICY]
147 Use [POLICY] to address disks and partitions.
148 POLICY can be any directory name found in /dev/disk.
149 E.g. "by-uuid", "by-label"
150 --fstab Use /etc/fstab to determine the root device.
151 --add-fstab [FILE] Add file to the initramfs fstab
152 --mount "[DEV] [MP] [FSTYPE] [FSOPTS]"
153 Mount device [DEV] on mountpoint [MP] with filesystem
154 [FSTYPE] and options [FSOPTS] in the initramfs
155 --add-device "[DEV]" Bring up [DEV] in initramfs
156 -i, --include [SOURCE] [TARGET]
157 Include the files in the SOURCE directory into the
158 Target directory in the final initramfs.
159 If SOURCE is a file, it will be installed to TARGET
160 in the final initramfs.
161 -I, --install [LIST] Install the space separated list of files into the
162 initramfs.
163 --install-optional [LIST] Install the space separated list of files into the
164 initramfs, if they exist.
165 --gzip Compress the generated initramfs using gzip.
166 This will be done by default, unless another
167 compression option or --no-compress is passed.
168 --bzip2 Compress the generated initramfs using bzip2.
169 Make sure your kernel has bzip2 decompression support
170 compiled in, otherwise you will not be able to boot.
171 --lzma Compress the generated initramfs using lzma.
172 Make sure your kernel has lzma support compiled in,
173 otherwise you will not be able to boot.
174 --xz Compress the generated initramfs using xz.
175 Make sure that your kernel has xz support compiled
176 in, otherwise you will not be able to boot.
177 --lzo Compress the generated initramfs using lzop.
178 Make sure that your kernel has lzo support compiled
179 in, otherwise you will not be able to boot.
180 --lz4 Compress the generated initramfs using lz4.
181 Make sure that your kernel has lz4 support compiled
182 in, otherwise you will not be able to boot.
183 --compress [COMPRESSION] Compress the generated initramfs with the
184 passed compression program. Make sure your kernel
185 knows how to decompress the generated initramfs,
186 otherwise you will not be able to boot.
187 --no-compress Do not compress the generated initramfs. This will
188 override any other compression options.
189 --list-modules List all available dracut modules.
190 -M, --show-modules Print included module's name to standard output during
191 build.
192 --keep Keep the temporary initramfs for debugging purposes
193 --printsize Print out the module install size
194 --sshkey [SSHKEY] Add ssh key to initramfs (use with ssh-client module)
195 --logfile [FILE] Logfile to use (overrides configuration setting)
196 --reproducible Create reproducible images
197 --loginstall [DIR] Log all files installed from the host to [DIR]
198
199 If [LIST] has multiple arguments, then you have to put these in quotes.
200
201 For example:
202
203 # dracut --add-drivers "module1 module2" ...
204
205 EOF
206 }
207
208 # function push()
209 # push values to a stack
210 # $1 = stack variable
211 # $2.. values
212 # example:
213 # push stack 1 2 "3 4"
214 push() {
215 local _i
216 local __stack=$1; shift
217 for _i in "$@"; do
218 eval ${__stack}'[${#'${__stack}'[@]}]="$_i"'
219 done
220 }
221
222 # function pop()
223 # pops the last value from a stack
224 # assigns value to second argument variable
225 # or echo to stdout, if no second argument
226 # $1 = stack variable
227 # $2 = optional variable to store the value
228 # example:
229 # pop stack val
230 # val=$(pop stack)
231 pop() {
232 local __stack=$1; shift
233 local __resultvar=$1
234 local _value;
235 # check for empty stack
236 eval '[[ ${#'${__stack}'[@]} -eq 0 ]] && return 1'
237
238 eval _value='${'${__stack}'[${#'${__stack}'[@]}-1]}'
239
240 if [[ "$__resultvar" ]]; then
241 eval $__resultvar="'$_value'"
242 else
243 printf "%s" "$_value"
244 fi
245 eval unset ${__stack}'[${#'${__stack}'[@]}-1]'
246 return 0
247 }
248
249 # Little helper function for reading args from the commandline.
250 # it automatically handles -a b and -a=b variants, and returns 1 if
251 # we need to shift $3.
252 read_arg() {
253 # $1 = arg name
254 # $2 = arg value
255 # $3 = arg parameter
256 local rematch='^[^=]*=(.*)$'
257 if [[ $2 =~ $rematch ]]; then
258 read "$1" <<< "${BASH_REMATCH[1]}"
259 else
260 read "$1" <<< "$3"
261 # There is no way to shift our callers args, so
262 # return 1 to indicate they should do it instead.
263 return 1
264 fi
265 }
266
267 dropindirs_sort()
268 {
269 local suffix=$1; shift
270 local -a files
271 local f d
272
273 for d in "$@"; do
274 for i in "$d/"*"$suffix"; do
275 if [[ -e "$i" ]]; then
276 printf "%s\n" "${i##*/}"
277 fi
278 done
279 done | sort -Vu | {
280 readarray -t files
281
282 for f in "${files[@]}"; do
283 for d in "$@"; do
284 if [[ -e "$d/$f" ]]; then
285 printf "%s\n" "$d/$f"
286 continue 2
287 fi
288 done
289 done
290 }
291 }
292
293 rearrange_params()
294 {
295 # Workaround -i, --include taking 2 arguments
296 set -- "${@/--include/++include}"
297
298 # This prevents any long argument ending with "-i"
299 # -i, like --opt-i but I think we can just prevent that
300 set -- "${@/%-i/++include}"
301
302 TEMP=$(unset POSIXLY_CORRECT; getopt \
303 -o "a:m:o:d:I:k:c:L:fvqlHhMN" \
304 --long kver: \
305 --long add: \
306 --long force-add: \
307 --long add-drivers: \
308 --long force-drivers: \
309 --long omit-drivers: \
310 --long modules: \
311 --long omit: \
312 --long drivers: \
313 --long filesystems: \
314 --long install: \
315 --long install-optional: \
316 --long fwdir: \
317 --long libdirs: \
318 --long fscks: \
319 --long add-fstab: \
320 --long mount: \
321 --long device: \
322 --long add-device: \
323 --long nofscks: \
324 --long ro-mnt \
325 --long kmoddir: \
326 --long conf: \
327 --long confdir: \
328 --long tmpdir: \
329 --long stdlog: \
330 --long compress: \
331 --long prefix: \
332 --long rebuild: \
333 --long force \
334 --long kernel-only \
335 --long no-kernel \
336 --long print-cmdline \
337 --long kernel-cmdline: \
338 --long strip \
339 --long nostrip \
340 --long prelink \
341 --long noprelink \
342 --long hardlink \
343 --long nohardlink \
344 --long noprefix \
345 --long mdadmconf \
346 --long nomdadmconf \
347 --long lvmconf \
348 --long nolvmconf \
349 --long debug \
350 --long profile \
351 --long sshkey: \
352 --long logfile: \
353 --long verbose \
354 --long quiet \
355 --long local \
356 --long hostonly \
357 --long host-only \
358 --long no-hostonly \
359 --long no-host-only \
360 --long hostonly-cmdline \
361 --long no-hostonly-cmdline \
362 --long persistent-policy: \
363 --long fstab \
364 --long help \
365 --long bzip2 \
366 --long lzma \
367 --long xz \
368 --long lzo \
369 --long lz4 \
370 --long no-compress \
371 --long gzip \
372 --long list-modules \
373 --long show-modules \
374 --long keep \
375 --long printsize \
376 --long regenerate-all \
377 --long noimageifnotneeded \
378 --long early-microcode \
379 --long no-early-microcode \
380 --long reproducible \
381 --long loginstall: \
382 -- "$@")
383
384 if (( $? != 0 )); then
385 usage
386 exit 1
387 fi
388 }
389
390 verbosity_mod_l=0
391 unset kernel
392 unset outfile
393
394 rearrange_params "$@"
395 eval set -- "$TEMP"
396
397 # parse command line args to check if '--rebuild' option is present
398 unset append_args_l
399 unset rebuild_file
400 while :
401 do
402 if [ "$1" == "--" ]; then
403 shift; break
404 fi
405 if [ "$1" == "--rebuild" ]; then
406 append_args_l="yes"
407 rebuild_file=$2
408 if [ ! -e $rebuild_file ]; then
409 echo "Image file '$rebuild_file', for rebuild, does not exist!"
410 exit 1
411 fi
412 abs_rebuild_file=$(readlink -f "$rebuild_file") && rebuild_file="$abs_rebuild_file"
413 shift; continue
414 fi
415 shift
416 done
417
418 # get output file name and kernel version from command line arguments
419 while (($# > 0)); do
420 case ${1%%=*} in
421 ++include)
422 shift 2;;
423 *)
424 if ! [[ ${outfile+x} ]]; then
425 outfile=$1
426 elif ! [[ ${kernel+x} ]]; then
427 kernel=$1
428 else
429 printf "\nUnknown arguments: %s\n\n" "$*" >&2
430 usage; exit 1;
431 fi
432 ;;
433 esac
434 shift
435 done
436
437 # extract input image file provided with rebuild option to get previous parameters, if any
438 if [[ $append_args_l == "yes" ]]; then
439 unset rebuild_param
440
441 # determine resultant file
442 if ! [[ $outfile ]]; then
443 outfile=$rebuild_file
444 fi
445
446 if ! rebuild_param=$(lsinitrd $rebuild_file '*lib/dracut/build-parameter.txt'); then
447 echo "Image '$rebuild_file' has no rebuild information stored"
448 exit 1
449 fi
450
451 # prepend previous parameters to current command line args
452 if [[ $rebuild_param ]]; then
453 TEMP="$rebuild_param $TEMP"
454 eval set -- "$TEMP"
455 rearrange_params "$@"
456 fi
457
458 # clean the temporarily used scratch-pad directory
459 rm -rf $scratch_dir
460 fi
461
462 unset PARMS_TO_STORE
463 PARMS_TO_STORE=""
464
465 eval set -- "$TEMP"
466
467 while :; do
468 if [ $1 != "--" ] && [ $1 != "--rebuild" ]; then
469 PARMS_TO_STORE+=" $1";
470 fi
471 case $1 in
472 --kver) kernel="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
473 -a|--add) push add_dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
474 --force-add) push force_add_dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
475 --add-drivers) push add_drivers_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
476 --force-drivers) push force_drivers_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
477 --omit-drivers) push omit_drivers_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
478 -m|--modules) push dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
479 -o|--omit) push omit_dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
480 -d|--drivers) push drivers_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
481 --filesystems) push filesystems_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
482 -I|--install) push install_items_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
483 --install-optional)
484 push install_optional_items_l \
485 "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
486 --fwdir) push fw_dir_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
487 --libdirs) push libdirs_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
488 --fscks) push fscks_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
489 --add-fstab) push add_fstab_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
490 --mount) push fstab_lines "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
491 --add-device|--device)
492 push add_device_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
493 --kernel-cmdline) push kernel_cmdline_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
494 --nofscks) nofscks_l="yes";;
495 --ro-mnt) ro_mnt_l="yes";;
496 -k|--kmoddir) drivers_dir_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
497 -c|--conf) conffile="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
498 --confdir) confdir="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
499 --tmpdir) tmpdir_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
500 -L|--stdlog) stdloglvl_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
501 --compress) compress_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
502 --prefix) prefix_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
503 --loginstall) loginstall_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
504 --rebuild) if [ $rebuild_file == $outfile ]; then
505 force=yes
506 fi
507 shift
508 ;;
509 -f|--force) force=yes;;
510 --kernel-only) kernel_only="yes"; no_kernel="no";;
511 --no-kernel) kernel_only="no"; no_kernel="yes";;
512 --print-cmdline)
513 print_cmdline="yes"; hostonly_l="yes"; kernel_only="yes"; no_kernel="yes";;
514 --early-microcode)
515 early_microcode_l="yes";;
516 --no-early-microcode)
517 early_microcode_l="no";;
518 --strip) do_strip_l="yes";;
519 --nostrip) do_strip_l="no";;
520 --prelink) do_prelink_l="yes";;
521 --noprelink) do_prelink_l="no";;
522 --hardlink) do_hardlink_l="yes";;
523 --nohardlink) do_hardlink_l="no";;
524 --noprefix) prefix_l="/";;
525 --mdadmconf) mdadmconf_l="yes";;
526 --nomdadmconf) mdadmconf_l="no";;
527 --lvmconf) lvmconf_l="yes";;
528 --nolvmconf) lvmconf_l="no";;
529 --debug) debug="yes";;
530 --profile) profile="yes";;
531 --sshkey) sshkey="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
532 --logfile) logfile_l="$2"; shift;;
533 -v|--verbose) ((verbosity_mod_l++));;
534 -q|--quiet) ((verbosity_mod_l--));;
535 -l|--local)
536 allowlocal="yes"
537 [[ -f "$(readlink -f "${0%/*}")/dracut-functions.sh" ]] \
538 && dracutbasedir="$(readlink -f "${0%/*}")"
539 ;;
540 -H|--hostonly|--host-only)
541 hostonly_l="yes" ;;
542 -N|--no-hostonly|--no-host-only)
543 hostonly_l="no" ;;
544 --hostonly-cmdline)
545 hostonly_cmdline_l="yes" ;;
546 --no-hostonly-cmdline)
547 hostonly_cmdline_l="no" ;;
548 --persistent-policy)
549 persistent_policy_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
550 --fstab) use_fstab_l="yes" ;;
551 -h|--help) long_usage; exit 1 ;;
552 -i|--include) push include_src "$2"; PARMS_TO_STORE+=" '$2'";
553 shift;;
554 --bzip2) compress_l="bzip2";;
555 --lzma) compress_l="lzma";;
556 --xz) compress_l="xz";;
557 --lzo) compress_l="lzo";;
558 --lz4) compress_l="lz4";;
559 --no-compress) _no_compress_l="cat";;
560 --gzip) compress_l="gzip";;
561 --list-modules) do_list="yes";;
562 -M|--show-modules)
563 show_modules_l="yes"
564 ;;
565 --keep) keep="yes";;
566 --printsize) printsize="yes";;
567 --regenerate-all) regenerate_all="yes";;
568 --noimageifnotneeded) noimageifnotneeded="yes";;
569 --reproducible) reproducible_l="yes";;
570 --) shift; break;;
571
572 *) # should not even reach this point
573 printf "\n!Unknown option: '%s'\n\n" "$1" >&2; usage; exit 1;;
574 esac
575 shift
576 done
577
578 # getopt cannot handle multiple arguments, so just handle "-I,--include"
579 # the old fashioned way
580
581 while (($# > 0)); do
582 if [ "${1%%=*}" == "++include" ]; then
583 push include_src "$2"
584 push include_target "$3"
585 PARMS_TO_STORE+=" --include '$2' '$3'"
586 shift 2
587 fi
588 shift
589 done
590
591 if [[ $regenerate_all == "yes" ]]; then
592 ret=0
593 if [[ $kernel ]]; then
594 printf -- "--regenerate-all cannot be called with a kernel version\n" >&2
595 exit 1
596 fi
597
598 if [[ $outfile ]]; then
599 printf -- "--regenerate-all cannot be called with a image file\n" >&2
600 exit 1
601 fi
602
603 ((len=${#dracut_args[@]}))
604 for ((i=0; i < len; i++)); do
605 [[ ${dracut_args[$i]} == "--regenerate-all" ]] && \
606 unset dracut_args[$i]
607 done
608
609 cd /lib/modules
610 for i in *; do
611 [[ -f $i/modules.dep ]] || [[ -f $i/modules.dep.bin ]] || continue
612 dracut --kver="$i" "${dracut_args[@]}"
613 ((ret+=$?))
614 done
615 exit $ret
616 fi
617
618 if ! [[ $kernel ]]; then
619 kernel=$(uname -r)
620 fi
621
622 if [[ $kernel ]]; then
623 if ! [[ -d /lib/modules/$kernel ]] && [[ $no_kernel != yes ]]; then
624 printf -- "Kernel version $kernel has no module directory /lib/modules/$kernel\n" >&2
625 fi
626 fi
627
628 if ! [[ $outfile ]]; then
629 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
630
631 if [[ $MACHINE_ID ]] && ( [[ -d /boot/${MACHINE_ID} ]] || [[ -L /boot/${MACHINE_ID} ]] ); then
632 outfile="/boot/${MACHINE_ID}/$kernel/initrd"
633 else
634 outfile="/boot/initramfs-$kernel.img"
635 fi
636 fi
637
638 unset LC_MESSAGES
639 unset LC_CTYPE
640 export LC_ALL=C
641 export LANG=C
642 unset LD_LIBRARY_PATH
643 unset LD_PRELOAD
644 unset GREP_OPTIONS
645
646 export DRACUT_LOG_LEVEL=warning
647 [[ $debug ]] && {
648 export DRACUT_LOG_LEVEL=debug
649 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
650 set -x
651 }
652
653 [[ $profile ]] && {
654 export PS4='+ $(date "+%s.%N") ${BASH_SOURCE}@${LINENO}: ';
655 set -x
656 debug=yes
657 }
658
659 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
660
661 # if we were not passed a config file, try the default one
662 if [[ ! -f $conffile ]]; then
663 if [[ $allowlocal ]]; then
664 conffile="$dracutbasedir/dracut.conf"
665 else
666 conffile="/etc/dracut.conf"
667 fi
668 fi
669
670 if [[ ! -d $confdir ]]; then
671 if [[ $allowlocal ]]; then
672 confdir="$dracutbasedir/dracut.conf.d"
673 else
674 confdir="/etc/dracut.conf.d"
675 fi
676 fi
677
678 # source our config file
679 [[ -f $conffile ]] && . "$conffile"
680
681 # source our config dir
682 for f in $(dropindirs_sort ".conf" "$confdir" "$dracutbasedir/dracut.conf.d"); do
683 [[ -e $f ]] && . "$f"
684 done
685
686 DRACUT_PATH=${DRACUT_PATH:-/sbin /bin /usr/sbin /usr/bin}
687
688 for i in $DRACUT_PATH; do
689 rl=$i
690 if [ -L "$i" ]; then
691 rl=$(readlink -f $i)
692 fi
693 if [[ "$NPATH" != *:$rl* ]] ; then
694 NPATH+=":$rl"
695 fi
696 done
697 export PATH="${NPATH#:}"
698 unset NPATH
699
700 # these optins add to the stuff in the config file
701 if (( ${#add_dracutmodules_l[@]} )); then
702 while pop add_dracutmodules_l val; do
703 add_dracutmodules+=" $val "
704 done
705 fi
706
707 if (( ${#force_add_dracutmodules_l[@]} )); then
708 while pop force_add_dracutmodules_l val; do
709 force_add_dracutmodules+=" $val "
710 done
711 fi
712
713 if (( ${#fscks_l[@]} )); then
714 while pop fscks_l val; do
715 fscks+=" $val "
716 done
717 fi
718
719 if (( ${#add_fstab_l[@]} )); then
720 while pop add_fstab_l val; do
721 add_fstab+=" $val "
722 done
723 fi
724
725 if (( ${#fstab_lines_l[@]} )); then
726 while pop fstab_lines_l val; do
727 push fstab_lines $val
728 done
729 fi
730
731 if (( ${#install_items_l[@]} )); then
732 while pop install_items_l val; do
733 install_items+=" $val "
734 done
735 fi
736
737 if (( ${#install_optional_items_l[@]} )); then
738 while pop install_optional_items_l val; do
739 install_optional_items+=" $val "
740 done
741 fi
742
743 # these options override the stuff in the config file
744 if (( ${#dracutmodules_l[@]} )); then
745 dracutmodules=''
746 while pop dracutmodules_l val; do
747 dracutmodules+="$val "
748 done
749 fi
750
751 if (( ${#omit_dracutmodules_l[@]} )); then
752 omit_dracutmodules=''
753 while pop omit_dracutmodules_l val; do
754 omit_dracutmodules+="$val "
755 done
756 fi
757
758 if (( ${#filesystems_l[@]} )); then
759 filesystems=''
760 while pop filesystems_l val; do
761 filesystems+="$val "
762 done
763 fi
764
765 if (( ${#fw_dir_l[@]} )); then
766 fw_dir=''
767 while pop fw_dir_l val; do
768 fw_dir+="$val "
769 done
770 fi
771
772 if (( ${#libdirs_l[@]} )); then
773 libdirs=''
774 while pop libdirs_l val; do
775 libdirs+="$val "
776 done
777 fi
778
779 [[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
780 [[ ! $stdloglvl ]] && stdloglvl=4
781 stdloglvl=$((stdloglvl + verbosity_mod_l))
782 ((stdloglvl > 6)) && stdloglvl=6
783 ((stdloglvl < 0)) && stdloglvl=0
784
785 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
786 [[ $do_strip_l ]] && do_strip=$do_strip_l
787 [[ $do_strip ]] || do_strip=yes
788 [[ $do_prelink_l ]] && do_prelink=$do_prelink_l
789 [[ $do_prelink ]] || do_prelink=yes
790 [[ $do_hardlink_l ]] && do_hardlink=$do_hardlink_l
791 [[ $do_hardlink ]] || do_hardlink=yes
792 [[ $prefix_l ]] && prefix=$prefix_l
793 [[ $prefix = "/" ]] && unset prefix
794 [[ $hostonly_l ]] && hostonly=$hostonly_l
795 [[ $hostonly_cmdline_l ]] && hostonly_cmdline=$hostonly_cmdline_l
796 [[ "$hostonly" == "yes" ]] && ! [[ $hostonly_cmdline ]] && hostonly_cmdline="yes"
797 [[ $persistent_policy_l ]] && persistent_policy=$persistent_policy_l
798 [[ $use_fstab_l ]] && use_fstab=$use_fstab_l
799 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
800 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
801 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
802 [[ $fw_dir ]] || fw_dir="/lib/firmware/updates /lib/firmware"
803 [[ $tmpdir_l ]] && tmpdir="$tmpdir_l"
804 [[ $tmpdir ]] || tmpdir=/var/tmp
805 [[ $INITRD_COMPRESS ]] && compress=$INITRD_COMPRESS
806 [[ $compress_l ]] && compress=$compress_l
807 [[ $show_modules_l ]] && show_modules=$show_modules_l
808 [[ $nofscks_l ]] && nofscks="yes"
809 [[ $ro_mnt_l ]] && ro_mnt="yes"
810 [[ $early_microcode_l ]] && early_microcode=$early_microcode_l
811 [[ $early_microcode ]] || early_microcode=no
812 [[ $logfile_l ]] && logfile="$logfile_l"
813 [[ $reproducible_l ]] && reproducible="$reproducible_l"
814 [[ $loginstall_l ]] && loginstall="$loginstall_l"
815
816 # eliminate IFS hackery when messing with fw_dir
817 fw_dir=${fw_dir//:/ }
818
819 # handle compression options.
820 [[ $compress ]] || compress="gzip"
821 case $compress in
822 bzip2) compress="bzip2 -9";;
823 lzma) compress="lzma -9 -T0";;
824 xz) compress="xz --check=crc32 --lzma2=dict=1MiB -T0";;
825 gzip) compress="gzip -n -9"; command -v gzip --help 2>&1 | grep -q rsyncable && compress="gzip -n -9 --rsyncable"; command -v pigz > /dev/null 2>&1 && compress="pigz -9 -n -T -R";;
826 lzo) compress="lzop -9";;
827 lz4) compress="lz4 -l -9";;
828 esac
829 if [[ $_no_compress_l = "cat" ]]; then
830 compress="cat"
831 fi
832
833 [[ $hostonly = yes ]] && hostonly="-h"
834 [[ $hostonly != "-h" ]] && unset hostonly
835
836 [[ $reproducible == yes ]] && DRACUT_REPRODUCIBLE=1
837
838 readonly TMPDIR="$tmpdir"
839 readonly initdir="$(mktemp --tmpdir="$TMPDIR/" -d -t initramfs.XXXXXX)"
840 [ -d "$initdir" ] || {
841 printf "%s\n" "dracut: mktemp --tmpdir='$TMPDIR/' -d -t initramfs.XXXXXX failed." >&2
842 exit 1
843 }
844
845 if [[ $early_microcode = yes ]] || ( [[ $acpi_override = yes ]] && [[ -d $acpi_table_dir ]] ); then
846 readonly early_cpio_dir="$(mktemp --tmpdir="$TMPDIR/" -d -t early_cpio.XXXXXX)"
847 [ -d "$early_cpio_dir" ] || {
848 printf "%s\n" "dracut: mktemp --tmpdir='$TMPDIR/' -d -t early_cpio.XXXXXX failed." >&2
849 exit 1
850 }
851 fi
852 # clean up after ourselves no matter how we die.
853 trap '
854 ret=$?;
855 [[ $keep ]] && echo "Not removing $initdir." >&2 || { [[ $initdir ]] && rm -rf -- "$initdir"; };
856 [[ $keep ]] && echo "Not removing $early_cpio_dir." >&2 || { [[ $early_cpio_dir ]] && rm -Rf -- "$early_cpio_dir"; };
857 [[ $_dlogdir ]] && rm -Rf -- "$_dlogdir";
858 exit $ret;
859 ' EXIT
860
861 # clean up after ourselves no matter how we die.
862 trap 'exit 1;' SIGINT
863
864 export DRACUT_KERNEL_LAZY="1"
865 export DRACUT_RESOLVE_LAZY="1"
866
867 if [[ $print_cmdline ]]; then
868 stdloglvl=0
869 sysloglvl=0
870 fileloglvl=0
871 kmsgloglvl=0
872 fi
873
874 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
875 . $dracutbasedir/dracut-version.sh
876 fi
877
878 if [[ -f $dracutbasedir/dracut-functions.sh ]]; then
879 . $dracutbasedir/dracut-functions.sh
880 else
881 printf "%s\n" "dracut: Cannot find $dracutbasedir/dracut-functions.sh." >&2
882 printf "%s\n" "dracut: Are you running from a git checkout?" >&2
883 printf "%s\n" "dracut: Try passing -l as an argument to $0" >&2
884 exit 1
885 fi
886
887 if ! [[ $print_cmdline ]]; then
888 inst /bin/sh
889 if ! $DRACUT_INSTALL ${initdir:+-D "$initdir"} -R "$initdir/bin/sh" &>/dev/null; then
890 unset DRACUT_RESOLVE_LAZY
891 export DRACUT_RESOLVE_DEPS=1
892 fi
893 rm -fr -- ${initdir}/*
894 fi
895
896 # Verify bash version, current minimum is 3.1
897 if (( BASH_VERSINFO[0] < 4 )); then
898 dfatal 'You need at least Bash 4 to use dracut, sorry.'
899 exit 1
900 fi
901
902 dracutfunctions=$dracutbasedir/dracut-functions.sh
903 export dracutfunctions
904
905 if (( ${#drivers_l[@]} )); then
906 drivers=''
907 while pop drivers_l val; do
908 drivers+="$val "
909 done
910 fi
911 drivers=${drivers/-/_}
912
913 if (( ${#add_drivers_l[@]} )); then
914 while pop add_drivers_l val; do
915 add_drivers+=" $val "
916 done
917 fi
918 add_drivers=${add_drivers/-/_}
919
920 if (( ${#force_drivers_l[@]} )); then
921 while pop force_drivers_l val; do
922 force_drivers+=" $val "
923 done
924 fi
925 force_drivers=${force_drivers/-/_}
926
927 if (( ${#omit_drivers_l[@]} )); then
928 while pop omit_drivers_l val; do
929 omit_drivers+=" $val "
930 done
931 fi
932 omit_drivers=${omit_drivers/-/_}
933
934 if (( ${#kernel_cmdline_l[@]} )); then
935 while pop kernel_cmdline_l val; do
936 kernel_cmdline+=" $val "
937 done
938 fi
939
940 omit_drivers_corrected=""
941 for d in $omit_drivers; do
942 [[ " $drivers $add_drivers " == *\ $d\ * ]] && continue
943 [[ " $drivers $force_drivers " == *\ $d\ * ]] && continue
944 omit_drivers_corrected+="$d|"
945 done
946 omit_drivers="${omit_drivers_corrected%|}"
947 unset omit_drivers_corrected
948
949 # prepare args for logging
950 for ((i=0; i < ${#dracut_args[@]}; i++)); do
951 [[ "${dracut_args[$i]}" == *\ * ]] && \
952 dracut_args[$i]="\"${dracut_args[$i]}\""
953 #" keep vim happy
954 done
955
956 dinfo "Executing: $0 ${dracut_args[@]}"
957
958 [[ $do_list = yes ]] && {
959 for mod in $dracutbasedir/modules.d/*; do
960 [[ -d $mod ]] || continue;
961 [[ -e $mod/install || -e $mod/installkernel || \
962 -e $mod/module-setup.sh ]] || continue
963 printf "%s\n" "${mod##*/??}"
964 done
965 exit 0
966 }
967
968 # This is kinda legacy -- eventually it should go away.
969 case $dracutmodules in
970 ""|auto) dracutmodules="all" ;;
971 esac
972
973 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
974
975 if [[ -d $srcmods ]]; then
976 [[ -f $srcmods/modules.dep ]] || {
977 dwarn "$srcmods/modules.dep is missing. Did you run depmod?"
978 }
979 fi
980
981 if [[ ! $print_cmdline ]]; then
982 if [[ -f $outfile && ! $force ]]; then
983 dfatal "Will not override existing initramfs ($outfile) without --force"
984 exit 1
985 fi
986
987 outdir=${outfile%/*}
988 [[ $outdir ]] || outdir="/"
989
990 if [[ ! -d "$outdir" ]]; then
991 dfatal "Can't write to $outdir: Directory $outdir does not exist or is not accessible."
992 exit 1
993 elif [[ ! -w "$outdir" ]]; then
994 dfatal "No permission to write to $outdir."
995 exit 1
996 elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
997 dfatal "No permission to write $outfile."
998 exit 1
999 fi
1000
1001 if [[ $loginstall ]]; then
1002 if ! mkdir -p "$loginstall"; then
1003 dfatal "Could not create directory to log installed files to '$loginstall'."
1004 exit 1
1005 fi
1006 loginstall=$(readlink -f "$loginstall")
1007 fi
1008 fi
1009
1010 if [[ $acpi_override = yes ]] && ! check_kernel_config CONFIG_ACPI_INITRD_TABLE_OVERRIDE; then
1011 dwarn "Disabling ACPI override, because kernel does not support it. CONFIG_ACPI_INITRD_TABLE_OVERRIDE!=y"
1012 unset acpi_override
1013 fi
1014
1015 if [[ $early_microcode = yes ]]; then
1016 if [[ $hostonly ]]; then
1017 [[ $(get_cpu_vendor) == "AMD" ]] \
1018 && ! check_kernel_config CONFIG_MICROCODE_AMD_EARLY \
1019 && unset early_microcode
1020 [[ $(get_cpu_vendor) == "Intel" ]] \
1021 && ! check_kernel_config CONFIG_MICROCODE_INTEL_EARLY \
1022 && unset early_microcode
1023 else
1024 ! check_kernel_config CONFIG_MICROCODE_AMD_EARLY \
1025 && ! check_kernel_config CONFIG_MICROCODE_INTEL_EARLY \
1026 && unset early_microcode
1027 fi
1028 [[ $early_microcode != yes ]] \
1029 && dwarn "Disabling early microcode, because kernel does not support it. CONFIG_MICROCODE_[AMD|INTEL]_EARLY!=y"
1030 fi
1031
1032 # Need to be able to have non-root users read stuff (rpcbind etc)
1033 chmod 755 "$initdir"
1034
1035 if [[ $hostonly ]]; then
1036 for i in /sys /proc /run /dev; do
1037 if ! findmnt --target "$i" &>/dev/null; then
1038 dwarning "Turning off host-only mode: '$i' is not mounted!"
1039 unset hostonly
1040 fi
1041 done
1042 fi
1043
1044 declare -A host_fs_types
1045
1046 for line in "${fstab_lines[@]}"; do
1047 set -- $line
1048 #dev mp fs fsopts
1049 push host_devs "$1"
1050 host_fs_types["$1"]="$3"
1051 done
1052
1053 for f in $add_fstab; do
1054 [[ -e $f ]] || continue
1055 while read dev rest; do
1056 push host_devs "$dev"
1057 done < "$f"
1058 done
1059
1060 for dev in $add_device; do
1061 push host_devs "$dev"
1062 done
1063
1064 if (( ${#add_device_l[@]} )); then
1065 while pop add_device_l val; do
1066 add_device+=" $val "
1067 push host_devs "$val"
1068 done
1069 fi
1070
1071 if [[ $hostonly ]]; then
1072 # in hostonly mode, determine all devices, which have to be accessed
1073 # and examine them for filesystem types
1074
1075 for mp in \
1076 "/" \
1077 "/etc" \
1078 "/bin" \
1079 "/sbin" \
1080 "/lib" \
1081 "/lib64" \
1082 "/usr" \
1083 "/usr/bin" \
1084 "/usr/sbin" \
1085 "/usr/lib" \
1086 "/usr/lib64" \
1087 "/boot" \
1088 "/boot/efi" \
1089 ;
1090 do
1091 mp=$(readlink -f "$mp")
1092 mountpoint "$mp" >/dev/null 2>&1 || continue
1093 _dev=$(find_block_device "$mp")
1094 _bdev=$(readlink -f "/dev/block/$_dev")
1095 [[ -b $_bdev ]] && _dev=$_bdev
1096 push host_devs $_dev
1097 [[ "$mp" == "/" ]] && root_dev="$_dev"
1098 push host_devs "$_dev"
1099 done
1100
1101 if [[ -f /proc/swaps ]] && [[ -f /etc/fstab ]]; then
1102 while read dev type rest; do
1103 [[ -b $dev ]] || continue
1104 [[ "$type" == "partition" ]] || continue
1105
1106 while read _d _m _t _o _r; do
1107 [[ "$_d" == \#* ]] && continue
1108 [[ $_d ]] || continue
1109 [[ $_t != "swap" ]] && continue
1110 [[ $_m != "swap" ]] && [[ $_m != "none" ]] && continue
1111 [[ "$_o" == *noauto* ]] && continue
1112 _d=$(expand_persistent_dev "$_d")
1113 [[ "$_d" -ef "$dev" ]] || continue
1114
1115 if [[ -f /etc/crypttab ]]; then
1116 while read _mapper _a _p _o; do
1117 [[ $_mapper = \#* ]] && continue
1118 [[ "$_d" -ef /dev/mapper/"$_mapper" ]] || continue
1119 [[ "$_o" ]] || _o="$_p"
1120 # skip entries with password files
1121 [[ "$_p" == /* ]] && [[ -f $_p ]] && continue 2
1122 # skip mkswap swap
1123 [[ $_o == *swap* ]] && continue 2
1124 done < /etc/crypttab
1125 fi
1126
1127 push host_devs "$(readlink -f "$dev")"
1128 break
1129 done < /etc/fstab
1130 done < /proc/swaps
1131 fi
1132 # record all host modaliases
1133 declare -A host_modalias
1134 find /sys/devices/ -name uevent -print > "$initdir/.modalias"
1135 while read m; do
1136 while read line; do
1137 [[ "$line" != MODALIAS\=* ]] && continue
1138 modalias="${line##MODALIAS=}" && [[ $modalias ]] && host_modalias["$modalias"]=1
1139 break
1140 done < "$m"
1141 done < "$initdir/.modalias"
1142
1143 rm -f -- "$initdir/.modalias"
1144
1145 while read _k _s _v; do
1146 [ "$_k" != "name" -a "$_k" != "driver" ] && continue
1147 host_modalias["$_v"]=1
1148 done </proc/crypto
1149
1150 # check /proc/modules
1151 declare -A host_modules
1152 while read m rest; do
1153 host_modules["$m"]=1
1154 done </proc/modules
1155 fi
1156
1157 unset m
1158 unset rest
1159
1160 _get_fs_type() {
1161 [[ $1 ]] || return
1162 if [[ -b /dev/block/$1 ]]; then
1163 ID_FS_TYPE=$(get_fs_env "/dev/block/$1") && host_fs_types["$(readlink -f "/dev/block/$1")"]="$ID_FS_TYPE"
1164 return 1
1165 fi
1166 if [[ -b $1 ]]; then
1167 ID_FS_TYPE=$(get_fs_env "$1") && host_fs_types["$(readlink -f "$1")"]="$ID_FS_TYPE"
1168 return 1
1169 fi
1170 if fstype=$(find_dev_fstype "$1"); then
1171 host_fs_types["$1"]="$fstype"
1172 return 1
1173 fi
1174 return 1
1175 }
1176
1177 for dev in ${host_devs[@]}; do
1178 _get_fs_type "$dev"
1179 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
1180 done
1181
1182 for dev in "${!host_fs_types[@]}"; do
1183 [[ ${host_fs_types[$dev]} = "reiserfs" ]] || [[ ${host_fs_types[$dev]} = "xfs" ]] || continue
1184 rootopts=$(find_dev_fsopts "$dev")
1185 if [[ ${host_fs_types[$dev]} = "reiserfs" ]]; then
1186 journaldev=$(fs_get_option $rootopts "jdev")
1187 elif [[ ${host_fs_types[$dev]} = "xfs" ]]; then
1188 journaldev=$(fs_get_option $rootopts "logdev")
1189 fi
1190 if [[ $journaldev ]]; then
1191 dev="$(readlink -f "$dev")"
1192 push host_devs "$dev"
1193 _get_fs_type "$dev"
1194 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
1195 fi
1196 done
1197
1198 [[ -d $udevdir ]] \
1199 || udevdir="$(pkg-config udev --variable=udevdir 2>/dev/null)"
1200 if ! [[ -d "$udevdir" ]]; then
1201 [[ -d /lib/udev ]] && udevdir=/lib/udev
1202 [[ -d /usr/lib/udev ]] && udevdir=/usr/lib/udev
1203 fi
1204
1205 [[ -d $systemdutildir ]] \
1206 || systemdutildir=$(pkg-config systemd --variable=systemdutildir 2>/dev/null)
1207
1208 if ! [[ -d "$systemdutildir" ]]; then
1209 [[ -d /lib/systemd ]] && systemdutildir=/lib/systemd
1210 [[ -d /usr/lib/systemd ]] && systemdutildir=/usr/lib/systemd
1211 fi
1212
1213 [[ -d $systemdsystemunitdir ]] \
1214 || systemdsystemunitdir=$(pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null)
1215
1216 [[ -d "$systemdsystemunitdir" ]] || systemdsystemunitdir=${systemdutildir}/system
1217
1218 [[ -d $systemdsystemconfdir ]] \
1219 || systemdsystemconfdir=$(pkg-config systemd --variable=systemdsystemconfdir 2>/dev/null)
1220
1221 [[ -d "$systemdsystemconfdir" ]] || systemdsystemconfdir=/etc/systemd/system
1222
1223 export initdir dracutbasedir \
1224 dracutmodules force_add_dracutmodules add_dracutmodules omit_dracutmodules \
1225 mods_to_load \
1226 fw_dir drivers_dir debug no_kernel kernel_only \
1227 omit_drivers mdadmconf lvmconf root_dev \
1228 use_fstab fstab_lines libdirs fscks nofscks ro_mnt \
1229 stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
1230 debug host_fs_types host_devs sshkey add_fstab \
1231 DRACUT_VERSION udevdir prefix filesystems drivers \
1232 systemdutildir systemdsystemunitdir systemdsystemconfdir \
1233 host_modalias host_modules hostonly_cmdline loginstall
1234
1235 mods_to_load=""
1236 # check all our modules to see if they should be sourced.
1237 # This builds a list of modules that we will install next.
1238 for_each_module_dir check_module
1239 for_each_module_dir check_mount
1240
1241 dracut_module_included "fips" && export DRACUT_FIPS_MODE=1
1242
1243 if [[ $print_cmdline ]]; then
1244 modules_loaded=" "
1245 # source our modules.
1246 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1247 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
1248 module_cmdline "$_d_mod"
1249 done
1250 unset moddir
1251 printf "\n"
1252 exit 0
1253 fi
1254
1255 # Create some directory structure first
1256 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
1257
1258 [[ -h /lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
1259 [[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
1260
1261 if [[ $prefix ]]; then
1262 for d in bin etc lib sbin tmp usr var $libdirs; do
1263 [[ "$d" == */* ]] && continue
1264 ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
1265 done
1266 fi
1267
1268 if [[ $kernel_only != yes ]]; then
1269 for d in usr/bin usr/sbin bin etc lib sbin tmp usr var $libdirs; do
1270 [[ -e "${initdir}${prefix}/$d" ]] && continue
1271 if [ -L "/$d" ]; then
1272 inst_symlink "/$d" "${prefix}/$d"
1273 else
1274 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1275 fi
1276 done
1277
1278 for d in dev proc sys sysroot root run; do
1279 if [ -L "/$d" ]; then
1280 inst_symlink "/$d"
1281 else
1282 mkdir -m 0755 -p "$initdir/$d"
1283 fi
1284 done
1285
1286 ln -sfn ../run "$initdir/var/run"
1287 ln -sfn ../run/lock "$initdir/var/lock"
1288 ln -sfn ../run/log "$initdir/var/log"
1289 else
1290 for d in lib "$libdir"; do
1291 [[ -e "${initdir}${prefix}/$d" ]] && continue
1292 if [ -h "/$d" ]; then
1293 inst "/$d" "${prefix}/$d"
1294 else
1295 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1296 fi
1297 done
1298 fi
1299
1300 if [[ $kernel_only != yes ]]; then
1301 mkdir -p "${initdir}/etc/cmdline.d"
1302 for _d in $hookdirs; do
1303 mkdir -m 0755 -p ${initdir}/lib/dracut/hooks/$_d
1304 done
1305 if [[ "$UID" = "0" ]]; then
1306 [ -c ${initdir}/dev/null ] || mknod ${initdir}/dev/null c 1 3
1307 [ -c ${initdir}/dev/kmsg ] || mknod ${initdir}/dev/kmsg c 1 11
1308 [ -c ${initdir}/dev/console ] || mknod ${initdir}/dev/console c 5 1
1309 fi
1310 fi
1311
1312 _isize=0 #initramfs size
1313 modules_loaded=" "
1314 # source our modules.
1315 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1316 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
1317 if [[ "$mods_to_load" == *\ $_d_mod\ * ]]; then
1318 if [[ $show_modules = yes ]]; then
1319 printf "%s\n" "$_d_mod"
1320 else
1321 dinfo "*** Including module: $_d_mod ***"
1322 fi
1323 if [[ $kernel_only == yes ]]; then
1324 module_installkernel "$_d_mod" || {
1325 dfatal "installkernel failed in module $_d_mod"
1326 exit 1
1327 }
1328 else
1329 module_install "$_d_mod"
1330 if [[ $no_kernel != yes ]]; then
1331 module_installkernel "$_d_mod" || {
1332 dfatal "installkernel failed in module $_d_mod"
1333 exit 1
1334 }
1335 fi
1336 fi
1337 mods_to_load=${mods_to_load// $_d_mod /}
1338 modules_loaded+="$_d_mod "
1339
1340 #print the module install size
1341 if [ -n "$printsize" ]; then
1342 _isize_new=$(du -sk ${initdir}|cut -f1)
1343 _isize_delta=$((_isize_new - _isize))
1344 printf "%s\n" "$_d_mod install size: ${_isize_delta}k"
1345 _isize=$_isize_new
1346 fi
1347 fi
1348 done
1349 unset moddir
1350
1351 for i in $modules_loaded; do
1352 mkdir -p $initdir/lib/dracut
1353 printf "%s\n" "$i" >> $initdir/lib/dracut/modules.txt
1354 done
1355
1356 dinfo "*** Including modules done ***"
1357
1358 ## final stuff that has to happen
1359 if [[ $no_kernel != yes ]]; then
1360
1361 if [[ $drivers ]]; then
1362 hostonly='' instmods $drivers
1363 fi
1364
1365 if [[ $add_drivers ]]; then
1366 hostonly='' instmods -c $add_drivers
1367 fi
1368 if [[ $force_drivers ]]; then
1369 hostonly='' instmods -c $force_drivers
1370 rm -f $initdir/etc/cmdline.d/20-force_driver.conf
1371 for mod in $force_drivers; do
1372 echo "rd.driver.pre=$mod" >>$initdir/etc/cmdline.d/20-force_drivers.conf
1373 done
1374 fi
1375 if [[ $filesystems ]]; then
1376 hostonly='' instmods -c $filesystems
1377 fi
1378
1379 dinfo "*** Installing kernel module dependencies and firmware ***"
1380 dracut_kernel_post
1381 dinfo "*** Installing kernel module dependencies and firmware done ***"
1382
1383 if [[ $noimageifnotneeded == yes ]] && [[ $hostonly ]]; then
1384 if [[ ! -f "$initdir/lib/dracut/need-initqueue" ]] && \
1385 [[ -f ${initdir}/lib/modules/$kernel/modules.dep && ! -s ${initdir}/lib/modules/$kernel/modules.dep ]]; then
1386 for i in ${initdir}/etc/cmdline.d/*.conf; do
1387 # We need no initramfs image and do not generate one.
1388 [[ $i == "${initdir}/etc/cmdline.d/*.conf" ]] && exit 0
1389 done
1390 fi
1391 fi
1392 fi
1393
1394 if [[ $kernel_only != yes ]]; then
1395 (( ${#install_items[@]} > 0 )) && inst_multiple ${install_items[@]}
1396 (( ${#install_optional_items[@]} > 0 )) && inst_multiple -o ${install_optional_items[@]}
1397
1398 [[ $kernel_cmdline ]] && printf "%s\n" "$kernel_cmdline" >> "${initdir}/etc/cmdline.d/01-default.conf"
1399
1400 while pop fstab_lines line; do
1401 line=($line)
1402 [ -z "${line[3]}" ] && line[3]="defaults"
1403 [ -z "${line[4]}" ] && line[4]="0"
1404 [ -z "${line[5]}" ] && line[5]="2"
1405 echo "${line[@]}" >> "${initdir}/etc/fstab"
1406 done
1407
1408 for f in $add_fstab; do
1409 cat "$f" >> "${initdir}/etc/fstab"
1410 done
1411
1412 if [ -d ${initdir}/$systemdutildir ]; then
1413 mkdir -p ${initdir}/etc/conf.d
1414 {
1415 printf "%s\n" "systemdutildir=\"$systemdutildir\""
1416 printf "%s\n" "systemdsystemunitdir=\"$systemdsystemunitdir\""
1417 printf "%s\n" "systemdsystemconfdir=\"$systemdsystemconfdir\""
1418 } > ${initdir}/etc/conf.d/systemd.conf
1419 fi
1420
1421 if [[ $DRACUT_RESOLVE_LAZY ]] && [[ $DRACUT_INSTALL ]]; then
1422 dinfo "*** Resolving executable dependencies ***"
1423 find "$initdir" -type f \
1424 '(' -perm -0100 -or -perm -0010 -or -perm -0001 ')' \
1425 -not -path '*.ko' -print0 \
1426 | xargs -r -0 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -R ${DRACUT_FIPS_MODE:+-H} --
1427 dinfo "*** Resolving executable dependencies done***"
1428 fi
1429
1430 # libpthread workaround: pthread_cancel wants to dlopen libgcc_s.so
1431 for _dir in $libdirs; do
1432 for _f in "$_dir/libpthread.so"*; do
1433 [[ -e "$_f" ]] || continue
1434 inst_libdir_file "libgcc_s.so*"
1435 break 2
1436 done
1437 done
1438 fi
1439
1440 while pop include_src src && pop include_target tgt; do
1441 if [[ $src && $tgt ]]; then
1442 if [[ -f $src ]]; then
1443 inst $src $tgt
1444 else
1445 ddebug "Including directory: $src"
1446 mkdir -p "${initdir}/${tgt}"
1447 # check for preexisting symlinks, so we can cope with the
1448 # symlinks to $prefix
1449 for i in "$src"/*; do
1450 [[ -e "$i" || -h "$i" ]] || continue
1451 s=${initdir}/${tgt}/${i#$src/}
1452 if [[ -d "$i" ]]; then
1453 if ! [[ -e "$s" ]]; then
1454 mkdir -m 0755 -p "$s"
1455 chmod --reference="$i" "$s"
1456 fi
1457 cp --reflink=auto --sparse=auto -fa -t "$s" "$i"/*
1458 else
1459 cp --reflink=auto --sparse=auto -fa -t "$s" "$i"
1460 fi
1461 done
1462 fi
1463 fi
1464 done
1465
1466 if [[ $kernel_only != yes ]]; then
1467 # make sure that library links are correct and up to date
1468 for f in /etc/ld.so.conf /etc/ld.so.conf.d/*; do
1469 [[ -f $f ]] && inst_simple "$f"
1470 done
1471 if ! ldconfig -r "$initdir"; then
1472 if [[ $UID = 0 ]]; then
1473 derror "ldconfig exited ungracefully"
1474 else
1475 derror "ldconfig might need uid=0 (root) for chroot()"
1476 fi
1477 fi
1478 fi
1479
1480 PRELINK_BIN="$(command -v prelink)"
1481 if [[ $UID = 0 ]] && [[ $PRELINK_BIN ]]; then
1482 if [[ $DRACUT_FIPS_MODE ]]; then
1483 dinfo "*** Installing prelink files ***"
1484 inst_multiple -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf /etc/prelink.cache
1485 elif [[ $do_prelink == yes ]]; then
1486 dinfo "*** Pre-linking files ***"
1487 inst_multiple -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf
1488 chroot "$initdir" "$PRELINK_BIN" -a
1489 rm -f -- "$initdir/$PRELINK_BIN"
1490 rm -fr -- "$initdir"/etc/prelink.*
1491 dinfo "*** Pre-linking files done ***"
1492 fi
1493 fi
1494
1495 if [[ $do_hardlink = yes ]] && command -v hardlink >/dev/null; then
1496 dinfo "*** Hardlinking files ***"
1497 hardlink "$initdir" 2>&1
1498 dinfo "*** Hardlinking files done ***"
1499 fi
1500
1501 # strip binaries
1502 if [[ $do_strip = yes ]] ; then
1503 for p in strip xargs find; do
1504 if ! type -P $p >/dev/null; then
1505 dinfo "Could not find '$p'. Not stripping the initramfs."
1506 do_strip=no
1507 fi
1508 done
1509 fi
1510
1511 if [[ $do_strip = yes ]] && ! [[ $DRACUT_FIPS_MODE ]]; then
1512 dinfo "*** Stripping files ***"
1513 find "$initdir" -type f \
1514 -executable -not -path '*/lib/modules/*.ko' -print0 \
1515 | xargs -r -0 strip -g 2>/dev/null
1516
1517 # strip kernel modules, but do not touch signed modules
1518 find "$initdir" -type f -path '*/lib/modules/*.ko' -print0 \
1519 | while read -r -d $'\0' f; do
1520 SIG=$(tail -c 28 "$f")
1521 [[ $SIG == '~Module signature appended~' ]] || { printf "%s\000" "$f"; }
1522 done | xargs -r -0 strip -g
1523
1524 dinfo "*** Stripping files done ***"
1525 fi
1526 if [[ $early_microcode = yes ]]; then
1527 dinfo "*** Generating early-microcode cpio image ***"
1528 ucode_dir=(amd-ucode intel-ucode)
1529 ucode_dest=(AuthenticAMD.bin GenuineIntel.bin)
1530 _dest_dir="$early_cpio_dir/d/kernel/x86/microcode"
1531 _dest_idx="0 1"
1532 mkdir -p $_dest_dir
1533 if [[ $hostonly ]]; then
1534 [[ $(get_cpu_vendor) == "AMD" ]] && _dest_idx="0"
1535 [[ $(get_cpu_vendor) == "Intel" ]] && _dest_idx="1"
1536 fi
1537 for idx in $_dest_idx; do
1538 _fw=${ucode_dir[$idx]}
1539 for _fwdir in $fw_dir; do
1540 if [[ -d $_fwdir && -d $_fwdir/$_fw ]]; then
1541 _src="*"
1542 dinfo "*** Constructing ${ucode_dest[$idx]} ****"
1543 if [[ $hostonly ]]; then
1544 _src=$(get_ucode_file)
1545 if ! [[ -r $_fwdir/$_fw/$_src ]];then
1546 break;
1547 fi
1548 fi
1549 cat $_fwdir/$_fw/$_src > $_dest_dir/${ucode_dest[$idx]}
1550 create_early_cpio="yes"
1551 fi
1552 done
1553 done
1554 fi
1555
1556 if [[ $acpi_override = yes ]] && [[ -d $acpi_table_dir ]]; then
1557 dinfo "*** Packaging ACPI tables to override BIOS provided ones ***"
1558 _dest_dir="$early_cpio_dir/d/kernel/firmware/acpi"
1559 mkdir -p $_dest_dir
1560 for table in $acpi_table_dir/*.aml; do
1561 dinfo " Adding ACPI table: $table"
1562 cp -a $table $_dest_dir
1563 create_early_cpio="yes"
1564 done
1565 fi
1566
1567 dinfo "*** Store current command line parameters ***"
1568 if ! ( echo $PARMS_TO_STORE > $initdir/lib/dracut/build-parameter.txt ); then
1569 dfatal "Could not store the current command line parameters"
1570 exit 1
1571 fi
1572
1573 rm -f -- "$outfile"
1574 dinfo "*** Creating image file ***"
1575
1576 if [[ $DRACUT_REPRODUCIBLE ]]; then
1577 find "$initdir" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
1578 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
1579
1580 [[ "$(cpio --help)" == *--reproducible* ]] && CPIO_REPRODUCIBLE=1
1581 fi
1582
1583 [[ "$UID" != 0 ]] && cpio_owner_root="-R 0:0"
1584
1585 if [[ $create_early_cpio = yes ]]; then
1586 echo 1 > "$early_cpio_dir/d/early_cpio"
1587
1588 if [[ $DRACUT_REPRODUCIBLE ]]; then
1589 find "$early_cpio_dir/d" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
1590 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
1591 fi
1592
1593 # The microcode blob is _before_ the initramfs blob, not after
1594 (
1595 cd "$early_cpio_dir/d"
1596 find . -print0 | sort -z \
1597 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null $cpio_owner_root -H newc -o --quiet > $outfile
1598 )
1599 fi
1600
1601 if ! (
1602 umask 077; cd "$initdir"
1603 find . -print0 | sort -z \
1604 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null $cpio_owner_root -H newc -o --quiet \
1605 | $compress >> "$outfile"
1606 ); then
1607 dfatal "dracut: creation of $outfile failed"
1608 exit 1
1609 fi
1610 dinfo "*** Creating image file done ***"
1611
1612 if (( maxloglvl >= 5 )); then
1613 if [[ $allowlocal ]]; then
1614 "$dracutbasedir/lsinitrd.sh" "$outfile"| ddebug
1615 else
1616 lsinitrd "$outfile"| ddebug
1617 fi
1618 fi
1619
1620 exit 0