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