]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut.sh
dracut.sh: Adjust squash and strip order
[thirdparty/dracut.git] / dracut.sh
1 #!/bin/bash -p
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
26 unset BASH_ENV
27
28 # Verify bash version, current minimum is 4
29 if (( BASH_VERSINFO[0] < 4 )); then
30 printf -- 'You need at least Bash 4 to use dracut, sorry.' >&2
31 exit 1
32 fi
33
34 dracut_args=( "$@" )
35 readonly dracut_cmd="$(readlink -f $0)"
36
37 set -o pipefail
38
39 usage() {
40 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
41 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
42 . $dracutbasedir/dracut-version.sh
43 fi
44
45 # 80x25 linebreak here ^
46 cat << EOF
47 Usage: $dracut_cmd [OPTION]... [<initramfs> [<kernel-version>]]
48
49 Version: $DRACUT_VERSION
50
51 Creates initial ramdisk images for preloading modules
52
53 -h, --help Display all options
54
55 If a [LIST] has multiple arguments, then you have to put these in quotes.
56
57 For example:
58
59 # dracut --add-drivers "module1 module2" ...
60
61 EOF
62 }
63
64 long_usage() {
65 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
66 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
67 . $dracutbasedir/dracut-version.sh
68 fi
69
70 # 80x25 linebreak here ^
71 cat << EOF
72 Usage: $dracut_cmd [OPTION]... [<initramfs> [<kernel-version>]]
73
74 Version: $DRACUT_VERSION
75
76 Creates initial ramdisk images for preloading modules
77
78 --kver [VERSION] Set kernel version to [VERSION].
79 -f, --force Overwrite existing initramfs file.
80 -a, --add [LIST] Add a space-separated list of dracut modules.
81 --rebuild Append arguments to those of existing image and rebuild
82 -m, --modules [LIST] Specify a space-separated list of dracut modules to
83 call when building the initramfs. Modules are located
84 in /usr/lib/dracut/modules.d.
85 -o, --omit [LIST] Omit a space-separated list of dracut modules.
86 --force-add [LIST] Force to add a space-separated list of dracut modules
87 to the default set of modules, when -H is specified.
88 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
89 exclusively include in the initramfs.
90 --add-drivers [LIST] Specify a space-separated list of kernel
91 modules to add to the initramfs.
92 --force-drivers [LIST] Specify a space-separated list of kernel
93 modules to add to the initramfs and make sure they
94 are tried to be loaded via modprobe same as passing
95 rd.driver.pre=DRIVER kernel parameter.
96 --omit-drivers [LIST] Specify a space-separated list of kernel
97 modules not to add to the initramfs.
98 --filesystems [LIST] Specify a space-separated list of kernel filesystem
99 modules to exclusively include in the generic
100 initramfs.
101 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
102 modules
103 --fwdir [DIR] Specify additional directories, where to look for
104 firmwares, separated by :
105 --kernel-only Only install kernel drivers and firmware files
106 --no-kernel Do not install kernel drivers and firmware files
107 --print-cmdline Print the kernel command line for the given disk layout
108 --early-microcode Combine early microcode with ramdisk
109 --no-early-microcode Do not combine early microcode with ramdisk
110 --kernel-cmdline [PARAMETERS] Specify default kernel command line parameters
111 --strip Strip binaries in the initramfs
112 --nostrip Do not strip binaries in the initramfs
113 --hardlink Hardlink files in the initramfs
114 --nohardlink Do not hardlink files in the initramfs
115 --prefix [DIR] Prefix initramfs files with [DIR]
116 --noprefix Do not prefix initramfs files
117 --mdadmconf Include local /etc/mdadm.conf
118 --nomdadmconf Do not include local /etc/mdadm.conf
119 --lvmconf Include local /etc/lvm/lvm.conf
120 --nolvmconf Do not include local /etc/lvm/lvm.conf
121 --fscks [LIST] Add a space-separated list of fsck helpers.
122 --nofscks Inhibit installation of any fsck helpers.
123 --ro-mnt Mount / and /usr read-only by default.
124 -h, --help This message
125 --debug Output debug information of the build process
126 --profile Output profile information of the build process
127 -L, --stdlog [0-6] Specify logging level (to standard error)
128 0 - suppress any messages
129 1 - only fatal errors
130 2 - all errors
131 3 - warnings
132 4 - info
133 5 - debug info (here starts lots of output)
134 6 - trace info (and even more)
135 -v, --verbose Increase verbosity level
136 -q, --quiet Decrease verbosity level
137 -c, --conf [FILE] Specify configuration file to use.
138 Default: /etc/dracut.conf
139 --confdir [DIR] Specify configuration directory to use *.conf files
140 from. Default: /etc/dracut.conf.d
141 --tmpdir [DIR] Temporary directory to be used instead of default
142 /var/tmp.
143 -l, --local Local mode. Use modules from the current working
144 directory instead of the system-wide installed in
145 /usr/lib/dracut/modules.d.
146 Useful when running dracut from a git checkout.
147 -H, --hostonly Host-Only mode: Install only what is needed for
148 booting the local host instead of a generic host.
149 -N, --no-hostonly Disables Host-Only mode
150 --hostonly-mode <mode>
151 Specify the hostonly mode to use. <mode> could be
152 one of "sloppy" or "strict". "sloppy" mode is used
153 by default.
154 In "sloppy" hostonly mode, extra drivers and modules
155 will be installed, so minor hardware change won't make
156 the image unbootable (eg. changed keyboard), and the
157 image is still portable among similar hosts.
158 With "strict" mode enabled, anything not necessary
159 for booting the local host in its current state will
160 not be included, and modules may do some extra job
161 to save more space. Minor change of hardware or
162 environment could make the image unbootable.
163 DO NOT use "strict" mode unless you know what you
164 are doing.
165 --hostonly-cmdline Store kernel command line arguments needed
166 in the initramfs
167 --no-hostonly-cmdline Do not store kernel command line arguments needed
168 in the initramfs
169 --no-hostonly-default-device
170 Do not generate implicit host devices like root,
171 swap, fstab, etc. Use "--mount" or "--add-device"
172 to explicitly add devices as needed.
173 --hostonly-i18n Install only needed keyboard and font files according
174 to the host configuration (default).
175 --no-hostonly-i18n Install all keyboard and font files available.
176 --persistent-policy [POLICY]
177 Use [POLICY] to address disks and partitions.
178 POLICY can be any directory name found in /dev/disk.
179 E.g. "by-uuid", "by-label"
180 --fstab Use /etc/fstab to determine the root device.
181 --add-fstab [FILE] Add file to the initramfs fstab
182 --mount "[DEV] [MP] [FSTYPE] [FSOPTS]"
183 Mount device [DEV] on mountpoint [MP] with filesystem
184 [FSTYPE] and options [FSOPTS] in the initramfs
185 --mount "[MP]" Same as above, but [DEV], [FSTYPE] and [FSOPTS] are
186 determined by looking at the current mounts.
187 --add-device "[DEV]" Bring up [DEV] in initramfs
188 -i, --include [SOURCE] [TARGET]
189 Include the files in the SOURCE directory into the
190 Target directory in the final initramfs.
191 If SOURCE is a file, it will be installed to TARGET
192 in the final initramfs.
193 -I, --install [LIST] Install the space separated list of files into the
194 initramfs.
195 --install-optional [LIST] Install the space separated list of files into the
196 initramfs, if they exist.
197 --gzip Compress the generated initramfs using gzip.
198 This will be done by default, unless another
199 compression option or --no-compress is passed.
200 --bzip2 Compress the generated initramfs using bzip2.
201 Make sure your kernel has bzip2 decompression support
202 compiled in, otherwise you will not be able to boot.
203 --lzma Compress the generated initramfs using lzma.
204 Make sure your kernel has lzma support compiled in,
205 otherwise you will not be able to boot.
206 --xz Compress the generated initramfs using xz.
207 Make sure that your kernel has xz support compiled
208 in, otherwise you will not be able to boot.
209 --lzo Compress the generated initramfs using lzop.
210 Make sure that your kernel has lzo support compiled
211 in, otherwise you will not be able to boot.
212 --lz4 Compress the generated initramfs using lz4.
213 Make sure that your kernel has lz4 support compiled
214 in, otherwise you will not be able to boot.
215 --zstd Compress the generated initramfs using Zstandard.
216 Make sure that your kernel has zstd support compiled
217 in, otherwise you will not be able to boot.
218 --compress [COMPRESSION] Compress the generated initramfs with the
219 passed compression program. Make sure your kernel
220 knows how to decompress the generated initramfs,
221 otherwise you will not be able to boot.
222 --no-compress Do not compress the generated initramfs. This will
223 override any other compression options.
224 --list-modules List all available dracut modules.
225 -M, --show-modules Print included module's name to standard output during
226 build.
227 --keep Keep the temporary initramfs for debugging purposes
228 --printsize Print out the module install size
229 --sshkey [SSHKEY] Add ssh key to initramfs (use with ssh-client module)
230 --logfile [FILE] Logfile to use (overrides configuration setting)
231 --reproducible Create reproducible images
232 --no-reproducible Do not create reproducible images
233 --loginstall [DIR] Log all files installed from the host to [DIR]
234 --uefi Create an UEFI executable with the kernel cmdline and
235 kernel combined
236 --uefi-stub [FILE] Use the UEFI stub [FILE] to create an UEFI executable
237 --kernel-image [FILE] location of the kernel image
238
239 If [LIST] has multiple arguments, then you have to put these in quotes.
240
241 For example:
242
243 # dracut --add-drivers "module1 module2" ...
244
245 EOF
246 }
247
248 # Fills up host_devs stack variable and makes sure there are no duplicates
249 push_host_devs() {
250 local _dev
251 for _dev in "$@"; do
252 [[ " ${host_devs[@]} " == *" $_dev "* ]] && return
253 host_devs+=( "$_dev" )
254 done
255 }
256
257 # Little helper function for reading args from the commandline.
258 # it automatically handles -a b and -a=b variants, and returns 1 if
259 # we need to shift $3.
260 read_arg() {
261 # $1 = arg name
262 # $2 = arg value
263 # $3 = arg parameter
264 local rematch='^[^=]*=(.*)$'
265 if [[ $2 =~ $rematch ]]; then
266 read "$1" <<< "${BASH_REMATCH[1]}"
267 else
268 read "$1" <<< "$3"
269 # There is no way to shift our callers args, so
270 # return 1 to indicate they should do it instead.
271 return 1
272 fi
273 }
274
275 dropindirs_sort()
276 {
277 local suffix=$1; shift
278 local -a files
279 local f d
280
281 for d in "$@"; do
282 for i in "$d/"*"$suffix"; do
283 if [[ -e "$i" ]]; then
284 printf "%s\n" "${i##*/}"
285 fi
286 done
287 done | sort -Vu | {
288 readarray -t files
289
290 for f in "${files[@]}"; do
291 for d in "$@"; do
292 if [[ -e "$d/$f" ]]; then
293 printf "%s\n" "$d/$f"
294 continue 2
295 fi
296 done
297 done
298 }
299 }
300
301 rearrange_params()
302 {
303 # Workaround -i, --include taking 2 arguments
304 newat=()
305 for i in "$@"; do
306 if [[ $i == "-i" ]] || [[ $i == "--include" ]]; then
307 newat+=("++include") # Replace --include by ++include
308 else
309 newat+=("$i")
310 fi
311 done
312 set -- "${newat[@]}" # Set new $@
313
314 TEMP=$(unset POSIXLY_CORRECT; getopt \
315 -o "a:m:o:d:I:k:c:L:fvqlHhMN" \
316 --long kver: \
317 --long add: \
318 --long force-add: \
319 --long add-drivers: \
320 --long force-drivers: \
321 --long omit-drivers: \
322 --long modules: \
323 --long omit: \
324 --long drivers: \
325 --long filesystems: \
326 --long install: \
327 --long install-optional: \
328 --long fwdir: \
329 --long libdirs: \
330 --long fscks: \
331 --long add-fstab: \
332 --long mount: \
333 --long device: \
334 --long add-device: \
335 --long nofscks \
336 --long ro-mnt \
337 --long kmoddir: \
338 --long conf: \
339 --long confdir: \
340 --long tmpdir: \
341 --long stdlog: \
342 --long compress: \
343 --long prefix: \
344 --long rebuild: \
345 --long force \
346 --long kernel-only \
347 --long no-kernel \
348 --long print-cmdline \
349 --long kernel-cmdline: \
350 --long strip \
351 --long nostrip \
352 --long hardlink \
353 --long nohardlink \
354 --long noprefix \
355 --long mdadmconf \
356 --long nomdadmconf \
357 --long lvmconf \
358 --long nolvmconf \
359 --long debug \
360 --long profile \
361 --long sshkey: \
362 --long logfile: \
363 --long verbose \
364 --long quiet \
365 --long local \
366 --long hostonly \
367 --long host-only \
368 --long no-hostonly \
369 --long no-host-only \
370 --long hostonly-mode: \
371 --long hostonly-cmdline \
372 --long no-hostonly-cmdline \
373 --long no-hostonly-default-device \
374 --long persistent-policy: \
375 --long fstab \
376 --long help \
377 --long bzip2 \
378 --long lzma \
379 --long xz \
380 --long lzo \
381 --long lz4 \
382 --long zstd \
383 --long no-compress \
384 --long gzip \
385 --long list-modules \
386 --long show-modules \
387 --long keep \
388 --long printsize \
389 --long regenerate-all \
390 --long noimageifnotneeded \
391 --long early-microcode \
392 --long no-early-microcode \
393 --long reproducible \
394 --long no-reproducible \
395 --long loginstall: \
396 --long uefi \
397 --long uefi-stub: \
398 --long kernel-image: \
399 --long no-hostonly-i18n \
400 --long hostonly-i18n \
401 --long no-machineid \
402 -- "$@")
403
404 if (( $? != 0 )); then
405 usage
406 exit 1
407 fi
408 }
409
410 verbosity_mod_l=0
411 unset kernel
412 unset outfile
413
414 rearrange_params "$@"
415 eval set -- "$TEMP"
416
417 # parse command line args to check if '--rebuild' option is present
418 unset append_args_l
419 unset rebuild_file
420 while :
421 do
422 if [ "$1" == "--" ]; then
423 shift; break
424 fi
425 if [ "$1" == "--rebuild" ]; then
426 append_args_l="yes"
427 rebuild_file=$2
428 if [ ! -e $rebuild_file ]; then
429 echo "Image file '$rebuild_file', for rebuild, does not exist!"
430 exit 1
431 fi
432 abs_rebuild_file=$(readlink -f "$rebuild_file") && rebuild_file="$abs_rebuild_file"
433 shift; continue
434 fi
435 shift
436 done
437
438 # get output file name and kernel version from command line arguments
439 while (($# > 0)); do
440 case ${1%%=*} in
441 ++include)
442 shift 2;;
443 *)
444 if ! [[ ${outfile+x} ]]; then
445 outfile=$1
446 elif ! [[ ${kernel+x} ]]; then
447 kernel=$1
448 else
449 printf "\nUnknown arguments: %s\n\n" "$*" >&2
450 usage; exit 1;
451 fi
452 ;;
453 esac
454 shift
455 done
456
457 # extract input image file provided with rebuild option to get previous parameters, if any
458 if [[ $append_args_l == "yes" ]]; then
459 unset rebuild_param
460
461 # determine resultant file
462 if ! [[ $outfile ]]; then
463 outfile=$rebuild_file
464 fi
465
466 if ! rebuild_param=$(lsinitrd $rebuild_file '*lib/dracut/build-parameter.txt'); then
467 echo "Image '$rebuild_file' has no rebuild information stored"
468 exit 1
469 fi
470
471 # prepend previous parameters to current command line args
472 if [[ $rebuild_param ]]; then
473 TEMP="$rebuild_param $TEMP"
474 eval set -- "$TEMP"
475 rearrange_params "$@"
476 fi
477 fi
478
479 unset PARMS_TO_STORE
480 PARMS_TO_STORE=""
481
482 eval set -- "$TEMP"
483
484 while :; do
485 if [ $1 != "--" ] && [ $1 != "--rebuild" ]; then
486 PARMS_TO_STORE+=" $1";
487 fi
488 case $1 in
489 --kver) kernel="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
490 -a|--add) add_dracutmodules_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
491 --force-add) force_add_dracutmodules_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
492 --add-drivers) add_drivers_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
493 --force-drivers) force_drivers_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
494 --omit-drivers) omit_drivers_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
495 -m|--modules) dracutmodules_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
496 -o|--omit) omit_dracutmodules_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
497 -d|--drivers) drivers_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
498 --filesystems) filesystems_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
499 -I|--install) install_items_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
500 --install-optional) install_optional_items_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
501 --fwdir) fw_dir_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
502 --libdirs) libdirs_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
503 --fscks) fscks_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
504 --add-fstab) add_fstab_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
505 --mount) fstab_lines+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
506 --add-device|--device) add_device_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
507 --kernel-cmdline) kernel_cmdline_l+=("$2"); PARMS_TO_STORE+=" '$2'"; shift;;
508 --nofscks) nofscks_l="yes";;
509 --ro-mnt) ro_mnt_l="yes";;
510 -k|--kmoddir) drivers_dir_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
511 -c|--conf) conffile="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
512 --confdir) confdir="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
513 --tmpdir) tmpdir_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
514 -L|--stdlog) stdloglvl_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
515 --compress) compress_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
516 --prefix) prefix_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
517 --loginstall) loginstall_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
518 --rebuild) if [ $rebuild_file == $outfile ]; then
519 force=yes
520 fi
521 shift
522 ;;
523 -f|--force) force=yes;;
524 --kernel-only) kernel_only="yes"; no_kernel="no";;
525 --no-kernel) kernel_only="no"; no_kernel="yes";;
526 --print-cmdline)
527 print_cmdline="yes"; hostonly_l="yes"; kernel_only="yes"; no_kernel="yes";;
528 --early-microcode)
529 early_microcode_l="yes";;
530 --no-early-microcode)
531 early_microcode_l="no";;
532 --strip) do_strip_l="yes";;
533 --nostrip) do_strip_l="no";;
534 --hardlink) do_hardlink_l="yes";;
535 --nohardlink) do_hardlink_l="no";;
536 --noprefix) prefix_l="/";;
537 --mdadmconf) mdadmconf_l="yes";;
538 --nomdadmconf) mdadmconf_l="no";;
539 --lvmconf) lvmconf_l="yes";;
540 --nolvmconf) lvmconf_l="no";;
541 --debug) debug="yes";;
542 --profile) profile="yes";;
543 --sshkey) sshkey="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
544 --logfile) logfile_l="$2"; shift;;
545 -v|--verbose) ((verbosity_mod_l++));;
546 -q|--quiet) ((verbosity_mod_l--));;
547 -l|--local)
548 allowlocal="yes"
549 [[ -f "$(readlink -f "${0%/*}")/dracut-init.sh" ]] \
550 && dracutbasedir="$(readlink -f "${0%/*}")"
551 ;;
552 -H|--hostonly|--host-only)
553 hostonly_l="yes" ;;
554 -N|--no-hostonly|--no-host-only)
555 hostonly_l="no" ;;
556 --hostonly-mode)
557 hostonly_mode_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
558 --hostonly-cmdline)
559 hostonly_cmdline_l="yes" ;;
560 --hostonly-i18n)
561 i18n_install_all_l="no" ;;
562 --no-hostonly-i18n)
563 i18n_install_all_l="yes" ;;
564 --no-hostonly-cmdline)
565 hostonly_cmdline_l="no" ;;
566 --no-hostonly-default-device)
567 hostonly_default_device="no" ;;
568 --persistent-policy)
569 persistent_policy_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
570 --fstab) use_fstab_l="yes" ;;
571 -h|--help) long_usage; exit 1 ;;
572 -i|--include) include_src+=("$2"); PARMS_TO_STORE+=" '$2'";
573 shift;;
574 --bzip2) compress_l="bzip2";;
575 --lzma) compress_l="lzma";;
576 --xz) compress_l="xz";;
577 --lzo) compress_l="lzo";;
578 --lz4) compress_l="lz4";;
579 --zstd) compress_l="zstd";;
580 --no-compress) _no_compress_l="cat";;
581 --gzip) compress_l="gzip";;
582 --list-modules) do_list="yes";;
583 -M|--show-modules)
584 show_modules_l="yes"
585 ;;
586 --keep) keep="yes";;
587 --printsize) printsize="yes";;
588 --regenerate-all) regenerate_all="yes";;
589 --noimageifnotneeded) noimageifnotneeded="yes";;
590 --reproducible) reproducible_l="yes";;
591 --no-reproducible) reproducible_l="no";;
592 --uefi) uefi="yes";;
593 --uefi-stub)
594 uefi_stub_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
595 --kernel-image)
596 kernel_image_l="$2"; PARMS_TO_STORE+=" '$2'"; shift;;
597 --no-machineid)
598 machine_id_l="no";;
599 --) shift; break;;
600
601 *) # should not even reach this point
602 printf "\n!Unknown option: '%s'\n\n" "$1" >&2; usage; exit 1;;
603 esac
604 shift
605 done
606
607 # getopt cannot handle multiple arguments, so just handle "-I,--include"
608 # the old fashioned way
609
610 while (($# > 0)); do
611 if [ "${1%%=*}" == "++include" ]; then
612 include_src+=("$2")
613 include_target+=("$3")
614 PARMS_TO_STORE+=" --include '$2' '$3'"
615 shift 2
616 fi
617 shift
618 done
619
620 if [[ $regenerate_all == "yes" ]]; then
621 ret=0
622 if [[ $kernel ]]; then
623 printf -- "--regenerate-all cannot be called with a kernel version\n" >&2
624 exit 1
625 fi
626
627 if [[ $outfile ]]; then
628 printf -- "--regenerate-all cannot be called with a image file\n" >&2
629 exit 1
630 fi
631
632 ((len=${#dracut_args[@]}))
633 for ((i=0; i < len; i++)); do
634 [[ ${dracut_args[$i]} == "--regenerate-all" ]] && \
635 unset dracut_args[$i]
636 done
637
638 cd /lib/modules
639 for i in *; do
640 [[ -f $i/modules.dep ]] || [[ -f $i/modules.dep.bin ]] || continue
641 "$dracut_cmd" --kver="$i" "${dracut_args[@]}"
642 ((ret+=$?))
643 done
644 exit $ret
645 fi
646
647 if ! [[ $kernel ]]; then
648 kernel=$(uname -r)
649 fi
650
651 export LC_ALL=C
652 export LANG=C
653 unset LC_MESSAGES
654 unset LC_CTYPE
655 unset LD_LIBRARY_PATH
656 unset LD_PRELOAD
657 unset GREP_OPTIONS
658
659 export DRACUT_LOG_LEVEL=warning
660 [[ $debug ]] && {
661 export DRACUT_LOG_LEVEL=debug
662 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
663 set -x
664 }
665
666 [[ $profile ]] && {
667 export PS4='+ $(date "+%s.%N") ${BASH_SOURCE}@${LINENO}: ';
668 set -x
669 debug=yes
670 }
671
672 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
673
674 # if we were not passed a config file, try the default one
675 if [[ ! -f $conffile ]]; then
676 if [[ $allowlocal ]]; then
677 conffile="$dracutbasedir/dracut.conf"
678 else
679 conffile="/etc/dracut.conf"
680 fi
681 fi
682
683 if [[ ! -d $confdir ]]; then
684 if [[ $allowlocal ]]; then
685 confdir="$dracutbasedir/dracut.conf.d"
686 else
687 confdir="/etc/dracut.conf.d"
688 fi
689 fi
690
691 # source our config file
692 [[ -f $conffile ]] && . "$conffile"
693
694 # source our config dir
695 for f in $(dropindirs_sort ".conf" "$confdir" "$dracutbasedir/dracut.conf.d"); do
696 [[ -e $f ]] && . "$f"
697 done
698
699 DRACUT_PATH=${DRACUT_PATH:-/sbin /bin /usr/sbin /usr/bin}
700
701 for i in $DRACUT_PATH; do
702 rl=$i
703 if [ -L "$i" ]; then
704 rl=$(readlink -f $i)
705 fi
706 if [[ "$NPATH" != *:$rl* ]] ; then
707 NPATH+=":$rl"
708 fi
709 done
710 export PATH="${NPATH#:}"
711 unset NPATH
712
713 # these options add to the stuff in the config file
714 (( ${#add_dracutmodules_l[@]} )) && add_dracutmodules+=" ${add_dracutmodules_l[@]} "
715 (( ${#force_add_dracutmodules_l[@]} )) && force_add_dracutmodules+=" ${force_add_dracutmodules_l[@]} "
716 (( ${#fscks_l[@]} )) && fscks+=" ${fscks_l[@]} "
717 (( ${#add_fstab_l[@]} )) && add_fstab+=" ${add_fstab_l[@]} "
718 (( ${#fstab_lines_l[@]} )) && fstab_lines+=( "${fstab_lines_l[@]}" )
719 (( ${#install_items_l[@]} )) && install_items+=" ${install_items_l[@]} "
720 (( ${#install_optional_items_l[@]} )) && install_optional_items+=" ${install_optional_items_l[@]} "
721
722 # these options override the stuff in the config file
723 (( ${#dracutmodules_l[@]} )) && dracutmodules="${dracutmodules_l[@]}"
724 (( ${#omit_dracutmodules_l[@]} )) && omit_dracutmodules="${omit_dracutmodules_l[@]}"
725 (( ${#filesystems_l[@]} )) && filesystems="${filesystems_l[@]}"
726 (( ${#fw_dir_l[@]} )) && fw_dir="${fw_dir_l[@]}"
727 (( ${#libdirs_l[@]} ))&& libdirs="${libdirs_l[@]}"
728
729 [[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
730 [[ ! $stdloglvl ]] && stdloglvl=4
731 stdloglvl=$((stdloglvl + verbosity_mod_l))
732 ((stdloglvl > 6)) && stdloglvl=6
733 ((stdloglvl < 0)) && stdloglvl=0
734
735 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
736 [[ $do_strip_l ]] && do_strip=$do_strip_l
737 [[ $do_strip ]] || do_strip=yes
738 [[ $do_hardlink_l ]] && do_hardlink=$do_hardlink_l
739 [[ $do_hardlink ]] || do_hardlink=yes
740 [[ $prefix_l ]] && prefix=$prefix_l
741 [[ $prefix = "/" ]] && unset prefix
742 [[ $hostonly_l ]] && hostonly=$hostonly_l
743 [[ $hostonly_cmdline_l ]] && hostonly_cmdline=$hostonly_cmdline_l
744 [[ $hostonly_mode_l ]] && hostonly_mode=$hostonly_mode_l
745 [[ "$hostonly" == "yes" ]] && ! [[ $hostonly_cmdline ]] && hostonly_cmdline="yes"
746 [[ $i18n_install_all_l ]] && i18n_install_all=$i18n_install_all_l
747 [[ $persistent_policy_l ]] && persistent_policy=$persistent_policy_l
748 [[ $use_fstab_l ]] && use_fstab=$use_fstab_l
749 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
750 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
751 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
752 [[ $fw_dir ]] || fw_dir="/lib/firmware/updates:/lib/firmware:/lib/firmware/$kernel"
753 [[ $tmpdir_l ]] && tmpdir="$tmpdir_l"
754 [[ $tmpdir ]] || tmpdir=/var/tmp
755 [[ $INITRD_COMPRESS ]] && compress=$INITRD_COMPRESS
756 [[ $compress_l ]] && compress=$compress_l
757 [[ $show_modules_l ]] && show_modules=$show_modules_l
758 [[ $nofscks_l ]] && nofscks="yes"
759 [[ $ro_mnt_l ]] && ro_mnt="yes"
760 [[ $early_microcode_l ]] && early_microcode=$early_microcode_l
761 [[ $early_microcode ]] || early_microcode=yes
762 [[ $logfile_l ]] && logfile="$logfile_l"
763 [[ $reproducible_l ]] && reproducible="$reproducible_l"
764 [[ $loginstall_l ]] && loginstall="$loginstall_l"
765 [[ $uefi_stub_l ]] && uefi_stub="$uefi_stub_l"
766 [[ $kernel_image_l ]] && kernel_image="$kernel_image_l"
767 [[ $machine_id_l ]] && machine_id="$machine_id_l"
768
769 if ! [[ $outfile ]]; then
770 if [[ $machine_id != "no" ]]; then
771 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
772 fi
773
774 if [[ $uefi == "yes" ]]; then
775 if [[ -n "$uefi_secureboot_key" && -z "$uefi_secureboot_cert" ]] || [[ -z $uefi_secureboot_key && -n $uefi_secureboot_cert ]]; then
776 dfatal "Need 'uefi_secureboot_key' and 'uefi_secureboot_cert' both to be set."
777 exit 1
778 fi
779
780 if [[ -n "$uefi_secureboot_key" && -n "$uefi_secureboot_cert" ]] && !command -v sbsign &>/dev/null; then
781 dfatal "Need 'sbsign' to create a signed UEFI executable"
782 exit 1
783 fi
784
785 BUILD_ID=$(cat /etc/os-release /usr/lib/os-release \
786 | while read -r line || [[ $line ]]; do \
787 [[ $line =~ BUILD_ID\=* ]] && eval "$line" && echo "$BUILD_ID" && break; \
788 done)
789 if [[ -d /efi ]] && mountpoint -q /efi; then
790 efidir=/efi/EFI
791 else
792 efidir=/boot/EFI
793 if [[ -d /boot/efi/EFI ]] && mountpoint -q /boot/efi; then
794 efidir=/boot/efi/EFI
795 fi
796 fi
797 mkdir -p "$efidir/Linux"
798 outfile="$efidir/Linux/linux-$kernel${MACHINE_ID:+-${MACHINE_ID}}${BUILD_ID:+-${BUILD_ID}}.efi"
799 else
800 if [[ -e "/boot/vmlinuz-$kernel" ]]; then
801 outfile="/boot/initramfs-$kernel.img"
802 elif [[ $MACHINE_ID ]] && ( [[ -d /boot/${MACHINE_ID} ]] || [[ -L /boot/${MACHINE_ID} ]] ); then
803 outfile="/boot/${MACHINE_ID}/$kernel/initrd"
804 else
805 outfile="/boot/initramfs-$kernel.img"
806 fi
807 fi
808 fi
809
810 # eliminate IFS hackery when messing with fw_dir
811 export DRACUT_FIRMWARE_PATH=${fw_dir// /:}
812 fw_dir=${fw_dir//:/ }
813
814 # check for logfile and try to create one if it doesn't exist
815 if [[ -n "$logfile" ]];then
816 if [[ ! -f "$logfile" ]];then
817 touch "$logfile"
818 if [ ! $? -eq 0 ] ;then
819 printf "%s\n" "dracut: touch $logfile failed." >&2
820 fi
821 fi
822 fi
823
824 # handle compression options.
825 if [[ $_no_compress_l = "cat" ]]; then
826 compress="cat"
827 fi
828
829 if ! [[ $compress ]]; then
830 # check all known compressors, if none specified
831 for i in pigz gzip lz4 lzop zstd lzma xz lbzip2 bzip2 cat; do
832 command -v "$i" &>/dev/null || continue
833 compress="$i"
834 break
835 done
836 if [[ $compress = cat ]]; then
837 printf "%s\n" "dracut: no compression tool available. Initramfs image is going to be big." >&2
838 fi
839 fi
840
841 # choose the right arguments for the compressor
842 case $compress in
843 bzip2|lbzip2)
844 if [[ "$compress" = lbzip2 ]] || command -v lbzip2 &>/dev/null; then
845 compress="lbzip2 -9"
846 else
847 compress="bzip2 -9"
848 fi
849 ;;
850 lzma)
851 compress="lzma -9 -T0"
852 ;;
853 xz)
854 compress="xz --check=crc32 --lzma2=dict=1MiB -T0"
855 ;;
856 gzip|pigz)
857 if [[ "$compress" = pigz ]] || command -v pigz &>/dev/null; then
858 compress="pigz -9 -n -T -R"
859 elif command -v gzip &>/dev/null && gzip --help 2>&1 | grep -q rsyncable; then
860 compress="gzip -n -9 --rsyncable"
861 else
862 compress="gzip -n -9"
863 fi
864 ;;
865 lzo|lzop)
866 compress="lzop -9"
867 ;;
868 lz4)
869 compress="lz4 -l -9"
870 ;;
871 zstd)
872 compress="zstd -15 -q -T0"
873 ;;
874 esac
875
876 [[ $hostonly = yes ]] && hostonly="-h"
877 [[ $hostonly != "-h" ]] && unset hostonly
878
879 case $hostonly_mode in
880 '')
881 [[ $hostonly ]] && hostonly_mode="sloppy" ;;
882 sloppy|strict)
883 if [[ ! $hostonly ]]; then
884 unset hostonly_mode
885 fi
886 ;;
887 *)
888 printf "%s\n" "dracut: Invalid hostonly mode '$hostonly_mode'." >&2
889 exit 1
890 esac
891
892 [[ $reproducible == yes ]] && DRACUT_REPRODUCIBLE=1
893
894 readonly TMPDIR="$(realpath -e "$tmpdir")"
895 [ -d "$TMPDIR" ] || {
896 printf "%s\n" "dracut: Invalid tmpdir '$tmpdir'." >&2
897 exit 1
898 }
899 readonly DRACUT_TMPDIR="$(mktemp -p "$TMPDIR/" -d -t dracut.XXXXXX)"
900 [ -d "$DRACUT_TMPDIR" ] || {
901 printf "%s\n" "dracut: mktemp -p '$TMPDIR/' -d -t dracut.XXXXXX failed." >&2
902 exit 1
903 }
904
905 # clean up after ourselves no matter how we die.
906 trap '
907 ret=$?;
908 [[ $keep ]] && echo "Not removing $DRACUT_TMPDIR." >&2 || { [[ $DRACUT_TMPDIR ]] && rm -rf -- "$DRACUT_TMPDIR"; };
909 exit $ret;
910 ' EXIT
911
912 # clean up after ourselves no matter how we die.
913 trap 'exit 1;' SIGINT
914
915 readonly initdir="${DRACUT_TMPDIR}/initramfs"
916 mkdir "$initdir"
917
918 if [[ $early_microcode = yes ]] || ( [[ $acpi_override = yes ]] && [[ -d $acpi_table_dir ]] ); then
919 readonly early_cpio_dir="${DRACUT_TMPDIR}/earlycpio"
920 mkdir "$early_cpio_dir"
921 fi
922
923 export DRACUT_RESOLVE_LAZY="1"
924
925 if [[ $print_cmdline ]]; then
926 stdloglvl=0
927 sysloglvl=0
928 fileloglvl=0
929 kmsgloglvl=0
930 fi
931
932 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
933 . $dracutbasedir/dracut-version.sh
934 fi
935
936 if [[ -f $dracutbasedir/dracut-init.sh ]]; then
937 . $dracutbasedir/dracut-init.sh
938 else
939 printf "%s\n" "dracut: Cannot find $dracutbasedir/dracut-init.sh." >&2
940 printf "%s\n" "dracut: Are you running from a git checkout?" >&2
941 printf "%s\n" "dracut: Try passing -l as an argument to $dracut_cmd" >&2
942 exit 1
943 fi
944
945 if [[ $no_kernel != yes ]] && ! [[ -d $srcmods ]]; then
946 printf "%s\n" "dracut: Cannot find module directory $srcmods" >&2
947 printf "%s\n" "dracut: and --no-kernel was not specified" >&2
948 exit 1
949 fi
950
951 if ! [[ $print_cmdline ]]; then
952 inst /bin/sh
953 if ! $DRACUT_INSTALL ${initdir:+-D "$initdir"} -R "$initdir/bin/sh" &>/dev/null; then
954 unset DRACUT_RESOLVE_LAZY
955 export DRACUT_RESOLVE_DEPS=1
956 fi
957 rm -fr -- ${initdir}/*
958 fi
959
960 dracutfunctions=$dracutbasedir/dracut-functions.sh
961 export dracutfunctions
962
963 (( ${#drivers_l[@]} )) && drivers="${drivers_l[@]}"
964 drivers=${drivers/-/_}
965
966 (( ${#add_drivers_l[@]} )) && add_drivers+=" ${add_drivers_l[@]} "
967 add_drivers=${add_drivers/-/_}
968
969 (( ${#force_drivers_l[@]} )) && force_drivers+=" ${force_drivers_l[@]} "
970 force_drivers=${force_drivers/-/_}
971
972 (( ${#omit_drivers_l[@]} )) && omit_drivers+=" ${omit_drivers_l[@]} "
973 omit_drivers=${omit_drivers/-/_}
974
975 (( ${#kernel_cmdline_l[@]} )) && kernel_cmdline+=" ${kernel_cmdline_l[@]} "
976
977 omit_drivers_corrected=""
978 for d in $omit_drivers; do
979 [[ " $drivers $add_drivers " == *\ $d\ * ]] && continue
980 [[ " $drivers $force_drivers " == *\ $d\ * ]] && continue
981 omit_drivers_corrected+="$d|"
982 done
983 omit_drivers="${omit_drivers_corrected%|}"
984 unset omit_drivers_corrected
985
986 # prepare args for logging
987 for ((i=0; i < ${#dracut_args[@]}; i++)); do
988 [[ "${dracut_args[$i]}" == *\ * ]] && \
989 dracut_args[$i]="\"${dracut_args[$i]}\""
990 #" keep vim happy
991 done
992
993 dinfo "Executing: $dracut_cmd ${dracut_args[@]}"
994
995 [[ $do_list = yes ]] && {
996 for mod in $dracutbasedir/modules.d/*; do
997 [[ -d $mod ]] || continue;
998 [[ -e $mod/install || -e $mod/installkernel || \
999 -e $mod/module-setup.sh ]] || continue
1000 printf "%s\n" "${mod##*/??}"
1001 done
1002 exit 0
1003 }
1004
1005 # This is kinda legacy -- eventually it should go away.
1006 case $dracutmodules in
1007 ""|auto) dracutmodules="all" ;;
1008 esac
1009
1010 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
1011
1012
1013 [[ -d $systemdutildir ]] \
1014 || systemdutildir=$(pkg-config systemd --variable=systemdutildir 2>/dev/null)
1015
1016 if ! [[ -d "$systemdutildir" ]]; then
1017 [[ -e /lib/systemd/systemd-udevd ]] && systemdutildir=/lib/systemd
1018 [[ -e /usr/lib/systemd/systemd-udevd ]] && systemdutildir=/usr/lib/systemd
1019 fi
1020
1021
1022 if [[ $no_kernel != yes ]] && [[ -d $srcmods ]]; then
1023 if ! [[ -f $srcmods/modules.dep ]]; then
1024 if [[ -n "$(find "$srcmods" -name '*.ko*')" ]]; then
1025 dfatal "$srcmods/modules.dep is missing. Did you run depmod?"
1026 exit 1
1027 else
1028 dwarn "$srcmods/modules.dep is missing. Did you run depmod?"
1029 fi
1030 elif ! ( command -v gzip &>/dev/null && command -v xz &>/dev/null); then
1031 read _mod < $srcmods/modules.dep
1032 _mod=${_mod%%:*}
1033 if [[ -f $srcmods/"$_mod" ]]; then
1034 # Check, if kernel modules are compressed, and if we can uncompress them
1035 case "$_mod" in
1036 *.ko.gz) kcompress=gzip;;
1037 *.ko.xz) kcompress=xz;;
1038 esac
1039 if [[ $kcompress ]]; then
1040 if ! command -v "$kcompress" &>/dev/null; then
1041 dfatal "Kernel modules are compressed with $kcompress, but $kcompress is not available."
1042 exit 1
1043 fi
1044 fi
1045 fi
1046 fi
1047 fi
1048
1049 if [[ ! $print_cmdline ]]; then
1050 if [[ -f $outfile && ! $force ]]; then
1051 dfatal "Will not override existing initramfs ($outfile) without --force"
1052 exit 1
1053 fi
1054
1055 outdir=${outfile%/*}
1056 [[ $outdir ]] || outdir="/"
1057
1058 if [[ ! -d "$outdir" ]]; then
1059 dfatal "Can't write to $outdir: Directory $outdir does not exist or is not accessible."
1060 exit 1
1061 elif [[ ! -w "$outdir" ]]; then
1062 dfatal "No permission to write to $outdir."
1063 exit 1
1064 elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
1065 dfatal "No permission to write $outfile."
1066 exit 1
1067 fi
1068
1069 if [[ $loginstall ]]; then
1070 if ! mkdir -p "$loginstall"; then
1071 dfatal "Could not create directory to log installed files to '$loginstall'."
1072 exit 1
1073 fi
1074 loginstall=$(readlink -f "$loginstall")
1075 fi
1076
1077 if [[ $uefi = yes ]]; then
1078 if ! command -v objcopy &>/dev/null; then
1079 dfatal "Need 'objcopy' to create a UEFI executable"
1080 exit 1
1081 fi
1082 unset EFI_MACHINE_TYPE_NAME
1083 case $(uname -m) in
1084 x86_64)
1085 EFI_MACHINE_TYPE_NAME=x64;;
1086 ia32)
1087 EFI_MACHINE_TYPE_NAME=ia32;;
1088 *)
1089 dfatal "Architecture '$(uname -m)' not supported to create a UEFI executable"
1090 exit 1
1091 ;;
1092 esac
1093
1094 if ! [[ -s $uefi_stub ]]; then
1095 for uefi_stub in \
1096 "${systemdutildir}/boot/efi/linux${EFI_MACHINE_TYPE_NAME}.efi.stub" \
1097 "/usr/lib/gummiboot/linux${EFI_MACHINE_TYPE_NAME}.efi.stub"; do
1098 [[ -s $uefi_stub ]] || continue
1099 break
1100 done
1101 fi
1102 if ! [[ -s $uefi_stub ]]; then
1103 dfatal "Can't find a uefi stub '$uefi_stub' to create a UEFI executable"
1104 exit 1
1105 fi
1106
1107 if ! [[ $kernel_image ]]; then
1108 for kernel_image in "/lib/modules/$kernel/vmlinuz" "/boot/vmlinuz-$kernel"; do
1109 [[ -s "$kernel_image" ]] || continue
1110 break
1111 done
1112 fi
1113 if ! [[ -s $kernel_image ]]; then
1114 dfatal "Can't find a kernel image '$kernel_image' to create a UEFI executable"
1115 exit 1
1116 fi
1117 fi
1118 fi
1119
1120 if [[ $acpi_override = yes ]] && ! ( check_kernel_config CONFIG_ACPI_TABLE_UPGRADE || check_kernel_config CONFIG_ACPI_INITRD_TABLE_OVERRIDE ); then
1121 dwarn "Disabling ACPI override, because kernel does not support it. CONFIG_ACPI_INITRD_TABLE_OVERRIDE!=y or CONFIG_ACPI_TABLE_UPGRADE!=y"
1122 unset acpi_override
1123 fi
1124
1125 if [[ $early_microcode = yes ]]; then
1126 if [[ $hostonly ]]; then
1127 [[ $(get_cpu_vendor) == "AMD" ]] \
1128 && ! check_kernel_config CONFIG_MICROCODE_AMD \
1129 && unset early_microcode
1130 [[ $(get_cpu_vendor) == "Intel" ]] \
1131 && ! check_kernel_config CONFIG_MICROCODE_INTEL \
1132 && unset early_microcode
1133 else
1134 ! check_kernel_config CONFIG_MICROCODE_AMD \
1135 && ! check_kernel_config CONFIG_MICROCODE_INTEL \
1136 && unset early_microcode
1137 fi
1138 [[ $early_microcode != yes ]] \
1139 && dwarn "Disabling early microcode, because kernel does not support it. CONFIG_MICROCODE_[AMD|INTEL]!=y"
1140 fi
1141
1142 # Need to be able to have non-root users read stuff (rpcbind etc)
1143 chmod 755 "$initdir"
1144
1145 if [[ $hostonly ]]; then
1146 for i in /sys /proc /run /dev; do
1147 if ! findmnt --target "$i" &>/dev/null; then
1148 dwarning "Turning off host-only mode: '$i' is not mounted!"
1149 unset hostonly
1150 fi
1151 done
1152 fi
1153
1154 declare -A host_fs_types
1155
1156 for line in "${fstab_lines[@]}"; do
1157 set -- $line
1158 dev="$1"
1159 #dev mp fs fsopts
1160 case "$dev" in
1161 UUID=*)
1162 dev=$(blkid -l -t UUID=${dev#UUID=} -o device)
1163 ;;
1164 LABEL=*)
1165 dev=$(blkid -l -t LABEL=${dev#LABEL=} -o device)
1166 ;;
1167 PARTUUID=*)
1168 dev=$(blkid -l -t PARTUUID=${dev#PARTUUID=} -o device)
1169 ;;
1170 PARTLABEL=*)
1171 dev=$(blkid -l -t PARTLABEL=${dev#PARTLABEL=} -o device)
1172 ;;
1173 esac
1174 [ -z "$dev" ] && dwarn "Bad fstab entry $@" && continue
1175 if [[ "$3" == btrfs ]]; then
1176 for i in $(btrfs_devs "$2"); do
1177 push_host_devs "$i"
1178 done
1179 fi
1180 push_host_devs "$dev"
1181 host_fs_types["$dev"]="$3"
1182 done
1183
1184 for f in $add_fstab; do
1185 [[ -e $f ]] || continue
1186 while read dev rest || [ -n "$dev" ]; do
1187 push_host_devs "$dev"
1188 done < "$f"
1189 done
1190
1191 for dev in $add_device; do
1192 push_host_devs "$dev"
1193 done
1194
1195 if (( ${#add_device_l[@]} )); then
1196 add_device+=" ${add_device_l[@]} "
1197 push_host_devs "${add_device_l[@]}"
1198 fi
1199
1200 if [[ $hostonly ]] && [[ "$hostonly_default_device" != "no" ]]; then
1201 # in hostonly mode, determine all devices, which have to be accessed
1202 # and examine them for filesystem types
1203
1204 for mp in \
1205 "/" \
1206 "/etc" \
1207 "/bin" \
1208 "/sbin" \
1209 "/lib" \
1210 "/lib64" \
1211 "/usr" \
1212 "/usr/bin" \
1213 "/usr/sbin" \
1214 "/usr/lib" \
1215 "/usr/lib64" \
1216 "/boot" \
1217 "/boot/efi" \
1218 "/boot/zipl" \
1219 ;
1220 do
1221 mp=$(readlink -f "$mp")
1222 mountpoint "$mp" >/dev/null 2>&1 || continue
1223 _dev=$(find_block_device "$mp")
1224 _bdev=$(readlink -f "/dev/block/$_dev")
1225 [[ -b $_bdev ]] && _dev=$_bdev
1226 [[ "$mp" == "/" ]] && root_devs+=("$_dev")
1227 push_host_devs "$_dev"
1228 if [[ $(find_mp_fstype "$mp") == btrfs ]]; then
1229 for i in $(btrfs_devs "$mp"); do
1230 [[ "$mp" == "/" ]] && root_devs+=("$i")
1231 push_host_devs "$i"
1232 done
1233 fi
1234 done
1235
1236 if [[ -f /proc/swaps ]] && [[ -f /etc/fstab ]]; then
1237 while read dev type rest || [ -n "$dev" ]; do
1238 [[ -b $dev ]] || continue
1239 [[ "$type" == "partition" ]] || continue
1240
1241 while read _d _m _t _o _r || [ -n "$_d" ]; do
1242 [[ "$_d" == \#* ]] && continue
1243 [[ $_d ]] || continue
1244 [[ $_t != "swap" ]] && continue
1245 [[ $_m != "swap" ]] && [[ $_m != "none" ]] && continue
1246 [[ "$_o" == *noauto* ]] && continue
1247 _d=$(expand_persistent_dev "$_d")
1248 [[ "$_d" -ef "$dev" ]] || continue
1249
1250 if [[ -f /etc/crypttab ]]; then
1251 while read _mapper _a _p _o || [ -n "$_mapper" ]; do
1252 [[ $_mapper = \#* ]] && continue
1253 [[ "$_d" -ef /dev/mapper/"$_mapper" ]] || continue
1254 [[ "$_o" ]] || _o="$_p"
1255 # skip entries with password files
1256 [[ "$_p" == /* ]] && [[ -f $_p ]] && continue 2
1257 # skip mkswap swap
1258 [[ $_o == *swap* ]] && continue 2
1259 done < /etc/crypttab
1260 fi
1261
1262 _dev="$(readlink -f "$dev")"
1263 push_host_devs "$_dev"
1264 swap_devs+=("$_dev")
1265 break
1266 done < /etc/fstab
1267 done < /proc/swaps
1268 fi
1269
1270 # collect all "x-initrd.mount" entries from /etc/fstab
1271 if [[ -f /etc/fstab ]]; then
1272 while read _d _m _t _o _r || [ -n "$_d" ]; do
1273 [[ "$_d" == \#* ]] && continue
1274 [[ $_d ]] || continue
1275 [[ "$_o" != *x-initrd.mount* ]] && continue
1276 _dev=$(expand_persistent_dev "$_d")
1277 _dev="$(readlink -f "$_dev")"
1278 [[ -b $_dev ]] || continue
1279
1280 push_host_devs "$_dev"
1281 if [[ "$_t" == btrfs ]]; then
1282 for i in $(btrfs_devs "$_m"); do
1283 push_host_devs "$i"
1284 done
1285 fi
1286 done < /etc/fstab
1287 fi
1288 fi
1289
1290 unset m
1291 unset rest
1292
1293 _get_fs_type() {
1294 [[ $1 ]] || return
1295 if [[ -b /dev/block/$1 ]]; then
1296 ID_FS_TYPE=$(get_fs_env "/dev/block/$1") && host_fs_types["$(readlink -f "/dev/block/$1")"]="$ID_FS_TYPE"
1297 return 1
1298 fi
1299 if [[ -b $1 ]]; then
1300 ID_FS_TYPE=$(get_fs_env "$1") && host_fs_types["$(readlink -f "$1")"]="$ID_FS_TYPE"
1301 return 1
1302 fi
1303 if fstype=$(find_dev_fstype "$1"); then
1304 host_fs_types["$1"]="$fstype"
1305 return 1
1306 fi
1307 return 1
1308 }
1309
1310 for dev in "${host_devs[@]}"; do
1311 _get_fs_type "$dev"
1312 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
1313 done
1314
1315 for dev in "${!host_fs_types[@]}"; do
1316 [[ ${host_fs_types[$dev]} = "reiserfs" ]] || [[ ${host_fs_types[$dev]} = "xfs" ]] || continue
1317 rootopts=$(find_dev_fsopts "$dev")
1318 if [[ ${host_fs_types[$dev]} = "reiserfs" ]]; then
1319 journaldev=$(fs_get_option $rootopts "jdev")
1320 elif [[ ${host_fs_types[$dev]} = "xfs" ]]; then
1321 journaldev=$(fs_get_option $rootopts "logdev")
1322 fi
1323 if [[ $journaldev ]]; then
1324 dev="$(readlink -f "$dev")"
1325 push_host_devs "$dev"
1326 _get_fs_type "$dev"
1327 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
1328 fi
1329 done
1330
1331 [[ -d $udevdir ]] \
1332 || udevdir="$(pkg-config udev --variable=udevdir 2>/dev/null)"
1333 if ! [[ -d "$udevdir" ]]; then
1334 [[ -e /lib/udev/ata_id ]] && udevdir=/lib/udev
1335 [[ -e /usr/lib/udev/ata_id ]] && udevdir=/usr/lib/udev
1336 fi
1337
1338 [[ -d $systemdsystemunitdir ]] \
1339 || systemdsystemunitdir=$(pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null)
1340
1341 [[ -d "$systemdsystemunitdir" ]] || systemdsystemunitdir=${systemdutildir}/system
1342
1343 [[ -d $systemdsystemconfdir ]] \
1344 || systemdsystemconfdir=$(pkg-config systemd --variable=systemdsystemconfdir 2>/dev/null)
1345
1346 [[ -d "$systemdsystemconfdir" ]] || systemdsystemconfdir=/etc/systemd/system
1347
1348 [[ -d $tmpfilesdir ]] \
1349 || tmpfilesdir=$(pkg-config systemd --variable=tmpfilesdir 2>/dev/null)
1350
1351 if ! [[ -d "$tmpfilesdir" ]]; then
1352 [[ -d /lib/tmpfiles.d ]] && tmpfilesdir=/lib/tmpfiles.d
1353 [[ -d /usr/lib/tmpfiles.d ]] && tmpfilesdir=/usr/lib/tmpfiles.d
1354 fi
1355
1356 export initdir dracutbasedir \
1357 dracutmodules force_add_dracutmodules add_dracutmodules omit_dracutmodules \
1358 mods_to_load \
1359 fw_dir drivers_dir debug no_kernel kernel_only \
1360 omit_drivers mdadmconf lvmconf root_devs \
1361 use_fstab fstab_lines libdirs fscks nofscks ro_mnt \
1362 stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
1363 debug host_fs_types host_devs swap_devs sshkey add_fstab \
1364 DRACUT_VERSION udevdir prefix filesystems drivers \
1365 systemdutildir systemdsystemunitdir systemdsystemconfdir \
1366 hostonly_cmdline loginstall \
1367 tmpfilesdir
1368
1369 mods_to_load=""
1370 # check all our modules to see if they should be sourced.
1371 # This builds a list of modules that we will install next.
1372 for_each_module_dir check_module
1373 for_each_module_dir check_mount
1374
1375 dracut_module_included "fips" && export DRACUT_FIPS_MODE=1
1376
1377 do_print_cmdline()
1378 {
1379 local -A _mods_to_print
1380 for i in $modules_loaded $mods_to_load; do
1381 _mods_to_print[$i]=1
1382 done
1383
1384 # source our modules.
1385 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1386 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
1387 [[ ${_mods_to_print[$_d_mod]} ]] || continue
1388 module_cmdline "$_d_mod" "$moddir"
1389 done
1390 unset moddir
1391 }
1392
1393 if [[ $print_cmdline ]]; then
1394 do_print_cmdline
1395 printf "\n"
1396 exit 0
1397 fi
1398
1399 # Create some directory structure first
1400 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
1401
1402 [[ -h /lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
1403 [[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
1404
1405 if [[ $prefix ]]; then
1406 for d in bin etc lib sbin tmp usr var $libdirs; do
1407 [[ "$d" == */* ]] && continue
1408 ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
1409 done
1410 fi
1411
1412 if [[ $kernel_only != yes ]]; then
1413 for d in usr/bin usr/sbin bin etc lib sbin tmp usr var var/tmp $libdirs; do
1414 [[ -e "${initdir}${prefix}/$d" ]] && continue
1415 if [ -L "/$d" ]; then
1416 inst_symlink "/$d" "${prefix}/$d"
1417 else
1418 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1419 fi
1420 done
1421
1422 for d in dev proc sys sysroot root run; do
1423 if [ -L "/$d" ]; then
1424 inst_symlink "/$d"
1425 else
1426 mkdir -m 0755 -p "$initdir/$d"
1427 fi
1428 done
1429
1430 ln -sfn ../run "$initdir/var/run"
1431 ln -sfn ../run/lock "$initdir/var/lock"
1432 else
1433 for d in lib "$libdir"; do
1434 [[ -e "${initdir}${prefix}/$d" ]] && continue
1435 if [ -h "/$d" ]; then
1436 inst "/$d" "${prefix}/$d"
1437 else
1438 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1439 fi
1440 done
1441 fi
1442
1443 if [[ $kernel_only != yes ]]; then
1444 mkdir -p "${initdir}/etc/cmdline.d"
1445 for _d in $hookdirs; do
1446 mkdir -m 0755 -p ${initdir}/lib/dracut/hooks/$_d
1447 done
1448 if [[ "$EUID" = "0" ]]; then
1449 [ -c ${initdir}/dev/null ] || mknod ${initdir}/dev/null c 1 3
1450 [ -c ${initdir}/dev/kmsg ] || mknod ${initdir}/dev/kmsg c 1 11
1451 [ -c ${initdir}/dev/console ] || mknod ${initdir}/dev/console c 5 1
1452 [ -c ${initdir}/dev/random ] || mknod ${initdir}/dev/random c 1 8
1453 [ -c ${initdir}/dev/urandom ] || mknod ${initdir}/dev/urandom c 1 9
1454 fi
1455 fi
1456
1457 _isize=0 #initramfs size
1458 modules_loaded=" "
1459 # source our modules.
1460 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1461 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
1462 [[ "$mods_to_load" == *\ $_d_mod\ * ]] || continue
1463 if [[ $show_modules = yes ]]; then
1464 printf "%s\n" "$_d_mod"
1465 else
1466 dinfo "*** Including module: $_d_mod ***"
1467 fi
1468 if [[ $kernel_only == yes ]]; then
1469 module_installkernel "$_d_mod" "$moddir" || {
1470 dfatal "installkernel failed in module $_d_mod"
1471 exit 1
1472 }
1473 else
1474 module_install "$_d_mod" "$moddir"
1475 if [[ $no_kernel != yes ]]; then
1476 module_installkernel "$_d_mod" "$moddir" || {
1477 dfatal "installkernel failed in module $_d_mod"
1478 exit 1
1479 }
1480 fi
1481 fi
1482 mods_to_load=${mods_to_load// $_d_mod /}
1483 modules_loaded+="$_d_mod "
1484
1485 #print the module install size
1486 if [ -n "$printsize" ]; then
1487 _isize_new=$(du -sk ${initdir}|cut -f1)
1488 _isize_delta=$((_isize_new - _isize))
1489 printf "%s\n" "$_d_mod install size: ${_isize_delta}k"
1490 _isize=$_isize_new
1491 fi
1492 done
1493 unset moddir
1494
1495 for i in $modules_loaded; do
1496 mkdir -p $initdir/lib/dracut
1497 printf "%s\n" "$i" >> $initdir/lib/dracut/modules.txt
1498 done
1499
1500 dinfo "*** Including modules done ***"
1501
1502 ## final stuff that has to happen
1503 if [[ $no_kernel != yes ]]; then
1504 if [[ $hostonly ]]; then
1505 cp "$DRACUT_KERNEL_MODALIASES" $initdir/lib/dracut/hostonly-kernel-modules.txt
1506 fi
1507
1508 if [[ $drivers ]]; then
1509 hostonly='' instmods $drivers
1510 fi
1511
1512 if [[ -n "${add_drivers// }" ]]; then
1513 hostonly='' instmods -c $add_drivers
1514 fi
1515 if [[ $force_drivers ]]; then
1516 hostonly='' instmods -c $force_drivers
1517 rm -f $initdir/etc/cmdline.d/20-force_driver.conf
1518 for mod in $force_drivers; do
1519 echo "rd.driver.pre=$mod" >>$initdir/etc/cmdline.d/20-force_drivers.conf
1520 done
1521 fi
1522 if [[ $filesystems ]]; then
1523 hostonly='' instmods -c $filesystems
1524 fi
1525
1526 dinfo "*** Installing kernel module dependencies ***"
1527 dracut_kernel_post
1528 dinfo "*** Installing kernel module dependencies done ***"
1529
1530 if [[ $noimageifnotneeded == yes ]] && [[ $hostonly ]]; then
1531 if [[ ! -f "$initdir/lib/dracut/need-initqueue" ]] && \
1532 [[ -f ${initdir}/lib/modules/$kernel/modules.dep && ! -s ${initdir}/lib/modules/$kernel/modules.dep ]]; then
1533 for i in ${initdir}/etc/cmdline.d/*.conf; do
1534 # We need no initramfs image and do not generate one.
1535 [[ $i == "${initdir}/etc/cmdline.d/*.conf" ]] && exit 0
1536 done
1537 fi
1538 fi
1539 fi
1540
1541 if [[ $kernel_only != yes ]]; then
1542 (( ${#install_items[@]} > 0 )) && inst_multiple ${install_items[@]}
1543 (( ${#install_optional_items[@]} > 0 )) && inst_multiple -o ${install_optional_items[@]}
1544
1545 [[ $kernel_cmdline ]] && printf "%s\n" "$kernel_cmdline" >> "${initdir}/etc/cmdline.d/01-default.conf"
1546
1547 for line in "${fstab_lines[@]}"; do
1548 line=($line)
1549
1550 if [ -z "${line[1]}" ]; then
1551 # Determine device and mount options from current system
1552 mountpoint -q "${line[0]}" || derror "${line[0]} is not a mount point!"
1553 line=($(findmnt --raw -n --target "${line[0]}" --output=source,target,fstype,options))
1554 dinfo "Line for ${line[1]}: ${line[@]}"
1555 else
1556 # Use default options
1557 [ -z "${line[3]}" ] && line[3]="defaults"
1558 fi
1559
1560 # Default options for freq and passno
1561 [ -z "${line[4]}" ] && line[4]="0"
1562 [ -z "${line[5]}" ] && line[5]="2"
1563
1564 strstr "${line[2]}" "nfs" && line[5]="0"
1565 echo "${line[@]}" >> "${initdir}/etc/fstab"
1566 done
1567
1568 for f in $add_fstab; do
1569 cat "$f" >> "${initdir}/etc/fstab"
1570 done
1571
1572 if [[ $systemdutildir ]]; then
1573 if [ -d ${initdir}/$systemdutildir ]; then
1574 mkdir -p ${initdir}/etc/conf.d
1575 {
1576 printf "%s\n" "systemdutildir=\"$systemdutildir\""
1577 printf "%s\n" "systemdsystemunitdir=\"$systemdsystemunitdir\""
1578 printf "%s\n" "systemdsystemconfdir=\"$systemdsystemconfdir\""
1579 } > ${initdir}/etc/conf.d/systemd.conf
1580 fi
1581 fi
1582
1583 if [[ $DRACUT_RESOLVE_LAZY ]] && [[ $DRACUT_INSTALL ]]; then
1584 dinfo "*** Resolving executable dependencies ***"
1585 find "$initdir" -type f -perm /0111 -not -path '*.ko' -print0 \
1586 | xargs -r -0 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -R ${DRACUT_FIPS_MODE:+-f} --
1587 dinfo "*** Resolving executable dependencies done ***"
1588 fi
1589
1590 # Now we are done with lazy resolving, always install dependencies
1591 unset DRACUT_RESOLVE_LAZY
1592 export DRACUT_RESOLVE_DEPS=1
1593
1594 # libpthread workaround: pthread_cancel wants to dlopen libgcc_s.so
1595 for _dir in $libdirs; do
1596 for _f in "$_dir/libpthread.so"*; do
1597 [[ -e "$_f" ]] || continue
1598 inst_libdir_file "libgcc_s.so*"
1599 break 2
1600 done
1601 done
1602 fi
1603
1604 for ((i=0; i < ${#include_src[@]}; i++)); do
1605 src="${include_src[$i]}"
1606 target="${include_target[$i]}"
1607 if [[ $src && $target ]]; then
1608 if [[ -f $src ]]; then
1609 inst $src $target
1610 else
1611 ddebug "Including directory: $src"
1612 destdir="${initdir}/${target}"
1613 mkdir -p "$destdir"
1614 # check for preexisting symlinks, so we can cope with the
1615 # symlinks to $prefix
1616 # Objectname is a file or a directory
1617 for objectname in "$src"/*; do
1618 [[ -e "$objectname" || -h "$objectname" ]] || continue
1619 if [[ -d "$objectname" ]]; then
1620 # objectname is a directory, let's compute the final directory name
1621 object_destdir=${destdir}/${objectname#$src/}
1622 if ! [[ -e "$object_destdir" ]]; then
1623 mkdir -m 0755 -p "$object_destdir"
1624 chmod --reference="$objectname" "$object_destdir"
1625 fi
1626 $DRACUT_CP -t "$object_destdir" "$objectname"/*
1627 else
1628 $DRACUT_CP -t "$destdir" "$objectname"
1629 fi
1630 done
1631 fi
1632 fi
1633 done
1634
1635 if [[ $kernel_only != yes ]]; then
1636 # make sure that library links are correct and up to date
1637 for f in /etc/ld.so.conf /etc/ld.so.conf.d/*; do
1638 [[ -f $f ]] && inst_simple "$f"
1639 done
1640 if ! ldconfig -r "$initdir"; then
1641 if [[ $EUID = 0 ]]; then
1642 derror "ldconfig exited ungracefully"
1643 else
1644 derror "ldconfig might need uid=0 (root) for chroot()"
1645 fi
1646 fi
1647 fi
1648
1649 if [[ $do_hardlink = yes ]] && command -v hardlink >/dev/null; then
1650 dinfo "*** Hardlinking files ***"
1651 hardlink "$initdir" 2>&1
1652 dinfo "*** Hardlinking files done ***"
1653 fi
1654
1655 # strip binaries
1656 if [[ $do_strip = yes ]] ; then
1657 # Prefer strip from elfutils for package size
1658 declare strip_cmd=$(command -v eu-strip)
1659 test -z "$strip_cmd" && strip_cmd="strip"
1660
1661 for p in $strip_cmd xargs find; do
1662 if ! type -P $p >/dev/null; then
1663 dinfo "Could not find '$p'. Not stripping the initramfs."
1664 do_strip=no
1665 fi
1666 done
1667 fi
1668
1669 # cleanup empty ldconfig_paths directories
1670 for d in $(ldconfig_paths); do
1671 rmdir -p --ignore-fail-on-non-empty "$initdir/$d" >/dev/null 2>&1
1672 done
1673
1674 if [[ $early_microcode = yes ]]; then
1675 dinfo "*** Generating early-microcode cpio image ***"
1676 ucode_dir=(amd-ucode intel-ucode)
1677 ucode_dest=(AuthenticAMD.bin GenuineIntel.bin)
1678 _dest_dir="$early_cpio_dir/d/kernel/x86/microcode"
1679 _dest_idx="0 1"
1680 mkdir -p $_dest_dir
1681 if [[ $hostonly ]]; then
1682 [[ $(get_cpu_vendor) == "AMD" ]] && _dest_idx="0"
1683 [[ $(get_cpu_vendor) == "Intel" ]] && _dest_idx="1"
1684 fi
1685 for idx in $_dest_idx; do
1686 _fw=${ucode_dir[$idx]}
1687 for _fwdir in $fw_dir; do
1688 if [[ -d $_fwdir && -d $_fwdir/$_fw ]]; then
1689 _src="*"
1690 dinfo "*** Constructing ${ucode_dest[$idx]} ***"
1691 if [[ $hostonly ]]; then
1692 _src=$(get_ucode_file)
1693 [[ $_src ]] || break
1694 [[ -r $_fwdir/$_fw/$_src ]] || break
1695 fi
1696
1697 for i in $_fwdir/$_fw/$_src; do
1698 [ -e "$i" ] && break
1699 break 2
1700 done
1701 for i in $_fwdir/$_fw/$_src; do
1702 [[ -e "$i" ]] || continue
1703 # skip gpg files
1704 str_ends "$i" ".asc" && continue
1705 cat "$i" >> $_dest_dir/${ucode_dest[$idx]}
1706 done
1707 create_early_cpio="yes"
1708 fi
1709 done
1710 done
1711 fi
1712
1713 if [[ $acpi_override = yes ]] && [[ -d $acpi_table_dir ]]; then
1714 dinfo "*** Packaging ACPI tables to override BIOS provided ones ***"
1715 _dest_dir="$early_cpio_dir/d/kernel/firmware/acpi"
1716 mkdir -p $_dest_dir
1717 for table in $acpi_table_dir/*.aml; do
1718 dinfo " Adding ACPI table: $table"
1719 $DRACUT_CP $table $_dest_dir
1720 create_early_cpio="yes"
1721 done
1722 fi
1723
1724 dinfo "*** Store current command line parameters ***"
1725 if ! ( echo $PARMS_TO_STORE > $initdir/lib/dracut/build-parameter.txt ); then
1726 dfatal "Could not store the current command line parameters"
1727 exit 1
1728 fi
1729
1730 if [[ $hostonly_cmdline == "yes" ]] ; then
1731 unset _stored_cmdline
1732 if [ -d $initdir/etc/cmdline.d ];then
1733 dinfo "Stored kernel commandline:"
1734 for conf in $initdir/etc/cmdline.d/*.conf ; do
1735 [ -e "$conf" ] || continue
1736 dinfo "$(< $conf)"
1737 _stored_cmdline=1
1738 done
1739 fi
1740 if ! [[ $_stored_cmdline ]]; then
1741 dinfo "No dracut internal kernel commandline stored in the initramfs"
1742 fi
1743 fi
1744
1745 if dracut_module_included "squash"; then
1746 dinfo "*** Install squash loader ***"
1747 if ! check_kernel_config CONFIG_SQUASHFS; then
1748 dfatal "CONFIG_SQUASHFS have to be enabled for dracut squash module to work"
1749 exit 1
1750 fi
1751 if ! check_kernel_config CONFIG_OVERLAY_FS; then
1752 dfatal "CONFIG_OVERLAY_FS have to be enabled for dracut squash module to work"
1753 exit 1
1754 fi
1755 if ! check_kernel_config CONFIG_DEVTMPFS; then
1756 dfatal "CONFIG_DEVTMPFS have to be enabled for dracut squash module to work"
1757 exit 1
1758 fi
1759
1760 readonly squash_dir="$initdir/squash/root"
1761 readonly squash_img=$initdir/squash/root.img
1762
1763 # Currently only move "usr" "etc" to squashdir
1764 readonly squash_candidate=( "usr" "etc" )
1765
1766 mkdir -m 0755 -p $squash_dir
1767 for folder in "${squash_candidate[@]}"; do
1768 mv $initdir/$folder $squash_dir/$folder
1769 done
1770
1771 # Move some files out side of the squash image, including:
1772 # - Files required to boot and mount the squashfs image
1773 # - Files need to be accessible without mounting the squash image
1774 required_in_root() {
1775 local file=$1
1776 local _sqsh_file=$squash_dir/$file
1777 local _init_file=$initdir/$file
1778
1779 if [[ -e $_init_file ]]; then
1780 return
1781 fi
1782
1783 if [[ ! -e $_sqsh_file ]] && [[ ! -L $_sqsh_file ]]; then
1784 derror "$file is required to boot a squashed initramfs but it's not installed!"
1785 return
1786 fi
1787
1788 if [[ ! -d $(dirname $_init_file) ]]; then
1789 required_in_root $(dirname $file)
1790 fi
1791
1792 if [[ -L $_sqsh_file ]]; then
1793 cp --preserve=all -P $_sqsh_file $_init_file
1794 _sqsh_file=$(realpath $_sqsh_file 2>/dev/null)
1795 if [[ -e $_sqsh_file ]] && [[ "$_sqsh_file" == "$squash_dir"* ]]; then
1796 # Relative symlink
1797 required_in_root ${_sqsh_file#$squash_dir/}
1798 return
1799 fi
1800 if [[ -e $squash_dir$_sqsh_file ]]; then
1801 # Absolute symlink
1802 required_in_root ${_sqsh_file#/}
1803 return
1804 fi
1805 required_in_root ${module_spec#$squash_dir/}
1806 else
1807 if [[ -d $_sqsh_file ]]; then
1808 mkdir $_init_file
1809 else
1810 mv $_sqsh_file $_init_file
1811 fi
1812 fi
1813 }
1814
1815 required_in_root etc/initrd-release
1816
1817 for module_spec in $squash_dir/usr/lib/modules/*/modules.*;
1818 do
1819 required_in_root ${module_spec#$squash_dir/}
1820 done
1821
1822 for dracut_spec in $squash_dir/usr/lib/dracut/*;
1823 do
1824 required_in_root ${dracut_spec#$squash_dir/}
1825 done
1826
1827 mv $initdir/init $initdir/init.stock
1828 ln -s squash/init.sh $initdir/init
1829
1830 # Reinstall required files for the squash image setup script.
1831 # We have moved them inside the squashed image, but they need to be
1832 # accessible before mounting the image.
1833 inst_multiple "echo" "sh" "mount" "modprobe" "mkdir"
1834 hostonly="" instmods "loop" "squashfs" "overlay"
1835
1836 # Only keep systemctl outsite if we need switch root
1837 if [[ ! -f "$initdir/lib/dracut/no-switch-root" ]]; then
1838 inst "systemctl"
1839 fi
1840
1841 for folder in "${squash_candidate[@]}"; do
1842 # Remove duplicated files in squashfs image, save some more space
1843 [[ ! -d $initdir/$folder/ ]] && continue
1844 for file in $(find $initdir/$folder/ -not -type d);
1845 do
1846 if [[ -e $squash_dir${file#$initdir} ]]; then
1847 mv $squash_dir${file#$initdir} $file
1848 fi
1849 done
1850 done
1851 fi
1852
1853 if [[ $do_strip = yes ]] && ! [[ $DRACUT_FIPS_MODE ]]; then
1854 dinfo "*** Stripping files ***"
1855 find "$initdir" -type f \
1856 -executable -not -path '*/lib/modules/*.ko' -print0 \
1857 | xargs -r -0 $strip_cmd -g -p 2>/dev/null
1858
1859 # strip kernel modules, but do not touch signed modules
1860 find "$initdir" -type f -path '*/lib/modules/*.ko' -print0 \
1861 | while read -r -d $'\0' f || [ -n "$f" ]; do
1862 SIG=$(tail -c 28 "$f" | tr -d '\000')
1863 [[ $SIG == '~Module signature appended~' ]] || { printf "%s\000" "$f"; }
1864 done | xargs -r -0 $strip_cmd -g -p
1865 dinfo "*** Stripping files done ***"
1866 fi
1867
1868 if dracut_module_included "squash"; then
1869 dinfo "*** Squashing the files inside the initramfs ***"
1870 mksquashfs $squash_dir $squash_img -comp xz -b 64K -Xdict-size 100% &> /dev/null
1871
1872 if [[ $? != 0 ]]; then
1873 dfatal "dracut: Failed making squash image"
1874 exit 1
1875 fi
1876
1877 rm -rf $squash_dir
1878 dinfo "*** Squashing the files inside the initramfs done ***"
1879 fi
1880
1881 dinfo "*** Creating image file '$outfile' ***"
1882
1883 if [[ $uefi = yes ]]; then
1884 readonly uefi_outdir="$DRACUT_TMPDIR/uefi"
1885 mkdir "$uefi_outdir"
1886 fi
1887
1888 if [[ $DRACUT_REPRODUCIBLE ]]; then
1889 find "$initdir" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
1890 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
1891
1892 if [[ "$(cpio --help)" == *--reproducible* ]]; then
1893 CPIO_REPRODUCIBLE=1
1894 else
1895 dinfo "cpio does not support '--reproducible'. Resulting image will not be reproducible."
1896 fi
1897 fi
1898
1899 [[ "$EUID" != 0 ]] && cpio_owner_root="-R 0:0"
1900
1901 if [[ $create_early_cpio = yes ]]; then
1902 echo 1 > "$early_cpio_dir/d/early_cpio"
1903
1904 if [[ $DRACUT_REPRODUCIBLE ]]; then
1905 find "$early_cpio_dir/d" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
1906 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
1907 fi
1908
1909 # The microcode blob is _before_ the initramfs blob, not after
1910 if ! (
1911 umask 077; cd "$early_cpio_dir/d"
1912 find . -print0 | sort -z \
1913 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null $cpio_owner_root -H newc -o --quiet > "${DRACUT_TMPDIR}/initramfs.img"
1914 ); then
1915 dfatal "dracut: creation of $outfile failed"
1916 exit 1
1917 fi
1918 fi
1919
1920 if ! (
1921 umask 077; cd "$initdir"
1922 find . -print0 | sort -z \
1923 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null $cpio_owner_root -H newc -o --quiet \
1924 | $compress >> "${DRACUT_TMPDIR}/initramfs.img"
1925 ); then
1926 dfatal "dracut: creation of $outfile failed"
1927 exit 1
1928 fi
1929
1930 if (( maxloglvl >= 5 )) && (( verbosity_mod_l >= 0 )); then
1931 if [[ $allowlocal ]]; then
1932 "$dracutbasedir/lsinitrd.sh" "${DRACUT_TMPDIR}/initramfs.img"| ddebug
1933 else
1934 lsinitrd "${DRACUT_TMPDIR}/initramfs.img"| ddebug
1935 fi
1936 fi
1937
1938 umask 077
1939
1940 if [[ $uefi = yes ]]; then
1941 if [[ $kernel_cmdline ]]; then
1942 echo -n "$kernel_cmdline" > "$uefi_outdir/cmdline.txt"
1943 elif [[ $hostonly_cmdline = yes ]] && [ -d $initdir/etc/cmdline.d ];then
1944 for conf in $initdir/etc/cmdline.d/*.conf ; do
1945 [ -e "$conf" ] || continue
1946 printf "%s " "$(< $conf)" >> "$uefi_outdir/cmdline.txt"
1947 done
1948 else
1949 do_print_cmdline > "$uefi_outdir/cmdline.txt"
1950 fi
1951 echo -ne "\x00" >> "$uefi_outdir/cmdline.txt"
1952
1953 dinfo "Using UEFI kernel cmdline:"
1954 dinfo $(< "$uefi_outdir/cmdline.txt")
1955
1956 [[ -s /usr/lib/os-release ]] && uefi_osrelease="/usr/lib/os-release"
1957 [[ -s /etc/os-release ]] && uefi_osrelease="/etc/os-release"
1958
1959 if objcopy \
1960 ${uefi_osrelease:+--add-section .osrel=$uefi_osrelease --change-section-vma .osrel=0x20000} \
1961 --add-section .cmdline="${uefi_outdir}/cmdline.txt" --change-section-vma .cmdline=0x30000 \
1962 --add-section .linux="$kernel_image" --change-section-vma .linux=0x40000 \
1963 --add-section .initrd="${DRACUT_TMPDIR}/initramfs.img" --change-section-vma .initrd=0x3000000 \
1964 "$uefi_stub" "${uefi_outdir}/linux.efi"; then
1965 if [[ -n "${uefi_secureboot_key}" && -n "${uefi_secureboot_cert}" ]]; then \
1966 if sbsign \
1967 --key "${uefi_secureboot_key}" \
1968 --cert "${uefi_secureboot_cert}" \
1969 --output "$outfile" "${uefi_outdir}/linux.efi"; then
1970 dinfo "*** Creating signed UEFI image file '$outfile' done ***"
1971 else
1972 dfatal "*** Creating signed UEFI image file '$outfile' failed ***"
1973 exit 1
1974 fi
1975 else
1976 if cp --reflink=auto "${uefi_outdir}/linux.efi" "$outfile"; then
1977 dinfo "*** Creating UEFI image file '$outfile' done ***"
1978 fi
1979 fi
1980 else
1981 rm -f -- "$outfile"
1982 dfatal "*** Creating UEFI image file '$outfile' failed ***"
1983 exit 1
1984 fi
1985 else
1986 if cp --reflink=auto "${DRACUT_TMPDIR}/initramfs.img" "$outfile"; then
1987 dinfo "*** Creating initramfs image file '$outfile' done ***"
1988 else
1989 rm -f -- "$outfile"
1990 dfatal "dracut: creation of $outfile failed"
1991 exit 1
1992 fi
1993 fi
1994
1995 command -v restorecon &>/dev/null && restorecon -- "$outfile"
1996
1997 # We sync/fsfreeze only if we're operating on a live booted system.
1998 # It's possible for e.g. `kernel` to be installed as an RPM BuildRequires or equivalent,
1999 # and there's no reason to sync, and *definitely* no reason to fsfreeze.
2000 # Another case where this happens is rpm-ostree, which performs its own sync/fsfreeze
2001 # globally. See e.g. https://github.com/ostreedev/ostree/commit/8642ef5ab3fec3ac8eb8f193054852f83a8bc4d0
2002 if test -d /run/systemd/system; then
2003 if ! sync "$outfile" 2> /dev/null; then
2004 dinfo "dracut: sync operation on newly created initramfs $outfile failed"
2005 exit 1
2006 fi
2007
2008 # use fsfreeze only if we're not writing to /
2009 if [[ "$(stat -c %m -- "$outfile")" != "/" && "$(stat -f -c %T -- "$outfile")" != "msdos" ]]; then
2010 if ! $(fsfreeze -f $(dirname "$outfile") 2>/dev/null && fsfreeze -u $(dirname "$outfile") 2>/dev/null); then
2011 dinfo "dracut: warning: could not fsfreeze $(dirname "$outfile")"
2012 fi
2013 fi
2014 fi
2015
2016 exit 0