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