Install bootgrub.mksh 0755
[grml-debootstrap.git] / grml-debootstrap
index b030ff0..3f6e304 100755 (executable)
@@ -7,8 +7,6 @@
 ################################################################################
 # http://www.debian.org/releases/stable/i386/index.html.en
 
-set -e # exit on any error
-
 # variables {{{
 PN="$(basename $0)"
 VERSION="$(dpkg --list $PN 2>/dev/null| awk '/^i/ {print $3}')"
@@ -28,11 +26,6 @@ export LC_ALL=C
 INTERACTIVE=''
 # }}}
 
-# source core functions {{{
-. /etc/grml/lsb-functions
-. /etc/grml/script-functions
-# }}}
-
 # help text {{{
 usage() {
   echo "$PN - wrapper around debootstrap for installing Debian
@@ -103,6 +96,120 @@ if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
 fi
 # }}}
 
+# early helper functions {{{
+GOOD='\e[32;01m'
+WARN='\e[33;01m'
+BAD='\e[31;01m'
+NORMAL='\e[0m'
+HILITE='\e[36;01m'
+BRACKET='\e[34;01m'
+
+einfo() {
+  einfon "$1\n"
+  return 0
+}
+
+einfon() {
+  [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
+  printf " ${GOOD}*${NORMAL} $*"
+  LAST_E_CMD=einfon
+  return 0
+}
+
+eerror() {
+  [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
+  printf " ${BAD}*${NORMAL} $*\n"
+  LAST_E_CMD=eerror
+  return 0
+}
+
+eend() {
+  local retval="${1:-0}"
+  shift
+  if [ $retval -gt 0 ]; then
+    printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
+  fi
+  return $retval
+}
+
+check4root(){
+  if [ "$(id -u 2>/dev/null)" != 0 ] ; then
+    echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
+  fi
+}
+
+check4progs(){
+  local RC=''
+  for arg in $* ; do
+    which $arg >/dev/null 2>&1 || RC="$arg"
+  done
+  if [ -n "$RC" ] ; then
+     echo "$RC not installed"
+     return 1
+  fi
+}
+# }}}
+
+# helper functions {{{
+# we want to exit smoothly and clean:
+bailout(){
+  # make sure $TARGET is not mounted when exiting grml-debootstrap
+  if [ -n "$MNTPOINT" ] ; then
+     if grep -q $MNTPOINT /proc/mounts ; then
+        # make sure nothing is left inside chroot so we can unmount it
+        [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
+        [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
+        # ugly, but make sure we really don't leave anything (/proc /proc is intended)
+        for ARG in /sys /proc /proc ; do
+          [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
+        done
+        umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
+
+        [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
+
+        if [ -n "$DIRECTORY" ] ; then
+          einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
+        else
+          einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
+        fi
+
+        if [ -n "$STAGES" ] ; then
+           echo -n "Removing stages directory ${STAGES}: "
+           rm -rf "$STAGES" && echo done
+        fi
+
+        # remove directory only if we used the default with process id inside the name
+        if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
+           einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
+        fi
+     fi
+  fi
+
+  if [ -n "${ORIG_TARGET}" ] ; then
+    einfo "Removing loopback mount of file ${ORIG_TARGET}."
+    kpartx -d "${ORIG_TARGET}" ; eend $?
+  fi
+
+  [ -n "$1" ] && EXIT="$1" || EXIT="1"
+  [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
+
+  exit "$EXIT"
+}
+trap bailout HUP INT QUIT TERM
+
+# we want to execute all the functions only once, simple check for it:
+stage() {
+  if [ -n "$2" ] ; then
+     echo "$2" > "${STAGES}/${1}"
+     return 0
+  elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
+     ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
+     ewarn "  To reexecute it clean up the according directory inside $STAGES" ; eend 0
+     return 1
+  fi
+}
+# }}}
+
 # make sure we have what we need {{{
 check4progs debootstrap dialog || exit 1
 # }}}
@@ -120,7 +227,8 @@ if [ -r ./cmdlineopts.clp ] ; then
 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
    . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
 else
-   echo "Error: cmdline function file not found, exiting.">&2
+   eerror "Error: cmdline function file not found, exiting."
+   eend 1
    exit 1
 fi
 
@@ -206,15 +314,17 @@ fi
 
 # backwards compability checks {{{
 if [ -n "$GROOT" ] ; then
-   echo "Error: you seem to have \$GROOT configured." >&2
-   echo "This variable is no longer supported, please visit the" >&2
-   echo "grml-debootstrap documentation for details." >&2
+   eerror "Error: you seem to have \$GROOT configured."
+   eerror "This variable is no longer supported, please visit the"
+   eerror "grml-debootstrap documentation for details."
+   eend 1
    exit 1
 fi
 
 if echo "$GRUB" | grep -q '^hd' ; then
-   echo "Error: this syntax for the grub configuration variable is no longer supported." >&2
-   echo "Please do not use hd... any longer but /dev/sdX instead." >&2
+   eerror "Error: this syntax for the grub configuration variable is no longer supported."
+   eerror "Please do not use hd... any longer but /dev/sdX instead."
+   eend 1
    exit 1
 fi
 # }}}
@@ -223,7 +333,7 @@ fi
 welcome_dialog()
 {
    dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
-Do you want to continue installing Debian using this frontend?" 0 0
+Do you want to continue installing Debian using this frontend?" 0 0 || bailout 0
 }
 # }}}
 
@@ -250,6 +360,7 @@ prompt_for_target()
   TARGET=$(dialog --title "$PN" --single-quoted --stdout \
          --menu "Please select the target partition:" 0 0 0 \
          $PARTITION_LIST)
+  [ $? -eq 0 ] || bailout
 }
 # }}}
 
@@ -336,6 +447,7 @@ prompt_for_release()
             squeeze  Debian/stable \
             wheezy   Debian/testing \
             sid      Debian/unstable)"
+  [ $? -eq 0 ] || bailout
 }
 # }}}
 
@@ -345,6 +457,7 @@ prompt_for_hostname()
   HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
             "Please enter the hostname you would like to use for installation:" \
             0 0 $HOSTNAME)"
+  [ $? -eq 0 ] || bailout
 }
 # }}}
 
@@ -356,9 +469,11 @@ prompt_for_password()
      while [ "$ROOTPW1" != "$ROOTPW2" ]; do
        ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
        "Please enter the password for the root account:" 10 60)
+       [ $? -eq 0 ] || bailout
        ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
        "Please enter the password for the root account again for \
        confirmation:" 10 60)
+       [ $? -eq 0 ] || bailout
 
        if [ "$ROOTPW1" != "$ROOTPW2" ]; then
          $(dialog --stdout --title "${PN}" --ok-label \
@@ -379,17 +494,20 @@ prompt_for_mirror()
             net   "install via network (downloading from mirror)" \
             local "install from local directory/mirror"
           )
+  [ $? -eq 0 ] || bailout
 
   if [ "$CHOOSE_MIRROR" = 'net' ] ; then
      [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
      MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
                "Please enter Debian mirror you would like to use for installing packages." \
                0 0 $MIRROR)"
+     [ $? -eq 0 ] || bailout
   else # CHOOSE_MIRROR == local
      [ -n "$ISO" ] || ISO='/mnt/mirror'
      ISO="$(dialog --stdout --title "${PN}" --inputbox \
                "Please enter directory name you would like to use for installing packages." \
                0 0 $ISO)"
+     [ $? -eq 0 ] || bailout
   fi
 }
 # }}}
