Do not try to create /etc/debootstrap if it already exists
[grml-debootstrap.git] / grml-debootstrap
index 46d2526..0d2efbb 100755 (executable)
@@ -11,7 +11,7 @@ set -e # exit on any error
 
 # variables {{{
 PN="$(basename $0)"
-VERSION='0.27'
+VERSION='0.28'
 MNTPOINT="/mnt/debootstrap.$$"
 
 # inside the chroot system locales might not be available, so use minimum:
@@ -39,9 +39,11 @@ Bootstrap options:
   -m, --mirror <URL>     Mirror which should be used for apt-get/aptitude.
   -i, --iso <mnt>        Mountpoint where a Debian ISO is mounted to, for use
                            instead of fetching packages from a mirror.
-  -r, --release <name>   Release of new Debian system (default: stable).
-  -t, --target <target>  Target partition (/dev/...) or directory.
-  -p, --mntpoint <mnt>   Mountpoint used for mounting the target system.
+  -r, --release <name>   Release of new Debian system (default: lenny).
+  -t, --target <target>  Target partition (/dev/...) or directory where the
+                         system should be installed to.
+  -p, --mntpoint <mnt>   Mountpoint used for mounting the target system,
+                         has no effect if -t is given and represents a directory.
       --debopt <params>  Extra parameters passed to the debootstrap command.
       --interactive      Use interactive mode (frontend).
       --nodebootstrap    Skip debootstrap, only do configuration to the target.
@@ -637,7 +639,12 @@ bailout(){
         [ -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
         [ -d "$MNTPOINT/$ISODIR" ]    && umount "$MNTPOINT/$ISODIR" 1>/dev/null 2>&1
-        einfo "Unmounting $MNTPOINT"   ; umount "$MNTPOINT" ; eend $?
+
+        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}: "
@@ -675,11 +682,21 @@ stage() {
 
 # create filesystem {{{
 mkfs() {
-  if [ -n "$MKFS" ] ; then
-     einfo "Running $MKFS on $TARGET"
-     $MKFS $TARGET
-     TARGET_UUID="$(vol_id -u $TARGET 2>/dev/null || echo '')"
-     eend $?
+  if [ -n "$DIRECTORY" ] ; then
+     einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
+  else
+    if grep -q "$TARGET" /proc/mounts ; then
+      eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
+      exit 1
+    fi
+
+    if [ -n "$MKFS" ] ; then
+       einfo "Running $MKFS on $TARGET"
+       $MKFS $TARGET
+       TARGET_UUID="$(vol_id -u $TARGET 2>/dev/null || echo '')"
+       eend $?
+    fi
+
   fi
 }
 # }}}
@@ -700,7 +717,7 @@ mount_target() {
      einfo "Running grml-debootstrap on a directory, nothing to mount."
   else
      if grep -q $TARGET /proc/mounts ; then
-        eerror "$TARGET already mounted, exiting."
+        ewarn "$TARGET already mounted, continuing anyway." ; eend 0
      else
        [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
        einfo "Mounting $TARGET to $MNTPOINT"
@@ -719,14 +736,14 @@ mount_target() {
 
 # install main chroot {{{
 debootstrap_system() {
-  if ! grep -q $MNTPOINT /proc/mounts ; then
+  if ! grep -q "$MNTPOINT" /proc/mounts ; then
      mount_target
   fi
   if [ "$_opt_nodebootstrap" ]; then
      einfo "Skipping debootstrap as requested."
      return
   fi
-  if grep -q $MNTPOINT /proc/mounts || [ -n "$DIRECTORY" ] ; then
+  if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
      einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
      [ -n "$MIRROR" ] && $DEBOOTSTRAP $ARCHCMD $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR || \
      $DEBOOTSTRAP $ARCHCMD $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
@@ -762,7 +779,7 @@ preparechroot() {
 
   cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
   chmod 755 $MNTPOINT/bin/chroot-script
-  mkdir $MNTPOINT/etc/debootstrap/
+  [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
 
   # make sure we have our files for later use via chroot-script
   cp $VERBOSE $CONFFILES/config    $MNTPOINT/etc/debootstrap/
@@ -892,6 +909,7 @@ umount_chroot() {
         eend $?
      fi
   fi
+
   if grep -q "$MNTPOINT" /proc/mounts ; then
      if [ -n "$PARTITION" ] ; then
         einfo "Unmount $MNTPOINT"