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