do not run fsck when deploying virtual machine
[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 # http://www.debian.org/releases/stable/i386/index.html.en
9
10 # variables {{{
11 PN="$(basename $0)"
12 VERSION="$(dpkg --list $PN 2>/dev/null| awk '/^i/ {print $3}')"
13 VERSION="${VERSION:-unknown}"
14 MNTPOINT="/mnt/debootstrap.$$"
15
16 # defaults
17 VMSIZE="2G"
18 FORCE=''
19
20 # inside the chroot system locales might not be available, so use minimum:
21 export LANG=C
22 export LC_ALL=C
23
24 # make sure interactive mode is only executed when
25 # using an empty configuration file or option --interactive
26 INTERACTIVE=''
27 # }}}
28
29 # help text {{{
30 usage() {
31   echo "$PN - wrapper around debootstrap for installing Debian
32
33 Usage: $PN [options]
34
35 Bootstrap options:
36
37   -m, --mirror <URL>     Mirror which should be used for apt-get/aptitude.
38   -i, --iso <mnt>        Mountpoint where a Debian ISO is mounted to, for use
39                          instead of fetching packages from a mirror.
40   -r, --release <name>   Release of new Debian system (default: squeeze).
41   -t, --target <target>  Target partition (/dev/...) or directory where the
42                          system should be installed to.
43   -p, --mntpoint <mnt>   Mountpoint used for mounting the target system,
44                          has no effect if -t is given and represents a directory.
45       --debopt <params>  Extra parameters passed to the debootstrap command.
46       --interactive      Use interactive mode (frontend).
47       --nodebootstrap    Skip debootstrap, only do configuration to the target.
48       --grub <device>    Target for grub installation. Usage example: /dev/sda
49       --arch <arch>      Architecture to use. Currently only i386 is supported.
50       --filesystem <fs>  Filesystem that should be used when target is a partition
51                          or Virtual Machine (see --vmfile).
52       --insecure         Do not download and check Release file signatures.
53       --force            Do not prompt for user acknowledgement.
54
55 Options for Virtual Machine deployment:
56
57       --vmfile           Set up a Virtual Machine instead of installing to
58                          a partition or directory, to be combined with --target,
59                          like: --vmfile --target /mnt/sda1/qemu.img
60       --vmsize <size>    Use specified size for size of VM file (default: 2G).
61                          Syntax as supported by qemu-img, like: --vmsize 3G
62
63 Configuration options:
64
65   -c, --config <file>      Use specified configuration file, defaults to
66                              /etc/debootstrap/config
67   -d, --confdir <path>     Place of config files for debootstrap, defaults
68                              to /etc/debootstrap
69       --packages <file>    Install packages defined in specified list file
70                              instead of using /etc/debootstrap/packages.
71       --nopackages         Skip installation of packages defined in
72                              /etc/debootstrap/packages
73       --debconf <file>     Pre-seed packages using specified pre-seed db file.
74       --keep_src_list      Do not overwrite user provided apt sources.list.
75       --hostname <name>    Hostname of Debian system.
76       --nopassword         Do not prompt for the root password.
77       --password <pwd>     Use specified password as password for user root.
78       --bootappend <line>  Add specified appendline to kernel whilst booting.
79       --chroot-scripts <d> Execute chroot scripts from specified directory.
80       --pre-scripts <dir>  Execute scripts from specified directory (before chroot-scripts).
81       --scripts <dir>      Execute scripts from specified directory (after chroot-scripts).
82
83 Other options:
84
85   -v, --verbose            Increase verbosity.
86   -h, --help               Print this usage information and exit.
87   -V, --version            Show summary of options and exit.
88
89 Usage examples can be found in the grml-debootstrap manpage.
90 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
91 "
92 }
93
94 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
95    usage
96    echo 'Please notice that this script requires root permissions!'
97    exit 0
98 fi
99 # }}}
100
101 # early helper functions {{{
102 GOOD='\e[32;01m'
103 WARN='\e[33;01m'
104 BAD='\e[31;01m'
105 NORMAL='\e[0m'
106 HILITE='\e[36;01m'
107 BRACKET='\e[34;01m'
108
109 einfo() {
110   einfon "$1\n"
111   return 0
112 }
113
114 einfon() {
115   [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
116   printf " ${GOOD}*${NORMAL} $*"
117   LAST_E_CMD=einfon
118   return 0
119 }
120
121 eerror() {
122   [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
123   printf " ${BAD}*${NORMAL} $*\n"
124   LAST_E_CMD=eerror
125   return 0
126 }
127
128 eend() {
129   local retval="${1:-0}"
130   shift
131   if [ $retval -gt 0 ]; then
132     printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
133   fi
134   return $retval
135 }
136
137 check4root(){
138   if [ "$(id -u 2>/dev/null)" != 0 ] ; then
139     echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
140   fi
141 }
142
143 check4progs(){
144   local RC=''
145   for arg in $* ; do
146     which $arg >/dev/null 2>&1 || RC="$arg"
147   done
148   if [ -n "$RC" ] ; then
149      echo "$RC not installed"
150      return 1
151   fi
152 }
153 # }}}
154
155 # helper functions {{{
156 # we want to exit smoothly and clean:
157 bailout(){
158   # make sure $TARGET is not mounted when exiting grml-debootstrap
159   if [ -n "$MNTPOINT" ] ; then
160      if grep -q $MNTPOINT /proc/mounts ; then
161         # make sure nothing is left inside chroot so we can unmount it
162         [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
163         [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
164         # ugly, but make sure we really don't leave anything (/proc /proc is intended)
165         for ARG in /sys /proc /proc ; do
166           [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
167         done
168         umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
169
170         [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
171
172         if [ -n "$DIRECTORY" ] ; then
173           einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
174         else
175           einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
176         fi
177
178         if [ -n "$STAGES" ] ; then
179            echo -n "Removing stages directory ${STAGES}: "
180            rm -rf "$STAGES" && echo done
181         fi
182
183         # remove directory only if we used the default with process id inside the name
184         if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
185            einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
186         fi
187      fi
188   fi
189
190   if [ -n "${ORIG_TARGET}" ] ; then
191     einfo "Removing loopback mount of file ${ORIG_TARGET}."
192     kpartx -d "${ORIG_TARGET}" ; eend $?
193   fi
194
195   [ -n "$1" ] && EXIT="$1" || EXIT="1"
196   [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
197
198   exit "$EXIT"
199 }
200 trap bailout HUP INT QUIT TERM
201
202 # we want to execute all the functions only once, simple check for it:
203 stage() {
204   if [ -n "$2" ] ; then
205      echo "$2" > "${STAGES}/${1}"
206      return 0
207   elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
208      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
209      ewarn "  To reexecute it clean up the according directory inside $STAGES" ; eend 0
210      return 1
211   fi
212 }
213 # }}}
214
215 # make sure we have what we need {{{
216 check4progs debootstrap dialog || exit 1
217 # }}}
218
219 # source main configuration file {{{
220 if [ -r /etc/debootstrap/config ] ; then
221   . /etc/debootstrap/config
222 fi
223 # }}}
224
225 # cmdline handling {{{
226 # source external command line parameter-processing script
227 if [ -r ./cmdlineopts.clp ] ; then
228    . ./cmdlineopts.clp
229 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
230    . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
231 else
232    eerror "Error: cmdline function file not found, exiting."
233    eend 1
234    exit 1
235 fi
236
237 # == business-logic of command line parameter-processing
238
239 # source configuration file in <confdir> if supplied. {{{
240 [ "$_opt_confdir" ] && {
241   CONFFILES=$_opt_confdir
242   einfo "Using config files under $CONFFILES/."
243   if ! [ -r "$CONFFILES/config" ] ; then
244     eerror "Error: config file $CONFFILES/config not found."; eend 1; exit 1
245   fi
246   if ! . "$CONFFILES/config" ; then
247     eerror "Error reading config file $CONFFILES/config" ; eend 1 ; exit 1
248   fi
249   # restore the command line parameter value
250   CONFFILES=$_opt_confdir
251 }
252 # }}}
253
254 [ "$_opt_mirror" ]              && MIRROR=$_opt_mirror
255 [ "$_opt_iso" ]                 && ISO=$_opt_iso
256 [ "$_opt_release" ]             && RELEASE=$_opt_release
257 [ "$_opt_target" ]              && TARGET=$_opt_target
258 [ "$_opt_vmfile" ]              && VIRTUAL=1
259 [ "$_opt_vmsize" ]              && VMSIZE=$_opt_vmsize
260 [ "$_opt_mntpoint" ]            && MNTPOINT=$_opt_mntpoint
261 [ "$_opt_debopt" ]              && DEBOOTSTRAP_OPT=$_opt_debopt
262 [ "$_opt_interactive" ]         && INTERACTIVE=1
263 [ "$_opt_config" ]              && CONFIGFILE=$_opt_config
264 [ "$_opt_filesystem" ]          && MKFS="mkfs.$_opt_filesystem"
265 [ "$_opt_packages_set" ]        && PACKAGES='yes'
266 [ "$_opt_nopackages" ]          && PACKAGES=''
267 [ "$_opt_debconf_set" ]         && DEBCONF='yes'
268 [ "$_opt_scripts_set" ]         && SCRIPTS='yes'
269 [ "$_opt_pre_scripts_set" ]     && PRE_SCRIPTS='yes'
270 [ "$_opt_chroot_scripts_set" ]  && CHROOT_SCRIPTS='yes'
271 [ "$_opt_keep_src_list" ]       && KEEP_SRC_LIST='yes'
272 [ "$_opt_hostname" ]            && HOSTNAME=$_opt_hostname
273 [ "$_opt_password" ]            && ROOTPASSWORD=$_opt_password
274 [ "$_opt_nopassword" ]          && NOPASSWORD='yes'
275 [ "$_opt_bootappend" ]          && BOOT_APPEND=$_opt_bootappend
276 [ "$_opt_grub" ]                && GRUB=$_opt_grub
277 [ "$_opt_arch" ]                && ARCH=$_opt_arch
278 [ "$_opt_insecure" ]            && SECURE='false'
279 [ "$_opt_force" ]               && FORCE=$_opt_force
280 [ "$_opt_verbose" ]             && VERBOSE="-v"
281
282 [ "$_opt_help" ] && {
283   usage ; eend 0
284   eend 0
285   exit 0
286 }
287
288 [ "$_opt_version" ] && {
289   einfo "$PN - version $VERSION"
290   einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
291   eend 0
292   exit 0
293 }
294 # }}}
295
296 # check for root permissions {{{
297 if ! check4root ; then
298    echo "For usage instructions please execute '$PN --help'."
299    exit 1
300 fi
301 # }}}
302
303 # make sure we have what we need {{{
304 if [ -n "$VIRTUAL" ] ; then
305   check4progs kpartx mksh qemu-img || exit 1
306 fi
307 # }}}
308
309 # source specified configuration file {{{
310 if [ -n "$CONFIGFILE" ] ; then
311    einfo "Reading specified config file $CONFIGFILE."
312    if ! . "$CONFIGFILE" ; then
313       eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
314    fi
315 fi
316 # }}}
317
318 # backwards compability checks {{{
319 if [ -n "$GROOT" ] ; then
320    eerror "Error: you seem to have \$GROOT configured."
321    eerror "This variable is no longer supported, please visit the"
322    eerror "grml-debootstrap documentation for details."
323    eend 1
324    exit 1
325 fi
326
327 if echo "$GRUB" | grep -q '^hd' ; then
328    eerror "Error: this syntax for the grub configuration variable is no longer supported."
329    eerror "Please do not use hd... any longer but /dev/sdX instead."
330    eend 1
331    exit 1
332 fi
333 # }}}
334
335 # welcome screen {{{
336 welcome_dialog()
337 {
338    dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
339 Do you want to continue installing Debian using this frontend?" 0 0 || bailout 0
340 }
341 # }}}
342
343 # ask for target {{{
344 prompt_for_target()
345 {
346   AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
347                sed 's/*//' | \
348                grep -v 'Extended$' | \
349                gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
350
351   if [ -z "$AVAILABLE_PARTITIONS" ] ; then
352      dialog --title "$PN" --trim \
353      --msgbox "Sorry, no partitions found. Please configure your
354      harddisks (see /proc/partitions) using a tool like fdisk,
355      cfdisk, gpart, gparted,..." 0 0
356      exit 0
357   fi
358
359   PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
360                        echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
361                    done)
362
363   TARGET=$(dialog --title "$PN" --single-quoted --stdout \
364          --menu "Please select the target partition:" 0 0 0 \
365          $PARTITION_LIST)
366   [ $? -eq 0 ] || bailout
367 }
368 # }}}
369
370 # ask for bootmanager {{{
371 prompt_for_bootmanager()
372 {
373   ADDITIONAL_PARAMS=""
374
375   if echo "$TARGET" | grep -q "/dev/md" ; then
376      MBRPART="all disks of Software RAID $TARGET"
377   else
378      # figure out whole disk device
379      found=
380      for device in /dev/disk/by-id/*
381      do
382         [ $(readlink -f $device) = ${TARGET} ] || continue
383         found=1
384         break
385      done
386      [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
387      if [ -e "$MBRDISK" ]; then
388         MBRDISK=$(readlink -f $MBRDISK)
389      else
390         # fall back to old behaviour
391         MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
392      fi
393
394      MBRPART="MBR of $MBRDISK"
395   fi
396
397   for device in cciss/c0d0 sda hda; do
398     if [ /dev/$device != ${MBRDISK} ]; then
399       grep -q $device /proc/partitions && \
400       ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
401     fi
402   done
403   ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
404
405   OIFS="$IFS"; IFS=:
406
407   GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
408           --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
409             mbr       "install bootmanager into $MBRPART" \
410             nowhere   "do not install bootmanager at all" \
411           ${ADDITIONAL_PARAMS})
412   [ $? -eq 0 ] || bailout 3
413   IFS="$OIFS"
414
415   case "$GETMBR" in
416     mbr)
417       # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
418       if echo "$TARGET" | grep -q "/dev/md" ; then
419          # using sw-raid:
420          if [ -n "$SELECTED_PARTITIONS" ] ; then
421             GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
422          else
423             GRUB="$TARGET"
424          fi
425       else
426         GRUB="$MBRDISK"
427       fi
428       ;;
429     hda)
430       GRUB="/dev/hda"
431       ;;
432     sda)
433       GRUB="/dev/sda"
434       ;;
435     nowhere)
436       GRUB=''
437       ;;
438   esac
439 }
440 # }}}
441
442 # ask for Debian release {{{
443 prompt_for_release()
444 {
445   [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='squeeze'
446   RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
447             "Please enter the Debian release you would like to use for installation:" \
448             0 50 4 \
449             lenny    Debian/old-stable \
450             squeeze  Debian/stable \
451             wheezy   Debian/testing \
452             sid      Debian/unstable)"
453   [ $? -eq 0 ] || bailout
454 }
455 # }}}
456
457 # ask for hostname {{{
458 prompt_for_hostname()
459 {
460   HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
461             "Please enter the hostname you would like to use for installation:" \
462             0 0 $HOSTNAME)"
463   [ $? -eq 0 ] || bailout
464 }
465 # }}}
466
467 # ask for password {{{
468 prompt_for_password()
469 {
470   if [ "$_opt_nopassword" ] ; then
471     einfo "Skip asking for root password as requested."
472     return 0
473   fi
474
475   ROOTPW1='PW1'
476   ROOTPW2='PW2'
477   while [ "$ROOTPW1" != "$ROOTPW2" ]; do
478     ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
479     "Please enter the password for the root account:" 10 60)
480     [ $? -eq 0 ] || bailout
481     ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
482     "Please enter the password for the root account again for \
483     confirmation:" 10 60)
484     [ $? -eq 0 ] || bailout
485
486     if [ "$ROOTPW1" != "$ROOTPW2" ]; then
487       $(dialog --stdout --title "${PN}" --ok-label \
488       "Retry" --msgbox "Passwords do not match!" 10 60)
489     fi
490   done
491   ROOTPASSWORD="$ROOTPW1"
492 }
493 # }}}
494
495 # ask for Debian mirror {{{
496 prompt_for_mirror()
497 {
498   [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
499
500   CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
501           --menu "Where do you want to install from?" 0 0 0 \
502             net   "install via network (downloading from mirror)" \
503             local "install from local directory/mirror"
504           )
505   [ $? -eq 0 ] || bailout
506
507   if [ "$CHOOSE_MIRROR" = 'net' ] ; then
508      [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
509      MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
510                "Please enter Debian mirror you would like to use for installing packages." \
511                0 0 $MIRROR)"
512      [ $? -eq 0 ] || bailout
513   else # CHOOSE_MIRROR == local
514      [ -n "$ISO" ] || ISO='/mnt/mirror'
515      ISO="$(dialog --stdout --title "${PN}" --inputbox \
516                "Please enter directory name you would like to use for installing packages." \
517                0 0 $ISO)"
518      [ $? -eq 0 ] || bailout
519   fi
520 }
521 # }}}
522
523 # software raid setup {{{
524 config_swraid_setup()
525 {
526 TMPFILE=$(mktemp)
527
528 # Currently we support only raid1:
529 RAIDLEVEL='raid1'
530
531 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
532 #                   --menu "Which RAID level do you want to use?" 0 0 0 \
533 #                     raid1 "Software RAID level 1" \
534 #                     raid5 "Software RAID level 5")
535 #[ $? -eq 0 ] || bailout 20
536
537 MD_LIST=$(for i in $(seq 0 9) ; do
538             awk '{print $4}' /proc/partitions | grep -q md$i || \
539             echo "/dev/md$i /dev/md$i"
540           done)
541
542 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
543 --menu "Which device do you want to use for ${RAIDLEVEL}?
544
545 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 \
546 $MD_LIST)
547 [ $? -eq 0 ] || bailout 20
548
549 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
550              sed 's/*//' | \
551              grep -v 'Extended$' | \
552              gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
553 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
554 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
555                      echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
556                  done)
557
558 dialog --title "$PN" --separate-output \
559        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
560        $PARTITION_LIST 2>$TMPFILE
561 [ $? -eq 0 ] || bailout
562 RETVAL=$?
563 SELECTED_PARTITIONS="$(cat $TMPFILE)"
564
565 NUM_PARTITIONS=0
566 for i in $(cat $TMPFILE) ; do
567    NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
568 done
569
570 # force metadata version 0.90 for lenny so old grub can boot from this array.
571 METADATA_VERSION=""
572 if [ $RELEASE = "lenny" ]; then
573    METADATA_VERSION="-e0"
574 fi
575
576 ERRORFILE=$(mktemp)
577 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
578       --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
579 RC=$?
580 if [ "$RC" = 0 ] ; then
581    dialog --title "$PN" --msgbox \
582    "Creating $TARGET was successful." 0 0
583    rm -f "$TMPFILE" "$ERRORFILE"
584 else
585    dialog --title "$PN" --msgbox \
586    "There was an error setting up $TARGET:
587
588 $(cat $ERRORFILE)
589
590 Exiting." 0 0
591    rm -f "$TMPFILE" "$ERRORFILE"
592    exit 1
593 fi
594
595 }
596
597 prompt_for_swraid()
598 {
599 if dialog --stdout --title "$PN" \
600           --defaultno --yesno "Do you want to configure Software RAID?
601
602 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
603   config_swraid_setup
604 fi
605 }
606 # }}}
607
608 # user should recheck his configuration {{{
609 # support full automatic installation:
610 checkforrun() {
611    dialog --timeout 10 --title "$PN" \
612           --yesno "Do you want to stop at this stage?
613
614 Notice: you are running ${PN} in non-interactive mode.
615 ${PN} will install Debian ${RELEASE} on ${TARGET}.
616 Last chance to quit. Timeout of 10 seconds running....
617
618 Do you want to stop now?" 0 0 2>/dev/null
619 }
620 # }}}
621
622 # make sure the user is aware of the used configuration {{{
623 checkconfiguration()
624 {
625 if [ -n "$AUTOINSTALL" ] ; then
626    if checkforrun ; then
627       eerror "Exiting as requested" ; eend 0
628       exit 1
629    fi
630 elif [ -n "$INTERACTIVE" ] ; then
631
632    INFOTEXT="Please recheck configuration before execution:
633    "
634    [ -n "$TARGET" ]  && INFOTEXT="$INFOTEXT
635    Target:          $TARGET"
636    [ -n "$GRUB" ]    && INFOTEXT="$INFOTEXT
637    Install grub:    $GRUB"
638    [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
639    Using release:   $RELEASE"
640    [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
641    Using hostname   $HOSTNAME"
642    [ -n "$MIRROR" ]  && INFOTEXT="$INFOTEXT
643    Using mirror:    $MIRROR"
644    [ -n "$ISO" ]  && INFOTEXT="$INFOTEXT
645    Using ISO:       $ISO"
646
647    INFOTEXT="$INFOTEXT
648
649 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
650
651    dialog --title "$PN" --no-collapse \
652           --yesno "$INFOTEXT" 0 0
653    [ $? -eq 0 ] || bailout 0
654
655 else # if not running automatic installation display configuration and prompt for execution:
656    einfo "$PN - Please recheck configuration before execution:"
657    echo
658    echo "   Target:          $TARGET"
659
660    # do not display if MNTPOINT is the default one
661    case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo "   Mount point:     $MNTPOINT" ;; esac
662
663    if [ -n "$VIRTUAL" ] ; then
664       echo "   Install grub:    yes"
665    else
666      [ -n "$GRUB" ]     && echo "   Install grub:    $GRUB" || echo "   Install grub:    no"
667    fi
668
669    [ -n "$RELEASE" ]  && echo "   Using release:   $RELEASE"
670    [ -n "$MIRROR" ]   && echo "   Using mirror:    $MIRROR"
671    [ -n "$HOSTNAME" ] && echo "   Using hostname:  $HOSTNAME"
672    [ -n "$ISO" ]      && echo "   Using ISO:       $ISO"
673    if [ -n "$VIRTUAL" ] ; then
674       echo "   Deploying as Virtual Machine."
675       [ -n "$VMSIZE" ] && echo "   Using Virtual Disk file with size of ${VMSIZE}."
676    fi
677
678    echo
679    echo "   Important! Continuing will delete all data from ${TARGET}!"
680
681    if [ -n "$FORCE" ] ; then
682      einfo "Skip user acknowledgement as requested via --force option."
683    else
684      echo
685      einfon "Is this ok for you? [y/N] "
686      read a
687      if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
688         eerror "Exiting as requested." ; eend 1
689         exit 1
690      fi
691    fi
692 fi
693 }
694 # }}}
695
696 # interactive mode {{{
697 interactive_mode()
698 {
699   welcome_dialog
700
701   prompt_for_release
702
703   prompt_for_swraid
704
705   prompt_for_target
706
707   prompt_for_bootmanager
708
709   prompt_for_hostname
710
711   prompt_for_password
712
713   prompt_for_mirror
714 }
715
716 # run interactive mode if we didn't get the according configuration yet
717 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
718    # only target might be unset, so make sure the INTERACTIVE flag is set as well
719    INTERACTIVE=1
720    interactive_mode
721 fi
722 # }}}
723
724 checkconfiguration
725
726 # finally make sure at least $TARGET is set [the partition for the new system] {{{
727 if [ -n "$TARGET" ] ; then
728    SHORT_TARGET="${TARGET##*/}"
729 else
730    eerror "Please adjust $CONFFILES/config or..."
731    eerror "... use the interactive version for configuration before running ${0}" ; eend 1
732    exit 1
733 fi
734 # }}}
735
736 # stages setup {{{
737 if [ -z "$STAGES" ] ; then
738    STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
739    [ -d "$STAGES" ] || mkdir -p "$STAGES"
740 fi
741
742 if [ -r "$STAGES"/grml-debootstrap ] ; then
743    if grep -q done $STAGES/grml-debootstrap ; then
744       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
745       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
746    fi
747 fi
748 # }}}
749
750 # partition handling {{{
751 PARTITION=''
752 DIRECTORY=''
753
754 set_target_directory(){
755     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
756     DIRECTORY=1
757     MNTPOINT="$TARGET"
758     MKFS=''
759     TUNE2FS=''
760     FSCK=''
761     GRUB=''
762     # make sure we normalise the path to an absolute directory name so something like:
763     #  mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
764     # works
765     TARGET="$(readlink -f $TARGET)"
766 }
767
768 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
769     PARTITION=1
770 else
771     set_target_directory
772 fi
773 # }}}
774
775 # architecture setup {{{
776 if [ -n "$ARCH" ] ; then
777    ARCHCMD="--arch $ARCH"
778    ARCHINFO=" (${ARCH})"
779 else
780    ARCH="$(dpkg --print-architecture)"
781    ARCHCMD="--arch $ARCH"
782    ARCHINFO=" (${ARCH})"
783 fi
784 # }}}
785
786 # keyring setupt {{{
787 KEYRING=""
788 if [ "$SECURE" = 'yes' ] ; then
789    if [ -e '/etc/apt/trusted.gpg' ] ; then
790       KEYRING="--keyring /etc/apt/trusted.gpg"
791    else
792       eerror "Could not find /etc/apt/trusted.gpg."
793    fi
794 else
795    ewarn "Not checking Release signatures!"
796 fi
797 # }}}
798
799 # make sure we have the right syntax when using an iso image {{{
800 if [ -n "$ISO" ] ; then
801    case $ISO in
802       file*) # do nothing
803       ;;
804       *)
805       ISO=file:$1
806       ;;
807    esac
808 fi
809 ISODIR=${ISO##file:}
810 ISODIR=${ISODIR%%/}
811 # }}}
812
813 # create filesystem {{{
814 mkfs() {
815   if [ -n "$DIRECTORY" ] ; then
816      einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
817   else
818     if grep -q "$TARGET" /proc/mounts ; then
819       eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
820       exit 1
821     fi
822
823     if [ -n "$MKFS" ] ; then
824        einfo "Running $MKFS on $TARGET"
825        $MKFS $TARGET ; RC=$?
826
827        # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
828        # will fail to detect the uuid in the chroot
829        if echo "$TARGET" | grep -q "/dev/md" ; then
830          blockdev --rereadpt "${TARGET}"
831        elif ! [ -n "$VIRTUAL" ] ; then
832          blockdev --rereadpt "${TARGET%%[0-9]*}"
833        fi
834        # give the system 2 seconds, otherwise we might run into
835        # race conditions :-/
836        sleep 2
837
838        eval $(blkid -o udev $TARGET 2>/dev/null)
839        [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
840
841        eend $RC
842     fi
843
844   fi
845 }
846 # }}}
847
848 # modify filesystem settings {{{
849 tunefs() {
850   if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
851      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
852      $TUNE2FS $TARGET
853      eend $?
854   fi
855 }
856 # }}}
857
858 # mount the new partition or if it's a directory do nothing at all {{{
859 mount_target() {
860   if [ -n "$DIRECTORY" ] ; then
861      einfo "Running grml-debootstrap on a directory, nothing to mount."
862   else
863      if grep -q $TARGET /proc/mounts ; then
864         ewarn "$TARGET already mounted, continuing anyway." ; eend 0
865      else
866        if ! [ -d "${MNTPOINT}" ] ; then
867           [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
868        fi
869        einfo "Mounting $TARGET to $MNTPOINT"
870        mkdir -p "$MNTPOINT"
871        mount -o rw,suid,dev $TARGET $MNTPOINT
872        eend $?
873      fi
874   fi
875   if [ -n "$ISODIR" ] ; then
876      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
877      mkdir -p "$MNTPOINT/$ISODIR"
878      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
879      eend $?
880   fi
881 }
882 # }}}
883
884 # prepare VM image for usage with debootstrap {{{
885 prepare_vm() {
886   if [ -z "$VIRTUAL" ] ; then
887      return 0 # be quite by intention
888   fi
889
890   if [ -b "$TARGET" ] ; then
891      eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
892      eend 1
893      exit 1
894   fi
895
896   ORIG_TARGET="$TARGET" # store for later reuse
897
898   qemu-img create -f raw "${TARGET}" "${VMSIZE}"
899   echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
900   dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
901   parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
902
903   DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
904   if [ -z "${DEVINFO}" ] ; then
905     eerror "Error setting up loopback device." ; eend 1
906     exit 1
907   fi
908
909   # hopefully this always works as expected
910   LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
911   BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/')   # '253:0'
912   LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
913   export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
914
915   blockdev --rereadpt "${LOOP}"
916
917   if [ -z "$TARGET" ] ; then
918      eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
919      exit 1
920   fi
921 }
922 # }}}
923
924 # make VM image bootable and unmount it {{{
925 finalize_vm() {
926   if [ -z "${VIRTUAL}" ] ; then
927      return 0
928   fi
929
930   if ! mount "${TARGET}" "${MNTPOINT}" ; then
931     eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
932     exit 1
933   fi
934
935   einfo "Installing Grub as bootloader."
936   mount -t proc none "${MNTPOINT}"/proc
937   mount -t sysfs none "${MNTPOINT}"/sys
938   mount --bind /dev "${MNTPOINT}"/dev
939
940   mkdir -p "${MNTPOINT}/boot/grub"
941   if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
942      eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
943      exit 1
944   fi
945
946   cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
947   chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
948   dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
949   rm -f "${MNTPOINT}/tmp/core.img"
950
951   einfo "Updating grub configuration file."
952   chroot "${MNTPOINT}" update-grub
953
954   umount "${MNTPOINT}"/proc
955   umount "${MNTPOINT}"/sys
956   umount "${MNTPOINT}"/dev
957
958   einfo "Adjusting grub.cfg for successful boot sequence."
959   # ugly but needed to boot grub acordingly
960   sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
961   sed -i "s; root=/dev/.*; root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
962
963   umount "${MNTPOINT}"
964   kpartx -d "${ORIG_TARGET}" >/dev/null
965 }
966 # }}}
967
968 # install main chroot {{{
969 debootstrap_system() {
970   if [ "$_opt_nodebootstrap" ]; then
971      einfo "Skipping debootstrap as requested."
972      return
973   fi
974
975   if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
976      einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
977      if [ -n "$MIRROR" ] ; then
978         einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
979         $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
980      else
981         einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
982         $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
983      fi
984      eend $?
985   else
986      eerror "Error: $MNTPOINT not mounted, can not continue."
987      eend 1
988   fi
989 }
990 # }}}
991
992 # prepare chroot via chroot-script {{{
993 preparechroot() {
994   einfo "Preparing chroot system"
995
996   # provide variables to chroot system
997   CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
998   touch $CHROOT_VARIABLES
999   chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
1000   echo "# Configuration of ${PN}"                              > $CHROOT_VARIABLES
1001   [ -n "$ARCH" ]          && echo "ARCH=$ARCH"                 >> $CHROOT_VARIABLES
1002   [ -n "$GRUB" ]          && echo "GRUB=$GRUB"                 >> $CHROOT_VARIABLES
1003   [ -n "$HOSTNAME" ]      && echo "HOSTNAME=$HOSTNAME"         >> $CHROOT_VARIABLES
1004   [ -n "$INSTALL_NOTES" ] && echo "INSTALL_NOTES=$INSTALL_NOTES" >> $CHROOT_VARIABLES
1005   [ -n "$ISODIR" ]        && echo "ISODIR=$ISO"                >> $CHROOT_VARIABLES
1006   [ -n "$ISO" ]           && echo "ISO=$ISO"                   >> $CHROOT_VARIABLES
1007   [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
1008   [ -n "$MIRROR" ]        && echo "MIRROR=$MIRROR"             >> $CHROOT_VARIABLES
1009   [ -n "$PACKAGES" ]      && echo "PACKAGES=$PACKAGES"         >> $CHROOT_VARIABLES
1010   [ -n "$RM_APTCACHE" ]   && echo "RM_APTCACHE=$RM_APTCACHE"   >> $CHROOT_VARIABLES
1011   [ -n "$ROOTPASSWORD" ]  && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
1012   [ -n "$NOPASSWORD" ]    && echo "NOPASSWORD=true"            >> $CHROOT_VARIABLES
1013   [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=$SELECTED_PARTITIONS" >> $CHROOT_VARIABLES
1014   [ -n "$TARGET" ]        && echo "TARGET=$TARGET"             >> $CHROOT_VARIABLES
1015   [ -n "$TARGET_UUID" ]   && echo "TARGET_UUID=$TARGET_UUID"   >> $CHROOT_VARIABLES
1016
1017   cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
1018   chmod 755 $MNTPOINT/bin/chroot-script
1019   [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1020
1021   # make sure we have our files for later use via chroot-script
1022   cp $VERBOSE $CONFFILES/config    $MNTPOINT/etc/debootstrap/
1023   # make sure we adjust the configuration variables accordingly:
1024   sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
1025   sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#"    $MNTPOINT/etc/debootstrap/config
1026   sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#"          $MNTPOINT/etc/debootstrap/config
1027
1028   # install notes:
1029   if [ -n "$INSTALL_NOTES" ] ; then
1030      [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
1031   fi
1032
1033   # package selection:
1034   cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
1035     $MNTPOINT/etc/debootstrap/packages
1036
1037   # debconf preseeding:
1038   _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1039   [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
1040     cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
1041
1042   # copy scripts that should be executed inside the chroot:
1043   _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1044   [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
1045     mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
1046     cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
1047   }
1048
1049   # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1050   cp $VERBOSE $CHROOT_VARIABLES  $MNTPOINT/etc/debootstrap/variables
1051
1052   cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
1053
1054   # make sure we can access network [relevant for cdebootstrap]
1055   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
1056
1057   # provide system's /etc/hosts to the target:
1058   if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1059      cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
1060   fi
1061
1062   # setup default locales
1063   [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen  $MNTPOINT/etc/locale.gen
1064
1065   # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1066   ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1067
1068   # copy any existing files to chroot
1069   [ -d $CONFFILES/bin   ] && cp $VERBOSE -a -L $CONFFILES/bin/*   $MNTPOINT/bin/
1070   [ -d $CONFFILES/boot  ] && cp $VERBOSE -a -L $CONFFILES/boot/*  $MNTPOINT/boot/
1071   [ -d $CONFFILES/etc   ] && cp $VERBOSE -a -L $CONFFILES/etc/*   $MNTPOINT/etc/
1072   [ -d $CONFFILES/sbin  ] && cp $VERBOSE -a -L $CONFFILES/sbin/*  $MNTPOINT/sbin/
1073   [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1074   [ -d $CONFFILES/usr   ] && cp $VERBOSE -a -L $CONFFILES/usr/*   $MNTPOINT/usr/
1075   [ -d $CONFFILES/var   ] && cp $VERBOSE -a -L $CONFFILES/var/*   $MNTPOINT/var/
1076
1077   # copy local network setup to chroot
1078   if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1079      [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1080      cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1081   fi
1082
1083   # install config file providing some example entries
1084   if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1085      cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1086   fi
1087
1088   eend 0
1089 }
1090 # }}}
1091
1092 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1093 execute_pre_scripts() {
1094    # make sure we have $MNTPOINT available for our scripts
1095    export MNTPOINT
1096    if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1097       [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1098       for script in ${pre_scripts}/* ; do
1099          if [ -x "$script" ] ; then
1100             einfo "Executing pre-script $script"
1101             $script ; eend $?
1102          fi
1103       done
1104    fi
1105 }
1106 # }}}
1107
1108 # execute all scripts in /etc/debootstrap/scripts/ {{{
1109 execute_scripts() {
1110    # make sure we have $MNTPOINT available for our scripts
1111    export MNTPOINT
1112    if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1113       [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1114       for script in ${scripts}/* ; do
1115          if [ -x "$script" ] ; then
1116             einfo "Executing script $script"
1117             $script ; eend $?
1118          fi
1119       done
1120    fi
1121 }
1122 # }}}
1123
1124 # execute chroot-script {{{
1125 chrootscript() {
1126   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1127      mount_target
1128   fi
1129
1130   if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1131      einfo "Executing chroot-script now"
1132      mount --bind /dev "$MNTPOINT"/dev
1133      chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1134      umount "$MNTPOINT"/dev
1135      eend $RC
1136   else
1137      eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1138      eend 1
1139   fi
1140 }
1141 # }}}
1142
1143 # unmount $MNTPOINT {{{
1144 umount_chroot() {
1145
1146   # display installation notes:
1147   if [ -n "$INSTALL_NOTES" ] ; then
1148      [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1149   fi
1150
1151   if [ -n "$ISODIR" ] ; then
1152      if grep -q "$ISODIR" /proc/mounts ; then
1153         einfo "Unmount $MNTPOINT/$ISODIR"
1154         umount "$MNTPOINT/$ISODIR"
1155         eend $?
1156      fi
1157   fi
1158
1159   if grep -q "$MNTPOINT" /proc/mounts ; then
1160      if [ -n "$PARTITION" ] ; then
1161         einfo "Unmount $MNTPOINT"
1162         umount $MNTPOINT
1163         eend $?
1164      fi
1165   fi
1166 }
1167 # }}}
1168
1169 # execute filesystem check {{{
1170 fscktool() {
1171  if [ -n "$VIRTUAL" ] ; then
1172    einfo "Skipping filesystem check since we deploy a virtual machine."
1173    return 0
1174  fi
1175
1176  if [ "$FSCK" = 'yes' ] ; then
1177    [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1178    einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1179    $FSCKTOOL $TARGET
1180    eend $?
1181  fi
1182 }
1183 # }}}
1184
1185 # now execute all the functions {{{
1186 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1187          preparechroot execute_pre_scripts chrootscript execute_scripts \
1188          umount_chroot finalize_vm fscktool ; do
1189     if stage "${i}" ; then
1190        $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
1191     fi
1192 done
1193 # }}}
1194
1195 # finalize {{{
1196 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
1197 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
1198
1199 # Remove temporary mountpoint again
1200 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
1201    einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
1202 fi
1203 # }}}
1204
1205 # end dialog of autoinstallation {{{
1206 if [ -n "$AUTOINSTALL" ] ; then
1207    if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1208 Automatically rebooting in 10 seconds.
1209
1210 Choose Cancel to skip rebooting." 10 60 10 ; then
1211      noeject noprompt reboot
1212   fi
1213 else
1214    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1215 fi
1216 # }}}
1217
1218 ## END OF FILE #################################################################
1219 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3