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