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