do not run fsck when deploying virtual machine
[grml-debootstrap.git] / grml-debootstrap
index 8c145b5..3f14f75 100755 (executable)
@@ -66,12 +66,14 @@ Configuration options:
                              /etc/debootstrap/config
   -d, --confdir <path>     Place of config files for debootstrap, defaults
                              to /etc/debootstrap
-      --packages <file>    Install packages defined in specified list file.
+      --packages <file>    Install packages defined in specified list file
+                             instead of using /etc/debootstrap/packages.
       --nopackages         Skip installation of packages defined in
                              /etc/debootstrap/packages
       --debconf <file>     Pre-seed packages using specified pre-seed db file.
       --keep_src_list      Do not overwrite user provided apt sources.list.
       --hostname <name>    Hostname of Debian system.
+      --nopassword         Do not prompt for the root password.
       --password <pwd>     Use specified password as password for user root.
       --bootappend <line>  Add specified appendline to kernel whilst booting.
       --chroot-scripts <d> Execute chroot scripts from specified directory.
@@ -96,7 +98,7 @@ if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
 fi
 # }}}
 
-# {{{
+# early helper functions {{{
 GOOD='\e[32;01m'
 WARN='\e[33;01m'
 BAD='\e[31;01m'
@@ -148,7 +150,66 @@ check4progs(){
      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 {{{
@@ -210,6 +271,7 @@ fi
 [ "$_opt_keep_src_list" ]       && KEEP_SRC_LIST='yes'
 [ "$_opt_hostname" ]            && HOSTNAME=$_opt_hostname
 [ "$_opt_password" ]            && ROOTPASSWORD=$_opt_password
+[ "$_opt_nopassword" ]          && NOPASSWORD='yes'
 [ "$_opt_bootappend" ]          && BOOT_APPEND=$_opt_bootappend
 [ "$_opt_grub" ]                && GRUB=$_opt_grub
 [ "$_opt_arch" ]                && ARCH=$_opt_arch
@@ -274,7 +336,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
 }
 # }}}
 
@@ -301,6 +363,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
 }
 # }}}
 
@@ -387,6 +450,7 @@ prompt_for_release()
             squeeze  Debian/stable \
             wheezy   Debian/testing \
             sid      Debian/unstable)"
+  [ $? -eq 0 ] || bailout
 }
 # }}}
 
@@ -396,27 +460,35 @@ 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
 }
 # }}}
 
 # ask for password {{{
 prompt_for_password()
 {
-     ROOTPW1='PW1'
-     ROOTPW2='PW2'
-     while [ "$ROOTPW1" != "$ROOTPW2" ]; do
-       ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
-       "Please enter the password for the root account:" 10 60)
-       ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
-       "Please enter the password for the root account again for \
-       confirmation:" 10 60)
-
-       if [ "$ROOTPW1" != "$ROOTPW2" ]; then
-         $(dialog --stdout --title "${PN}" --ok-label \
-         "Retry" --msgbox "Passwords do not match!" 10 60)
-       fi
-     done
-     ROOTPASSWORD="$ROOTPW1"
+  if [ "$_opt_nopassword" ] ; then
+    einfo "Skip asking for root password as requested."
+    return 0
+  fi
+
+  ROOTPW1='PW1'
+  ROOTPW2='PW2'
+  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 \
+      "Retry" --msgbox "Passwords do not match!" 10 60)
+    fi
+  done
+  ROOTPASSWORD="$ROOTPW1"
 }
 # }}}
 
@@ -430,17 +502,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
 }
 # }}}
@@ -483,6 +558,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)"
 
@@ -574,6 +650,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:"
@@ -733,66 +810,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
-     ewarn "  To reexecute it clean up the according directory inside $STAGES" ; eend 0
-     return 1
-  fi
-}
-# }}}
-
 # create filesystem {{{
 mkfs() {
   if [ -n "$DIRECTORY" ] ; then
@@ -879,7 +896,7 @@ 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'
 
@@ -992,6 +1009,7 @@ preparechroot() {
   [ -n "$PACKAGES" ]      && echo "PACKAGES=$PACKAGES"         >> $CHROOT_VARIABLES
   [ -n "$RM_APTCACHE" ]   && echo "RM_APTCACHE=$RM_APTCACHE"   >> $CHROOT_VARIABLES
   [ -n "$ROOTPASSWORD" ]  && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
+  [ -n "$NOPASSWORD" ]    && echo "NOPASSWORD=true"            >> $CHROOT_VARIABLES
   [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=$SELECTED_PARTITIONS" >> $CHROOT_VARIABLES
   [ -n "$TARGET" ]        && echo "TARGET=$TARGET"             >> $CHROOT_VARIABLES
   [ -n "$TARGET_UUID" ]   && echo "TARGET_UUID=$TARGET_UUID"   >> $CHROOT_VARIABLES
@@ -1150,12 +1168,17 @@ umount_chroot() {
 
 # execute filesystem check {{{
 fscktool() {
-  if [ "$FSCK" = 'yes' ] ; then
-     [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
-     einfo "Checking filesystem on $TARGET using $FSCKTOOL"
-     $FSCKTOOL $TARGET
-     eend $?
-  fi
+ if [ -n "$VIRTUAL" ] ; then
+   einfo "Skipping filesystem check since we deploy a virtual machine."
+   return 0
+ fi
+
+ if [ "$FSCK" = 'yes' ] ; then
+   [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
+   einfo "Checking filesystem on $TARGET using $FSCKTOOL"
+   $FSCKTOOL $TARGET
+   eend $?
+ fi
 }
 # }}}