Don't try to re-read partition table when building VMs using loop devices
[grml-debootstrap.git] / grml-debootstrap
1 #!/bin/bash
2 # Filename:      grml-debootstrap
3 # Purpose:       wrapper around debootstrap for installing plain Debian via Grml
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2+
7 ################################################################################
8
9 # error_handler {{{
10 [ -n "$REPORT_TRAP_ERR" ] || REPORT_TRAP_ERR='no'
11 [ -n "$FAIL_TRAP_ERR" ] || FAIL_TRAP_ERR='no'
12
13 error_handler() {
14    last_exit_code="$?"
15    last_bash_command="$BASH_COMMAND"
16    if [ "$REPORT_TRAP_ERR" = "yes" ]; then
17       echo "Unexpected non-zero exit code $last_exit_code in $BASH_SOURCE at line $BASH_LINENO detected!
18 last bash command: $last_bash_command"
19    fi
20    if [ ! "$FAIL_TRAP_ERR" = "yes" ]; then
21       return
22    fi
23    ## Check if "bailout" function is available.
24    ## This is not the case in chroot-script.
25    if command -v bailout >/dev/null 2>&1; then
26       bailout 1
27    else
28       echo 'FAIL_TRAP_ERR is set to "yes", exit 1.'
29       exit 1
30    fi
31 }
32
33 if [ "$REPORT_TRAP_ERR" = "yes" ] || [ "$FAIL_TRAP_ERR" = "yes" ]; then
34    set -E
35    set -o pipefail
36    trap "error_handler" ERR
37    export -f "error_handler"
38 fi
39 # }}}
40
41 # variables {{{
42 PN="$(basename "$0")"
43 if [[ -d "$(dirname "$(which "$0")")"/.git ]]; then
44   VERSION="$(git describe | sed 's|^v||')"
45 else
46   VERSION="$(dpkg-query --show --showformat='${Version}' "$PN")"
47 fi
48 VERSION="${VERSION:-unknown}"
49 MNTPOINT="/mnt/debootstrap.$$"
50
51 # defaults
52 [ -n "$CHROOT_SCRIPTS" ] || CHROOT_SCRIPTS='yes'
53 [ -n "$CONFFILES" ] || CONFFILES='/etc/debootstrap'
54 [ -n "$DEBCONF" ] || DEBCONF='yes'
55 [ -n "$DEBIAN_FRONTEND" ] || DEBIAN_FRONTEND='noninteractive'
56 [ -n "$DEBOOTSTRAP" ] || DEBOOTSTRAP='debootstrap'
57 [ -n "$DEFAULT_LANGUAGE" ] || DEFAULT_LANGUAGE='en_US:en'
58 [ -n "$DEFAULT_LOCALES" ] || DEFAULT_LOCALES='en_US.UTF-8'
59 [ -n "$DISK_IDENTIFIER" ] || DISK_IDENTIFIER='26ada0c0-1165-4098-884d-aafd2220c2c6'
60 [ -n "$EXTRAPACKAGES" ] || EXTRAPACKAGES='yes'
61 [ -n "$FALLBACK_MIRROR" ] || FALLBACK_MIRROR='http://http.debian.net/debian'
62 [ -n "$FIXED_DISK_IDENTIFIERS" ] || FIXED_DISK_IDENTIFIERS="no"
63 [ -n "$FORCE" ] || FORCE=''
64 [ -n "$HOSTNAME" ] || HOSTNAME='grml'
65 [ -n "$INITRD" ] || INITRD='yes'
66 [ -n "$INSTALL_NOTES" ] || INSTALL_NOTES='/etc/debootstrap/install_notes'
67 [ -n "$LOCALES" ] || LOCALES='yes'
68 [ -n "$MIRROR" ] || MIRROR="$FALLBACK_MIRROR"
69 [ -n "$MKFS" ] || MKFS='mkfs.ext4'
70 [ -n "$MKFS_OPTS" ] || MKFS_OPTS=''
71 [ -n "$PACKAGES" ] || PACKAGES='yes'
72 [ -n "$PRE_SCRIPTS" ] || PRE_SCRIPTS='yes'
73 [ -n "$RECONFIGURE" ] || RECONFIGURE='console-data'
74 [ -n "$RELEASE" ] || RELEASE='jessie'
75 [ -n "$RM_APTCACHE" ] || RM_APTCACHE='yes'
76 [ -n "$SCRIPTS" ] || SCRIPTS='yes'
77 [ -n "$SECURE" ] || SECURE='yes'
78 [ -n "$TIMEZONE" ] || TIMEZONE='Europe/Vienna'
79 [ -n "$TUNE2FS" ] || TUNE2FS='tune2fs -c0 -i0'
80 [ -n "$UPGRADE_SYSTEM" ] || UPGRADE_SYSTEM='yes'
81 [ -n "$VMSIZE" ] || VMSIZE="2G"
82
83 # inside the chroot system locales might not be available, so use minimum:
84 export LANG=C
85 export LC_ALL=C
86
87 # make sure interactive mode is only executed when
88 # using an empty configuration file or option --interactive
89 INTERACTIVE=''
90 # }}}
91
92 # help text {{{
93 usage() {
94   echo "$PN - wrapper around debootstrap for installing Debian
95
96 Usage: $PN [options]
97
98 Bootstrap options:
99
100   -m, --mirror <URL>     Mirror which should be used for apt-get/aptitude.
101   -i, --iso <mnt>        Mountpoint where a Debian ISO is mounted to, for use
102                          instead of fetching packages from a mirror.
103   -r, --release <name>   Release of new Debian system (default: jessie).
104   -t, --target <target>  Target partition (/dev/...) or directory where the
105                          system should be installed to.
106   -p, --mntpoint <mnt>   Mountpoint used for mounting the target system,
107                          has no effect if -t is given and represents a directory.
108       --debopt <params>  Extra parameters passed to the debootstrap command.
109       --interactive      Use interactive mode (frontend).
110       --nodebootstrap    Skip debootstrap, only do configuration to the target.
111       --grub <device>    Target for grub installation. Usage example: /dev/sda
112       --arch <arch>      Set target architecture, use for installing i386 on amd64.
113       --filesystem <fs>  Filesystem that should be used when target is a partition
114                          or Virtual Machine (see --vmfile).
115       --force            Do not prompt for user acknowledgement.
116
117 Options for Virtual Machine deployment:
118
119       --vm               Set up a Virtual Machine, instead of plainly installing
120                          to a partition or directory, to be combined with --target,
121                          like: --vm --target /dev/mapper/your-vm-disk
122       --vmfile           Like --vm, but install into a file (raw format).
123                          Example: --vmfile --target /mnt/sda1/qemu.img
124       --vmsize <size>    Use specified size for size of VM file (default: 2G).
125                          Syntax as supported by qemu-img, like: --vmsize 3G
126
127 Configuration options:
128
129   -c, --config <file>      Use specified configuration file, defaults to
130                              /etc/debootstrap/config
131   -d, --confdir <path>     Place of config files for debootstrap, defaults
132                              to /etc/debootstrap
133       --packages <file>    Install packages defined in specified list file
134                              instead of using /etc/debootstrap/packages.
135       --nopackages         Skip installation of packages defined in
136                              /etc/debootstrap/packages
137       --nokernel           Skip installation of default kernel images.
138       --nointerfaces       Do not copy /etc/network/interfaces from host system
139                            to target system.
140                            (This option is automatically enabled when using --vmfile.)
141       --defaultinterfaces  Install a default /etc/network/interfaces file (enabling
142                            DHCP for eth0) instead of taking over config from host system.
143       --debconf <file>     Pre-seed packages using specified pre-seed db file.
144       --grmlrepos          Enable Grml's Debian repository (deb.grml.org).
145       --backportrepos      Enable Debian's backports repository (backports.debian.org).
146       --keep_src_list      Do not overwrite user provided apt sources.list.
147       --contrib            Enable 'contrib' in COMPONENTS (defaults to 'main' only).
148       --non-free           Enable non-free in COMPONENTS (defaults to 'main' only).
149       --hostname <name>    Hostname of Debian system.
150       --nopassword         Do not prompt for the root password.
151       --password <pwd>     Use specified password as password for user root.
152       --bootappend <line>  Add specified appendline to kernel whilst booting.
153       --chroot-scripts <d> Execute chroot scripts from specified directory.
154       --pre-scripts <dir>  Execute scripts from specified directory (before chroot-scripts).
155       --scripts <dir>      Execute scripts from specified directory (after chroot-scripts).
156
157 Other options:
158
159   -v, --verbose            Increase verbosity.
160       --debug              Execute in very verbose way.
161   -h, --help               Print this usage information and exit.
162   -V, --version            Show summary of options and exit.
163
164 Usage examples can be found in the grml-debootstrap manpage.
165 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
166 "
167 }
168
169 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
170    usage
171    echo 'Please notice that this script requires root permissions!'
172    exit 0
173 fi
174 # }}}
175
176 # early helper functions {{{
177 GOOD='\e[32;01m'
178 BAD='\e[31;01m'
179 WARN='\e[33;01m'
180 NORMAL='\e[0m'
181
182 einfo() {
183   einfon "$1\n"
184   return 0
185 }
186
187 einfon() {
188   [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
189   printf " ${GOOD}*${NORMAL} $*"
190   LAST_E_CMD=einfon
191   return 0
192 }
193
194 ewarn() {
195   printf " ${WARN}*${NORMAL} $*\n"
196   return 0
197 }
198
199 eerror() {
200   [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
201   printf " ${BAD}*${NORMAL} $*\n" >&2
202   LAST_E_CMD=eerror
203   return 0
204 }
205
206 eend() {
207   local retval="${1:-0}"
208   shift
209   if [ "$retval" -gt 0 ]; then
210     printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
211   fi
212   return "$retval"
213 }
214
215 check4root(){
216   if [ "$(id -u 2>/dev/null)" != 0 ] ; then
217     echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
218   fi
219 }
220
221 check4progs(){
222   local RC=''
223   for arg in "$@" ; do
224     which "$arg" >/dev/null 2>&1 || RC="$arg"
225   done
226   if [ -n "$RC" ] ; then
227      echo "$RC not installed"
228      return 1
229   fi
230 }
231 # }}}
232
233 # helper functions {{{
234 cleanup() {
235   if [ -n "$CHROOT_VARIABLES" ] ; then
236     einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
237   fi
238
239   if [ -n "$STAGES" ] ; then
240     einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
241   fi
242
243   # Remove temporary mountpoint again
244   if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
245     rmdir "$MNTPOINT" 2>/dev/null
246   fi
247
248   # make sure $TARGET is not mounted when exiting grml-debootstrap
249   if [ -n "$MNTPOINT" ] ; then
250     if grep -q "$MNTPOINT" /proc/mounts ; then
251       # make sure nothing is left inside chroot so we can unmount it
252       for service in ssh mdadm ; do
253         if [ -x "${MNTPOINT}/etc/init.d/${service}" ] ; then
254           chroot "$MNTPOINT" "/etc/init.d/${service}" stop
255         fi
256       done
257
258       [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a >/dev/null 2>&1
259
260       # ugly, but make sure we really don't leave anything (/proc /proc and
261       # /dev /dev are intended, trying to work around timing issues, see #657023)
262       for ARG in /sys /proc /proc /dev/pts /dev/pts /dev /dev ; do
263         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1
264         umount "$MNTPOINT"/$ARG >/dev/null 2>&1
265       done
266
267       if [ -n "$ISODIR" ] ; then
268         [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1
269       fi
270
271       if [ -n "$DIRECTORY" ] ; then
272         einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
273       else
274         einfo "Unmounting $MNTPOINT"
275         umount "$MNTPOINT"
276         eend $?
277       fi
278
279       if [ -n "$STAGES" ] ; then
280         echo -n "Removing stages directory ${STAGES}: "
281         rm -rf "$STAGES" && echo 'done'
282       fi
283
284       # remove directory only if we used the default with process id inside the name
285       if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
286         einfo "Removing directory ${MNTPOINT}"
287         rmdir "$MNTPOINT"
288         eend $?
289       fi
290     fi
291   fi
292
293   if [ -n "${ORIG_TARGET}" ] ; then
294     einfo "Removing loopback mount of file ${ORIG_TARGET}."
295     kpartx -d "${ORIG_TARGET}" ; eend $?
296   fi
297 }
298
299 # we want to exit smoothly and clean:
300 bailout(){
301
302   cleanup
303
304   [ -n "$1" ] && EXIT="$1" || EXIT="1"
305   [ -n "$2" ] && einfo "Notice: remove $STAGES/$2 to reexecute the stage"
306
307   exit "$EXIT"
308 }
309 trap bailout HUP INT QUIT TERM
310
311 # we want to execute all the functions only once, simple check for it:
312 stage() {
313   if [ -n "$2" ] ; then
314      echo "$2" > "${STAGES}/${1}"
315      return 0
316   elif grep -q 'done' "${STAGES}/${1}" 2>/dev/null ; then
317      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
318      ewarn "  To reexecute it clean up the according directory inside $STAGES" ; eend 0
319      return 1
320   fi
321 }
322 # }}}
323
324 # make sure we have what we need {{{
325 check4progs debootstrap || bailout 1
326 # }}}
327
328 # source main configuration file {{{
329 if [ -r /etc/debootstrap/config ] ; then
330   . /etc/debootstrap/config
331 fi
332 # }}}
333
334 # cmdline handling {{{
335 # source external command line parameter-processing script
336 self_dir="$(dirname "$(which "$0")")"
337 if [ -r "${self_dir}"/cmdlineopts.clp ] ; then
338    . "${self_dir}"/cmdlineopts.clp
339 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
340    . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
341 else
342    eerror "Error: cmdline function file not found, exiting."
343    eend 1
344    bailout 1
345 fi
346
347 # == business-logic of command line parameter-processing
348
349 # source configuration file in <confdir> if supplied. {{{
350 [ "$_opt_confdir" ] && {
351   CONFFILES=$_opt_confdir
352   einfo "Using config files under $CONFFILES/."
353   if ! [ -r "$CONFFILES/config" ] ; then
354     eerror "Error: config file $CONFFILES/config not found."; eend 1; bailout 1
355   fi
356   if ! . "$CONFFILES/config" ; then
357     eerror "Error reading config file $CONFFILES/config" ; eend 1 ; bailout 1
358   fi
359   # restore the command line parameter value
360   CONFFILES=$_opt_confdir
361 }
362 # }}}
363
364 [ "$_opt_mirror" ]              && MIRROR=$_opt_mirror
365 [ "$_opt_iso" ]                 && ISO=$_opt_iso
366 [ "$_opt_release" ]             && RELEASE=$_opt_release
367 [ "$_opt_target" ]              && TARGET=$_opt_target
368 [ "$_opt_vm" ]                  && VIRTUAL=1
369 [ "$_opt_vmfile" ]              && VMFILE=1 && VIRTUAL=1
370 [ "$_opt_vmsize" ]              && VMSIZE=$_opt_vmsize
371 [ "$_opt_mntpoint" ]            && MNTPOINT=$_opt_mntpoint
372 [ "$_opt_debopt" ]              && DEBOOTSTRAP_OPT=$_opt_debopt
373 [ "$_opt_interactive" ]         && INTERACTIVE=1
374 [ "$_opt_config" ]              && CONFIGFILE=$_opt_config
375 [ "$_opt_filesystem" ]          && MKFS="mkfs.$_opt_filesystem"
376 [ "$_opt_packages_set" ]        && PACKAGES='yes'
377 [ "$_opt_nopackages" ]          && PACKAGES=''
378 [ "$_opt_debconf_set" ]         && DEBCONF='yes'
379 [ "$_opt_scripts_set" ]         && SCRIPTS='yes'
380 [ "$_opt_pre_scripts_set" ]     && PRE_SCRIPTS='yes'
381 [ "$_opt_chroot_scripts_set" ]  && CHROOT_SCRIPTS='yes'
382 [ "$_opt_keep_src_list" ]       && KEEP_SRC_LIST='yes'
383 [ "$_opt_grmlrepos" ]           && GRMLREPOS='yes'
384 [ "$_opt_backportrepos" ]       && BACKPORTREPOS='yes'
385 [ "$_opt_hostname" ]            && HOSTNAME=$_opt_hostname
386 [ "$_opt_password" ]            && ROOTPASSWORD=$_opt_password
387 [ "$_opt_nopassword" ]          && NOPASSWORD='yes'
388 [ "$_opt_defaultinterfaces" ]   && USE_DEFAULT_INTERFACES="true"
389 [ "$_opt_nointerfaces" ]        && NOINTERFACES="true"
390 [ "$_opt_nokernel" ]            && NOKERNEL="true"
391 [ "$_opt_bootappend" ]          && BOOT_APPEND=$_opt_bootappend
392 [ "$_opt_grub" ]                && GRUB=$_opt_grub
393 [ "$_opt_arch" ]                && ARCH=$_opt_arch
394 [ "$_opt_insecure" ]            && echo "Warning: --insecure is deprecated, continuing anyway."
395 [ "$_opt_force" ]               && FORCE=$_opt_force
396 [ "$_opt_verbose" ]             && VERBOSE="-v"
397 [ "$_opt_debug" ]               && DEBUG="true"
398
399 # make sure main is always included
400 [ -z "$COMPONENTS" ]            && COMPONENTS="main"
401 [ "$_opt_contrib" ]             && COMPONENTS="$COMPONENTS contrib"
402 [ "$_opt_non_free" ]            && COMPONENTS="$COMPONENTS non-free"
403
404 if [ "$DEBUG" = "true" ] ; then
405   set -x
406 fi
407
408 [ "$_opt_help" ] && {
409   usage ; eend 0
410   eend 0
411   exit 0
412 }
413
414 [ "$_opt_version" ] && {
415   einfo "$PN - version $VERSION"
416   einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
417   eend 0
418   exit 0
419 }
420 # }}}
421
422 # check for root permissions {{{
423 if ! check4root ; then
424    echo "For usage instructions please execute '$PN --help'."
425    bailout 1
426 fi
427 # }}}
428
429 # make sure we have what we need {{{
430 if [ -n "$VIRTUAL" ] ; then
431   check4progs kpartx mksh parted qemu-img || bailout 1
432 fi
433 # }}}
434
435 # source specified configuration file {{{
436 if [ -n "$CONFIGFILE" ] ; then
437    einfo "Reading specified config file $CONFIGFILE."
438    if ! . "$CONFIGFILE" ; then
439       eerror "Error reading config file $CONFIGFILE" ; eend 1 ; bailout 1
440    fi
441 fi
442 # }}}
443
444 # backwards compability checks {{{
445 if [ -n "$GROOT" ] ; then
446    eerror "Error: you seem to have \$GROOT configured."
447    eerror "This variable is no longer supported, please visit the"
448    eerror "grml-debootstrap documentation for details."
449    eend 1
450    bailout 1
451 fi
452
453 if echo "$GRUB" | grep -q '^hd' ; then
454    eerror "Error: this syntax for the grub configuration variable is no longer supported."
455    eerror "Please do not use hd... any longer but /dev/sdX instead."
456    eend 1
457    bailout 1
458 fi
459 # }}}
460
461 # welcome screen {{{
462 welcome_dialog()
463 {
464    dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
465 Do you want to continue installing Debian using this frontend?" 0 0 || bailout 0
466 }
467 # }}}
468
469 # ask for target {{{
470 prompt_for_target()
471 {
472   AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
473                sed 's/*//' | \
474                grep -v 'Extended$' | \
475                gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md[0-9]* 2>/dev/null || true);
476
477   if [ -z "$AVAILABLE_PARTITIONS" ] ; then
478      dialog --title "$PN" --trim \
479      --msgbox "Sorry, no partitions found. Please configure your
480      harddisks (see /proc/partitions) using a tool like fdisk,
481      cfdisk, gpart, gparted,..." 0 0
482      bailout 1
483   fi
484
485   PARTITION_LIST=$(for i in $AVAILABLE_PARTITIONS ; do
486                      fs="$(blkid -s TYPE -o value "$i" 2>/dev/null)"
487                      [ -n "$fs" ] || fs='[no_filesystem_yet]'
488                      echo "$i" "$fs"
489                      unset fs
490                    done)
491
492   TARGET=$(dialog --title "$PN" --single-quoted --stdout \
493          --menu "Please select the target partition:" 0 0 0 \
494          $PARTITION_LIST)
495   [ $? -eq 0 ] || bailout 1
496 }
497 # }}}
498
499 # ask for bootmanager {{{
500 prompt_for_bootmanager()
501 {
502   ADDITIONAL_PARAMS=""
503
504   if echo "$TARGET" | grep -q "/dev/md" ; then
505      MBRPART="all disks of Software RAID $TARGET"
506   else
507      # figure out whole disk device
508      found=
509      for device in /dev/disk/by-id/*
510      do
511         [ "$(readlink -f "$device")" = "${TARGET}" ] || continue
512         found=1
513         break
514      done
515      [ -n "$found" ] && MBRDISK=$(echo "${device}" |sed -e 's/-part[0-9][0-9]*$//')
516      if [ -e "$MBRDISK" ]; then
517         MBRDISK=$(readlink -f "$MBRDISK")
518      else
519         # fall back to old behaviour
520         MBRDISK=$(echo "${TARGET}" | sed -e 's/[0-9][0-9]*$//')
521      fi
522
523      MBRPART="MBR of $MBRDISK"
524   fi
525
526   for device in cciss/c0d0 sda hda; do
527     if [ "/dev/$device" != "${MBRDISK}" ]; then
528       grep -q $device /proc/partitions && \
529       ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
530     fi
531   done
532   ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
533
534   OIFS="$IFS"; IFS=:
535
536   GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
537           --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
538             mbr       "install bootmanager into $MBRPART" \
539             nowhere   "do not install bootmanager at all" \
540           ${ADDITIONAL_PARAMS})
541   [ $? -eq 0 ] || bailout 3
542   IFS="$OIFS"
543
544   case "$GETMBR" in
545     mbr)
546       # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
547       if echo "$TARGET" | grep -q "/dev/md" ; then
548         GRUB="$TARGET"
549       else
550         GRUB="$MBRDISK"
551       fi
552       ;;
553     hda)
554       GRUB="/dev/hda"
555       ;;
556     sda)
557       GRUB="/dev/sda"
558       ;;
559     nowhere)
560       GRUB=''
561       ;;
562   esac
563 }
564 # }}}
565
566 # ask for Debian release {{{
567 prompt_for_release()
568 {
569   [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='jessie'
570   RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
571             "Please enter the Debian release you would like to use for installation:" \
572             0 50 5 \
573             lenny    Debian/5.0 \
574             squeeze  Debian/6.0 \
575             wheezy   Debian/7.0 \
576             jessie   Debian/8.0 \
577             stretch  Debian/9.0 \
578             sid      Debian/unstable)"
579   [ $? -eq 0 ] || bailout
580 }
581 # }}}
582
583 # ask for hostname {{{
584 prompt_for_hostname()
585 {
586   HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
587             "Please enter the hostname you would like to use for installation:" \
588             0 0 "$HOSTNAME")"
589   [ $? -eq 0 ] || bailout
590 }
591 # }}}
592
593 # ask for password {{{
594 prompt_for_password()
595 {
596   if [ "$_opt_nopassword" ] ; then
597     einfo "Skip asking for root password as requested."
598     return 0
599   fi
600
601   ROOTPW1='PW1'
602   ROOTPW2='PW2'
603   while [ "$ROOTPW1" != "$ROOTPW2" ]; do
604     ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
605     "Please enter the password for the root account:" 10 60)
606     [ $? -eq 0 ] || bailout
607
608     ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
609     "Please enter the password for the root account again for \
610     confirmation:" 10 60)
611     [ $? -eq 0 ] || bailout
612
613     if [ "$ROOTPW1" != "$ROOTPW2" ]; then
614       dialog --stdout --title "${PN}" --ok-label \
615         "Retry" --msgbox "Passwords do not match!" 10 60
616     fi
617   done
618   ROOTPASSWORD="$ROOTPW1"
619 }
620 # }}}
621
622 # ask for Debian mirror {{{
623 prompt_for_mirror()
624 {
625   [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
626
627   CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
628           --menu "Where do you want to install from?" 0 0 0 \
629             net   "install via network (downloading from mirror)" \
630             local "install from local directory/mirror"
631           )
632   [ $? -eq 0 ] || bailout
633
634   if [ "$CHOOSE_MIRROR" = 'net' ] ; then
635      [ -n "$MIRROR" ] || MIRROR='http://http.debian.net/debian'
636      MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
637                "Please enter Debian mirror you would like to use for installing packages." \
638                0 0 $MIRROR)"
639      [ $? -eq 0 ] || bailout
640   else # CHOOSE_MIRROR == local
641      [ -n "$ISO" ] || ISO='/mnt/mirror'
642      ISO="$(dialog --stdout --title "${PN}" --inputbox \
643                "Please enter directory name you would like to use for installing packages." \
644                0 0 $ISO)"
645      [ $? -eq 0 ] || bailout
646   fi
647 }
648 # }}}
649
650 # software raid setup {{{
651 config_swraid_setup()
652 {
653 TMPFILE=$(mktemp)
654
655 # Currently we support only raid1:
656 RAIDLEVEL='raid1'
657
658 MD_LIST=$(for i in $(seq 0 9) ; do
659             awk '{print $4}' /proc/partitions | grep -q "md$i" || \
660             echo "/dev/md$i /dev/md$i"
661           done)
662
663 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
664 --menu "Which device do you want to use for ${RAIDLEVEL}?
665
666 Notice: activated devices will not be listed for security reasons. Anyway, please make sure the selected device is not in use already!" 0 0 0 \
667 $MD_LIST)
668 [ $? -eq 0 ] || bailout 20
669
670 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
671              sed 's/*//' | \
672              grep -v 'Extended$' | \
673              gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
674 [ -n "$AVAILABLE_PARTITIONS" ] || echo "Fatal error: no partitions available?"
675 PARTITION_LIST=$(for i in $AVAILABLE_PARTITIONS ; do
676                      echo "$i $(blkid -s TYPE -o value "$i" 2>/dev/null || echo '[no_filesystem_yet]') off"
677                  done)
678
679 dialog --title "$PN" --separate-output \
680        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
681        $PARTITION_LIST 2>"$TMPFILE"
682 [ $? -eq 0 ] || bailout
683 SELECTED_PARTITIONS="$(cat "$TMPFILE")"
684
685 NUM_PARTITIONS=0
686 for i in $(cat "$TMPFILE") ; do
687    NUM_PARTITIONS=$(( NUM_PARTITIONS + 1 ))
688 done
689
690 # force metadata version 0.90 for lenny so old grub can boot from this array.
691 METADATA_VERSION=""
692 if [ "$RELEASE" = "lenny" ]; then
693    METADATA_VERSION="-e0"
694 fi
695
696 ERRORFILE=$(mktemp)
697 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
698       --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
699 RC=$?
700 if [ "$RC" = 0 ] ; then
701    dialog --title "$PN" --msgbox \
702    "Creating $TARGET was successful." 0 0
703    rm -f "$TMPFILE" "$ERRORFILE"
704 else
705    dialog --title "$PN" --msgbox \
706    "There was an error setting up $TARGET:
707
708 $(cat "$ERRORFILE")
709
710 Exiting." 0 0
711    rm -f "$TMPFILE" "$ERRORFILE"
712    bailout 1
713 fi
714
715 }
716
717 prompt_for_swraid()
718 {
719 if dialog --stdout --title "$PN" \
720           --defaultno --yesno "Do you want to configure Software RAID?
721
722 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
723   config_swraid_setup
724 fi
725 }
726 # }}}
727
728 # user should recheck his configuration {{{
729 # support full automatic installation:
730 checkforrun() {
731    dialog --timeout 10 --title "$PN" \
732           --yesno "Do you want to stop at this stage?
733
734 Notice: you are running ${PN} in non-interactive mode.
735 ${PN} will install Debian ${RELEASE} on ${TARGET}.
736 Last chance to quit. Timeout of 10 seconds running....
737
738 Do you want to stop now?" 0 0 2>/dev/null
739 }
740 # }}}
741
742 # make sure the user is aware of the used configuration {{{
743 checkconfiguration()
744 {
745 if [ -n "$AUTOINSTALL" ] ; then
746    if checkforrun ; then
747       eerror "Exiting as requested" ; eend 0
748       bailout 1
749    fi
750 elif [ -n "$INTERACTIVE" ] ; then
751
752    INFOTEXT="Please recheck configuration before execution:
753    "
754    [ -n "$TARGET" ]  && INFOTEXT="$INFOTEXT
755    Target:          $TARGET"
756    [ -n "$GRUB" ]    && INFOTEXT="$INFOTEXT
757    Install grub:    $GRUB"
758    [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
759    Using release:   $RELEASE"
760    [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
761    Using hostname:  $HOSTNAME"
762    [ -n "$MIRROR" ]  && INFOTEXT="$INFOTEXT
763    Using mirror:    $MIRROR"
764    [ -n "$ISO" ]  && INFOTEXT="$INFOTEXT
765    Using ISO:       $ISO"
766    [ -n "$ARCH" ]  && INFOTEXT="$INFOTEXT
767    Using arch:      $ARCH"
768
769    INFOTEXT="$INFOTEXT
770
771 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
772
773    dialog --title "$PN" --no-collapse \
774           --yesno "$INFOTEXT" 0 0
775    [ $? -eq 0 ] || bailout 0
776
777 else # if not running automatic installation display configuration and prompt for execution:
778    einfo "$PN [${VERSION}] - Please recheck configuration before execution:"
779    echo
780    echo "   Target:          $TARGET"
781
782    # do not display if MNTPOINT is the default one
783    case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo "   Mount point:     $MNTPOINT" ;; esac
784
785    if [ -n "$VIRTUAL" ] ; then
786       echo "   Install grub:    yes"
787    else
788      [ -n "$GRUB" ]     && echo "   Install grub:    $GRUB" || echo "   Install grub:    no"
789    fi
790
791    [ -n "$RELEASE" ]  && echo "   Using release:   $RELEASE"
792    [ -n "$HOSTNAME" ] && echo "   Using hostname:  $HOSTNAME"
793    [ -n "$MIRROR" ]   && echo "   Using mirror:    $MIRROR"
794    [ -n "$ISO" ]      && echo "   Using ISO:       $ISO"
795    [ -n "$ARCH" ]     && echo "   Using arch:      $ARCH"
796    if [ -n "$VIRTUAL" ] ; then
797       echo "   Deploying as Virtual Machine."
798       if [ -n "$VMSIZE" -a -n "$VMFILE" ]; then
799          echo "   Using Virtual Disk file with size of ${VMSIZE}."
800       fi
801    fi
802
803    if [ ! -t 0 -a -z "$ROOTPASSWORD" -a -z "$NOPASSWORD" ] ; then
804       echo
805       echo "   You do not have a TTY allocated, your password will be shown in"
806       echo "   plaintext on the terminal! If you are using SSH, try its -t option!"
807    fi
808
809    echo
810    echo "   Important! Continuing will delete all data from ${TARGET}!"
811
812    if [ -n "$FORCE" ] ; then
813      einfo "Skip user acknowledgement as requested via --force option."
814    else
815      echo
816      einfon "Is this ok for you? [y/N] "
817      read a
818      if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
819         eerror "Exiting as requested." ; eend 1
820         bailout 1
821      fi
822    fi
823 fi
824 }
825 # }}}
826
827 # interactive mode {{{
828 interactive_mode()
829 {
830   check4progs dialog || bailout 1
831
832   welcome_dialog
833
834   prompt_for_release
835
836   prompt_for_swraid
837
838   prompt_for_target
839
840   prompt_for_bootmanager
841
842   prompt_for_hostname
843
844   prompt_for_password
845
846   prompt_for_mirror
847 }
848
849 # run interactive mode if we didn't get the according configuration yet
850 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
851    # only target might be unset, so make sure the INTERACTIVE flag is set as well
852    INTERACTIVE=1
853    interactive_mode
854 fi
855 # }}}
856
857 # architecture setup {{{
858 if [ -n "$ARCH" ] ; then
859    ARCHCMD="--arch $ARCH"
860    ARCHINFO=" (${ARCH})"
861 else
862    ARCH="$(dpkg --print-architecture)"
863    ARCHCMD="--arch $ARCH"
864    ARCHINFO=" (${ARCH})"
865 fi
866 # }}}
867
868 # It is not possible to build amd64 on i686. {{{
869 CURRENT_ARCH="$(uname -m)"
870 if [ "$CURRENT_ARCH" != "x86_64" ] ; then
871    if [ "$ARCH" = "amd64" ] ; then
872       eerror "It is not possible to build amd64 on $CURRENT_ARCH." ; eend 1
873       bailout 1
874    fi
875 fi
876 # }}}
877
878 # Support for generic release codenames is unavailable. {{{
879 if [ "$RELEASE" = "stable" ] || [ "$RELEASE" = "testing" ] ; then
880    eerror "Generic release codenames (stable, testing) are unsupported. \
881 Please use specific codenames such as lenny, squeeze, wheezy, jessie or stretch." ; eend 1
882    bailout 1
883 fi
884 # }}}
885
886 checkconfiguration
887
888 # finally make sure at least $TARGET is set [the partition for the new system] {{{
889 if [ -n "$TARGET" ] ; then
890    SHORT_TARGET="${TARGET##*/}"
891 else
892    eerror "Please adjust $CONFFILES/config or..."
893    eerror "... use the interactive version for configuration before running ${0}" ; eend 1
894    bailout 1
895 fi
896 # }}}
897
898 # stages setup {{{
899 if [ -z "$STAGES" ] ; then
900    STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
901    [ -d "$STAGES" ] || mkdir -p "$STAGES"
902 fi
903
904 if [ -r "$STAGES"/grml-debootstrap ] ; then
905    if grep -q 'done' "${STAGES}/grml-debootstrap" ; then
906       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
907       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
908    fi
909 fi
910 # }}}
911
912 # partition handling {{{
913 PARTITION=''
914 DIRECTORY=''
915
916 set_target_directory(){
917     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
918     DIRECTORY=1
919     MNTPOINT="$TARGET"
920     MKFS=''
921     TUNE2FS=''
922     FSCK=''
923     # make sure we normalise the path to an absolute directory name so something like:
924     #  mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
925     # works
926     TARGET="$(readlink -f "$TARGET")"
927 }
928
929 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
930     PARTITION=1
931 else
932     set_target_directory
933 fi
934 # }}}
935
936 # make sure we have the right syntax when using an iso image {{{
937 if [ -n "$ISO" ] ; then
938    case $ISO in
939       file*) # do nothing
940       ;;
941       *)
942       ISO=file:$ISO
943       ;;
944    esac
945 fi
946 ISODIR=${ISO##file:}
947 ISODIR=${ISODIR%%/}
948 # }}}
949
950 # Debian ISOs do not contain signed Release files {{{
951 if [ -n "$ISO" ] ; then
952     DEBOOTSTRAP_OPT="$DEBOOTSTRAP_OPT --no-check-gpg"
953 fi
954 # }}}
955
956 # create filesystem {{{
957 mkfs() {
958   if [ -n "$DIRECTORY" ] ; then
959      einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
960      return 0
961   fi
962
963   if grep -q "$TARGET" /proc/mounts ; then
964     eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
965     bailout 1
966   fi
967
968   # mkfs.ext* might prompt with "/dev/sdX# contains a ext* file system
969   # created on ... Proceed anyway? (y,n)" which we want to skip in force mode
970   if [ -n "$MKFS" ] && [ -n "$FORCE" ] ; then
971     case "$MKFS" in
972       mkfs.ext*)
973         einfo "Enabling force option (-F) for mkfs.ext* tool as requested via --force switch."
974         MKFS_OPTS="$MKFS_OPTS -F"
975         eend 0
976         ;;
977     esac
978   fi
979
980   if [ -n "$MKFS" ] ; then
981     einfo "Running $MKFS $MKFS_OPTS on $TARGET"
982     "$MKFS" $MKFS_OPTS "$TARGET" ; RC=$?
983
984     if [ "$FIXED_DISK_IDENTIFIERS" = "yes" ] ; then
985       if ! echo "$MKFS" | grep -q "mkfs.ext" ; then
986         eerror "Not changing disk uuid for $TARGET because $MKFS doesn't seem to match for ext{2,3,4} file system"
987         eend 1
988         bailout 1
989       else
990         einfo "Changing disk uuid for $TARGET to fixed (non-random) value $DISK_IDENTIFIER using tune2fs"
991         tune2fs "$TARGET" -U "$DISK_IDENTIFIER"
992         eend $?
993       fi
994     fi
995
996     # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
997     # will fail to detect the uuid in the chroot
998     if [ -n "$VIRTUAL" ] ; then
999       einfo "Virtual environment doesn't require blockdev --rereadpt, skipping therefore"
1000     elif echo "$TARGET" | grep -q "/dev/md" ; then
1001       blockdev --rereadpt "${TARGET}"
1002     else
1003       # if we deploy to /dev/sdX# then let's see if /dev/sdX exists
1004       local main_device="${TARGET%%[0-9]*}"
1005       # sanity check to not try to e.g. access /dev/loop if we get /dev/loop0
1006       if [ -f "/sys/block/$(basename ${main_device})/$(basename ${TARGET})/dev" ] ; then
1007         blockdev --rereadpt "$main_device"
1008       else
1009         einfo "No underlying block device for $TARGET identified, skipping blockdev --rereadpt."
1010       fi
1011     fi
1012     # give the system 2 seconds, otherwise we might run into
1013     # race conditions :-/
1014     sleep 2
1015
1016     eval "$(blkid -o udev "$TARGET" 2>/dev/null)"
1017     [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
1018
1019     eend $RC
1020   fi
1021 }
1022 # }}}
1023
1024 # modify filesystem settings {{{
1025 tunefs() {
1026   if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
1027      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
1028      $TUNE2FS "$TARGET"
1029      eend $?
1030   fi
1031 }
1032 # }}}
1033
1034 # mount the new partition or if it's a directory do nothing at all {{{
1035 mount_target() {
1036   if [ -n "$DIRECTORY" ] ; then
1037      einfo "Running grml-debootstrap on a directory, nothing to mount."
1038   else
1039      if grep -q "$TARGET" /proc/mounts ; then
1040         ewarn "$TARGET already mounted, continuing anyway." ; eend 0
1041      else
1042        if ! [ -d "${MNTPOINT}" ] ; then
1043           [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
1044        fi
1045        einfo "Mounting $TARGET to $MNTPOINT"
1046        mkdir -p "$MNTPOINT"
1047        mount -o rw,suid,dev "$TARGET" "$MNTPOINT"
1048        eend $?
1049      fi
1050   fi
1051   if [ -n "$ISODIR" ] ; then
1052      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
1053      mkdir -p "$MNTPOINT/$ISODIR"
1054      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
1055      eend $?
1056   fi
1057 }
1058 # }}}
1059
1060 # prepare VM image for usage with debootstrap {{{
1061 prepare_vm() {
1062   if [ -z "$VIRTUAL" ] ; then
1063      return 0 # be quiet by intention
1064   fi
1065
1066   if [ -b "$TARGET" -a -n "$VMFILE" ] ; then
1067      eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
1068      eend 1
1069      bailout 1
1070   fi
1071   if [ ! -b "$TARGET" -a -z "$VMFILE" ] ; then
1072      eerror "Error: specified virtual disk target ($TARGET) does not exist yet."
1073      eend 1
1074      bailout 1
1075   fi
1076
1077   ORIG_TARGET="$TARGET" # store for later reuse
1078
1079   if [ -n "$VMFILE" ]; then
1080     qemu-img create -f raw "${TARGET}" "${VMSIZE}"
1081   fi
1082   echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
1083   dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
1084   if [ "$FIXED_DISK_IDENTIFIERS" = "yes" ] ; then
1085     einfo "Adjusting disk signature to a fixed (non-random) value"
1086     MBRTMPFILE=$(mktemp)
1087     dd if="${TARGET}" of="${MBRTMPFILE}" bs=512 count=1
1088     echo -en "\x41\x41\x41\x41" | dd of="${MBRTMPFILE}" conv=notrunc seek=440 bs=1
1089     dd if="${MBRTMPFILE}" of="${TARGET}" conv=notrunc
1090     eend $?
1091   fi
1092   parted -s "${TARGET}" 'mkpart primary ext4 2M -1'
1093
1094   # if dm-mod isn't available then kpartx will fail with
1095   # "Is device-mapper driver missing from kernel? [...]"
1096   if ! kpartx -av "$TARGET" >/dev/null 2>&1 || ! grep -q 'device-mapper' /proc/misc >/dev/null 2>&1 ; then
1097     einfo "Device-mapper not ready yet, trying to load dm-mod module."
1098     modprobe dm-mod ; eend $?
1099   fi
1100
1101   # make sure loop module is present
1102   if ! losetup -f >/dev/null 2>&1; then
1103     einfo "Can not find a usable loop device, retrying after loading loop module."
1104     modprobe loop
1105     if losetup -f >/dev/null 2>&1; then
1106       einfo "Found a usable loop device now, continuing."
1107     else
1108       eerror "Error finding usable loop device" ; eend 1
1109       bailout 1
1110     fi
1111   fi
1112
1113   DEVINFO=$(kpartx -asv "$TARGET") # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
1114   if [ -z "${DEVINFO}" ] ; then
1115     eerror "Error setting up loopback device." ; eend 1
1116     bailout 1
1117   fi
1118
1119   # hopefully this always works as expected
1120   LOOP=$(echo "${DEVINFO}" | sed 's/.* linear //; s/ [[:digit:]]*//') # 'loop1'
1121   LOOP_PART="$(echo "${DEVINFO##add map }" | sed 's/ .*//')" # 'loop1p1'
1122   export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
1123
1124   if [ -z "$TARGET" ] ; then
1125      eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
1126      bailout 1
1127   fi
1128 }
1129 # }}}
1130
1131 # make VM image bootable and unmount it {{{
1132 finalize_vm() {
1133   if [ -z "${VIRTUAL}" ] ; then
1134      return 0
1135   fi
1136
1137   if ! mount "${TARGET}" "${MNTPOINT}" ; then
1138     eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
1139     bailout 1
1140   fi
1141
1142   mount -t proc none "${MNTPOINT}"/proc
1143   mount -t sysfs none "${MNTPOINT}"/sys
1144   mount --bind /dev "${MNTPOINT}"/dev
1145   mount --bind /dev/pts "${MNTPOINT}"/dev/pts
1146
1147 # Has chroot-script installed GRUB to MBR using grub-install (successfully), already?
1148 # chroot-script skips installation for unset ${GRUB}
1149 if [[ -z "${GRUB}" ]] || ! dd if="${GRUB}" bs=512 count=1 2>/dev/null | cat -v | fgrep -q GRUB; then
1150   einfo "Installing Grub as bootloader."
1151   mkdir -p "${MNTPOINT}/boot/grub"
1152   if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
1153      eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
1154      bailout 1
1155   fi
1156
1157   case "$RELEASE" in
1158     lenny|squeeze|wheezy)
1159       cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
1160       ;;
1161     *)
1162       cp -a "${MNTPOINT}"/usr/lib/grub/i386-pc "${MNTPOINT}/boot/grub/"
1163       ;;
1164   esac
1165   chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
1166   dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
1167   rm -f "${MNTPOINT}/tmp/core.img"
1168 fi
1169
1170   einfo "Updating grub configuration file."
1171   if [ -n "$BOOT_APPEND" ] ; then
1172      sed -i "/GRUB_CMDLINE_LINUX_DEFAULT/ s#\"\$# ${BOOT_APPEND}\"#" "${MNTPOINT}"/etc/default/grub
1173   fi
1174   chroot "${MNTPOINT}" update-grub
1175
1176   case "$RELEASE" in
1177     jessie)
1178       einfo "Applying workaround for GRUB font path bug in jessie (Debian #787685)."
1179       mkdir -p "${MNTPOINT}/boot/grub/fonts/"
1180       cp "${MNTPOINT}/usr/share/grub/unicode.pf2" "${MNTPOINT}/boot/grub/fonts/"
1181       ;;
1182   esac
1183
1184   case "$RELEASE" in
1185     lenny|squeeze|wheezy)
1186       einfo "Adjusting grub.cfg for successful boot sequence."
1187       sed -i "s;root=[^ ]\+;root=UUID=$TARGET_UUID;" "${MNTPOINT}"/boot/grub/grub.cfg
1188       ;;
1189   esac
1190
1191   umount "${MNTPOINT}"/proc
1192   umount "${MNTPOINT}"/sys
1193   umount "${MNTPOINT}"/dev/pts
1194   try_umount 3 "${MNTPOINT}"/dev
1195   umount "${MNTPOINT}"
1196   kpartx -d "${ORIG_TARGET}" >/dev/null
1197 }
1198 # }}}
1199
1200 # install main chroot {{{
1201 debootstrap_system() {
1202   if [ "$_opt_nodebootstrap" ]; then
1203      einfo "Skipping debootstrap as requested."
1204      return
1205   fi
1206
1207   if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
1208     :
1209   else
1210     eerror "Error: $MNTPOINT not mounted, can not continue."
1211     eend 1 ; exit 1
1212   fi
1213
1214   if [ -n "$ISO" ] ; then
1215     einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${ISO}"
1216     einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
1217     "$DEBOOTSTRAP" $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT "$RELEASE" "$MNTPOINT" "$ISO"
1218     RC=$?
1219   else
1220     einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}"
1221     einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
1222     "$DEBOOTSTRAP" $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT "$RELEASE" "$MNTPOINT" "$MIRROR"
1223     RC=$?
1224   fi
1225
1226   if [ $RC -ne 0 ] ; then
1227     if [ -r "$MNTPOINT/debootstrap/debootstrap.log" ] && \
1228       [ -s "$MNTPOINT/debootstrap/debootstrap.log" ] ; then
1229       einfo "Presenting last ten lines of debootstrap.log:"
1230       tail -10 "${MNTPOINT}"/debootstrap/debootstrap.log
1231       einfo "End of debootstrap.log"
1232     fi
1233   fi
1234
1235   eend $RC
1236 }
1237 # }}}
1238
1239 # prepare chroot via chroot-script {{{
1240 preparechroot() {
1241   einfo "Preparing chroot system"
1242
1243   # provide variables to chroot system
1244   CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
1245   touch "$CHROOT_VARIABLES"
1246   chmod 600 "$CHROOT_VARIABLES" # make sure nobody except root can read it
1247   echo "# Configuration of ${PN}"                                                                                   > "$CHROOT_VARIABLES"
1248   # Resorting to sed(1) for escaping since "VAR='${VAR//\'/\'\\\'\'}'" does not work with all versions of Bash,
1249   #   e.g. not with 4.2.37(1)-release (a.k.a 4.2+dfsg-0.1+deb7u3) of Debian wheezy
1250   [ -n "$ARCH" ]                && echo "ARCH='$(sed "s,','\\\\'',g" <<<"${ARCH}")'"                               >> "$CHROOT_VARIABLES"
1251   [ -n "$BACKPORTREPOS" ]       && echo "BACKPORTREPOS='$(sed "s,','\\\\'',g" <<<"${BACKPORTREPOS}")'"             >> "$CHROOT_VARIABLES"
1252   [ -n "$CHROOT_SCRIPTS" ]      && echo "CHROOT_SCRIPTS='$(sed "s,','\\\\'',g" <<<"${CHROOT_SCRIPTS}")'"           >> "$CHROOT_VARIABLES"
1253   [ -n "$COMPONENTS" ]          && echo "COMPONENTS='$(sed "s,','\\\\'',g" <<<"${COMPONENTS}")'"                   >> "$CHROOT_VARIABLES"
1254   [ -n "$CONFFILES" ]           && echo "CONFFILES='$(sed "s,','\\\\'',g" <<<"${CONFFILES}")'"                     >> "$CHROOT_VARIABLES"
1255   [ -n "$DEBCONF" ]             && echo "DEBCONF='$(sed "s,','\\\\'',g" <<<"${DEBCONF}")'"                         >> "$CHROOT_VARIABLES"
1256   [ -n "$DEBIAN_FRONTEND" ]     && echo "DEBIAN_FRONTEND='$(sed "s,','\\\\'',g" <<<"${DEBIAN_FRONTEND}")'"         >> "$CHROOT_VARIABLES"
1257   [ -n "$DEBOOTSTRAP" ]         && echo "DEBOOTSTRAP='$(sed "s,','\\\\'',g" <<<"${DEBOOTSTRAP}")'"                 >> "$CHROOT_VARIABLES"
1258   [ -n "$DEFAULT_LOCALES" ]     && echo "DEFAULT_LOCALES='$(sed "s,','\\\\'',g" <<<"${DEFAULT_LOCALES}")'"         >> "$CHROOT_VARIABLES"
1259   [ -n "$DEFAULT_LANGUAGE" ]    && echo "DEFAULT_LANGUAGE='$(sed "s,','\\\\'',g" <<<"${DEFAULT_LANGUAGE}")'"       >> "$CHROOT_VARIABLES"
1260   [ -n "$EXTRAPACKAGES" ]       && echo "EXTRAPACKAGES='$(sed "s,','\\\\'',g" <<<"${EXTRAPACKAGES}")'"             >> "$CHROOT_VARIABLES"
1261   [ -n "$FALLBACK_MIRROR" ]     && echo "FALLBACK_MIRROR='$(sed "s,','\\\\'',g" <<<"${FALLBACK_MIRROR}")'"         >> "$CHROOT_VARIABLES"
1262   [ -n "$FORCE" ]               && echo "FORCE='$(sed "s,','\\\\'',g" <<<"${FORCE}")'"                             >> "$CHROOT_VARIABLES"
1263   [ -n "$GRMLREPOS" ]           && echo "GRMLREPOS='$(sed "s,','\\\\'',g" <<<"${GRMLREPOS}")'"                     >> "$CHROOT_VARIABLES"
1264   [ -n "$GRUB" ]                && echo "GRUB='$(sed "s,','\\\\'',g" <<<"${GRUB}")'"                               >> "$CHROOT_VARIABLES"
1265   [ -n "$HOSTNAME" ]            && echo "HOSTNAME='$(sed "s,','\\\\'',g" <<<"${HOSTNAME}")'"                       >> "$CHROOT_VARIABLES"
1266   [ -n "$INITRD" ]              && echo "INITRD='$(sed "s,','\\\\'',g" <<<"${INITRD}")'"                           >> "$CHROOT_VARIABLES"
1267   [ -n "$INSTALL_NOTES" ]       && echo "INSTALL_NOTES='$(sed "s,','\\\\'',g" <<<"${INSTALL_NOTES}")'"             >> "$CHROOT_VARIABLES"
1268   [ -n "$ISODIR" ]              && echo "ISODIR='$(sed "s,','\\\\'',g" <<<"${ISO}")'"                              >> "$CHROOT_VARIABLES"
1269   [ -n "$ISO" ]                 && echo "ISO='$(sed "s,','\\\\'',g" <<<"${ISO}")'"                                 >> "$CHROOT_VARIABLES"
1270   [ -n "$KEEP_SRC_LIST" ]       && echo "KEEP_SRC_LIST='$(sed "s,','\\\\'',g" <<<"${KEEP_SRC_LIST}")'"             >> "$CHROOT_VARIABLES"
1271   [ -n "$LOCALES" ]             && echo "LOCALES='$(sed "s,','\\\\'',g" <<<"${LOCALES}")'"                         >> "$CHROOT_VARIABLES"
1272   [ -n "$MIRROR" ]              && echo "MIRROR='$(sed "s,','\\\\'',g" <<<"${MIRROR}")'"                           >> "$CHROOT_VARIABLES"
1273   [ -n "$MKFS" ]                && echo "MKFS='$(sed "s,','\\\\'',g" <<<"${MKFS}")'"                               >> "$CHROOT_VARIABLES"
1274   [ -n "$NOPASSWORD" ]          && echo "NOPASSWORD=\"true\""                                                      >> "$CHROOT_VARIABLES"
1275   [ -n "$NOKERNEL" ]            && echo "NOKERNEL=\"true\""                                                        >> "$CHROOT_VARIABLES"
1276   [ -n "$PACKAGES" ]            && echo "PACKAGES='$(sed "s,','\\\\'',g" <<<"${PACKAGES}")'"                       >> "$CHROOT_VARIABLES"
1277   [ -n "$PRE_SCRIPTS" ]         && echo "PRE_SCRIPTS='$(sed "s,','\\\\'',g" <<<"${PRE_SCRIPTS}")'"                 >> "$CHROOT_VARIABLES"
1278   [ -n "$RECONFIGURE" ]         && echo "RECONFIGURE='$(sed "s,','\\\\'',g" <<<"${RECONFIGURE}")'"                 >> "$CHROOT_VARIABLES"
1279   [ -n "$RELEASE" ]             && echo "RELEASE='$(sed "s,','\\\\'',g" <<<"${RELEASE}")'"                         >> "$CHROOT_VARIABLES"
1280   [ -n "$RM_APTCACHE" ]         && echo "RM_APTCACHE='$(sed "s,','\\\\'',g" <<<"${RM_APTCACHE}")'"                 >> "$CHROOT_VARIABLES"
1281   [ -n "$ROOTPASSWORD" ]        && echo "ROOTPASSWORD='$(sed "s,','\\\\'',g" <<<"${ROOTPASSWORD}")'"               >> "$CHROOT_VARIABLES"
1282   [ -n "$SCRIPTS" ]             && echo "SCRIPTS='$(sed "s,','\\\\'',g" <<<"${SCRIPTS}")'"                         >> "$CHROOT_VARIABLES"
1283   [ -n "$SECURE" ]              && echo "SECURE='$(sed "s,','\\\\'',g" <<<"${SECURE}")'"                           >> "$CHROOT_VARIABLES"
1284   [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS='$(sed "s,','\\\\'',g" <<<"${SELECTED_PARTITIONS}")'" >> "$CHROOT_VARIABLES"
1285   [ -n "$TARGET" ]              && echo "TARGET='$(sed "s,','\\\\'',g" <<<"${TARGET}")'"                           >> "$CHROOT_VARIABLES"
1286   [ -n "$UPGRADE_SYSTEM" ]      && echo "UPGRADE_SYSTEM='$(sed "s,','\\\\'',g" <<<"${UPGRADE_SYSTEM}")'"           >> "$CHROOT_VARIABLES"
1287   [ -n "$TARGET_UUID" ]         && echo "TARGET_UUID='$(sed "s,','\\\\'',g" <<<"${TARGET_UUID}")'"                 >> "$CHROOT_VARIABLES"
1288   [ -n "$TIMEZONE" ]            && echo "TIMEZONE='$(sed "s,','\\\\'',g" <<<"${TIMEZONE}")'"                       >> "$CHROOT_VARIABLES"
1289   [ -n "$TUNE2FS" ]             && echo "TUNE2FS='$(sed "s,','\\\\'',g" <<<"${TUNE2FS}")'"                         >> "$CHROOT_VARIABLES"
1290   [ -n "$VMSIZE" ]              && echo "VMSIZE='$(sed "s,','\\\\'',g" <<<"${VMSIZE}")'"                           >> "$CHROOT_VARIABLES"
1291
1292   cp $VERBOSE "${CONFFILES}"/chroot-script "${MNTPOINT}"/bin/chroot-script
1293   chmod 755 "${MNTPOINT}"/bin/chroot-script
1294   [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1295
1296   # make sure we have our files for later use via chroot-script
1297   cp $VERBOSE "${CONFFILES}/config"           "${MNTPOINT}"/etc/debootstrap/
1298   # make sure we adjust the configuration variables accordingly:
1299   sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" "${MNTPOINT}"/etc/debootstrap/config
1300   sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#"    "${MNTPOINT}"/etc/debootstrap/config
1301   sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#"          "${MNTPOINT}"/etc/debootstrap/config
1302
1303   # install notes:
1304   if [ -n "$INSTALL_NOTES" ] ; then
1305      [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" "${MNTPOINT}"/etc/debootstrap/
1306   fi
1307
1308   # package selection:
1309   cp $VERBOSE "${_opt_packages:-$CONFFILES/packages}" \
1310     "${MNTPOINT}"/etc/debootstrap/packages
1311
1312   # debconf preseeding:
1313   _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1314   [ -f "${_opt_debconf}" ] && [ "$DEBCONF" = 'yes' ] && \
1315     cp $VERBOSE "${_opt_debconf}" "${MNTPOINT}"/etc/debootstrap/debconf-selections
1316
1317   # copy scripts that should be executed inside the chroot:
1318   _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1319   [ -d "$_opt_chroot_scripts" ] && [ "$CHROOT_SCRIPTS" = 'yes' ] && {
1320     mkdir -p "${MNTPOINT}"/etc/debootstrap/chroot-scripts
1321     cp -a $VERBOSE "${_opt_chroot_scripts}"/* "${MNTPOINT}"/etc/debootstrap/chroot-scripts/
1322   }
1323
1324   # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1325   cp $VERBOSE "${CHROOT_VARIABLES}" "${MNTPOINT}"/etc/debootstrap/variables
1326
1327   cp $VERBOSE -a -L "${CONFFILES}"/extrapackages/ "${MNTPOINT}"/etc/debootstrap/
1328
1329   # make sure we can access network [relevant for cdebootstrap]
1330   [ -f "${MNTPOINT}"/etc/resolv.conf ] || cp $VERBOSE /etc/resolv.conf "${MNTPOINT}"/etc/resolv.conf
1331
1332   # provide system's /etc/hosts to the target:
1333   if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1334      cp $VERBOSE /etc/hosts "${MNTPOINT}"/etc/hosts
1335   fi
1336
1337   # setup default locales
1338   [ -n "$LOCALES" ] && cp $VERBOSE "${CONFFILES}"/locale.gen "${MNTPOINT}"/etc/locale.gen
1339
1340   # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1341   ( cd "${MNTPOINT}"/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1342
1343   # copy any existing files to chroot
1344   [ -d "${CONFFILES}"/bin   ] && cp $VERBOSE -a -L "${CONFFILES}"/bin/*   "${MNTPOINT}"/bin/
1345   [ -d "${CONFFILES}"/boot  ] && cp $VERBOSE -a -L "${CONFFILES}"/boot/*  "${MNTPOINT}"/boot/
1346   [ -d "${CONFFILES}"/etc   ] && cp $VERBOSE -a -L "${CONFFILES}"/etc/*   "${MNTPOINT}"/etc/
1347   [ -d "${CONFFILES}"/sbin  ] && cp $VERBOSE -a -L "${CONFFILES}"/sbin/*  "${MNTPOINT}"/sbin/
1348   [ -d "${CONFFILES}"/share ] && cp $VERBOSE -a -L "${CONFFILES}"/share/* "${MNTPOINT}"/share/
1349   [ -d "${CONFFILES}"/usr   ] && cp $VERBOSE -a -L "${CONFFILES}"/usr/*   "${MNTPOINT}"/usr/
1350   [ -d "${CONFFILES}"/var   ] && cp $VERBOSE -a -L "${CONFFILES}"/var/*   "${MNTPOINT}"/var/
1351
1352   # network setup
1353   DEFAULT_INTERFACES="# /etc/network/interfaces - generated by grml-debootstrap
1354
1355 # Include files from /etc/network/interfaces.d when using
1356 # ifupdown v0.7.44 or newer:
1357 #source-directory /etc/network/interfaces.d
1358
1359 auto lo
1360 iface lo inet loopback
1361
1362 allow-hotplug eth0
1363 iface eth0 inet dhcp
1364 "
1365
1366   if [ -n "$NOINTERFACES" ] ; then
1367     einfo "Not installing /etc/network/interfaces as requested via --nointerfaces option" ; eend 0
1368   elif [ -n "$USE_DEFAULT_INTERFACES" ] ; then
1369     einfo "Installing default /etc/network/interfaces as requested via --defaultinterfaces options."
1370     echo "$DEFAULT_INTERFACES" > "${MNTPOINT}/etc/network/interfaces"
1371     eend $?
1372   elif [ -n "$VIRTUAL" ] ; then
1373     einfo "Setting up Virtual Machine, installing default /etc/network/interfaces"
1374     echo "$DEFAULT_INTERFACES" > "${MNTPOINT}/etc/network/interfaces"
1375     eend $?
1376   elif [ -r /etc/network/interfaces ] ; then
1377     einfo "Copying /etc/network/interfaces from host to target system"
1378     cp $VERBOSE /etc/network/interfaces "${MNTPOINT}/etc/network/interfaces"
1379     eend $?
1380   else
1381     ewarn "Couldn't read /etc/network/interfaces, installing default /etc/network/interfaces"
1382     echo "$DEFAULT_INTERFACES" > "${MNTPOINT}/etc/network/interfaces"
1383     eend $?
1384   fi
1385
1386   # install config file providing some example entries
1387   if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1388      cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1389   fi
1390
1391   eend 0
1392 }
1393 # }}}
1394
1395 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1396 execute_pre_scripts() {
1397    # make sure we have $MNTPOINT available for our scripts
1398    export MNTPOINT
1399    if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1400       [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1401       for script in ${pre_scripts}/* ; do
1402          if [ -x "$script" ] ; then
1403             einfo "Executing pre-script $script"
1404             $script ; eend $?
1405          fi
1406       done
1407    fi
1408 }
1409 # }}}
1410
1411 # execute all scripts in /etc/debootstrap/scripts/ {{{
1412 execute_scripts() {
1413    # make sure we have $MNTPOINT available for our scripts
1414    export MNTPOINT
1415    if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1416       [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1417       for script in ${scripts}/* ; do
1418          if [ -x "$script" ] ; then
1419             einfo "Executing script $script"
1420             $script ; eend $?
1421          fi
1422       done
1423    fi
1424 }
1425 # }}}
1426
1427 try_umount() {
1428   local tries=$1
1429   local mountpoint="$2"
1430
1431   for (( try=1; try<=tries; try++ )); do
1432     if [[ ${try} -eq ${tries} ]]; then
1433       # Last time, show errors this time
1434       umount "${mountpoint}" && return 0
1435     else
1436       # Not last time, hide errors until fatal
1437       if umount "${mountpoint}" 2>/dev/null ; then
1438         return 0
1439       else
1440         sleep 1
1441       fi
1442     fi
1443   done
1444   return 1  # Tried enough
1445 }
1446
1447 # execute chroot-script {{{
1448 chrootscript() {
1449   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1450     mount_target
1451   fi
1452
1453   if ! [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1454     eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1455     eend 1
1456   else
1457     einfo "Executing chroot-script now"
1458     mount --bind /dev "$MNTPOINT"/dev
1459     mount --bind /dev/pts "$MNTPOINT"/dev/pts
1460     if [ "$DEBUG" = "true" ] ; then
1461       chroot "$MNTPOINT" /bin/bash -x /bin/chroot-script ; RC=$?
1462     else
1463       chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1464     fi
1465     try_umount 3 "$MNTPOINT"/dev/pts
1466     try_umount 3 "$MNTPOINT"/dev
1467     eend $RC
1468   fi
1469
1470   # finally get rid of chroot-script again, there's no good reason to
1471   # keep it on the installed system
1472   if grep -q GRML_CHROOT_SCRIPT_MARKER "${MNTPOINT}/bin/chroot-script" ; then
1473     einfo "Removing chroot-script again"
1474     rm -f "${MNTPOINT}/bin/chroot-script"
1475     eend $?
1476   else
1477     einfo "Keeping chroot-script as string GRML_CHROOT_SCRIPT_MARKER could not be found"
1478     eend 0
1479   fi
1480 }
1481 # }}}
1482
1483 # unmount $MNTPOINT {{{
1484 umount_chroot() {
1485
1486   # display installation notes:
1487   if [ -n "$INSTALL_NOTES" ] ; then
1488      [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1489   fi
1490
1491   if [ -n "$ISODIR" ] ; then
1492      if grep -q "$ISODIR" /proc/mounts ; then
1493         einfo "Unmount $MNTPOINT/$ISODIR"
1494         umount "$MNTPOINT/$ISODIR"
1495         eend $?
1496      fi
1497   fi
1498
1499   if grep -q "$MNTPOINT" /proc/mounts ; then
1500      if [ -n "$PARTITION" ] ; then
1501         einfo "Unmount $MNTPOINT"
1502         umount "$MNTPOINT"
1503         eend $?
1504      fi
1505   fi
1506 }
1507 # }}}
1508
1509 # execute filesystem check {{{
1510 fscktool() {
1511  if [ -n "$VIRTUAL" ] ; then
1512    einfo "Skipping filesystem check because we deploy a virtual machine."
1513    return 0
1514  fi
1515
1516  if [ "$FSCK" = 'yes' ] ; then
1517    [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1518    einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1519    "$FSCKTOOL" "$TARGET"
1520    eend $?
1521  fi
1522 }
1523 # }}}
1524
1525 # now execute all the functions {{{
1526 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1527          preparechroot execute_pre_scripts chrootscript execute_scripts \
1528          umount_chroot finalize_vm fscktool ; do
1529     if stage "${i}" ; then
1530       if "$i" ; then
1531         stage "${i}" 'done' && rm -f "${STAGES}/${i}"
1532       else
1533         bailout 2 "$i"
1534       fi
1535     fi
1536 done
1537
1538 cleanup
1539 # }}}
1540
1541 # end dialog of autoinstallation {{{
1542 if [ -n "$AUTOINSTALL" ] ; then
1543    if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1544 Automatically rebooting in 10 seconds.
1545
1546 Choose Cancel to skip rebooting." 10 60 10 ; then
1547      noeject noprompt reboot
1548   fi
1549 else
1550    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1551 fi
1552 # }}}
1553
1554 ## END OF FILE #################################################################
1555 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=2