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