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