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