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