version inc
[dracut.git] / dracut
bloba50e14a28d7ea39c824ab7053d1e51a8d0208c6c
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 --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.
53 --strip Strip binaries in the initramfs
54 --nostrip Do not strip binaries in the initramfs (default)
55 --mdadmconf Include local /etc/mdadm.conf
56 --nomdadmconf Do not include local /etc/mdadm.conf
57 --lvmconf Include local /etc/lvm/lvm.conf
58 --nolvmconf Do not include local /etc/lvm/lvm.conf
59 -h, --help This message
60 --debug Output debug information of the build process
61 -v, --verbose Verbose output during the build process
62 -c, --conf [FILE] Specify configuration file to use.
63 Default: /etc/dracut.conf
64 --confdir [DIR] Specify configuration directory to use *.conf files from.
65 Default: /etc/dracut.conf.d
66 -l, --local Local mode. Use modules from the current working
67 directory instead of the system-wide installed in
68 /usr/share/dracut/modules.d.
69 Useful when running dracut from a git checkout.
70 -H, --hostonly Host-Only mode: Install only what is needed for
71 booting the local host instead of a generic host.
72 --fstab Use /etc/fstab to determine the root device.
73 -i, --include [SOURCE] [TARGET]
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.
81 while (($# > 0)); do
82 case $1 in
83 -f|--force) force=yes;;
84 -m|--modules) dracutmodules_l="$dracutmodules_l $2"; shift;;
85 -o|--omit) omit_dracutmodules_l="$omit_dracutmodules_l $2"; shift;;
86 -a|--add) add_dracutmodules_l="$add_dracutmodules_l $2"; shift;;
87 -d|--drivers) drivers_l="$drivers_l $2"; shift;;
88 --add-drivers) add_drivers_l="$add_drivers_l $2"; shift;;
89 --filesystems) filesystems_l="$filesystems_l $2"; shift;;
90 -k|--kmoddir) drivers_dir_l="$2"; shift;;
91 --fwdir) fw_dir_l="$fw_dir_l:$2"; shift;;
92 --kernel-only) kernel_only="yes"; no_kernel="no";;
93 --no-kernel) kernel_only="no"; no_kernel="yes";;
94 --ignore-kernel-modules) kernel_only="no"; no_kernel="yes"
95 ignore_kmodules="yes"
96 omit_dracutmodules_l+=\ kernel-modules
98 --strip) do_strip_l="yes";;
99 --nostrip) do_strip_l="no";;
100 --mdadmconf) mdadmconf_l="yes";;
101 --nomdadmconf) mdadmconf_l="no";;
102 --lvmconf) lvmconf_l="yes";;
103 --nolvmconf) lvmconf_l="no";;
104 -h|--help) usage; exit 1 ;;
105 --debug) debug="yes";;
106 -v|--verbose) beverbose="yes";;
107 -c|--conf) conffile="$2"; shift;;
108 --confdir) confdir="$2"; shift;;
109 -l|--local) allowlocal="yes" ;;
110 -H|--hostonly) hostonly_l="yes" ;;
111 --fstab) use_fstab_l="yes" ;;
112 -i|--include) include_src="$2"; include_target="$3"; shift 2;;
113 -I|--install) install_items="$2"; shift;;
114 -*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage; exit 1;;
115 *) break ;;
116 esac
117 shift
118 done
120 PATH=/sbin:/bin:/usr/sbin:/usr/bin
121 export PATH
123 [[ $debug ]] && {
124 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
125 set -x
128 [[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
130 [[ $allowlocal && -f "$(readlink -f $(dirname $0))/dracut-functions" ]] && dracutbasedir="$(dirname $0)"
132 # if we were not passed a config file, try the default one
133 if [[ ! -f $conffile ]]; then
134 [[ $allowlocal ]] || conffile="/etc/dracut.conf"
135 [[ $allowlocal ]] && conffile="$dracutbasedir/dracut.conf"
138 if [[ ! -d $confdir ]]; then
139 [[ $allowlocal ]] || confdir="/etc/dracut.conf.d"
140 [[ $allowlocal ]] && confdir="$dracutbasedir/dracut.conf.d"
143 # source our config file
144 [[ -f $conffile ]] && . "$conffile"
146 # source our config dir
147 if [ "$confdir" ] && [ -d "$confdir" ]; then
148 for f in "$confdir"/*.conf; do
149 [ -e "$f" ] && . "$f"
150 done
153 # these optins add to the stuff in the config file
154 [[ $add_dracutmodules_l ]] && add_dracutmodules+=" $add_dracutmodules_l"
155 [[ $add_drivers_l ]] && add_drivers+=" $add_drivers_l"
157 # these options override the stuff in the config file
158 [[ $dracutmodules_l ]] && dracutmodules=$dracutmodules_l
159 [[ $omit_dracutmodules_l ]] && omit_dracutmodules=$omit_dracutmodules_l
160 [[ $drivers_l ]] && drivers=$drivers_l
161 [[ $filesystems_l ]] && filesystems=$filesystems_l
162 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
163 [[ $fw_dir_l ]] && fw_dir=$fw_dir_l
164 [[ $do_strip_l ]] && do_strip=$do_strip_l
165 [[ $hostonly_l ]] && hostonly=$hostonly_l
166 [[ $use_fstab_l ]] && use_fstab=$use_fstab_l
167 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
168 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
169 [[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
170 [[ $fw_dir ]] || fw_dir=/lib/firmware
171 [[ $do_strip ]] || do_strip=no
172 # eliminate IFS hackery when messing with fw_dir
173 fw_dir=${fw_dir//:/ }
175 [[ $hostonly = yes ]] && hostonly="-h"
176 [[ $hostonly != "-h" ]] && unset hostonly
178 if [[ -f $dracutbasedir/dracut-functions ]]; then
179 . $dracutbasedir/dracut-functions
180 else
181 echo "Cannot find $dracutbasedir/dracut-functions. Are you running from a git checkout?"
182 echo "Try passing -l as an argument to $0"
183 exit 1
186 dracutfunctions=$dracutbasedir/dracut-functions
187 export dracutfunctions
189 # This is kinda legacy -- eventually it should go away.
190 case $dracutmodules in
191 ""|auto) dracutmodules="all" ;;
192 esac
194 [[ $2 ]] && kernel=$2 || kernel=$(uname -r)
195 [[ $1 ]] && outfile=$1 || outfile="/boot/initramfs-$kernel.img"
196 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
198 srcmods="/lib/modules/$kernel/"
199 [[ $drivers_dir ]] && {
200 if vercmp $(modprobe --version | cut -d' ' -f3) lt 3.7; then
201 derror 'To use --kmoddir option module-init-tools >= 3.7 is required.'
202 exit 1
204 srcmods="$drivers_dir"
206 export srcmods
208 if [[ -f $outfile && ! $force ]]; then
209 echo "Will not override existing initramfs ($outfile) without --force"
210 exit 1
213 outdir=$(dirname "$outfile")
214 if ! [[ -d "$outdir" ]]; then
215 echo "Can't write $outfile: Directory $outdir does not exist."
216 exit 1
219 if ! [[ -w "$outdir" ]]; then
220 echo "No permission to write $outdir."
221 exit 1
224 if [[ -f "$outfile" ]] && ! [[ -w "$outfile" ]]; then
225 echo "No permission to write $outfile."
226 exit 1
229 hookdirs="cmdline pre-udev pre-trigger netroot pre-mount pre-pivot mount emergency"
231 [[ -n "$TMPDIR" ]] && ! [[ -w "$TMPDIR" ]] && unset TMPDIR
232 readonly initdir=$(mktemp -d -t initramfs.XXXXXX)
234 trap 'ret=$?;rm -rf "$initdir";exit $ret;' EXIT # clean up after ourselves no matter how we die.
235 trap 'exit 1;' SIGINT # clean up after ourselves no matter how we die.
237 # Need to be able to have non-root users read stuff (rpcbind etc)
238 chmod 755 "$initdir"
240 export initdir hookdirs dracutbasedir dracutmodules drivers \
241 fw_dir drivers_dir debug beverbose no_kernel kernel_only \
242 add_drivers mdadmconf lvmconf filesystems ignore_kmodules \
243 use_fstab
245 if [[ $kernel_only != yes ]]; then
246 # Create some directory structure first
247 for d in bin sbin usr/bin usr/sbin usr/lib etc proc sys sysroot tmp dev/pts var/run; do
248 inst_dir "/$d";
249 done
252 # check all our modules to see if they should be sourced.
253 # This builds a list of modules that we will install next.
254 check_modules
256 # source our modules.
257 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
258 mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
259 if strstr "$mods_to_load" " $mod "; then
260 dinfo "*** Sourcing module $mod"
261 if [[ $kernel_only = yes ]]; then
262 [[ -x $moddir/installkernel ]] && . "$moddir/installkernel"
263 else
264 . "$moddir/install"
265 if [[ $no_kernel != yes && -x $moddir/installkernel ]]; then
266 . "$moddir/installkernel"
269 mods_to_load=${mods_to_load// $mod /}
271 done
272 unset moddir
274 ## final stuff that has to happen
276 # generate module dependencies for the initrd
277 if [[ -d $initdir/lib/modules/$kernel ]]; then
278 if ! depmod -a -b "$initdir" $kernel; then
279 derror "\"depmod -a $kernel\" failed."
280 exit 1
284 if [[ $include_src && $include_target ]]; then
285 mkdir -p "$initdir$include_target"
286 cp -a -t "$initdir$include_target" "$include_src"/*
289 for item in $install_items; do
290 dracut_install "$item"
291 done
292 unset item
294 # make sure that library links are correct and up to date
295 cp -ar /etc/ld.so.conf* "$initdir"/etc
296 ldconfig -r "$initdir" || [[ $(id -u) != "0" ]] && dinfo "ldconfig might need uid=0 (root) for chroot()"
298 [[ $beverbose = yes ]] && (du -c "$initdir" | sort -n)
300 # strip binaries
301 if [[ $do_strip = yes ]] ; then
302 for p in strip grep find; do
303 if ! which $p >/dev/null 2>&1; then
304 derror "Could not find '$p'. You should run $0 with '--nostrip'."
305 do_strip=no
307 done
310 if [[ $do_strip = yes ]] ; then
311 for f in $(find "$initdir" -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 -or -path '*/lib/modules/*.ko' \) ); do
312 dinfo "Stripping $f"
313 strip -g "$f" 2>/dev/null|| :
315 # FIXME: only strip -g for now
317 #strip -g --strip-unneeded "$f" || :
318 #note="-R .note"
319 #if objdump -h $f | grep '^[ ]*[0-9]*[ ]*.note[ ]' -A 1 | \
320 # grep -q ALLOC; then
321 # note=
323 #strip -R .comment $note "$f" || :
324 done
327 type hardlink &>/dev/null && {
328 hardlink "$initdir" 2>&1
331 type pigz &>/dev/null && gzip=pigz || gzip=gzip
332 ( cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet |$gzip -9 > "$outfile"; )
333 if [ $? -ne 0 ]; then
334 derror "dracut: creation of $outfile failed"
335 exit 1
338 [[ $beverbose = yes ]] && ls -lh "$outfile"
340 exit 0