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