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