@@ -432,6 +550,7 @@ PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
 dialog --title "$PN" --separate-output \
        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
        $PARTITION_LIST 2>$TMPFILE
+[ $? -eq 0 ] || bailout
 RETVAL=$?
 SELECTED_PARTITIONS="$(cat $TMPFILE)"
 
@@ -447,13 +566,9 @@ if [ $RELEASE = "lenny" ]; then
 fi
 
 ERRORFILE=$(mktemp)
-set +e
-# TODO: better error handling?
 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
       --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
 RC=$?
-set -e
-
 if [ "$RC" = 0 ] ; then
    dialog --title "$PN" --msgbox \
    "Creating $TARGET was successful." 0 0
@@ -494,6 +609,7 @@ Last chance to quit. Timeout of 10 seconds running....
 
 Do you want to stop now?" 0 0 2>/dev/null
 }
+# }}}
 
 # make sure the user is aware of the used configuration {{{
 checkconfiguration()
@@ -526,6 +642,7 @@ Is this ok for you? Notice: selecting 'No' will exit ${PN}."
 
    dialog --title "$PN" --no-collapse \
           --yesno "$INFOTEXT" 0 0
+   [ $? -eq 0 ] || bailout 0
 
 else # if not running automatic installation display configuration and prompt for execution:
    einfo "$PN - Please recheck configuration before execution:"
