Redirect eerror messages to stderr
[grml-debootstrap.git] / grml-debootstrap
index ec63a82..a82aaaf 100755 (executable)
@@ -71,7 +71,6 @@ Bootstrap options:
       --arch <arch>      Set target architecture, use for installing i386 on amd64.
       --filesystem <fs>  Filesystem that should be used when target is a partition
                          or Virtual Machine (see --vmfile).
-      --insecure         Do not download and check Release file signatures.
       --force            Do not prompt for user acknowledgement.
 
 Options for Virtual Machine deployment:
@@ -143,7 +142,7 @@ einfon() {
 
 eerror() {
   [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
-  printf " ${BAD}*${NORMAL} $*\n"
+  printf " ${BAD}*${NORMAL} $*\n" >&2
   LAST_E_CMD=eerror
   return 0
 }
@@ -177,15 +176,17 @@ check4progs(){
 
 # helper functions {{{
 cleanup() {
-  set -x
-  einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
-  einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
+  if [ -n "$CHROOT_VARIABLES" ] ; then
+    einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
+  fi
+
+  if [ -n "$STAGES" ] ; then
+    einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
+  fi
 
   # Remove temporary mountpoint again
   if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
-     einfo "Removing directory ${MNTPOINT}"
-     rmdir "$MNTPOINT"
-     eend $?
+    rmdir "$MNTPOINT" 2>/dev/null
   fi
 
   # make sure $TARGET is not mounted when exiting grml-debootstrap
@@ -197,14 +198,16 @@ cleanup() {
 
       [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a >/dev/null 2>&1
 
-      # ugly, but make sure we really don't leave anything (/proc /proc is intended)
-      for ARG in /sys /proc /proc ; do
+      # ugly, but make sure we really don't leave anything (/proc /proc and
+      # /dev /dev are intended, trying to work around timing issues, see #657023)
+      for ARG in /sys /proc /proc /dev /dev ; do
         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1
         umount "$MNTPOINT"/$ARG >/dev/null 2>&1
       done
-      umount "$MNTPOINT"/dev >/dev/null 2>&1
 
-      [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1
+      if [ -n "$ISODIR" ] ; then
+        [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1
+      fi
 
       if [ -n "$DIRECTORY" ] ; then
         einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
@@ -240,7 +243,7 @@ bailout(){
   cleanup
 
   [ -n "$1" ] && EXIT="$1" || EXIT="1"
-  [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
+  [ -n "$2" ] && einfo "Notice: remove $STAGES/$2 to reexecute the stage"
 
   exit "$EXIT"
 }
@@ -260,7 +263,7 @@ stage() {
 # }}}
 
 # make sure we have what we need {{{
-check4progs debootstrap dialog || bailout 1
+check4progs debootstrap || bailout 1
 # }}}
 
 # source main configuration file {{{
@@ -323,7 +326,7 @@ fi
 [ "$_opt_bootappend" ]          && BOOT_APPEND=$_opt_bootappend
 [ "$_opt_grub" ]                && GRUB=$_opt_grub
 [ "$_opt_arch" ]                && ARCH=$_opt_arch
-[ "$_opt_insecure" ]            && SECURE='false'
+[ "$_opt_insecure" ]            && echo "Warning: --insecure is deprecated, continuing anyway."
 [ "$_opt_force" ]               && FORCE=$_opt_force
 [ "$_opt_verbose" ]             && VERBOSE="-v"
 
@@ -350,7 +353,7 @@ fi
 
 # make sure we have what we need {{{
 if [ -n "$VIRTUAL" ] ; then
-  check4progs kpartx mksh qemu-img || bailout 1
+  check4progs kpartx mksh parted qemu-img || bailout 1
 fi
 # }}}
 
@@ -744,6 +747,8 @@ fi
 # interactive mode {{{
 interactive_mode()
 {
+  check4progs dialog || bailout 1
+
   welcome_dialog
 
   prompt_for_release
@@ -831,19 +836,6 @@ else
 fi
 # }}}
 
-# keyring setupt {{{
-KEYRING=""
-if [ "$SECURE" = 'yes' ] ; then
-   if [ -e '/etc/apt/trusted.gpg' ] ; then
-      KEYRING="--keyring /etc/apt/trusted.gpg"
-   else
-      eerror "Could not find /etc/apt/trusted.gpg."
-   fi
-else
-   ewarn "Not checking Release signatures!"
-fi
-# }}}
-
 # make sure we have the right syntax when using an iso image {{{
 if [ -n "$ISO" ] ; then
    case $ISO in
@@ -948,6 +940,25 @@ prepare_vm() {
   dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
   parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
 
+  # if dm-mod isn't available then kpartx will fail with
+  # "Is device-mapper driver missing from kernel? [...]"
+  if ! kpartx -av $TARGET >/dev/null 2>&1 ; then
+    einfo "Device-mapper not ready yet, trying to load dm-mod module."
+    modprobe dm-mod ; eend $?
+  fi
+
+  # make sure loop module is present
+  if ! losetup -f >/dev/null 2>&1; then
+    einfo "Can not find a usable loop device, retrying after loading loop module."
+    modprobe loop
+    if losetup -f >/dev/null 2>&1; then
+      einfo "Found a usable loop device now, continuing."
+    else
+      eerror "Error finding usable loop device" ; eend 1
+      bailout 1
+    fi
+  fi
+
   DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
   if [ -z "${DEVINFO}" ] ; then
     eerror "Error setting up loopback device." ; eend 1
@@ -1006,7 +1017,7 @@ finalize_vm() {
   einfo "Adjusting grub.cfg for successful boot sequence."
   # ugly but needed to boot grub acordingly
   sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
-  sed -i "s; root=/dev/.*; root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
+  sed -i "s;root=[^ ]\+;root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
 
   umount "${MNTPOINT}"
   kpartx -d "${ORIG_TARGET}" >/dev/null
@@ -1255,7 +1266,7 @@ for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
          preparechroot execute_pre_scripts chrootscript execute_scripts \
          umount_chroot finalize_vm fscktool ; do
     if stage "${i}" ; then
-       $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
+       $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "$i"
     fi
 done