#!/bin/bash
# 
# Generator script for a dracut initramfs
# Tries to retain some degree of compatibility with the command line
# of the various mkinitrd implementations out there
#

# Copyright 2008, Red Hat, Inc.  Jeremy Katz <katzj@redhat.com>
# GPLv2 header here


usage() {
#                                                       80x25 linebreak here ^
	echo "Usage: $0 [OPTION]... <initramfs> <kernel-version>
Creates initial ramdisk images for preloading modules

  -f, --force           Overwrite existing initramfs file.
  -m, --modules [LIST]  Specify a space-separated list of dracut modules to
                         call when building the initramfs. Modules are located
                         in /usr/share/dracut/modules.d.
  -o, --omit [LIST]     Omit a space-separated list of dracut modules.
  -a, --add [LIST]      Add a space-separated list of dracut modules.
  -d, --drivers [LIST]  Specify a space-separated list of kernel modules to
                         include in the initramfs.
  -h, --help            This message
  --debug               Output debug information of the build process
  -v, --verbose         Verbose output during the build process
  -c, --conf [FILE]     Specify configuration file to use.
                         Default: /etc/dracut.conf
  -l, --local           Local mode. Use modules from the current working
                         directory instead of the system-wide installed in
                         /usr/share/dracut/modules.d.
                         Useful when running dracut from a git checkout.
  -H, --hostonly          Host-Only mode: Install only what is needed for
                         booting the local host instead of a generic host.
  -i, --include [SOURCE] [TARGET]
                        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.
"
}

while (($# > 0)); do
    case $1 in
	-f|--force) force=yes;;
	-m|--modules) dracutmodules_l="$2"; shift;;
	-o|--omit) omit_dracutmodules_l="$2"; shift;;
	-a|--add) add_dracutmodules_l="$2"; shift;;
	-d|--drivers) drivers_l="$2"; shift;;
	-h|--help) usage; exit 1 ;;
	--debug) debug="yes";;
	-v|--verbose) beverbose="yes";;
	-c|--conf) conffile="$2"; shift;;
	-l|--local) allowlocal="yes" ;;
	-H|--hostonly) hostonly="-h" ;;
	-i|--include) include_src="$2"; include_target="$3"; shift 2;;
	-I|--install) install_items="$2"; shift;;
	*) break ;;
    esac
    shift
done

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH

[[ $debug ]] && { 
    export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
    set -x
}

# if we were not passed a config file, try the default one
[[ ! -f $conffile ]] && conffile="/etc/dracut.conf"

# source our config file
[[ -f $conffile ]] && . "$conffile"

# these options override the stuff in the config file
[[ $dracutmodules_l ]] && dracutmodules=$dracutmodules_l
[[ $omit_dracutmodules_l ]] && omit_dracutmodules=$omit_dracutmodules_l
[[ $add_dracutmodules_l ]] && add_dracutmodules="$add_dracutmodules $add_dracutmodules_l"
[[ $drivers_l ]] && drivers=$drivers_l
[[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut

[[ $allowlocal && -f "$(dirname $0)/dracut-functions" ]] && dsrc="$(dirname $0)" || dsrc=$dracutbasedir

if [[ -f $dsrc/dracut-functions ]]; then
   . $dsrc/dracut-functions
else
   echo "Cannot find $dsrc/dracut-functions. Are you running from a git checkout?"
   echo "Try passing -l as an argument to $0"
   exit 1
fi

dracutfunctions=$dsrc/dracut-functions
export dracutfunctions

# This is kinda legacy -- eventually it should go away.
case $dracutmodules in
    ""|auto) dracutmodules="all" ;;
esac

[[ $2 ]] && kernel=$2 || kernel=$(uname -r)
[[ $1 ]] && outfile=$(readlink -f $1) || outfile="/boot/initrd-$kernel.img"

if [[ -f $outfile && ! $force ]]; then
    echo "Will not override existing initramfs ($outfile) without --force"
    exit 1
fi

hookdirs="cmdline pre-udev pre-trigger netroot pre-mount pre-pivot mount emergency"

readonly initdir=$(mktemp -d -t initramfs.XXXXXX)
trap 'rm -rf "$initdir"' 0 # clean up after ourselves no matter how we die.

# Need to be able to have non-root users read stuff (rpcbind etc)
chmod 755 "$initdir"

export initdir hookdirs dsrc dracutmodules drivers debug beverbose

# Create some directory structure first
for d in bin sbin usr/bin usr/sbin usr/lib etc proc sys sysroot tmp dev/pts var/run; do 
    mkdir -p "$initdir/$d"; 
done

# check all our modules to see if they should be sourced.
# This builds a list of modules that we will install next.
check_modules
  
#source our modules.
for moddir in "$dsrc/modules.d"/[0-9][0-9]*; do
    mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
    if strstr "$mods_to_load" " $mod "; then
	. "$moddir/install"
	mods_to_load=${mods_to_load// $mod /}
    fi
done
unset moddir
echo $mods_to_load

## final stuff that has to happen

# generate module dependencies for the initrd
if [ -d "$initdir/lib/modules/$kernel" ]; then
    if ! depmod -a -b "$initdir" $kernel; then
        echo "\"depmod -a $kernel\" failed."
        exit 1
    fi
fi

# make sure that library links are correct and up to date
ldconfig -n -r "$initdir" /lib* /usr/lib*

if [[ $include_src && $include_target ]]; then
    mkdir -p "$initdir$include_target"
    cp -a -t "$initdir$include_target" "$include_src"/*
fi

for item in $install_items; do
   dracut_install "$item"
done
unset item

[[ "$beverbose" = "yes" ]] && (du -c "$initdir" | sort -n)

( cd "$initdir"; find . |cpio -H newc -o |gzip -9 > "$outfile"; )

[[ "$beverbose" = "yes" ]] && ls -lh "$outfile"

exit 0