@@ -685,68 +802,6 @@ ISODIR=${ISO##file:}
 ISODIR=${ISODIR%%/}
 # }}}
 
-# helper functions {{{
-# we want to exit smoothly and clean:
-bailout(){
-  # make sure $TARGET is not mounted when exiting grml-debootstrap
-  if [ -n "$MNTPOINT" ] ; then
-     if grep -q $MNTPOINT /proc/mounts ; then
-        # make sure nothing is left inside chroot so we can unmount it
-        [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
-        [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
-        # ugly, but make sure we really don't leave anything (/proc /proc is intended)
-        for ARG in /sys /proc /proc ; do
-          [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
-        done
-        umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
-
-        [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
-
-        if [ -n "$DIRECTORY" ] ; then
-          einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
-        else
-          einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
-        fi
-
-        if [ -n "$STAGES" ] ; then
-           echo -n "Removing stages directory ${STAGES}: "
-           rm -rf "$STAGES" && echo done
-        fi
-
-        # remove directory only if we used the default with process id inside the name
-        if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
-           einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
-        fi
-     fi
-  fi
-
-  if [ -n "${ORIG_TARGET}" ] ; then
-    einfo "Removing loopback mount of file ${ORIG_TARGET}."
-    kpartx -d "${ORIG_TARGET}" ; eend $?
-  fi
-
-  [ -n "$1" ] && EXIT="$1" || EXIT="1"
-  [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
-
-  exit "$EXIT"
-}
-trap bailout HUP INT QUIT TERM
-
-# we want to execute all the functions only once, simple check for it:
-stage() {
-  if [ -n "$2" ] ; then
-     echo "$2" > "${STAGES}/${1}"
-     return 0
-  elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
-     ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
-     eindent
-       ewarn "To reexecute it clean up the according directory inside $STAGES" ; eend 0
-     eoutdent
-     return 1
-  fi
-}
-# }}}
-
 # create filesystem {{{
 mkfs() {
   if [ -n "$DIRECTORY" ] ; then
@@ -833,13 +888,13 @@ prepare_vm() {
   ORIG_TARGET="$TARGET" # store for later reuse
 
   qemu-img create -f raw "${TARGET}" "${VMSIZE}"
-  echo 4 66 | mksh /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
+  echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
   dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
   parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
 
   DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
   if [ -z "${DEVINFO}" ] ; then
-    echo  Error setting up loopback device >&2
+    eerror "Error setting up loopback device." ; eend 1
     exit 1
   fi
 
@@ -852,7 +907,7 @@ prepare_vm() {
   blockdev --rereadpt "${LOOP}"
 
   if [ -z "$TARGET" ] ; then
-     echo "Error: target could not be set to according /dev/mapper/* device." >&2
+     eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
      exit 1
   fi
 }