+# feature to provide Debian backports repos {{{
+backportrepos() {
+ if [ -n "$BACKPORTREPOS" ] ; then
+ if ! checkbackports "$RELEASE" ; then
+ echo "Backports for ${RELEASE} are not available." >&2
+ exit 1
+ else
+ # user might have provided their own apt sources.list
+ if ! grep -q backports /etc/apt/sources.list.d/backports.list 2>/dev/null ; then
+ cat >> /etc/apt/sources.list.d/backports.list << EOF
+# debian backports: ${RELEASE}-backports repository:
+deb http://backports.debian.org/debian-backports ${RELEASE}-backports main
+deb-src http://backports.debian.org/debian-backports ${RELEASE}-backports main
+EOF
+ fi
+ fi
+ fi
+}
+# }}}
+
+# set up kernel-img.conf {{{
+kernelimg_conf() {
+ if ! [ -r /etc/kernel-img.conf ] ; then
+ echo "Setting up /etc/kernel-img.conf"
+ cat > /etc/kernel-img.conf << EOF
+# Kernel Image management overrides
+# See kernel-img.conf(5) for details
+do_initrd = Yes
+do_symlinks = Yes
+EOF
+ fi
+}
+# }}}
+
+# make sure services do not start up {{{
+install_policy_rcd() {
+ if ! [ -r /usr/sbin/policy-rc.d ] ; then
+ export POLICYRCD=1
+ cat > /usr/sbin/policy-rc.d << EOF
+#!/bin/sh
+exit 101
+EOF
+ chmod 775 /usr/sbin/policy-rc.d
+ fi
+}
+# }}}
+
+# make sure we have an up2date system {{{
+upgrade_system() {
+ if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
+ echo "Running update + upgrade"
+ $APTUPDATE
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
+ else
+ echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
+ fi
+}
+
+# }}}
+# remove now useless apt cache {{{
+remove_apt_cache() {
+ if [ "$RM_APTCACHE" = 'yes' ] ; then
+ echo "Cleaning apt cache."
+ apt-get clean $DPKG_OPTIONS
+ else
+ echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
+ fi
+}
+# }}}
+
+# install additional packages {{{
+packages() {
+ # Pre-seed the debconf database with answers. Each question will be marked
+ # as seen to prevent debconf from asking the question interactively.
+ [ -f /etc/debootstrap/debconf-selections ] && {
+ echo "Preseeding the debconf database, some lines might be skipped..."
+ debconf-set-selections < /etc/debootstrap/debconf-selections
+ }
+
+ if [ "$PACKAGES" = 'yes' ] ; then
+ if ! [ -r /etc/debootstrap/packages ] ; then
+ echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
+ exit 1
+ else
+ $APTUPDATE
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
+ fi
+ fi
+}
+# }}}
+
+# install extra packages {{{
+extrapackages() {
+ if [ "$EXTRAPACKAGES" = 'yes' ] ; then
+ PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
+ if [ -n "$PACKAGELIST" ]; then
+ dpkg -i $PACKAGELIST
+ # run apt again to resolve any deps
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
+ fi
+ fi
+}
+# }}}
+
+# check if the specified Debian package exists
+package_exists() {
+ output=$(apt-cache show "$1" 2>/dev/null)
+ [ -n "$output" ]
+ return $?
+}
+
+
+# determine the kernel version postfix
+get_kernel_version() {
+ # do not override $KERNEL if set via config file
+ if [ -n "$KERNEL" ] ; then
+ echo "$KERNEL"
+ return 0
+ fi
+
+ local KARCH
+
+ case "$ARCH" in
+ i386)
+ case "$RELEASE" in
+ lenny|squeeze|wheezy) KARCH='686' ;;
+ # since jessie the linux-image-686 image doesn't exist any longer
+ *) KARCH='686-pae' ;;
+ esac
+ ;;
+ amd64)
+ KARCH='amd64'
+ ;;
+ *)
+ echo "Only i386 and amd64 are currently supported" >&2
+ return 1
+ esac
+
+ for KPREFIX in "" "2.6-" ; do # iterate through the kernel prefixes,
+ # currently "" and "2.6-"
+ if package_exists linux-image-${KPREFIX}${KARCH} ; then
+ echo ${KPREFIX}${KARCH}
+ return 0
+ fi
+
+ done
+}
+
+# install kernel packages {{{
+kernel() {
+ if [ -n "$NOKERNEL" ] ; then
+ echo "Skipping installation of kernel packages as requested via --nokernel"
+ return 0
+ fi
+
+ $APTUPDATE
+ KVER=$(get_kernel_version)
+ if [ -n "$KVER" ] ; then
+ # note: install busybox to be able to debug initramfs
+ KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free"
+ # only add firmware-linux if we have non-free as a component
+ if expr "$COMPONENTS" : '.*non-free' >/dev/null ; then
+ KERNELPACKAGES="$KERNELPACKAGES firmware-linux"
+ fi
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
+ else
+ echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
+ fi
+}
+# }}}
+
+# reconfigure packages {{{
+reconfigure() {
+ if [ -n "$RECONFIGURE" ] ; then
+ for package in $RECONFIGURE ; do
+ if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
+ echo "Warning: $package does not exist, can not reconfigure it."
+ fi
+ done
+ fi
+}
+# }}}
+
+# set password of user root {{{
+passwords()
+{
+ if [ -n "$NOPASSWORD" ] ; then
+ echo "Skip setting root password as requested."
+ return 0
+ fi
+
+ echo "Activating shadow passwords."
+ shadowconfig on
+
+ CHPASSWD_OPTION=
+ if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
+ CHPASSWD_OPTION='-m'
+ fi
+
+ if [ -n "$ROOTPASSWORD" ] ; then
+ echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
+ export ROOTPASSWORD=''
+ else
+ a='1'
+ b='2'
+ echo "Setting password for user root:"
+ while [ "$a" != "$b" ] ; do
+ printf "Enter new UNIX password for user root: "
+ askpass
+ a="$resp"
+ unset resp
+ echo
+ printf "Retype new UNIX password for user root: "
+ askpass
+ b="$resp"
+ unset resp
+ echo
+ if [ "$a" != "$b" ] ; then
+ echo "Sorry, passwords do not match. Retry."
+ a='1'
+ b='2'
+ else
+ echo root:"$a" | chpasswd $CHPASSWD_OPTION
+ unset a
+ unset b
+ fi
+ done
+ fi
+}
+# }}}
+
+# set up /etc/hosts {{{
+hosts() {
+ if [ -f /etc/hosts ] ; then
+ sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
+ [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
+ else
+ cat > /etc/hosts << EOF
+127.0.0.1 localhost $HOSTNAME
+
+#127.0.0.1 localhost
+#127.0.1.1 $HOSTNAME.example.org $HOSTNAME
+
+# The following lines are desirable for IPv6 capable hosts
+#::1 ip6-localhost ip6-loopback $HOSTNAME
+::1 ip6-localhost ip6-loopback
+fe00::0 ip6-localnet
+ff00::0 ip6-mcastprefix
+ff02::1 ip6-allnodes
+ff02::2 ip6-allrouters
+ff02::3 ip6-allhosts
+EOF
+ fi
+}
+# }}}
+
+# set default locales {{{
+default_locales() {
+ if [ -n "$DEFAULT_LOCALES" ] ; then
+ if ! [ -x /usr/sbin/update-locale ] ; then
+ echo "Warning: update-locale executable not available (no locales package installed?)"
+ echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
+ return 0
+ fi
+
+ /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
+ fi
+}
+# }}}
+
+# adjust timezone {{{
+timezone() {
+ if [ -n "$TIMEZONE" ] ; then
+ echo "Adjusting /etc/localtime"
+ ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
+ fi
+}
+# }}}
+
+# helper function for fstab() {{{
+createfstab(){
+ echo "Setting up /etc/fstab"
+if [ -n "$TARGET_UUID" ] ; then
+ echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
+else
+ echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab