Install GRUB once, at most (second take, issue #78)
[grml-debootstrap.git] / grml-debootstrap
index 89c56f4..622a693 100755 (executable)
@@ -389,7 +389,7 @@ fi
 
 # make sure we have what we need {{{
 if [ -n "$VIRTUAL" ] ; then
-  check4progs grub-mkimage kpartx mksh parted qemu-img || bailout 1
+  check4progs kpartx mksh parted qemu-img || bailout 1
 fi
 # }}}
 
@@ -1093,12 +1093,15 @@ finalize_vm() {
     bailout 1
   fi
 
-  einfo "Installing Grub as bootloader."
   mount -t proc none "${MNTPOINT}"/proc
   mount -t sysfs none "${MNTPOINT}"/sys
   mount --bind /dev "${MNTPOINT}"/dev
   mount --bind /dev/pts "${MNTPOINT}"/dev/pts
 
+# Has chroot-script installed GRUB to MBR using grub-install (successfully), already?
+# chroot-script skips installation for unset ${GRUB}
+if [[ -z "${GRUB}" ]] || ! dd if="${GRUB}" bs=512 count=1 2>/dev/null | cat -v | fgrep -q GRUB; then
+  einfo "Installing Grub as bootloader."
   mkdir -p "${MNTPOINT}/boot/grub"
   if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
      eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
@@ -1116,6 +1119,7 @@ finalize_vm() {
   chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
   dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
   rm -f "${MNTPOINT}/tmp/core.img"
+fi
 
   einfo "Updating grub configuration file."
   if [ -n "$BOOT_APPEND" ] ; then
@@ -1133,7 +1137,7 @@ finalize_vm() {
   umount "${MNTPOINT}"/proc
   umount "${MNTPOINT}"/sys
   umount "${MNTPOINT}"/dev/pts
-  umount "${MNTPOINT}"/dev
+  try_umount 3 "${MNTPOINT}"/dev
   umount "${MNTPOINT}"
   kpartx -d "${ORIG_TARGET}" >/dev/null
 }