Switch default filesystem from ext3 to ext4
[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
9 # variables {{{
10 PN="$(basename $0)"
11 VERSION="$(dpkg-query --show --showformat='${Version}' "$PN")"
12 VERSION="${VERSION:-unknown}"
13 MNTPOINT="/mnt/debootstrap.$$"
14
15 # defaults
16 [ -n "$CHROOT_SCRIPTS" ] || CHROOT_SCRIPTS='yes'
17 [ -n "$CONFFILES" ] || CONFFILES='/etc/debootstrap'
18 [ -n "$DEBCONF" ] || DEBCONF='yes'
19 [ -n "$DEBIAN_FRONTEND" ] || DEBIAN_FRONTEND='noninteractive'
20 [ -n "$DEBOOTSTRAP" ] || DEBOOTSTRAP='debootstrap'
21 [ -n "$DEFAULT_LOCALES" ] || DEFAULT_LOCALES='en_US.UTF-8'
22 [ -n "$EXTRAPACKAGES" ] || EXTRAPACKAGES='yes'
23 [ -n "$FALLBACK_MIRROR" ] || FALLBACK_MIRROR='http://http.debian.net/debian'
24 [ -n "$FORCE" ] || FORCE=''
25 [ -n "$HOSTNAME" ] || HOSTNAME='grml'
26 [ -n "$INITRD" ] || INITRD='yes'
27 [ -n "$INSTALL_NOTES" ] || INSTALL_NOTES='/etc/debootstrap/install_notes'
28 [ -n "$LOCALES" ] || LOCALES='yes'
29 [ -n "$MIRROR" ] || MIRROR="$FALLBACK_MIRROR"
30 [ -n "$MKFS" ] || MKFS='mkfs.ext4'
31 [ -n "$PACKAGES" ] || PACKAGES='yes'
32 [ -n "$PRE_SCRIPTS" ] || PRE_SCRIPTS='yes'
33 [ -n "$RECONFIGURE" ] || RECONFIGURE='console-data'
34 [ -n "$RELEASE" ] || RELEASE='wheezy'
35 [ -n "$RM_APTCACHE" ] || RM_APTCACHE='yes'
36 [ -n "$SCRIPTS" ] || SCRIPTS='yes'
37 [ -n "$SECURE" ] || SECURE='yes'
38 [ -n "$TIMEZONE" ] || TIMEZONE='Europe/Vienna'
39 [ -n "$TUNE2FS" ] || TUNE2FS='tune2fs -c0 -i0'
40 [ -n "$UPGRADE_SYSTEM" ] || UPGRADE_SYSTEM='yes'
41 [ -n "$VMSIZE" ] || VMSIZE="2G"
42 [ -n "$FIXED_DISK_IDENTIFIERS" ] || FIXED_DISK_IDENTIFIERS="no"
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 (raw format) instead of installing
81                          to 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         GRUB="$TARGET"
475       else
476         GRUB="$MBRDISK"
477       fi
478       ;;
479     hda)
480       GRUB="/dev/hda"
481       ;;
482     sda)
483       GRUB="/dev/sda"
484       ;;
485     nowhere)
486       GRUB=''
487       ;;
488   esac
489 }
490 # }}}
491
492 # ask for Debian release {{{
493 prompt_for_release()
494 {
495   [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='wheezy'
496   RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
497             "Please enter the Debian release you would like to use for installation:" \
498             0 50 4 \
499             lenny    Debian/5.0 \
500             squeeze  Debian/6.0 \
501             wheezy   Debian/7.0 \
502             sid      Debian/unstable)"
503   [ $? -eq 0 ] || bailout
504 }
505 # }}}
506
507 # ask for hostname {{{
508 prompt_for_hostname()
509 {
510   HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
511             "Please enter the hostname you would like to use for installation:" \
512             0 0 $HOSTNAME)"
513   [ $? -eq 0 ] || bailout
514 }
515 # }}}
516
517 # ask for password {{{
518 prompt_for_password()
519 {
520   if [ "$_opt_nopassword" ] ; then
521     einfo "Skip asking for root password as requested."
522     return 0
523   fi
524
525   ROOTPW1='PW1'
526   ROOTPW2='PW2'
527   while [ "$ROOTPW1" != "$ROOTPW2" ]; do
528     ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
529     "Please enter the password for the root account:" 10 60)
530     [ $? -eq 0 ] || bailout
531     ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
532     "Please enter the password for the root account again for \
533     confirmation:" 10 60)
534     [ $? -eq 0 ] || bailout
535
536     if [ "$ROOTPW1" != "$ROOTPW2" ]; then
537       $(dialog --stdout --title "${PN}" --ok-label \
538       "Retry" --msgbox "Passwords do not match!" 10 60)
539     fi
540   done
541   ROOTPASSWORD="$ROOTPW1"
542 }
543 # }}}
544
545 # ask for Debian mirror {{{
546 prompt_for_mirror()
547 {
548   [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
549
550   CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
551           --menu "Where do you want to install from?" 0 0 0 \
552             net   "install via network (downloading from mirror)" \
553             local "install from local directory/mirror"
554           )
555   [ $? -eq 0 ] || bailout
556
557   if [ "$CHOOSE_MIRROR" = 'net' ] ; then
558      [ -n "$MIRROR" ] || MIRROR='http://http.debian.net/debian'
559      MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
560                "Please enter Debian mirror you would like to use for installing packages." \
561                0 0 $MIRROR)"
562      [ $? -eq 0 ] || bailout
563   else # CHOOSE_MIRROR == local
564      [ -n "$ISO" ] || ISO='/mnt/mirror'
565      ISO="$(dialog --stdout --title "${PN}" --inputbox \
566                "Please enter directory name you would like to use for installing packages." \
567                0 0 $ISO)"
568      [ $? -eq 0 ] || bailout
569   fi
570 }
571 # }}}
572
573 # software raid setup {{{
574 config_swraid_setup()
575 {
576 TMPFILE=$(mktemp)
577
578 # Currently we support only raid1:
579 RAIDLEVEL='raid1'
580
581 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
582 #                   --menu "Which RAID level do you want to use?" 0 0 0 \
583 #                     raid1 "Software RAID level 1" \
584 #                     raid5 "Software RAID level 5")
585 #[ $? -eq 0 ] || bailout 20
586
587 MD_LIST=$(for i in $(seq 0 9) ; do
588             awk '{print $4}' /proc/partitions | grep -q md$i || \
589             echo "/dev/md$i /dev/md$i"
590           done)
591
592 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
593 --menu "Which device do you want to use for ${RAIDLEVEL}?
594
595 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 \
596 $MD_LIST)
597 [ $? -eq 0 ] || bailout 20
598
599 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
600              sed 's/*//' | \
601              grep -v 'Extended$' | \
602              gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
603 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
604 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
605                      echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
606                  done)
607
608 dialog --title "$PN" --separate-output \
609        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
610        $PARTITION_LIST 2>$TMPFILE
611 [ $? -eq 0 ] || bailout
612 RETVAL=$?
613 SELECTED_PARTITIONS="$(cat $TMPFILE)"
614
615 NUM_PARTITIONS=0
616 for i in $(cat $TMPFILE) ; do
617    NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
618 done
619
620 # force metadata version 0.90 for lenny so old grub can boot from this array.
621 METADATA_VERSION=""
622 if [ $RELEASE = "lenny" ]; then
623    METADATA_VERSION="-e0"
624 fi
625
626 ERRORFILE=$(mktemp)
627 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
628       --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
629 RC=$?
630 if [ "$RC" = 0 ] ; then
631    dialog --title "$PN" --msgbox \
632    "Creating $TARGET was successful." 0 0
633    rm -f "$TMPFILE" "$ERRORFILE"
634 else
635    dialog --title "$PN" --msgbox \
636    "There was an error setting up $TARGET:
637
638 $(cat $ERRORFILE)
639
640 Exiting." 0 0
641    rm -f "$TMPFILE" "$ERRORFILE"
642    bailout 1
643 fi
644
645 }
646
647 prompt_for_swraid()
648 {
649 if dialog --stdout --title "$PN" \
650           --defaultno --yesno "Do you want to configure Software RAID?
651
652 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
653   config_swraid_setup
654 fi
655 }
656 # }}}
657
658 # user should recheck his configuration {{{
659 # support full automatic installation:
660 checkforrun() {
661    dialog --timeout 10 --title "$PN" \
662           --yesno "Do you want to stop at this stage?
663
664 Notice: you are running ${PN} in non-interactive mode.
665 ${PN} will install Debian ${RELEASE} on ${TARGET}.
666 Last chance to quit. Timeout of 10 seconds running....
667
668 Do you want to stop now?" 0 0 2>/dev/null
669 }
670 # }}}
671
672 # make sure the user is aware of the used configuration {{{
673 checkconfiguration()
674 {
675 if [ -n "$AUTOINSTALL" ] ; then
676    if checkforrun ; then
677       eerror "Exiting as requested" ; eend 0
678       bailout 1
679    fi
680 elif [ -n "$INTERACTIVE" ] ; then
681
682    INFOTEXT="Please recheck configuration before execution:
683    "
684    [ -n "$TARGET" ]  && INFOTEXT="$INFOTEXT
685    Target:          $TARGET"
686    [ -n "$GRUB" ]    && INFOTEXT="$INFOTEXT
687    Install grub:    $GRUB"
688    [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
689    Using release:   $RELEASE"
690    [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
691    Using hostname:  $HOSTNAME"
692    [ -n "$MIRROR" ]  && INFOTEXT="$INFOTEXT
693    Using mirror:    $MIRROR"
694    [ -n "$ISO" ]  && INFOTEXT="$INFOTEXT
695    Using ISO:       $ISO"
696    [ -n "$ARCH" ]  && INFOTEXT="$INFOTEXT
697    Using arch:      $ARCH"
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 [${VERSION}] - 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 "$HOSTNAME" ] && echo "   Using hostname:  $HOSTNAME"
723    [ -n "$MIRROR" ]   && echo "   Using mirror:    $MIRROR"
724    [ -n "$ISO" ]      && echo "   Using ISO:       $ISO"
725    [ -n "$ARCH" ]     && echo "   Using arch:      $ARCH"
726    if [ -n "$VIRTUAL" ] ; then
727       echo "   Deploying as Virtual Machine."
728       [ -n "$VMSIZE" ] && echo "   Using Virtual Disk file with size of ${VMSIZE}."
729    fi
730
731    if [ ! -t 0 -a -z "$ROOTPASSWORD" -a -z "$NOPASSWORD" ] ; then
732       echo
733       echo "   You do not have a TTY allocated, your password will be shown in"
734       echo "   plaintext on the terminal! If you are using SSH, try its -t option!"
735    fi
736
737    echo
738    echo "   Important! Continuing will delete all data from ${TARGET}!"
739
740    if [ -n "$FORCE" ] ; then
741      einfo "Skip user acknowledgement as requested via --force option."
742    else
743      echo
744      einfon "Is this ok for you? [y/N] "
745      read a
746      if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
747         eerror "Exiting as requested." ; eend 1
748         bailout 1
749      fi
750    fi
751 fi
752 }
753 # }}}
754
755 # interactive mode {{{
756 interactive_mode()
757 {
758   check4progs dialog || bailout 1
759
760   welcome_dialog
761
762   prompt_for_release
763
764   prompt_for_swraid
765
766   prompt_for_target
767
768   prompt_for_bootmanager
769
770   prompt_for_hostname
771
772   prompt_for_password
773
774   prompt_for_mirror
775 }
776
777 # run interactive mode if we didn't get the according configuration yet
778 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
779    # only target might be unset, so make sure the INTERACTIVE flag is set as well
780    INTERACTIVE=1
781    interactive_mode
782 fi
783 # }}}
784
785 # architecture setup {{{
786 if [ -n "$ARCH" ] ; then
787    ARCHCMD="--arch $ARCH"
788    ARCHINFO=" (${ARCH})"
789 else
790    ARCH="$(dpkg --print-architecture)"
791    ARCHCMD="--arch $ARCH"
792    ARCHINFO=" (${ARCH})"
793 fi
794 # }}}
795
796 # It is not possible to build amd64 on i686. {{{
797 CURRENT_ARCH="$(uname -m)"
798 if [ "$CURRENT_ARCH" != "x86_64" ] ; then
799    if [ "$ARCH" = "amd64" ] ; then
800       eerror "It is not possible to build amd64 on $CURRENT_ARCH." ; eend 1
801       bailout 1
802    fi
803 fi
804 # }}}
805
806 checkconfiguration
807
808 # finally make sure at least $TARGET is set [the partition for the new system] {{{
809 if [ -n "$TARGET" ] ; then
810    SHORT_TARGET="${TARGET##*/}"
811 else
812    eerror "Please adjust $CONFFILES/config or..."
813    eerror "... use the interactive version for configuration before running ${0}" ; eend 1
814    bailout 1
815 fi
816 # }}}
817
818 # stages setup {{{
819 if [ -z "$STAGES" ] ; then
820    STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
821    [ -d "$STAGES" ] || mkdir -p "$STAGES"
822 fi
823
824 if [ -r "$STAGES"/grml-debootstrap ] ; then
825    if grep -q done $STAGES/grml-debootstrap ; then
826       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
827       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
828    fi
829 fi
830 # }}}
831
832 # partition handling {{{
833 PARTITION=''
834 DIRECTORY=''
835
836 set_target_directory(){
837     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
838     DIRECTORY=1
839     MNTPOINT="$TARGET"
840     MKFS=''
841     TUNE2FS=''
842     FSCK=''
843     GRUB=''
844     # make sure we normalise the path to an absolute directory name so something like:
845     #  mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
846     # works
847     TARGET="$(readlink -f $TARGET)"
848 }
849
850 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
851     PARTITION=1
852 else
853     set_target_directory
854 fi
855 # }}}
856
857 # make sure we have the right syntax when using an iso image {{{
858 if [ -n "$ISO" ] ; then
859    case $ISO in
860       file*) # do nothing
861       ;;
862       *)
863       ISO=file:$ISO
864       ;;
865    esac
866 fi
867 ISODIR=${ISO##file:}
868 ISODIR=${ISODIR%%/}
869 # }}}
870
871 # Debian ISOs do not contain signed Release files {{{
872 if [ -n "$ISO" ] ; then
873     DEBOOTSTRAP_OPT="$DEBOOTSTRAP_OPT --no-check-gpg"
874 fi
875 # }}}
876
877 # create filesystem {{{
878 mkfs() {
879   if [ -n "$DIRECTORY" ] ; then
880      einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
881   else
882     if grep -q "$TARGET" /proc/mounts ; then
883       eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
884       bailout 1
885     fi
886
887     if [ -n "$MKFS" ] ; then
888        einfo "Running $MKFS on $TARGET"
889        $MKFS $TARGET ; RC=$?
890
891        if [ "$FIXED_DISK_IDENTIFIERS" = "yes" ] ; then
892          if ! echo "$MKFS" | grep -q "mkfs.ext" ; then
893            eerror "Not changing disk uuid for $TARGET because $MKFS doesn't seem to match for ext{2,3,4} file system"
894            eend 1
895            bailout 1
896          else
897            einfo "Changing disk uuid for $TARGET to fixed (non-random) value using tune2fs"
898            tune2fs "$TARGET" -U 26ada0c0-1165-4098-884d-aafd2220c2c6
899            eend $?
900          fi
901        fi
902
903        # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
904        # will fail to detect the uuid in the chroot
905        if echo "$TARGET" | grep -q "/dev/md" ; then
906          blockdev --rereadpt "${TARGET}"
907        elif ! [ -n "$VIRTUAL" ] ; then
908          blockdev --rereadpt "${TARGET%%[0-9]*}"
909        fi
910        # give the system 2 seconds, otherwise we might run into
911        # race conditions :-/
912        sleep 2
913
914        eval $(blkid -o udev $TARGET 2>/dev/null)
915        [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
916
917        eend $RC
918     fi
919
920   fi
921 }
922 # }}}
923
924 # modify filesystem settings {{{
925 tunefs() {
926   if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
927      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
928      $TUNE2FS $TARGET
929      eend $?
930   fi
931 }
932 # }}}
933
934 # mount the new partition or if it's a directory do nothing at all {{{
935 mount_target() {
936   if [ -n "$DIRECTORY" ] ; then
937      einfo "Running grml-debootstrap on a directory, nothing to mount."
938   else
939      if grep -q $TARGET /proc/mounts ; then
940         ewarn "$TARGET already mounted, continuing anyway." ; eend 0
941      else
942        if ! [ -d "${MNTPOINT}" ] ; then
943           [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
944        fi
945        einfo "Mounting $TARGET to $MNTPOINT"
946        mkdir -p "$MNTPOINT"
947        mount -o rw,suid,dev $TARGET $MNTPOINT
948        eend $?
949      fi
950   fi
951   if [ -n "$ISODIR" ] ; then
952      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
953      mkdir -p "$MNTPOINT/$ISODIR"
954      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
955      eend $?
956   fi
957 }
958 # }}}
959
960 # prepare VM image for usage with debootstrap {{{
961 prepare_vm() {
962   if [ -z "$VIRTUAL" ] ; then
963      return 0 # be quite by intention
964   fi
965
966   if [ -b "$TARGET" ] ; then
967      eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
968      eend 1
969      bailout 1
970   fi
971
972   ORIG_TARGET="$TARGET" # store for later reuse
973
974   qemu-img create -f raw "${TARGET}" "${VMSIZE}"
975   echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
976   dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
977   if [ "$FIXED_DISK_IDENTIFIERS" = "yes" ] ; then
978     einfo "Adjusting disk signature to a fixed (non-random) value"
979     MBRTMPFILE=$(mktemp)
980     dd if="${TARGET}" of="${MBRTMPFILE}" bs=512 count=1
981     echo -en "\x41\x41\x41\x41\x41" | dd of="${MBRTMPFILE}" conv=notrunc seek=440 bs=1
982     dd if="${MBRTMPFILE}" of="${TARGET}" conv=notrunc
983     eend $?
984   fi
985   parted -s "${TARGET}" 'mkpart primary ext4 2M -1'
986
987   # if dm-mod isn't available then kpartx will fail with
988   # "Is device-mapper driver missing from kernel? [...]"
989   if ! kpartx -av $TARGET >/dev/null 2>&1 || ! grep -q device-mapper /proc/misc >/dev/null 2>&1 ; then
990     einfo "Device-mapper not ready yet, trying to load dm-mod module."
991     modprobe dm-mod ; eend $?
992   fi
993
994   # make sure loop module is present
995   if ! losetup -f >/dev/null 2>&1; then
996     einfo "Can not find a usable loop device, retrying after loading loop module."
997     modprobe loop
998     if losetup -f >/dev/null 2>&1; then
999       einfo "Found a usable loop device now, continuing."
1000     else
1001       eerror "Error finding usable loop device" ; eend 1
1002       bailout 1
1003     fi
1004   fi
1005
1006   DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
1007   if [ -z "${DEVINFO}" ] ; then
1008     eerror "Error setting up loopback device." ; eend 1
1009     bailout 1
1010   fi
1011
1012   # hopefully this always works as expected
1013   LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
1014   BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/')   # '253:0'
1015   LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
1016   export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
1017
1018   blockdev --rereadpt "${LOOP}"
1019
1020   if [ -z "$TARGET" ] ; then
1021      eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
1022      bailout 1
1023   fi
1024 }
1025 # }}}
1026
1027 # make VM image bootable and unmount it {{{
1028 finalize_vm() {
1029   if [ -z "${VIRTUAL}" ] ; then
1030      return 0
1031   fi
1032
1033   if ! mount "${TARGET}" "${MNTPOINT}" ; then
1034     eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
1035     bailout 1
1036   fi
1037
1038   einfo "Installing Grub as bootloader."
1039   mount -t proc none "${MNTPOINT}"/proc
1040   mount -t sysfs none "${MNTPOINT}"/sys
1041   mount --bind /dev "${MNTPOINT}"/dev
1042
1043   mkdir -p "${MNTPOINT}/boot/grub"
1044   if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
1045      eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
1046      bailout 1
1047   fi
1048
1049   cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
1050   chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
1051   dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
1052   rm -f "${MNTPOINT}/tmp/core.img"
1053
1054   einfo "Updating grub configuration file."
1055   if [ -n "$BOOT_APPEND" ] ; then
1056      sed -i "/GRUB_CMDLINE_LINUX_DEFAULT/ s#\"\$# ${BOOT_APPEND}\"#" "${MNTPOINT}"/etc/default/grub
1057   fi
1058   chroot "${MNTPOINT}" update-grub
1059
1060   umount "${MNTPOINT}"/proc
1061   umount "${MNTPOINT}"/sys
1062   umount "${MNTPOINT}"/dev
1063
1064   einfo "Adjusting grub.cfg for successful boot sequence."
1065   # ugly but needed to boot grub acordingly
1066   sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
1067   sed -i "s;root=[^ ]\+;root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
1068
1069   umount "${MNTPOINT}"
1070   kpartx -d "${ORIG_TARGET}" >/dev/null
1071 }
1072 # }}}
1073
1074 # install main chroot {{{
1075 debootstrap_system() {
1076   if [ "$_opt_nodebootstrap" ]; then
1077      einfo "Skipping debootstrap as requested."
1078      return
1079   fi
1080
1081   if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
1082     :
1083   else
1084     eerror "Error: $MNTPOINT not mounted, can not continue."
1085     eend 1 ; exit 1
1086   fi
1087
1088   if [ -n "$ISO" ] ; then
1089     einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${ISO}"
1090     einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
1091     $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
1092     RC=$?
1093   else
1094     einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}"
1095     einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
1096     $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
1097     RC=$?
1098   fi
1099
1100   if [ $RC -ne 0 ] ; then
1101     if [ -r "$MNTPOINT/debootstrap/debootstrap.log" ] && \
1102       [ -s "$MNTPOINT/debootstrap/debootstrap.log" ] ; then
1103       einfo "Presenting last ten lines of debootstrap.log:"
1104       tail -10 $MNTPOINT/debootstrap/debootstrap.log
1105       einfo "End of debootstrap.log"
1106     fi
1107   fi
1108
1109   eend $RC
1110 }
1111 # }}}
1112
1113 # prepare chroot via chroot-script {{{
1114 preparechroot() {
1115   einfo "Preparing chroot system"
1116
1117   # provide variables to chroot system
1118   CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
1119   touch $CHROOT_VARIABLES
1120   chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
1121   echo "# Configuration of ${PN}"                              > $CHROOT_VARIABLES
1122   [ -n "$ARCH" ]                && echo "ARCH=\"$ARCH\""                               >> $CHROOT_VARIABLES
1123   [ -n "$BACKPORTREPOS" ]       && echo "BACKPORTREPOS=\"$BACKPORTREPOS\""             >> $CHROOT_VARIABLES
1124   [ -n "$CHROOT_SCRIPTS" ]      && echo "CHROOT_SCRIPTS=\"$CHROOT_SCRIPTS\""           >> $CHROOT_VARIABLES
1125   [ -n "$CONFFILES" ]           && echo "CONFFILES=\"$CONFFILES\""                     >> $CHROOT_VARIABLES
1126   [ -n "$DEBCONF" ]             && echo "DEBCONF=\"$DEBCONF\""                         >> $CHROOT_VARIABLES
1127   [ -n "$DEBIAN_FRONTEND" ]     && echo "DEBIAN_FRONTEND=\"$DEBIAN_FRONTEND\""         >> $CHROOT_VARIABLES
1128   [ -n "$DEBOOTSTRAP" ]         && echo "DEBOOTSTRAP=\"$DEBOOTSTRAP\""                 >> $CHROOT_VARIABLES
1129   [ -n "$DEFAULT_LOCALES" ]     && echo "DEFAULT_LOCALES=\"$DEFAULT_LOCALES\""         >> $CHROOT_VARIABLES
1130   [ -n "$EXTRAPACKAGES" ]       && echo "EXTRAPACKAGES=\"$EXTRAPACKAGES\""             >> $CHROOT_VARIABLES
1131   [ -n "$FALLBACK_MIRROR" ]     && echo "FALLBACK_MIRROR=\"$FALLBACK_MIRROR\""         >> $CHROOT_VARIABLES
1132   [ -n "$FORCE" ]               && echo "FORCE=\"$FORCE\""                             >> $CHROOT_VARIABLES
1133   [ -n "$GRMLREPOS" ]           && echo "GRMLREPOS=\"$GRMLREPOS\""                     >> $CHROOT_VARIABLES
1134   [ -n "$GRUB" ]                && echo "GRUB=\"$GRUB\""                               >> $CHROOT_VARIABLES
1135   [ -n "$HOSTNAME" ]            && echo "HOSTNAME=\"$HOSTNAME\""                       >> $CHROOT_VARIABLES
1136   [ -n "$INITRD" ]              && echo "INITRD=\"$INITRD\""                           >> $CHROOT_VARIABLES
1137   [ -n "$INSTALL_NOTES" ]       && echo "INSTALL_NOTES=\"$INSTALL_NOTES\""             >> $CHROOT_VARIABLES
1138   [ -n "$ISODIR" ]              && echo "ISODIR=\"$ISO\""                              >> $CHROOT_VARIABLES
1139   [ -n "$ISO" ]                 && echo "ISO=\"$ISO\""                                 >> $CHROOT_VARIABLES
1140   [ -n "$KEEP_SRC_LIST" ]       && echo "KEEP_SRC_LIST=\"$KEEP_SRC_LIST\""             >> $CHROOT_VARIABLES
1141   [ -n "$LOCALES" ]             && echo "LOCALES=\"$LOCALES\""                         >> $CHROOT_VARIABLES
1142   [ -n "$MIRROR" ]              && echo "MIRROR=\"$MIRROR\""                           >> $CHROOT_VARIABLES
1143   [ -n "$MKFS" ]                && echo "MKFS=\"$MKFS\""                               >> $CHROOT_VARIABLES
1144   [ -n "$NOPASSWORD" ]          && echo "NOPASSWORD=\"true\""                          >> $CHROOT_VARIABLES
1145   [ -n "$PACKAGES" ]            && echo "PACKAGES=\"$PACKAGES\""                       >> $CHROOT_VARIABLES
1146   [ -n "$PRE_SCRIPTS" ]         && echo "PRE_SCRIPTS=\"$PRE_SCRIPTS\""                 >> $CHROOT_VARIABLES
1147   [ -n "$RECONFIGURE" ]         && echo "RECONFIGURE=\"$RECONFIGURE\""                 >> $CHROOT_VARIABLES
1148   [ -n "$RELEASE" ]             && echo "RELEASE=\"$RELEASE\""                         >> $CHROOT_VARIABLES
1149   [ -n "$RM_APTCACHE" ]         && echo "RM_APTCACHE=\"$RM_APTCACHE\""                 >> $CHROOT_VARIABLES
1150   [ -n "$ROOTPASSWORD" ]        && echo "ROOTPASSWORD=\"$ROOTPASSWORD\""               >> $CHROOT_VARIABLES
1151   [ -n "$SCRIPTS" ]             && echo "SCRIPTS=\"$SCRIPTS\""                         >> $CHROOT_VARIABLES
1152   [ -n "$SECURE" ]              && echo "SECURE=\"$SECURE\""                           >> $CHROOT_VARIABLES
1153   [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=\"$SELECTED_PARTITIONS\"" >> $CHROOT_VARIABLES
1154   [ -n "$TARGET" ]              && echo "TARGET=\"$TARGET\""                           >> $CHROOT_VARIABLES
1155   [ -n "$UPGRADE_SYSTEM" ]      && echo "UPGRADE_SYSTEM=\"$UPGRADE_SYSTEM\""           >> $CHROOT_VARIABLES
1156   [ -n "$TARGET_UUID" ]         && echo "TARGET_UUID=\"$TARGET_UUID\""                 >> $CHROOT_VARIABLES
1157   [ -n "$TIMEZONE" ]            && echo "TIMEZONE=\"$TIMEZONE\""                       >> $CHROOT_VARIABLES
1158   [ -n "$TUNE2FS" ]             && echo "TUNE2FS=\"$TUNE2FS\""                         >> $CHROOT_VARIABLES
1159   [ -n "$VMSIZE" ]              && echo "VMSIZE=\"$VMSIZE\""                           >> $CHROOT_VARIABLES
1160
1161   cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
1162   chmod 755 $MNTPOINT/bin/chroot-script
1163   [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1164
1165   # make sure we have our files for later use via chroot-script
1166   cp $VERBOSE $CONFFILES/config    $MNTPOINT/etc/debootstrap/
1167   # make sure we adjust the configuration variables accordingly:
1168   sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
1169   sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#"    $MNTPOINT/etc/debootstrap/config
1170   sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#"          $MNTPOINT/etc/debootstrap/config
1171
1172   # install notes:
1173   if [ -n "$INSTALL_NOTES" ] ; then
1174      [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
1175   fi
1176
1177   # package selection:
1178   cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
1179     $MNTPOINT/etc/debootstrap/packages
1180
1181   # debconf preseeding:
1182   _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1183   [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
1184     cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
1185
1186   # copy scripts that should be executed inside the chroot:
1187   _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1188   [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
1189     mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
1190     cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
1191   }
1192
1193   # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1194   cp $VERBOSE $CHROOT_VARIABLES  $MNTPOINT/etc/debootstrap/variables
1195
1196   cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
1197
1198   # make sure we can access network [relevant for cdebootstrap]
1199   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
1200
1201   # provide system's /etc/hosts to the target:
1202   if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1203      cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
1204   fi
1205
1206   # setup default locales
1207   [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen  $MNTPOINT/etc/locale.gen
1208
1209   # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1210   ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1211
1212   # copy any existing files to chroot
1213   [ -d $CONFFILES/bin   ] && cp $VERBOSE -a -L $CONFFILES/bin/*   $MNTPOINT/bin/
1214   [ -d $CONFFILES/boot  ] && cp $VERBOSE -a -L $CONFFILES/boot/*  $MNTPOINT/boot/
1215   [ -d $CONFFILES/etc   ] && cp $VERBOSE -a -L $CONFFILES/etc/*   $MNTPOINT/etc/
1216   [ -d $CONFFILES/sbin  ] && cp $VERBOSE -a -L $CONFFILES/sbin/*  $MNTPOINT/sbin/
1217   [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1218   [ -d $CONFFILES/usr   ] && cp $VERBOSE -a -L $CONFFILES/usr/*   $MNTPOINT/usr/
1219   [ -d $CONFFILES/var   ] && cp $VERBOSE -a -L $CONFFILES/var/*   $MNTPOINT/var/
1220
1221   # copy local network setup to chroot
1222   if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1223      [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1224      cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1225   fi
1226
1227   # install config file providing some example entries
1228   if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1229      cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1230   fi
1231
1232   eend 0
1233 }
1234 # }}}
1235
1236 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1237 execute_pre_scripts() {
1238    # make sure we have $MNTPOINT available for our scripts
1239    export MNTPOINT
1240    if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1241       [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1242       for script in ${pre_scripts}/* ; do
1243          if [ -x "$script" ] ; then
1244             einfo "Executing pre-script $script"
1245             $script ; eend $?
1246          fi
1247       done
1248    fi
1249 }
1250 # }}}
1251
1252 # execute all scripts in /etc/debootstrap/scripts/ {{{
1253 execute_scripts() {
1254    # make sure we have $MNTPOINT available for our scripts
1255    export MNTPOINT
1256    if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1257       [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1258       for script in ${scripts}/* ; do
1259          if [ -x "$script" ] ; then
1260             einfo "Executing script $script"
1261             $script ; eend $?
1262          fi
1263       done
1264    fi
1265 }
1266 # }}}
1267
1268 # execute chroot-script {{{
1269 chrootscript() {
1270   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1271     mount_target
1272   fi
1273
1274   if ! [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1275     eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1276     eend 1
1277   else
1278     einfo "Executing chroot-script now"
1279     mount --bind /dev "$MNTPOINT"/dev
1280     chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1281     umount "$MNTPOINT"/dev
1282     eend $RC
1283   fi
1284
1285   # finally get rid of chroot-script again, there's no good reason to
1286   # keep it on the installed system
1287   if grep -q GRML_CHROOT_SCRIPT_MARKER "${MNTPOINT}/bin/chroot-script" ; then
1288     einfo "Removing chroot-script again"
1289     rm -f "${MNTPOINT}/bin/chroot-script"
1290     eend $?
1291   else
1292     einfo "Keeping chroot-script as string GRML_CHROOT_SCRIPT_MARKER could not be found"
1293     eend 0
1294   fi
1295 }
1296 # }}}
1297
1298 # unmount $MNTPOINT {{{
1299 umount_chroot() {
1300
1301   # display installation notes:
1302   if [ -n "$INSTALL_NOTES" ] ; then
1303      [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1304   fi
1305
1306   if [ -n "$ISODIR" ] ; then
1307      if grep -q "$ISODIR" /proc/mounts ; then
1308         einfo "Unmount $MNTPOINT/$ISODIR"
1309         umount "$MNTPOINT/$ISODIR"
1310         eend $?
1311      fi
1312   fi
1313
1314   if grep -q "$MNTPOINT" /proc/mounts ; then
1315      if [ -n "$PARTITION" ] ; then
1316         einfo "Unmount $MNTPOINT"
1317         umount $MNTPOINT
1318         eend $?
1319      fi
1320   fi
1321 }
1322 # }}}
1323
1324 # execute filesystem check {{{
1325 fscktool() {
1326  if [ -n "$VIRTUAL" ] ; then
1327    einfo "Skipping filesystem check because we deploy a virtual machine."
1328    return 0
1329  fi
1330
1331  if [ "$FSCK" = 'yes' ] ; then
1332    [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1333    einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1334    $FSCKTOOL $TARGET
1335    eend $?
1336  fi
1337 }
1338 # }}}
1339
1340 # now execute all the functions {{{
1341 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1342          preparechroot execute_pre_scripts chrootscript execute_scripts \
1343          umount_chroot finalize_vm fscktool ; do
1344     if stage "${i}" ; then
1345        $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "$i"
1346     fi
1347 done
1348
1349 cleanup
1350 # }}}
1351
1352 # end dialog of autoinstallation {{{
1353 if [ -n "$AUTOINSTALL" ] ; then
1354    if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1355 Automatically rebooting in 10 seconds.
1356
1357 Choose Cancel to skip rebooting." 10 60 10 ; then
1358      noeject noprompt reboot
1359   fi
1360 else
1361    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1362 fi
1363 # }}}
1364
1365 ## END OF FILE #################################################################
1366 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=2