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