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