Directly invoke /usr/sbin/grub-install in grml-debootstrap
[grml-debootstrap.git] / grml-debootstrap
old mode 100644 (file)
new mode 100755 (executable)
index f4838f0..0f61061
@@ -4,13 +4,13 @@
 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
 # Bug-Reports:   see http://grml.org/bugs/
 # License:       This file is licensed under the GPL v2.
-# Latest change: Don Apr 12 11:42:02 CEST 2007 [mika]
+# Latest change: Mon Apr 16 15:42:34 CEST 2007 [mika]
 ################################################################################
 # http://www.debian.org/releases/stable/i386/index.html.en
 
 set -e # exit on any error
 
-VERSION='0.6'
+VERSION='0.8'
 
 # source core functions {{{
 . /etc/grml/lsb-functions
@@ -22,23 +22,6 @@ check4progs debootstrap || exit 1
 check4root || exit 1
 # }}}
 
-# cmdline handling {{{
-case $* in
-   -h*|--h*)
-     einfo "$0 - wrapper around debootstrap for installing plain Debian via grml"
-     einfo "Adjust /etc/debootstrap/config and invoke $0 afterwards."
-     eend 0
-     exit 0
-   ;;
-   -v|--v*)
-     einfo "$0 version $VERSION"
-     einfo "Send bug reports to Michael Prokop <mika@grml.org>."
-     eend 0
-     exit 0
-   ;;
-esac
-# }}}
-
 # without config file it won't work {{{
 if [ -r /etc/debootstrap/config ] ; then
    . /etc/debootstrap/config
@@ -48,6 +31,60 @@ else
 fi
 # }}}
 
+# cmdline handling {{{
+usage() {
+  einfo "$0 - wrapper around debootstrap for installing plain Debian via grml"
+  einfo "Adjust /etc/debootstrap/config and invoke $0 afterwards."
+}
+
+while [ "$#" -gt "0" ] ; do
+    case $1 in
+        -v|--version)
+            einfo "$0 version $VERSION"
+            einfo "Send bug reports to Michael Prokop <mika@grml.org>."
+            eend 0
+            exit 0
+            ;;
+        -t|--target)
+            shift
+            TARGET=$1
+            ;;
+        --grub)
+            shift
+            GRUB=$1
+            ;;
+        --groot)
+            shift
+            GROOT=$1
+            ;;
+        --release)
+            shift
+            RELEASE=$1
+            ;;
+        -p|--mntpoint)
+            shift
+            MNTPOINT=$1
+            ;;
+        -m|--mirror)
+            shift
+            MIRROR=$1
+            CHROOTMIRROR=$1
+            ;;
+        -h|--help)
+            usage ; eend 0
+            eend 0
+            exit 0
+            ;;
+        *)
+            eerror "Syntax error."
+            usage ; eend 1
+            exit 1
+            ;;
+    esac
+    shift
+done
+# }}}
+
 # set/check variables {{{
 
 # inside the chroot system the locales might not be available, so use minimum:
@@ -94,6 +131,14 @@ else
    ARCHINFO=" (${ARCH})"
 fi
 
