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