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