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