lvm: also handle LVM1 volumes
[dracut.git] / dracut
blob9c1223a8359d41d3fffaa53bb445148ec5999a8d
1 #!/bin/bash
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
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
10 # Copyright 2005-2010 Red Hat, Inc. All rights reserved.
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.
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.
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/>.
27 usage() {
28 # 80x25 linebreak here ^
29 cat << EOF
30 Usage: $0 [OPTION]... <initramfs> <kernel-version>
31 Creates initial ramdisk images for preloading modules
33 -f, --force Overwrite existing initramfs file.
34 -m, --modules [LIST] Specify a space-separated list of dracut modules to
35 call when building the initramfs. Modules are located
36 in /usr/share/dracut/modules.d.
37 -o, --omit [LIST] Omit a space-separated list of dracut modules.
38 -a, --add [LIST] Add a space-separated list of dracut modules.
39 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
40 exclusively include in the initramfs.
41 --add-drivers [LIST] Specify a space-separated list of kernel
42 modules to add to the initramfs.
43 --filesystems [LIST] Specify a space-separated list of kernel filesystem
44 modules to exclusively include in the generic
45 initramfs.
46 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
47 modules
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
52 --ignore-kernel-modules
53 Don't try to load modules. It automatically implies
54 '--no-kernel'. It's assumed that everything needed
55 is built into kernel.
56 --strip Strip binaries in the initramfs
57 --nostrip Do not strip binaries in the initramfs (default)
58 --mdadmconf Include local /etc/mdadm.conf
59 --nomdadmconf Do not include local /etc/mdadm.conf
60 --lvmconf Include local /etc/lvm/lvm.conf
61 --nolvmconf Do not include local /etc/lvm/lvm.conf
62 -h, --help This message
63 --debug Output debug information of the build process
64 -v, --verbose Verbose output during the build process
65 -c, --conf [FILE] Specify configuration file to use.
66 Default: /etc/dracut.conf
67 --confdir [DIR] Specify configuration directory to use *.conf files
68 from. Default: /etc/dracut.conf.d
69 -l, --local Local mode. Use modules from the current working
70 directory instead of the system-wide installed in
71 /usr/share/dracut/modules.d.
72 Useful when running dracut from a git checkout.
73 -H, --hostonly Host-Only mode: Install only what is needed for
74 booting the local host instead of a generic host.
75 --fstab Use /etc/fstab to determine the root device.
76 -i, --include [SOURCE] [TARGET]
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.
81 --gzip Compress the generated initramfs using gzip.
82 This will be done by default, unless another
83 compression option or --no-compress is passed.
84 --bzip2 Compress the generated initramfs using bzip2.
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.
90 --no-compress Do not compress the generated initramfs. This will
91 override any other compression options.
92 --list-modules List all available dracut modules.
93 EOF
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.
99 read_arg() {
100 # $1 = arg name
101 # $2 = arg value
102 # $3 = arg parameter
103 local rematch='^[^=]*=(.*)$'
104 if [[ $2 =~ $rematch ]]; then
105 read "$1" <<< "${BASH_REMATCH[1]}"
106 else
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
114 while (($# > 0)); do
115 case ${1%%=*} in
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";;
132 --mdadmconf) mdadmconf_l="yes";;
133 --nomdadmconf) mdadmconf_l="no";;
134 --lvmconf) lvmconf_l="yes";;
135 --nolvmconf) lvmconf_l="no";;
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;;
143 --bzip2) [[ $compress != cat ]] && compress="bzip2 -9";;
144 --lzma) [[ $compress != cat ]] && compress="lzma -9";;
145 --no-compress) compress="cat";;
146 --gzip) if [[ $compress != cat ]]; then
147 type pigz > /dev/null 2>&1 && compress="pigz -9" || \
148 compress="gzip -9"
149 fi;;
150 --ignore-kernel-modules) kernel_only="no"; no_kernel="yes"
151 ignore_kmodules="yes"
152 omit_dracutmodules_l+=\ kernel-modules
154 --list-modules)
155 do_list="yes";
157 -*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage; exit 1;;
158 *) break ;;
159 esac
160 shift
161 done
163 PATH=/sbin:/bin:/usr/sbin:/usr/bin
164 export PATH
166 [[ $debug ]] && {
167 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
168 set -x
171 [[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
173 [[ $allowlocal && -f "$(readlink -f ${0%/*})/dracut-functions" ]] && \
174 dracutbasedir="${0%/*}"
176 # if we were not passed a config file, try the default one
177 if [[ ! -f $conffile ]]; then
178 [[ $allowlocal ]] && conffile="$dracutbasedir/dracut.conf" || \
179 conffile="/etc/dracut.conf"
182 if [[ ! -d $confdir ]]; then
183 [[ $allowlocal ]] && confdir="$dracutbasedir/dracut.conf.d" || \
184 confdir="/etc/dracut.conf.d"
187 # source our config file
188 [[ -f $conffile ]] && . "$conffile"
190 # source our config dir
191 if [[ $confdir && -d $confdir ]]; then
192 for f in "$confdir"/*.conf; do
193 [[ -e $f ]] && . "$f"
194 done
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"
201 # these options override the stuff in the config file
202 [[ $dracutmodules_l ]] && dracutmodules=$dracutmodules_l
203 [[ $omit_dracutmodules_l ]] && omit_dracutmodules=$omit_dracutmodules_l
204 [[ $drivers_l ]] && drivers=$drivers_l
205 [[ $filesystems_l ]] && filesystems=$filesystems_l
206 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
207 [[ $fw_dir_l ]] && fw_dir=$fw_dir_l
208 [[ $do_strip_l ]] && do_strip=$do_strip_l
209 [[ $hostonly_l ]] && hostonly=$hostonly_l
210 [[ $use_fstab_l ]] && use_fstab=$use_fstab_l
211 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
212 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
213 [[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
214 [[ $fw_dir ]] || fw_dir=/lib/firmware
215 [[ $do_strip ]] || do_strip=no
216 # eliminate IFS hackery when messing with fw_dir
217 fw_dir=${fw_dir//:/ }
219 [[ $hostonly = yes ]] && hostonly="-h"
220 [[ $hostonly != "-h" ]] && unset hostonly
221 [[ $compress ]] || compress="gzip -9"
223 if [[ -f $dracutbasedir/dracut-functions ]]; then
224 . $dracutbasedir/dracut-functions
225 else
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
232 dracutfunctions=$dracutbasedir/dracut-functions
233 export dracutfunctions
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
244 # Detect lib paths
245 [[ $libdir ]] || for libdir in /lib64 /lib; do
246 [[ -d $libdir ]] && break
247 done || {
248 derror 'No lib directory?!!!'
249 exit 1
251 [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
252 [[ -d $usrlibdir ]] && break
253 done || dwarning 'No usr/lib directory!'
255 # This is kinda legacy -- eventually it should go away.
256 case $dracutmodules in
257 ""|auto) dracutmodules="all" ;;
258 esac
260 [[ $2 ]] && kernel=$2 || kernel=$(uname -r)
261 [[ $1 ]] && outfile=$1 || outfile="/boot/initramfs-$kernel.img"
262 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
264 srcmods="/lib/modules/$kernel/"
265 [[ $drivers_dir ]] && {
266 if vercmp $(modprobe --version | cut -d' ' -f3) lt 3.7; then
267 derror 'To use --kmoddir option module-init-tools >= 3.7 is required.'
268 exit 1
270 srcmods="$drivers_dir"
272 export srcmods
274 if [[ -f $outfile && ! $force ]]; then
275 echo "Will not override existing initramfs ($outfile) without --force"
276 exit 1
279 outdir=${outfile%/*}
280 if [[ ! -d "$outdir" ]]; then
281 echo "Can't write $outfile: Directory $outdir does not exist."
282 exit 1
283 elif [[ ! -w "$outdir" ]]; then
284 echo "No permission to write $outdir."
285 exit 1
286 elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
287 echo "No permission to write $outfile."
288 exit 1
291 hookdirs="cmdline pre-udev pre-trigger netroot pre-mount"
292 hookdirs+=" pre-pivot mount emergency"
294 [[ $TMPDIR && ! -w $TMPDIR ]] && unset TMPDIR
295 readonly initdir=$(mktemp -d -t initramfs.XXXXXX)
297 # clean up after ourselves no matter how we die.
298 trap 'ret=$?;rm -rf "$initdir";exit $ret;' EXIT
299 # clean up after ourselves no matter how we die.
300 trap 'exit 1;' SIGINT
302 # Need to be able to have non-root users read stuff (rpcbind etc)
303 chmod 755 "$initdir"
305 export initdir hookdirs dracutbasedir dracutmodules drivers \
306 fw_dir drivers_dir debug beverbose no_kernel kernel_only \
307 add_drivers mdadmconf lvmconf filesystems ignore_kmodules \
308 use_fstab libdir usrlibdir
310 if [[ $kernel_only != yes ]]; then
311 # Create some directory structure first
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";
315 done
318 # check all our modules to see if they should be sourced.
319 # This builds a list of modules that we will install next.
320 check_modules
322 # source our modules.
323 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
324 mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
325 if strstr "$mods_to_load" " $mod "; then
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"
335 mods_to_load=${mods_to_load// $mod /}
337 done
338 unset moddir
340 ## final stuff that has to happen
342 # generate module dependencies for the initrd
343 if [[ -d $initdir/lib/modules/$kernel ]] && \
344 ! depmod -a -b "$initdir" $kernel; then
345 derror "\"depmod -a $kernel\" failed."
346 exit 1
349 if [[ $include_src && $include_target ]]; then
350 mkdir -p "$initdir$include_target"
351 cp -a -t "$initdir$include_target" "$include_src"/*
354 for item in $install_items; do
355 dracut_install "$item"
356 done
357 unset item
359 # make sure that library links are correct and up to date
360 cp -ar /etc/ld.so.conf* "$initdir"/etc
361 ldconfig -r "$initdir" || [[ $UID != "0" ]] && \
362 dinfo "ldconfig might need uid=0 (root) for chroot()"
364 [[ $beverbose = yes ]] && (du -c "$initdir" | sort -n)
366 # strip binaries
367 if [[ $do_strip = yes ]] ; then
368 for p in strip grep find; do
369 if ! type -P $p >/dev/null; then
370 derror "Could not find '$p'. You should run $0 with '--nostrip'."
371 do_strip=no
373 done
376 if [[ $do_strip = yes ]] ; then
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|| :
382 done
385 type hardlink &>/dev/null && {
386 hardlink "$initdir" 2>&1
389 if ! ( cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet | \
390 $compress > "$outfile"; ); then
391 derror "dracut: creation of $outfile failed"
392 exit 1
395 [[ $beverbose = yes ]] && ls -lh "$outfile"
397 exit 0