selinux: fixed error handling for load-policy
[dracut.git] / dracut
blobaea478bc784b319cd528f6e3dd223370df62e5b6
1 #!/bin/bash
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
8 # Copyright 2005-2009 Red Hat, Inc. All rights reserved.
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.
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.
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/>.
25 usage() {
26 # 80x25 linebreak here ^
27 echo "Usage: $0 [OPTION]... <initramfs> <kernel-version>
28 Creates initial ramdisk images for preloading modules
30 -f, --force Overwrite existing initramfs file.
31 -m, --modules [LIST] Specify a space-separated list of dracut modules to
32 call when building the initramfs. Modules are located
33 in /usr/share/dracut/modules.d.
34 -o, --omit [LIST] Omit a space-separated list of dracut modules.
35 -a, --add [LIST] Add a space-separated list of dracut modules.
36 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
37 exclusively include in the initramfs.
38 --add-drivers [LIST] Specify a space-separated list of kernel
39 modules to add to the initramfs.
40 --filesystems [LIST] Specify a space-separated list of kernel filesystem
41 modules to exclusively include in the generic
42 initramfs.
43 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
44 modules
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
49 --strip Strip binaries in the initramfs
50 --nostrip Do not strip binaries in the initramfs (default)
51 --mdadmconf Include local /etc/mdadm.conf
52 --nomdadmconf Do not include local /etc/mdadm.conf
53 --lvmconf Include local /etc/lvm/lvm.conf
54 --nolvmconf Do not include local /etc/lvm/lvm.conf
55 -h, --help This message
56 --debug Output debug information of the build process
57 -v, --verbose Verbose output during the build process
58 -c, --conf [FILE] Specify configuration file to use.
59 Default: /etc/dracut.conf
60 --confdir [DIR] Specify configuration directory to use *.conf files from.
61 Default: /etc/dracut.conf.d
62 -l, --local Local mode. Use modules from the current working
63 directory instead of the system-wide installed in
64 /usr/share/dracut/modules.d.
65 Useful when running dracut from a git checkout.
66 -H, --hostonly Host-Only mode: Install only what is needed for
67 booting the local host instead of a generic host.
68 -i, --include [SOURCE] [TARGET]
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.
76 while (($# > 0)); do
77 case $1 in
78 -f|--force) force=yes;;
79 -m|--modules) dracutmodules_l="$2"; shift;;
80 -o|--omit) omit_dracutmodules_l="$2"; shift;;
81 -a|--add) add_dracutmodules_l="$2"; shift;;
82 -d|--drivers) drivers_l="$2"; shift;;
83 --add-drivers) add_drivers_l="$2"; shift;;
84 --filesystems) filesystems_l="$2"; shift;;
85 -k|--kmoddir) drivers_dir_l="$2"; shift;;
86 --fwdir) fw_dir_l="$2"; shift;;
87 --kernel-only) kernel_only="yes"; no_kernel="no";;
88 --no-kernel) kernel_only="no"; no_kernel="yes";;
89 --strip) do_strip_l="yes";;
90 --nostrip) do_strip_l="no";;
91 --mdadmconf) mdadmconf_l="yes";;
92 --nomdadmconf) mdadmconf_l="no";;
93 --lvmconf) lvmconf_l="yes";;
94 --nolvmconf) lvmconf_l="no";;
95 -h|--help) usage; exit 1 ;;
96 --debug) debug="yes";;
97 -v|--verbose) beverbose="yes";;
98 -c|--conf) conffile="$2"; shift;;
99 --confdir) confdir="$2"; shift;;
100 -l|--local) allowlocal="yes" ;;
101 -H|--hostonly) hostonly_l="yes" ;;
102 -i|--include) include_src="$2"; include_target="$3"; shift 2;;
103 -I|--install) install_items="$2"; shift;;
104 -*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage; exit 1;;
105 *) break ;;
106 esac
107 shift
108 done
110 PATH=/sbin:/bin:/usr/sbin:/usr/bin
111 export PATH
113 [[ $debug ]] && {
114 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
115 set -x
118 [[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
120 [[ $allowlocal && -f "$(readlink -f $(dirname $0))/dracut-functions" ]] && dracutbasedir="$(dirname $0)"
122 # if we were not passed a config file, try the default one
123 if [[ ! -f $conffile ]]; then
124 [[ $allowlocal ]] || conffile="/etc/dracut.conf"
125 [[ $allowlocal ]] && conffile="$dracutbasedir/dracut.conf"
128 if [[ ! -d $confdir ]]; then
129 [[ $allowlocal ]] || confdir="/etc/dracut.conf.d"
130 [[ $allowlocal ]] && confdir="$dracutbasedir/dracut.conf.d"
133 # source our config file
134 [[ -f $conffile ]] && . "$conffile"
136 # source our config dir
137 if [ "$confdir" ] && [ -d "$confdir" ]; then
138 for f in "$confdir"/*.conf; do
139 [ -e "$f" ] && . "$f"
140 done
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"
147 # these options override the stuff in the config file
148 [[ $dracutmodules_l ]] && dracutmodules=$dracutmodules_l
149 [[ $omit_dracutmodules_l ]] && omit_dracutmodules=$omit_dracutmodules_l
150 [[ $drivers_l ]] && drivers=$drivers_l
151 [[ $filesystems_l ]] && filesystems=$filesystems_l
152 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
153 [[ $fw_dir_l ]] && fw_dir=$fw_dir_l
154 [[ $do_strip_l ]] && do_strip=$do_strip_l
155 [[ $hostonly_l ]] && hostonly=$hostonly_l
156 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
157 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
158 [[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
159 [[ $fw_dir ]] || fw_dir=/lib/firmware
160 [[ $do_strip ]] || do_strip=no
161 # eliminate IFS hackery when messing with fw_dir
162 fw_dir=${fw_dir//:/ }
164 [[ $hostonly = yes ]] && hostonly="-h"
165 [[ $hostonly != "-h" ]] && unset hostonly
167 if [[ -f $dracutbasedir/dracut-functions ]]; then
168 . $dracutbasedir/dracut-functions
169 else
170 echo "Cannot find $dracutbasedir/dracut-functions. Are you running from a git checkout?"
171 echo "Try passing -l as an argument to $0"
172 exit 1
175 dracutfunctions=$dracutbasedir/dracut-functions
176 export dracutfunctions
178 # This is kinda legacy -- eventually it should go away.
179 case $dracutmodules in
180 ""|auto) dracutmodules="all" ;;
181 esac
183 [[ $2 ]] && kernel=$2 || kernel=$(uname -r)
184 [[ $1 ]] && outfile=$1 || outfile="/boot/initramfs-$kernel.img"
185 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
187 srcmods="/lib/modules/$kernel/"
188 [[ $drivers_dir ]] && {
189 if vercmp $(modprobe --version | cut -d' ' -f3) lt 3.7; then
190 derror 'To use --kmoddir option module-init-tools >= 3.7 is required.'
191 exit 1
193 srcmods="$drivers_dir"
195 export srcmods
197 if [[ -f $outfile && ! $force ]]; then
198 echo "Will not override existing initramfs ($outfile) without --force"
199 exit 1
202 outdir=$(dirname "$outfile")
203 if ! [[ -d "$outdir" ]]; then
204 echo "Can't write $outfile: Directory $outdir does not exist."
205 exit 1
208 if ! [[ -w "$outdir" ]]; then
209 echo "No permission to write $outdir."
210 exit 1
213 if [[ -f "$outfile" ]] && ! [[ -w "$outfile" ]]; then
214 echo "No permission to write $outfile."
215 exit 1
218 hookdirs="cmdline pre-udev pre-trigger netroot pre-mount pre-pivot mount emergency"
220 [[ -n "$TMPDIR" ]] && ! [[ -w "$TMPDIR" ]] && unset TMPDIR
221 readonly initdir=$(mktemp -d -t initramfs.XXXXXX)
223 trap 'ret=$?;rm -rf "$initdir";exit $ret;' EXIT # clean up after ourselves no matter how we die.
224 trap 'exit 1;' SIGINT # clean up after ourselves no matter how we die.
226 # Need to be able to have non-root users read stuff (rpcbind etc)
227 chmod 755 "$initdir"
229 export initdir hookdirs dracutbasedir dracutmodules drivers \
230 fw_dir drivers_dir debug beverbose no_kernel kernel_only \
231 add_drivers mdadmconf lvmconf filesystems
233 if [[ $kernel_only != yes ]]; then
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
236 inst_dir "/$d";
237 done
240 # check all our modules to see if they should be sourced.
241 # This builds a list of modules that we will install next.
242 check_modules
244 # source our modules.
245 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
246 mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
247 if strstr "$mods_to_load" " $mod "; then
248 dinfo "*** Sourcing module $mod"
249 if [[ $kernel_only = yes ]]; then
250 [[ -x $moddir/installkernel ]] && . "$moddir/installkernel"
251 else
252 . "$moddir/install"
253 if [[ $no_kernel != yes && -x $moddir/installkernel ]]; then
254 . "$moddir/installkernel"
257 mods_to_load=${mods_to_load// $mod /}
259 done
260 unset moddir
262 ## final stuff that has to happen
264 # generate module dependencies for the initrd
265 if [[ -d $initdir/lib/modules/$kernel ]]; then
266 if ! depmod -a -b "$initdir" $kernel; then
267 derror "\"depmod -a $kernel\" failed."
268 exit 1
272 if [[ $include_src && $include_target ]]; then
273 mkdir -p "$initdir$include_target"
274 cp -a -t "$initdir$include_target" "$include_src"/*
277 for item in $install_items; do
278 dracut_install "$item"
279 done
280 unset item
282 # make sure that library links are correct and up to date
283 cp -ar /etc/ld.so.conf* "$initdir"/etc
284 ldconfig -r "$initdir" || [[ $(id -u) != "0" ]] && dinfo "ldconfig might need uid=0 (root) for chroot()"
286 [[ $beverbose = yes ]] && (du -c "$initdir" | sort -n)
288 # strip binaries
289 if [[ $do_strip = yes ]] ; then
290 for p in strip grep find; do
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
295 done
298 if [[ $do_strip = yes ]] ; then
299 for f in $(find "$initdir" -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 -or -path '*/lib/modules/*.ko' \) ); do
300 dinfo "Stripping $f"
301 strip -g "$f" 2>/dev/null|| :
303 # FIXME: only strip -g for now
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=
311 #strip -R .comment $note "$f" || :
312 done
315 type pigz &>/dev/null && gzip=pigz || gzip=gzip
316 ( cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet |$gzip -9 > "$outfile"; )
317 if [ $? -ne 0 ]; then
318 derror "dracut: creation of $outfile failed"
319 exit 1
322 [[ $beverbose = yes ]] && ls -lh "$outfile"
324 exit 0