]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut
init: symlink /dev/stdin /dev/stdout /dev/stderr
[thirdparty/dracut.git] / dracut
CommitLineData
c0815e4e 1#!/bin/bash
cc02093d
HH
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
641cc356
JK
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#
70c26b7f 9
cc02093d 10# Copyright 2005-2010 Red Hat, Inc. All rights reserved.
4f18fe82
HH
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#
ec9315e5 25
5616feb0
AT
26
27usage() {
28# 80x25 linebreak here ^
cc02093d
HH
29 cat << EOF
30Usage: $0 [OPTION]... <initramfs> <kernel-version>
5616feb0
AT
31Creates initial ramdisk images for preloading modules
32
39ff0682 33 -f, --force Overwrite existing initramfs file.
5616feb0
AT
34 -m, --modules [LIST] Specify a space-separated list of dracut modules to
35 call when building the initramfs. Modules are located
73198d53 36 in /usr/share/dracut/modules.d.
39ff0682 37 -o, --omit [LIST] Omit a space-separated list of dracut modules.
3e17f33b 38 -a, --add [LIST] Add a space-separated list of dracut modules.
5616feb0 39 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
cb47caf7
HH
40 exclusively include in the initramfs.
41 --add-drivers [LIST] Specify a space-separated list of kernel
42 modules to add to the initramfs.
8fa510d4
DH
43 --filesystems [LIST] Specify a space-separated list of kernel filesystem
44 modules to exclusively include in the generic
45 initramfs.
2b9dfbbe
HH
46 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
47 modules
33ee031c
HH
48 --fwdir [DIR] Specify additional directories, where to look for
49 firmwares, separated by :
50 --kernel-only Only install kernel drivers and firmware files
51 --no-kernel Do not install kernel drivers and firmware files
0ca3a5ee
52 --ignore-kernel-modules
53 Don't try to load modules. It automatically implies
cc02093d
HH
54 '--no-kernel'. It's assumed that everything needed
55 is built into kernel.
5be225d2
HH
56 --strip Strip binaries in the initramfs
57 --nostrip Do not strip binaries in the initramfs (default)
2f02ae9d
HH
58 --mdadmconf Include local /etc/mdadm.conf
59 --nomdadmconf Do not include local /etc/mdadm.conf
7a34efa5 60 --lvmconf Include local /etc/lvm/lvm.conf
cc02093d 61 --nolvmconf Do not include local /etc/lvm/lvm.conf
5616feb0 62 -h, --help This message
00531568 63 --debug Output debug information of the build process
5616feb0
AT
64 -v, --verbose Verbose output during the build process
65 -c, --conf [FILE] Specify configuration file to use.
66 Default: /etc/dracut.conf
cc02093d
HH
67 --confdir [DIR] Specify configuration directory to use *.conf files
68 from. Default: /etc/dracut.conf.d
5616feb0
AT
69 -l, --local Local mode. Use modules from the current working
70 directory instead of the system-wide installed in
73198d53 71 /usr/share/dracut/modules.d.
5616feb0 72 Useful when running dracut from a git checkout.
7c179686 73 -H, --hostonly Host-Only mode: Install only what is needed for
5616feb0 74 booting the local host instead of a generic host.
7c179686 75 --fstab Use /etc/fstab to determine the root device.
5616feb0 76 -i, --include [SOURCE] [TARGET]
39ff0682
AT
77 Include the files in the SOURCE directory into the
78 Target directory in the final initramfs.
79 -I, --install [LIST] Install the space separated list of files into the
80 initramfs.
5b158ad3 81 --gzip Compress the generated initramfs using gzip.
cc02093d
HH
82 This will be done by default, unless another
83 compression option or --no-compress is passed.
5b158ad3 84 --bzip2 Compress the generated initramfs using bzip2.
cc02093d
HH
85 Make sure your kernel has bzip2 decompression support
86 compiled in, otherwise you will not be able to boot.
87 --lzma Compress the generated initramfs using lzma.
88 Make sure your kernel has lzma support compiled in,
89 otherwise you will not be able to boot.
5b158ad3 90 --no-compress Do not compress the generated initramfs. This will
cc02093d 91 override any other compression options.
5b11bb73 92 --list-modules List all available dracut modules.
cc02093d 93EOF
5616feb0
AT
94}
95
5bc545ed
VL
96# Little helper function for reading args from the commandline.
97# it automatically handles -a b and -a=b variants, and returns 1 if
98# we need to shift $3.
99read_arg() {
100 # $1 = arg name
101 # $2 = arg value
102 # $3 = arg parameter
103 local rematch='^[^=]*=(.*)$'
104 if [[ $2 =~ $rematch ]]; then
cc02093d 105 read "$1" <<< "${BASH_REMATCH[1]}"
5bc545ed 106 else
cc02093d
HH
107 read "$1" <<< "$3"
108 # There is no way to shift our callers args, so
109 # return 1 to indicate they should do it instead.
110 return 1
5bc545ed
VL
111 fi
112}
113
b368a5f3 114while (($# > 0)); do
5bc545ed 115 case ${1%%=*} in
cc02093d
HH
116 -m|--modules) read_arg dracutmodules_l "$@" || shift;;
117 -o|--omit) read_arg omit_dracutmodules_l "$@" || shift;;
118 -a|--add) read_arg add_dracutmodules_l "$@" || shift;;
119 -d|--drivers) read_arg drivers_l "$@" || shift;;
120 --add-drivers) read_arg add_drivers_l "$@" || shift;;
121 --filesystems) read_arg filesystems_l "$@" || shift;;
122 -k|--kmoddir) read_arg drivers_dir_l "$@" || shift;;
123 -c|--conf) read_arg conffile "$@" || shift;;
124 --confdir) read_arg confdir "$@" || shift;;
125 -I|--install) read_arg install_items "$@" || shift;;
126 --fwdir) read_arg fw_dir_l "$@" || shift;;
127 -f|--force) force=yes;;
128 --kernel-only) kernel_only="yes"; no_kernel="no";;
129 --no-kernel) kernel_only="no"; no_kernel="yes";;
130 --strip) do_strip_l="yes";;
131 --nostrip) do_strip_l="no";;
690396a5
VL
132 --mdadmconf) mdadmconf_l="yes";;
133 --nomdadmconf) mdadmconf_l="no";;
134 --lvmconf) lvmconf_l="yes";;
135 --nolvmconf) lvmconf_l="no";;
cc02093d
HH
136 --debug) debug="yes";;
137 -v|--verbose) beverbose="yes";;
138 -l|--local) allowlocal="yes" ;;
139 -H|--hostonly) hostonly_l="yes" ;;
140 --fstab) use_fstab_l="yes" ;;
141 -h|--help) usage; exit 1 ;;
142 -i|--include) include_src="$2"; include_target="$3"; shift 2;;
690396a5 143 --bzip2) [[ $compress != cat ]] && compress="bzip2 -9";;
eaa62cd5 144 --lzma) [[ $compress != cat ]] && compress="lzma -9";;
690396a5 145 --no-compress) compress="cat";;
b2ff4317
VL
146 --gzip) if [[ $compress != cat ]]; then
147 type pigz > /dev/null 2>&1 && compress="pigz -9" || \
148 compress="gzip -9"
149 fi;;
5bc545ed
VL
150 --ignore-kernel-modules) kernel_only="no"; no_kernel="yes"
151 ignore_kmodules="yes"
152 omit_dracutmodules_l+=\ kernel-modules
153 ;;
5b11bb73
HH
154 --list-modules)
155 do_list="yes";
156 ;;
cc02093d
HH
157 -*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage; exit 1;;
158 *) break ;;
641cc356 159 esac
b368a5f3 160 shift
641cc356 161done
4cba351e 162
f8545d04
HH
163PATH=/sbin:/bin:/usr/sbin:/usr/bin
164export PATH
a55711cd 165
c36ce334
VL
166[[ $debug ]] && {
167 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
168 set -x
169}
170
5d791c0e
HH
171[[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
172
cc02093d
HH
173[[ $allowlocal && -f "$(readlink -f ${0%/*})/dracut-functions" ]] && \
174 dracutbasedir="${0%/*}"
42c71947 175
f1336ac7 176# if we were not passed a config file, try the default one
42c71947 177if [[ ! -f $conffile ]]; then
eebc929a
VL
178 [[ $allowlocal ]] && conffile="$dracutbasedir/dracut.conf" || \
179 conffile="/etc/dracut.conf"
42c71947 180fi
f1336ac7 181
2c2c4580 182if [[ ! -d $confdir ]]; then
ae24b114 183 [[ $allowlocal ]] && confdir="$dracutbasedir/dracut.conf.d" || \
eebc929a 184 confdir="/etc/dracut.conf.d"
2c2c4580
HH
185fi
186
2245f372
AB
187# source our config file
188[[ -f $conffile ]] && . "$conffile"
189
2c2c4580 190# source our config dir
6438b4fe 191if [[ $confdir && -d $confdir ]]; then
2c2c4580 192 for f in "$confdir"/*.conf; do
cc02093d 193 [[ -e $f ]] && . "$f"
2c2c4580
HH
194 done
195fi
196
d87c2708
HH
197# these optins add to the stuff in the config file
198[[ $add_dracutmodules_l ]] && add_dracutmodules+=" $add_dracutmodules_l"
199[[ $add_drivers_l ]] && add_drivers+=" $add_drivers_l"
200
f1336ac7 201# these options override the stuff in the config file
e12aac5e 202[[ $dracutmodules_l ]] && dracutmodules=$dracutmodules_l
3274a8f9 203[[ $omit_dracutmodules_l ]] && omit_dracutmodules=$omit_dracutmodules_l
e19d6bf6 204[[ $drivers_l ]] && drivers=$drivers_l
8fa510d4 205[[ $filesystems_l ]] && filesystems=$filesystems_l
26537a5b
HH
206[[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
207[[ $fw_dir_l ]] && fw_dir=$fw_dir_l
31f7db66 208[[ $do_strip_l ]] && do_strip=$do_strip_l
ba726310 209[[ $hostonly_l ]] && hostonly=$hostonly_l
7c179686 210[[ $use_fstab_l ]] && use_fstab=$use_fstab_l
2f02ae9d 211[[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
7a34efa5 212[[ $lvmconf_l ]] && lvmconf=$lvmconf_l
73198d53 213[[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
33ee031c 214[[ $fw_dir ]] || fw_dir=/lib/firmware
5be225d2 215[[ $do_strip ]] || do_strip=no
ddfd1d10
VL
216# eliminate IFS hackery when messing with fw_dir
217fw_dir=${fw_dir//:/ }
9a8a00cf 218
ba726310 219[[ $hostonly = yes ]] && hostonly="-h"
ba67b923 220[[ $hostonly != "-h" ]] && unset hostonly
5b158ad3 221[[ $compress ]] || compress="gzip -9"
ba726310 222
5d791c0e 223if [[ -f $dracutbasedir/dracut-functions ]]; then
cc02093d 224 . $dracutbasedir/dracut-functions
adbc8a42 225else
cc02093d
HH
226 echo "Cannot find $dracutbasedir/dracut-functions."
227 echo "Are you running from a git checkout?"
228 echo "Try passing -l as an argument to $0"
229 exit 1
adbc8a42 230fi
22fd1627 231
5d791c0e 232dracutfunctions=$dracutbasedir/dracut-functions
5cad5bb5 233export dracutfunctions
9a8a00cf 234
5b11bb73
HH
235[[ $do_list = yes ]] && {
236 for mod in $dracutbasedir/modules.d/*; do
237 [[ -d $mod ]] || continue;
238 [[ -e $mod/install || -e $mod/installkernel ]] || continue;
239 echo ${mod##*/??}
240 done
241 exit 0
242}
243
fa5cd2bf
244# Detect lib paths
245[[ $libdir ]] || for libdir in /lib64 /lib; do
246 [[ -d $libdir ]] && break
247done || {
248 derror 'No lib directory?!!!'
249 exit 1
250}
251[[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
252 [[ -d $usrlibdir ]] && break
253done || dwarning 'No usr/lib directory!'
254
66ac3cd1 255# This is kinda legacy -- eventually it should go away.
f1336ac7 256case $dracutmodules in
66ac3cd1 257 ""|auto) dracutmodules="all" ;;
f1336ac7 258esac
e12aac5e 259
c8937ec4 260[[ $2 ]] && kernel=$2 || kernel=$(uname -r)
454771cd
HH
261[[ $1 ]] && outfile=$1 || outfile="/boot/initramfs-$kernel.img"
262abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
ec9315e5 263
f24a2d46 264srcmods="/lib/modules/$kernel/"
ecf42850 265[[ $drivers_dir ]] && {
22ecea45 266 if vercmp $(modprobe --version | cut -d' ' -f3) lt 3.7; then
ecf42850
267 derror 'To use --kmoddir option module-init-tools >= 3.7 is required.'
268 exit 1
269 fi
270 srcmods="$drivers_dir"
271}
f24a2d46
HH
272export srcmods
273
c8937ec4 274if [[ -f $outfile && ! $force ]]; then
641cc356 275 echo "Will not override existing initramfs ($outfile) without --force"
ec9315e5
JK
276 exit 1
277fi
278
ebfdb219
VL
279outdir=${outfile%/*}
280if [[ ! -d "$outdir" ]]; then
454771cd
HH
281 echo "Can't write $outfile: Directory $outdir does not exist."
282 exit 1
ebfdb219 283elif [[ ! -w "$outdir" ]]; then
454771cd
HH
284 echo "No permission to write $outdir."
285 exit 1
0a325a91 286elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
734a0d9e
HH
287 echo "No permission to write $outfile."
288 exit 1
289fi
290
cc02093d
HH
291hookdirs="cmdline pre-udev pre-trigger netroot pre-mount"
292hookdirs+=" pre-pivot mount emergency"
49c68fa4 293
b2ff4317 294[[ $TMPDIR && ! -w $TMPDIR ]] && unset TMPDIR
49c68fa4 295readonly initdir=$(mktemp -d -t initramfs.XXXXXX)
734a0d9e 296
cc02093d
HH
297# clean up after ourselves no matter how we die.
298trap 'ret=$?;rm -rf "$initdir";exit $ret;' EXIT
299# clean up after ourselves no matter how we die.
300trap 'exit 1;' SIGINT
ec9315e5 301
f6f74096
DD
302# Need to be able to have non-root users read stuff (rpcbind etc)
303chmod 755 "$initdir"
304
5d791c0e 305export initdir hookdirs dracutbasedir dracutmodules drivers \
cb47caf7 306 fw_dir drivers_dir debug beverbose no_kernel kernel_only \
7c179686 307 add_drivers mdadmconf lvmconf filesystems ignore_kmodules \
fa5cd2bf 308 use_fstab libdir usrlibdir
f4fff04e 309
98adb06e 310if [[ $kernel_only != yes ]]; then
33ee031c 311 # Create some directory structure first
cc02093d
HH
312 for d in bin sbin usr/bin usr/sbin usr/lib etc \
313 proc sys sysroot tmp dev/pts var/run; do
314 inst_dir "/$d";
33ee031c
HH
315 done
316fi
0f86847d 317
66ac3cd1
VL
318# check all our modules to see if they should be sourced.
319# This builds a list of modules that we will install next.
0c2e3d12 320check_modules
cc02093d 321
95bde758 322# source our modules.
5d791c0e 323for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
0c2e3d12 324 mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
66ac3cd1 325 if strstr "$mods_to_load" " $mod "; then
cc02093d
HH
326 dinfo "*** Sourcing module $mod"
327 if [[ $kernel_only = yes ]]; then
328 [[ -x $moddir/installkernel ]] && . "$moddir/installkernel"
329 else
330 . "$moddir/install"
331 if [[ $no_kernel != yes && -x $moddir/installkernel ]]; then
332 . "$moddir/installkernel"
333 fi
334 fi
335 mods_to_load=${mods_to_load// $mod /}
66ac3cd1 336 fi
15136762 337done
20abd914 338unset moddir
aabc0553 339
0f86847d 340## final stuff that has to happen
bc6b0dec 341
0f86847d 342# generate module dependencies for the initrd
8a474569
VL
343if [[ -d $initdir/lib/modules/$kernel ]] && \
344 ! depmod -a -b "$initdir" $kernel; then
345 derror "\"depmod -a $kernel\" failed."
346 exit 1
f1336ac7 347fi
ec9315e5 348
f1336ac7 349if [[ $include_src && $include_target ]]; then
88ffd2df
VL
350 mkdir -p "$initdir$include_target"
351 cp -a -t "$initdir$include_target" "$include_src"/*
f1336ac7 352fi
88ffd2df 353
39ff0682 354for item in $install_items; do
cc02093d 355 dracut_install "$item"
39ff0682
AT
356done
357unset item
358
fdc421db
HH
359# make sure that library links are correct and up to date
360cp -ar /etc/ld.so.conf* "$initdir"/etc
cc02093d
HH
361ldconfig -r "$initdir" || [[ $UID != "0" ]] && \
362 dinfo "ldconfig might need uid=0 (root) for chroot()"
fdc421db 363
98adb06e 364[[ $beverbose = yes ]] && (du -c "$initdir" | sort -n)
c4ad7fff 365
31f7db66 366# strip binaries
98adb06e 367if [[ $do_strip = yes ]] ; then
d0ced35f 368 for p in strip grep find; do
cc02093d
HH
369 if ! type -P $p >/dev/null; then
370 derror "Could not find '$p'. You should run $0 with '--nostrip'."
371 do_strip=no
372 fi
31f7db66
HH
373 done
374fi
375
98adb06e 376if [[ $do_strip = yes ]] ; then
cc02093d
HH
377 for f in $(find "$initdir" -type f \
378 \( -perm -0100 -or -perm -0010 -or -perm -0001 \
379 -or -path '*/lib/modules/*.ko' \) ); do
380 dinfo "Stripping $f"
381 strip -g "$f" 2>/dev/null|| :
31f7db66
HH
382 done
383fi
384
6292ee9d 385type hardlink &>/dev/null && {
cc02093d 386 hardlink "$initdir" 2>&1
6292ee9d
HH
387}
388
937f678e
VL
389if ! ( cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet | \
390 $compress > "$outfile"; ); then
734a0d9e
HH
391 derror "dracut: creation of $outfile failed"
392 exit 1
393fi
c4ad7fff 394
98adb06e 395[[ $beverbose = yes ]] && ls -lh "$outfile"
1faecdc1 396
3da58569 397exit 0