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