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