2008-12-18 23:59:55 +03:00
#!/bin/bash
2010-09-10 17:23:06 +04:00
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh
2008-12-18 01:51:15 +03:00
#
# Generator script for a dracut initramfs
# Tries to retain some degree of compatibility with the command line
# of the various mkinitrd implementations out there
#
2009-02-13 15:41:50 +03:00
2010-09-10 17:23:06 +04:00
# Copyright 2005-2010 Red Hat, Inc. All rights reserved.
2009-07-24 17:12:47 +04:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2008-12-11 00:32:21 +03:00
2009-05-19 18:47:03 +04:00
usage() {
# 80x25 linebreak here ^
2010-09-10 17:23:06 +04:00
cat << EOF
Usage: $0 [OPTION]... <initramfs> <kernel-version>
2009-05-19 18:47:03 +04:00
Creates initial ramdisk images for preloading modules
2009-05-24 06:10:52 +04:00
-f, --force Overwrite existing initramfs file.
2009-05-19 18:47:03 +04:00
-m, --modules [LIST] Specify a space-separated list of dracut modules to
call when building the initramfs. Modules are located
2009-06-19 15:36:30 +04:00
in /usr/share/dracut/modules.d.
2009-05-24 06:10:52 +04:00
-o, --omit [LIST] Omit a space-separated list of dracut modules.
2009-07-02 13:32:32 +04:00
-a, --add [LIST] Add a space-separated list of dracut modules.
2009-05-19 18:47:03 +04:00
-d, --drivers [LIST] Specify a space-separated list of kernel modules to
2009-09-09 19:02:37 +04:00
exclusively include in the initramfs.
--add-drivers [LIST] Specify a space-separated list of kernel
modules to add to the initramfs.
2009-11-03 13:50:55 +03:00
--filesystems [LIST] Specify a space-separated list of kernel filesystem
modules to exclusively include in the generic
initramfs.
2009-07-24 15:09:57 +04:00
-k, --kmoddir [DIR] Specify the directory, where to look for kernel
modules
2009-07-22 14:43:26 +04:00
--fwdir [DIR] Specify additional directories, where to look for
firmwares, separated by :
--kernel-only Only install kernel drivers and firmware files
--no-kernel Do not install kernel drivers and firmware files
2010-05-07 12:44:30 +04:00
--strip Strip binaries in the initramfs
--nostrip Do not strip binaries in the initramfs (default)
2009-09-15 11:00:13 +04:00
--mdadmconf Include local /etc/mdadm.conf
--nomdadmconf Do not include local /etc/mdadm.conf
2009-09-18 13:40:12 +04:00
--lvmconf Include local /etc/lvm/lvm.conf
2010-09-10 17:23:06 +04:00
--nolvmconf Do not include local /etc/lvm/lvm.conf
2009-05-19 18:47:03 +04:00
-h, --help This message
2009-05-20 11:03:01 +04:00
--debug Output debug information of the build process
2009-05-19 18:47:03 +04:00
-v, --verbose Verbose output during the build process
-c, --conf [FILE] Specify configuration file to use.
Default: /etc/dracut.conf
2010-09-10 17:23:06 +04:00
--confdir [DIR] Specify configuration directory to use *.conf files
from. Default: /etc/dracut.conf.d
2009-05-19 18:47:03 +04:00
-l, --local Local mode. Use modules from the current working
directory instead of the system-wide installed in
2009-06-19 15:36:30 +04:00
/usr/share/dracut/modules.d.
2009-05-19 18:47:03 +04:00
Useful when running dracut from a git checkout.
2010-08-06 15:54:34 +04:00
-H, --hostonly Host-Only mode: Install only what is needed for
2009-05-19 18:47:03 +04:00
booting the local host instead of a generic host.
2010-08-06 15:54:34 +04:00
--fstab Use /etc/fstab to determine the root device.
2009-05-19 18:47:03 +04:00
-i, --include [SOURCE] [TARGET]
2009-05-24 06:10:52 +04:00
Include the files in the SOURCE directory into the
Target directory in the final initramfs.
-I, --install [LIST] Install the space separated list of files into the
initramfs.
2010-08-14 23:23:25 +04:00
--gzip Compress the generated initramfs using gzip.
2010-09-10 17:23:06 +04:00
This will be done by default, unless another
compression option or --no-compress is passed.
2010-08-14 23:23:25 +04:00
--bzip2 Compress the generated initramfs using bzip2.
2010-09-10 17:23:06 +04:00
Make sure your kernel has bzip2 decompression support
compiled in, otherwise you will not be able to boot.
--lzma Compress the generated initramfs using lzma.
Make sure your kernel has lzma support compiled in,
otherwise you will not be able to boot.
2010-08-14 23:23:25 +04:00
--no-compress Do not compress the generated initramfs. This will
2010-09-10 17:23:06 +04:00
override any other compression options.
2010-09-17 10:36:26 +04:00
--list-modules List all available dracut modules.
2010-09-10 17:23:06 +04:00
EOF
2009-05-19 18:47:03 +04:00
}
2010-08-14 23:23:26 +04:00
# Little helper function for reading args from the commandline.
# it automatically handles -a b and -a=b variants, and returns 1 if
# we need to shift $3.
read_arg() {
# $1 = arg name
# $2 = arg value
# $3 = arg parameter
local rematch='^[^=]*=(.*)$'
if [[ $2 =~ $rematch ]]; then
2010-09-10 17:23:06 +04:00
read "$1" <<< "${BASH_REMATCH[1]}"
2010-08-14 23:23:26 +04:00
else
2010-09-10 17:23:06 +04:00
read "$1" <<< "$3"
# There is no way to shift our callers args, so
# return 1 to indicate they should do it instead.
return 1
2010-08-14 23:23:26 +04:00
fi
}
2009-02-13 15:41:52 +03:00
while (($# > 0)); do
2010-08-14 23:23:26 +04:00
case ${1%%=*} in
2010-09-10 17:23:06 +04:00
-m|--modules) read_arg dracutmodules_l "$@" || shift;;
-o|--omit) read_arg omit_dracutmodules_l "$@" || shift;;
-a|--add) read_arg add_dracutmodules_l "$@" || shift;;
-d|--drivers) read_arg drivers_l "$@" || shift;;
--add-drivers) read_arg add_drivers_l "$@" || shift;;
--filesystems) read_arg filesystems_l "$@" || shift;;
-k|--kmoddir) read_arg drivers_dir_l "$@" || shift;;
-c|--conf) read_arg conffile "$@" || shift;;
--confdir) read_arg confdir "$@" || shift;;
-I|--install) read_arg install_items "$@" || shift;;
--fwdir) read_arg fw_dir_l "$@" || shift;;
-f|--force) force=yes;;
--kernel-only) kernel_only="yes"; no_kernel="no";;
--no-kernel) kernel_only="no"; no_kernel="yes";;
--strip) do_strip_l="yes";;
--nostrip) do_strip_l="no";;
2010-08-14 23:23:26 +04:00
--mdadmconf) mdadmconf_l="yes";;
--nomdadmconf) mdadmconf_l="no";;
--lvmconf) lvmconf_l="yes";;
--nolvmconf) lvmconf_l="no";;
2010-09-10 17:23:06 +04:00
--debug) debug="yes";;
-v|--verbose) beverbose="yes";;
-l|--local) allowlocal="yes" ;;
-H|--hostonly) hostonly_l="yes" ;;
--fstab) use_fstab_l="yes" ;;
-h|--help) usage; exit 1 ;;
-i|--include) include_src="$2"; include_target="$3"; shift 2;;
2010-08-14 23:23:26 +04:00
--bzip2) [[ $compress != cat ]] && compress="bzip2 -9";;
2010-08-23 14:06:28 +04:00
--lzma) [[ $compress != cat ]] && compress="lzma -9";;
2010-08-14 23:23:26 +04:00
--no-compress) compress="cat";;
2010-08-14 23:23:26 +04:00
--gzip) if [[ $compress != cat ]]; then
type pigz > /dev/null 2>&1 && compress="pigz -9" || \
compress="gzip -9"
fi;;
2010-09-17 10:36:26 +04:00
--list-modules)
do_list="yes";
;;
2010-09-10 17:23:06 +04:00
-*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage; exit 1;;
*) break ;;
2008-12-18 01:51:15 +03:00
esac
2009-02-13 15:41:52 +03:00
shift
2008-12-18 01:51:15 +03:00
done
2009-03-07 13:53:40 +03:00
2009-09-17 18:27:12 +04:00
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH
2009-07-02 18:03:39 +04:00
2009-06-05 06:35:31 +04:00
[[ $debug ]] && {
export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
set -x
}
2009-10-27 11:28:53 +03:00
[[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
2010-09-10 17:23:06 +04:00
[[ $allowlocal && -f "$(readlink -f ${0%/*})/dracut-functions" ]] && \
dracutbasedir="${0%/*}"
2009-10-09 15:15:32 +04:00
2009-05-23 06:24:29 +04:00
# if we were not passed a config file, try the default one
2009-10-09 15:15:32 +04:00
if [[ ! -f $conffile ]]; then
2010-08-14 23:23:24 +04:00
[[ $allowlocal ]] && conffile="$dracutbasedir/dracut.conf" || \
conffile="/etc/dracut.conf"
2009-10-09 15:15:32 +04:00
fi
2009-05-23 06:24:29 +04:00
2010-02-12 13:12:37 +03:00
if [[ ! -d $confdir ]]; then
2010-08-23 13:58:39 +04:00
[[ $allowlocal ]] && confdir="$dracutbasedir/dracut.conf.d" || \
2010-08-14 23:23:24 +04:00
confdir="/etc/dracut.conf.d"
2010-02-12 13:12:37 +03:00
fi
2010-07-14 03:30:16 +04:00
# source our config file
[[ -f $conffile ]] && . "$conffile"
2010-02-12 13:12:37 +03:00
# source our config dir
2010-08-14 23:23:24 +04:00
if [[ $confdir && -d $confdir ]]; then
2010-02-12 13:12:37 +03:00
for f in "$confdir"/*.conf; do
2010-09-10 17:23:06 +04:00
[[ -e $f ]] && . "$f"
2010-02-12 13:12:37 +03:00
done
fi
2010-05-03 13:35:10 +04:00
# these optins add to the stuff in the config file
[[ $add_dracutmodules_l ]] && add_dracutmodules+=" $add_dracutmodules_l"
[[ $add_drivers_l ]] && add_drivers+=" $add_drivers_l"
2009-05-23 06:24:29 +04:00
# these options override the stuff in the config file
2009-03-07 14:18:09 +03:00
[[ $dracutmodules_l ]] && dracutmodules=$dracutmodules_l
2009-05-20 16:02:44 +04:00
[[ $omit_dracutmodules_l ]] && omit_dracutmodules=$omit_dracutmodules_l
2009-05-28 22:27:02 +04:00
[[ $drivers_l ]] && drivers=$drivers_l
2009-11-03 13:50:55 +03:00
[[ $filesystems_l ]] && filesystems=$filesystems_l
2009-07-21 19:49:17 +04:00
[[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
[[ $fw_dir_l ]] && fw_dir=$fw_dir_l
2009-07-24 17:48:20 +04:00
[[ $do_strip_l ]] && do_strip=$do_strip_l
2009-08-31 13:39:21 +04:00
[[ $hostonly_l ]] && hostonly=$hostonly_l
2010-08-06 15:54:34 +04:00
[[ $use_fstab_l ]] && use_fstab=$use_fstab_l
2009-09-15 11:00:13 +04:00
[[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
2009-09-18 13:40:12 +04:00
[[ $lvmconf_l ]] && lvmconf=$lvmconf_l
2009-06-19 15:36:30 +04:00
[[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
2009-07-22 14:43:26 +04:00
[[ $fw_dir ]] || fw_dir=/lib/firmware
2010-05-07 12:44:30 +04:00
[[ $do_strip ]] || do_strip=no
2009-08-15 23:17:19 +04:00
# eliminate IFS hackery when messing with fw_dir
fw_dir=${fw_dir//:/ }
2009-03-06 23:06:48 +03:00
2009-08-31 13:39:21 +04:00
[[ $hostonly = yes ]] && hostonly="-h"
2009-11-27 16:29:32 +03:00
[[ $hostonly != "-h" ]] && unset hostonly
2010-08-14 23:23:25 +04:00
[[ $compress ]] || compress="gzip -9"
2009-08-31 13:39:21 +04:00
2009-10-27 11:28:53 +03:00
if [[ -f $dracutbasedir/dracut-functions ]]; then
2010-09-10 17:23:06 +04:00
. $dracutbasedir/dracut-functions
2009-05-19 18:46:33 +04:00
else
2011-02-23 15:32:31 +03:00
echo "Cannot find $dracutbasedir/dracut-functions." >&2
echo "Are you running from a git checkout?" >&2
echo "Try passing -l as an argument to $0" >&2
2010-09-10 17:23:06 +04:00
exit 1
2009-05-19 18:46:33 +04:00
fi
2009-05-25 16:09:57 +04:00
2009-10-27 11:28:53 +03:00
dracutfunctions=$dracutbasedir/dracut-functions
2009-03-07 16:01:32 +03:00
export dracutfunctions
2009-03-06 23:06:48 +03:00
2010-09-17 10:36:26 +04:00
[[ $do_list = yes ]] && {
for mod in $dracutbasedir/modules.d/*; do
[[ -d $mod ]] || continue;
[[ -e $mod/install || -e $mod/installkernel ]] || continue;
echo ${mod##*/??}
done
exit 0
}
2010-08-18 22:06:44 +04:00
# Detect lib paths
[[ $libdir ]] || for libdir in /lib64 /lib; do
[[ -d $libdir ]] && break
done || {
derror 'No lib directory?!!!'
exit 1
}
[[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
[[ -d $usrlibdir ]] && break
done || dwarning 'No usr/lib directory!'
2009-05-30 07:14:31 +04:00
# This is kinda legacy -- eventually it should go away.
2009-05-23 06:24:29 +04:00
case $dracutmodules in
2009-05-30 07:14:31 +04:00
""|auto) dracutmodules="all" ;;
2009-05-23 06:24:29 +04:00
esac
2009-03-07 14:18:09 +03:00
2009-02-13 15:41:54 +03:00
[[ $2 ]] && kernel=$2 || kernel=$(uname -r)
2010-02-18 20:27:39 +03:00
[[ $1 ]] && outfile=$1 || outfile="/boot/initramfs-$kernel.img"
abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
2008-12-11 00:32:21 +03:00
2009-07-21 20:26:12 +04:00
srcmods="/lib/modules/$kernel/"
2010-06-09 12:36:05 +04:00
[[ $drivers_dir ]] && {
2010-06-11 23:10:34 +04:00
if vercmp $(modprobe --version | cut -d' ' -f3) lt 3.7; then
2010-06-09 12:36:05 +04:00
derror 'To use --kmoddir option module-init-tools >= 3.7 is required.'
exit 1
fi
srcmods="$drivers_dir"
}
2009-07-21 20:26:12 +04:00
export srcmods
2009-02-13 15:41:54 +03:00
if [[ -f $outfile && ! $force ]]; then
2011-02-02 15:38:07 +03:00
derror "Will not override existing initramfs ($outfile) without --force"
2008-12-11 00:32:21 +03:00
exit 1
fi
2010-08-14 23:23:24 +04:00
outdir=${outfile%/*}
if [[ ! -d "$outdir" ]]; then
2011-02-02 15:38:07 +03:00
derror "Can't write $outfile: Directory $outdir does not exist."
2010-02-18 20:27:39 +03:00
exit 1
2010-08-14 23:23:24 +04:00
elif [[ ! -w "$outdir" ]]; then
2011-02-02 15:38:07 +03:00
derror "No permission to write $outdir."
2010-02-18 20:27:39 +03:00
exit 1
2010-08-23 13:57:39 +04:00
elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
2011-02-02 15:38:07 +03:00
derror "No permission to write $outfile."
2009-11-19 17:46:09 +03:00
exit 1
fi
2010-09-10 17:23:06 +04:00
hookdirs="cmdline pre-udev pre-trigger netroot pre-mount"
hookdirs+=" pre-pivot mount emergency"
2009-02-27 03:02:52 +03:00
2010-08-14 23:23:26 +04:00
[[ $TMPDIR && ! -w $TMPDIR ]] && unset TMPDIR
2009-02-27 03:02:52 +03:00
readonly initdir=$(mktemp -d -t initramfs.XXXXXX)
2009-11-19 17:46:09 +03:00
2010-09-10 17:23:06 +04:00
# clean up after ourselves no matter how we die.
trap 'ret=$?;rm -rf "$initdir";exit $ret;' EXIT
# clean up after ourselves no matter how we die.
trap 'exit 1;' SIGINT
2008-12-11 00:32:21 +03:00
2009-05-23 06:53:10 +04:00
# Need to be able to have non-root users read stuff (rpcbind etc)
chmod 755 "$initdir"
2009-10-27 11:28:53 +03:00
export initdir hookdirs dracutbasedir dracutmodules drivers \
2009-09-09 19:02:37 +04:00
fw_dir drivers_dir debug beverbose no_kernel kernel_only \
2010-10-13 21:39:13 +04:00
add_drivers mdadmconf lvmconf filesystems \
2010-08-18 22:06:44 +04:00
use_fstab libdir usrlibdir
2009-02-13 15:43:24 +03:00
2009-08-15 07:32:27 +04:00
if [[ $kernel_only != yes ]]; then
2009-07-22 14:43:26 +04:00
# Create some directory structure first
2010-09-10 17:23:06 +04:00
for d in bin sbin usr/bin usr/sbin usr/lib etc \
proc sys sysroot tmp dev/pts var/run; do
inst_dir "/$d";
2009-07-22 14:43:26 +04:00
done
fi
2009-02-13 15:43:07 +03:00
2009-05-30 07:14:31 +04:00
# check all our modules to see if they should be sourced.
# This builds a list of modules that we will install next.
2011-02-02 15:34:58 +03:00
check_module_dir
2010-09-10 17:23:06 +04:00
2009-10-29 12:29:58 +03:00
# source our modules.
2009-10-27 11:28:53 +03:00
for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
2009-05-24 09:29:44 +04:00
mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
2009-05-30 07:14:31 +04:00
if strstr "$mods_to_load" " $mod "; then
2010-09-10 17:23:06 +04:00
dinfo "*** Sourcing module $mod"
if [[ $kernel_only = yes ]]; then
2011-02-02 15:34:58 +03:00
module_installkernel $mod
2010-09-10 17:23:06 +04:00
else
2011-02-02 15:34:58 +03:00
module_install $mod
if [[ $no_kernel != yes ]]; then
module_installkernel $mod
2010-09-10 17:23:06 +04:00
fi
fi
mods_to_load=${mods_to_load// $mod /}
2009-05-30 07:14:31 +04:00
fi
2009-02-13 15:43:12 +03:00
done
2009-02-27 03:02:47 +03:00
unset moddir
2009-01-05 22:40:34 +03:00
2009-02-13 15:43:07 +03:00
## final stuff that has to happen
2009-02-13 15:42:33 +03:00
2009-02-13 15:43:07 +03:00
# generate module dependencies for the initrd
2010-08-14 23:23:25 +04:00
if [[ -d $initdir/lib/modules/$kernel ]] && \
! depmod -a -b "$initdir" $kernel; then
derror "\"depmod -a $kernel\" failed."
exit 1
2009-05-23 06:24:29 +04:00
fi
2008-12-11 00:32:21 +03:00
2009-05-23 06:24:29 +04:00
if [[ $include_src && $include_target ]]; then
2009-03-16 12:42:02 +03:00
mkdir -p "$initdir$include_target"
cp -a -t "$initdir$include_target" "$include_src"/*
2009-05-23 06:24:29 +04:00
fi
2009-03-16 12:42:02 +03:00
2009-05-24 06:10:52 +04:00
for item in $install_items; do
2010-09-10 17:23:06 +04:00
dracut_install "$item"
2009-05-24 06:10:52 +04:00
done
unset item
2010-03-05 13:14:28 +03:00
# make sure that library links are correct and up to date
cp -ar /etc/ld.so.conf* "$initdir"/etc
2010-09-10 17:23:06 +04:00
ldconfig -r "$initdir" || [[ $UID != "0" ]] && \
dinfo "ldconfig might need uid=0 (root) for chroot()"
2010-03-05 13:14:28 +03:00
2009-08-15 07:32:27 +04:00
[[ $beverbose = yes ]] && (du -c "$initdir" | sort -n)
2009-05-15 14:45:58 +04:00
2009-07-24 17:48:20 +04:00
# strip binaries
2009-08-15 07:32:27 +04:00
if [[ $do_strip = yes ]] ; then
2010-05-07 13:03:55 +04:00
for p in strip grep find; do
2010-09-10 17:23:06 +04:00
if ! type -P $p >/dev/null; then
derror "Could not find '$p'. You should run $0 with '--nostrip'."
do_strip=no
fi
2009-07-24 17:48:20 +04:00
done
fi
2009-08-15 07:32:27 +04:00
if [[ $do_strip = yes ]] ; then
2010-09-10 17:23:06 +04:00
for f in $(find "$initdir" -type f \
\( -perm -0100 -or -perm -0010 -or -perm -0001 \
-or -path '*/lib/modules/*.ko' \) ); do
dinfo "Stripping $f"
strip -g "$f" 2>/dev/null|| :
2009-07-24 17:48:20 +04:00
done
fi
2010-08-02 11:39:13 +04:00
type hardlink &>/dev/null && {
2010-09-10 17:23:06 +04:00
hardlink "$initdir" 2>&1
2010-08-02 11:39:13 +04:00
}
2010-08-14 23:23:25 +04:00
if ! ( cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet | \
$compress > "$outfile"; ); then
2009-11-19 17:46:09 +03:00
derror "dracut: creation of $outfile failed"
exit 1
fi
2009-05-15 14:45:58 +04:00
2009-08-15 07:32:27 +04:00
[[ $beverbose = yes ]] && ls -lh "$outfile"
2009-05-25 19:45:15 +04:00
2009-05-28 06:41:18 +04:00
exit 0