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