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