+# provide variables to chroot system
+[ -n "$ARCH" ]   && echo "ARCH=$ARCH"     > /etc/debootstrap/variables
+[ -n "$GRUB" ]   && echo "GRUB=$GRUB"     >> /etc/debootstrap/variables
+[ -n "$GROOT" ]  && echo "GROOT=$GROOT"   >> /etc/debootstrap/variables
+[ -n "$TARGET" ] && echo "TARGET=$TARGET" >> /etc/debootstrap/variables
+[ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> /etc/debootstrap/variables
+[ -n "$CHROOTMIRROR" ] && echo "CHROOTMIRROR=$CHROOTMIRROR" >> /etc/debootstrap/variables
+
 # make sure at least $TARGET is set [the partition for the new system]
 if [ -z "$TARGET" ] ; then
    eerror "Please adjust /etc/debootstrap/config before running ${0}" ; eend 1
@@ -105,30 +150,33 @@ fi
 # we want to exit smoothly and clean:
 bailout(){
   # make sure $TARGET is not mounted when exiting grml-debootstrap
-  if [ -n "$TARGET" ] ; then
-     if grep -q $TARGET /proc/mounts ; then
+  if [ -n "$MNTPOINT" ] ; then
+     if grep -q $MNTPOINT /proc/mounts ; then
         # make sure nothing is left inside chroot so we can unmount it
-        [ -x "$TARGET"/etc/init.d/ssh   ] && "$TARGET"/etc/init.d/ssh stop
-        [ -x "$TARGET"/etc/init.d/mdadm ] && "$TARGET"/etc/init.d/mdadm stop
-        chroot "$TARGET" umount /sys  1>/dev/null 2>&1
-        chroot "$TARGET" umount /proc 1>/dev/null 2>&1
-        echo "Unmounting $TARGET"
-        umount "$TARGET"
+        [ -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 leav anything
+        [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /sys  1>/dev/null 2>&1
+        [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a    1>/dev/null 2>&1
+        [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
+        [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
+        einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
      fi
   fi
   [ -n "$1" ] && EXIT="$1" || EXIT="1"
+  [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
   exit "$EXIT"
 }
 trap bailout 1 2 3 15
 
 # we want to execute all the functions only once, simple check for it:
 stage() {
-  if grep -q done "$STAGES/$1" 2>/dev/null ; then
-     ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
-     return 1
-  else
+  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
+     return 1
   fi
 }
 # }}}
@@ -138,7 +186,8 @@ einfo "$0 - Please recheck configuration before execution:"
 echo "
    Target:           $TARGET"
    case "$MNTPOINT" in "$TARGET") ;; *) echo "   Mount-point:      $MNTPOINT" ;; esac
-   [ -n "$GRUB" ] && echo "   Install grub to:  $GROOT / $GRUB"
+   [ -n "$GRUB" ]   && echo "   Install grub to:  $GROOT / $GRUB"
+   [ -n "$MIRROR" ] && echo "   Using mirror:     $MIRROR"
    case "$MNTPOINT" in "$TARGET") ;; *) echo "  Important! Continuing will delete all data from ${TARGET}!" ;; esac
    echo
 einfon "Is this ok for you? [y/N] "
@@ -190,9 +239,17 @@ mount_target() {
 
 # install main chroot {{{
 debootstrap_system() {
-  einfo "Running $DEBOOTSTRAP for release ${RELEASE}${ARCHINFO} using mirror $MIRROR"
-  $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $MIRROR
-  eend $?
+  if ! grep -q $MNTPOINT /proc/mounts ; then
+          mount_target
+  fi
+  if grep -q $MNTPOINT /proc/mounts ; then
+     einfo "Running $DEBOOTSTRAP for release ${RELEASE}${ARCHINFO} using mirror $MIRROR"
+     $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $MIRROR
+     eend $?
+  else
+     eerror "Error: $MNTPOINT not mounted, can not continue."
+     eend 1
+  fi
 }
 # }}}
 
@@ -204,8 +261,9 @@ preparechroot() {
   mkdir $MNTPOINT/etc/debootstrap/
 
   # make sure we have our files for later use via chroot-script
-  cp /etc/debootstrap/config $MNTPOINT/etc/debootstrap/
-  cp /etc/debootstrap/packages $MNTPOINT/etc/debootstrap/packages
+  cp /etc/debootstrap/config          $MNTPOINT/etc/debootstrap/
+  cp /etc/debootstrap/packages        $MNTPOINT/etc/debootstrap/packages
+  cp /etc/debootstrap/variables       $MNTPOINT/etc/debootstrap/variables
 
   # make sure we can access network [relevant for cdebootstrap]
   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
@@ -225,9 +283,17 @@ preparechroot() {
 
 # execute chroot-script {{{
 chrootscript() {
-  einfo "Executing chroot-script now"
-  chroot "$MNTPOINT" /bin/chroot-script
-  eend $?
+  if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
+     mount_target
+  fi
+  if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
+     einfo "Executing chroot-script now"
+     chroot "$MNTPOINT" /bin/chroot-script
+     eend $?
+  else
+     eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
+     eend 1
+  fi
 }
 # }}}
 
@@ -237,7 +303,8 @@ grub_install() {
      echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
   else
      einfo "Installing grub on ${GRUB}:"
-     grub-install --root-directory="$MNTPOINT" "(${GRUB})"
+     [ -x /usr/sbin/grub-install ] && GRUBINSTALL=/usr/sbin/grub-install || GRUBINSTALL=/sbin/grub-install
+     $GRUBINSTALL --root-directory="$MNTPOINT" "(${GRUB})"
      eend $?
   fi
 }
@@ -265,15 +332,12 @@ fscktool() {
 # }}}
 
 # now execute all the functions {{{
-  stage mkfs               && mkfs               && stage mkfs done
-  stage tunefs             && tunefs             && stage tunefs done
-  stage mount_target       && mount_target       && stage mount_target done
-  stage debootstrap_system && debootstrap_system && stage debootstrap_system done
-  stage preparechroot      && preparechroot      && stage preparechroot done
-  stage chrootscript       && chrootscript       && stage chrootscript done
-  stage grub_install       && grub_install       && stage grub_install done
-  stage umount_chroot      && umount_chroot      && stage umount_chroot done
-  stage fscktool           && fscktool           && stage fscktool done
+for i in mkfs tunefs mount_target debootstrap_system preparechroot \
+         chrootscript grub_install umount_chroot fscktool ; do
+    if stage $i ; then
+       $i && stage $i done || bailout 2 "i"
+    fi
+done
 # }}}
 
 # stages {{{