Release new version 0.80
[grml-debootstrap.git] / chroot-script
index 1a74aef..7d8c6d2 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 # Filename:      /etc/debootstrap/chroot-script
 # Purpose:       script executed in chroot when installing Debian via grml-debootstrap
 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
@@ -9,10 +9,19 @@
 # this script as /bin/chroot-script on your new installed system
 ################################################################################
 
+# error_handler {{{
+if [ "$REPORT_TRAP_ERR" = "yes" ] || [ "$FAIL_TRAP_ERR" = "yes" ]; then
+   set -E
+   set -o pipefail
+   trap "error_handler" ERR
+fi
+# }}}
+
 . /etc/debootstrap/config    || exit 1
 . /etc/debootstrap/variables || exit 1
 
 [ -r /proc/1 ] || mount -t proc none /proc
+[ -r /sys/kernel ] || mount -t sysfs none /sys
 
 # variable checks {{{
 
@@ -28,9 +37,9 @@ if [ -x /usr/bin/aptitude ] ; then
       APTUPGRADE="aptitude -y safe-upgrade $DPKG_OPTIONS"
    fi
 else
-   APTINSTALL="apt-get --force-yes -y --no-install-recommends install $DPKG_OPTIONS"
+   APTINSTALL="apt-get -y --no-install-recommends install $DPKG_OPTIONS"
    APTUPDATE="apt-get update $DPKG_OPTIONS"
-   APTUPGRADE="apt-get --force-yes -y upgrade $DPKG_OPTIONS"
+   APTUPGRADE="apt-get -y upgrade $DPKG_OPTIONS"
 fi
 
 if [ -z "$STAGES" ] ; then
@@ -70,7 +79,7 @@ chrootmirror() {
   fi
 
   if [ -z "$COMPONENTS" ] ; then
-    COMPONENTS='main contrib non-free'
+    COMPONENTS='main'
   fi
   echo "Using repository components $COMPONENTS"
 
@@ -154,8 +163,8 @@ EOF
        apt-get update $DPKG_OPTIONS
      else
        # make sure we have the keys available for aptitude
-       gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
-       gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
+       gpg --keyserver subkeys.pgp.net --recv-keys 709BCE51568573EBC160E590F61E2E7CECDEA787
+       gpg --export 709BCE51568573EBC160E590F61E2E7CECDEA787 | apt-key add - || true # not yet sure
        # why it's necessary, sometimes we get an error even though it works [mika]
      fi
 
@@ -343,7 +352,11 @@ kernel() {
   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 firmware-linux"
+     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!"
@@ -414,24 +427,12 @@ passwords()
 
 # 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
+  if ! [ -f /etc/hosts ] ; then
      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
+127.0.0.1       localhost
+::1             localhost ip6-localhost ip6-loopback
+ff02::1         ip6-allnodes
+ff02::2         ip6-allrouters
 EOF
   fi
 }
@@ -462,11 +463,15 @@ timezone() {
 
 # 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
+  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 "Warning: couldn't identify target UUID for rootfs, your /etc/fstab might be incomplete."
+  fi
+
+if [ -n "$EFI" ] ; then
+  echo "UUID=$(blkid -o value -s UUID $EFI)  /boot/efi       vfat    umask=0077      0       1" >> /etc/fstab
 fi
 
 cat >> /etc/fstab << EOF
@@ -549,6 +554,24 @@ initrd() {
 }
 # }}}
 
+efi_setup() {
+  if [ -z "$EFI" ] ; then
+    return 0
+  fi
+
+  if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
+    echo "Notice: efi option set but no efibootmgr package, installing it therefore."
+    DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
+  fi
+
+  mkdir -p /boot/efi
+  echo "Mounting $EFI on /boot/efi"
+  mount "$EFI" /boot/efi || return 1
+
+  echo "Invoking efibootmgr"
+  efibootmgr || return 1
+}
+
 # grub configuration/installation {{{
 grub_install() {
 
@@ -557,9 +580,22 @@ grub_install() {
     return 0
   fi
 
-  if ! dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then
-    echo "Notice: grub option set but no grub-pc package, installing it therefore."
-    DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL grub-pc
+  efi_setup || return 1
+
+  if [ -n "$EFI" ] ; then
+    GRUB_PACKAGE=grub-efi-amd64
+  else
+    GRUB_PACKAGE=grub-pc
+  fi
+
+  # make sure this is pre-defined so we have sane settings for automated
+  # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
+  echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
+  echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect $GRUB" | debconf-set-selections
+
+  if ! dpkg --list ${GRUB_PACKAGE} 2>/dev/null | grep -q '^ii' ; then
+    echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
+    DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL ${GRUB_PACKAGE}
   fi
 
   if ! [ -x "$(which grub-install)" ] ; then
@@ -573,9 +609,20 @@ grub_install() {
         echo "Installing grub on ${GRUB}:"
         grub-install --no-floppy "$GRUB"
      done
+     rm -f /boot/grub/device.map
   else
      echo "Installing grub on ${GRUB}:"
-     grub-install --no-floppy "$GRUB"
+     case "$RELEASE" in
+       lenny|squeeze|wheezy)
+         grub-install --no-floppy "$(readlink -f "${GRUB}")"
+         rm -f /boot/grub/device.map
+         ;;
+       *)
+         echo "(hd0) ${GRUB}" > /boot/grub/device.map
+         grub-install "(hd0)"
+         rm /boot/grub/device.map
+         ;;
+     esac
   fi
 
   echo "Adjusting grub configuration for use on ${GRUB}."
@@ -590,6 +637,8 @@ grub_install() {
     return 1
   fi
 
+  mountpoint /boot/efi >/dev/null && umount /boot/efi
+
   $UPDATEGRUB
 }
 # }}}
@@ -622,6 +671,7 @@ finalize() {
 
   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
 
+  umount /sys >/dev/null 2>/dev/null || true
   umount /proc >/dev/null 2>/dev/null || true
 }
 # }}}