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