fix kernel image search on i386
[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 # http://www.debian.org/releases/stable/i386/index.html.en
9
10 # variables {{{
11 PN="$(basename $0)"
12 VERSION="$(dpkg --list $PN 2>/dev/null| awk '/^i/ {print $3}')"
13 VERSION="${VERSION:-unknown}"
14 MNTPOINT="/mnt/debootstrap.$$"
15
16 # defaults
17 CHROOT_SCRIPTS='yes'
18 CONFFILES='/etc/debootstrap'
19 DEBCONF='yes'
20 DEBIAN_FRONTEND='noninteractive'
21 DEBOOTSTRAP='debootstrap'
22 EXTRAPACKAGES='yes'
23 FALLBACK_MIRROR='http://http.debian.net/debian'
24 FORCE=''
25 HOSTNAME='grml'
26 INITRD='yes'
27 INSTALL_NOTES='/etc/debootstrap/install_notes'
28 LOCALES='yes'
29 MIRROR="$FALLBACK_MIRROR"
30 MKFS='mkfs.ext3'
31 PACKAGES='yes'
32 PRE_SCRIPTS='yes'
33 RECONFIGURE='console-data'
34 RELEASE='wheezy'
35 RM_APTCACHE='yes'
36 SCRIPTS='yes'
37 SECURE='yes'
38 TIMEZONE='Europe/Vienna'
39 TUNE2FS='tune2fs -c0 -i0'
40 UPGRADE_SYSTEM='yes'
41 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 instead of installing to
80                          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          # using sw-raid:
474          if [ -n "$SELECTED_PARTITIONS" ] ; then
475             GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
476          else
477             GRUB="$TARGET"
478          fi
479       else
480         GRUB="$MBRDISK"
481       fi
482       ;;
483     hda)
484       GRUB="/dev/hda"
485       ;;
486     sda)
487       GRUB="/dev/sda"
488       ;;
489     nowhere)
490       GRUB=''
491       ;;
492   esac
493 }
494 # }}}
495
496 # ask for Debian release {{{
497 prompt_for_release()
498 {
499   [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='wheezy'
500   RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
501             "Please enter the Debian release you would like to use for installation:" \
502             0 50 4 \
503             lenny    Debian/5.0 \
504             squeeze  Debian/6.0 \
505             wheezy   Debian/7.0 \
506             sid      Debian/unstable)"
507   [ $? -eq 0 ] || bailout
508 }
509 # }}}
510
511 # ask for hostname {{{
512 prompt_for_hostname()
513 {
514   HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
515             "Please enter the hostname you would like to use for installation:" \
516             0 0 $HOSTNAME)"
517   [ $? -eq 0 ] || bailout
518 }
519 # }}}
520
521 # ask for password {{{
522 prompt_for_password()
523 {
524   if [ "$_opt_nopassword" ] ; then
525     einfo "Skip asking for root password as requested."
526     return 0
527   fi
528
529   ROOTPW1='PW1'
530   ROOTPW2='PW2'
531   while [ "$ROOTPW1" != "$ROOTPW2" ]; do
532     ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
533     "Please enter the password for the root account:" 10 60)
534     [ $? -eq 0 ] || bailout
535     ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
536     "Please enter the password for the root account again for \
537     confirmation:" 10 60)
538     [ $? -eq 0 ] || bailout
539
540     if [ "$ROOTPW1" != "$ROOTPW2" ]; then
541       $(dialog --stdout --title "${PN}" --ok-label \
542       "Retry" --msgbox "Passwords do not match!" 10 60)
543     fi
544   done
545   ROOTPASSWORD="$ROOTPW1"
546 }
547 # }}}
548
549 # ask for Debian mirror {{{
550 prompt_for_mirror()
551 {
552   [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
553
554   CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
555           --menu "Where do you want to install from?" 0 0 0 \
556             net   "install via network (downloading from mirror)" \
557             local "install from local directory/mirror"
558           )
559   [ $? -eq 0 ] || bailout
560
561   if [ "$CHOOSE_MIRROR" = 'net' ] ; then
562      [ -n "$MIRROR" ] || MIRROR='http://http.debian.net/debian'
563      MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
564                "Please enter Debian mirror you would like to use for installing packages." \
565                0 0 $MIRROR)"
566      [ $? -eq 0 ] || bailout
567   else # CHOOSE_MIRROR == local
568      [ -n "$ISO" ] || ISO='/mnt/mirror'
569      ISO="$(dialog --stdout --title "${PN}" --inputbox \
570                "Please enter directory name you would like to use for installing packages." \
571                0 0 $ISO)"
572      [ $? -eq 0 ] || bailout
573   fi
574 }
575 # }}}
576
577 # software raid setup {{{
578 config_swraid_setup()
579 {
580 TMPFILE=$(mktemp)
581
582 # Currently we support only raid1:
583 RAIDLEVEL='raid1'
584
585 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
586 #                   --menu "Which RAID level do you want to use?" 0 0 0 \
587 #                     raid1 "Software RAID level 1" \
588 #                     raid5 "Software RAID level 5")
589 #[ $? -eq 0 ] || bailout 20
590
591 MD_LIST=$(for i in $(seq 0 9) ; do
592             awk '{print $4}' /proc/partitions | grep -q md$i || \
593             echo "/dev/md$i /dev/md$i"
594           done)
595
596 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
597 --menu "Which device do you want to use for ${RAIDLEVEL}?
598
599 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 \
600 $MD_LIST)
601 [ $? -eq 0 ] || bailout 20
602
603 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
604              sed 's/*//' | \
605              grep -v 'Extended$' | \
606              gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
607 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
608 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
609                      echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
610                  done)
611
612 dialog --title "$PN" --separate-output \
613        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
614        $PARTITION_LIST 2>$TMPFILE
615 [ $? -eq 0 ] || bailout
616 RETVAL=$?
617 SELECTED_PARTITIONS="$(cat $TMPFILE)"
618
619 NUM_PARTITIONS=0
620 for i in $(cat $TMPFILE) ; do
621    NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
622 done
623
624 # force metadata version 0.90 for lenny so old grub can boot from this array.
625 METADATA_VERSION=""
626 if [ $RELEASE = "lenny" ]; then
627    METADATA_VERSION="-e0"
628 fi
629
630 ERRORFILE=$(mktemp)
631 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
632       --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
633 RC=$?
634 if [ "$RC" = 0 ] ; then
635    dialog --title "$PN" --msgbox \
636    "Creating $TARGET was successful." 0 0
637    rm -f "$TMPFILE" "$ERRORFILE"
638 else
639    dialog --title "$PN" --msgbox \
640    "There was an error setting up $TARGET:
641
642 $(cat $ERRORFILE)
643
644 Exiting." 0 0
645    rm -f "$TMPFILE" "$ERRORFILE"
646    bailout 1
647 fi
648
649 }
650
651 prompt_for_swraid()
652 {
653 if dialog --stdout --title "$PN" \
654           --defaultno --yesno "Do you want to configure Software RAID?
655
656 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
657   config_swraid_setup
658 fi
659 }
660 # }}}
661
662 # user should recheck his configuration {{{
663 # support full automatic installation:
664 checkforrun() {
665    dialog --timeout 10 --title "$PN" \
666           --yesno "Do you want to stop at this stage?
667
668 Notice: you are running ${PN} in non-interactive mode.
669 ${PN} will install Debian ${RELEASE} on ${TARGET}.
670 Last chance to quit. Timeout of 10 seconds running....
671
672 Do you want to stop now?" 0 0 2>/dev/null
673 }
674 # }}}
675
676 # make sure the user is aware of the used configuration {{{
677 checkconfiguration()
678 {
679 if [ -n "$AUTOINSTALL" ] ; then
680    if checkforrun ; then
681       eerror "Exiting as requested" ; eend 0
682       bailout 1
683    fi
684 elif [ -n "$INTERACTIVE" ] ; then
685
686    INFOTEXT="Please recheck configuration before execution:
687    "
688    [ -n "$TARGET" ]  && INFOTEXT="$INFOTEXT
689    Target:          $TARGET"
690    [ -n "$GRUB" ]    && INFOTEXT="$INFOTEXT
691    Install grub:    $GRUB"
692    [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
693    Using release:   $RELEASE"
694    [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
695    Using hostname:  $HOSTNAME"
696    [ -n "$MIRROR" ]  && INFOTEXT="$INFOTEXT
697    Using mirror:    $MIRROR"
698    [ -n "$ISO" ]  && INFOTEXT="$INFOTEXT
699    Using ISO:       $ISO"
700    [ -n "$ARCH" ]  && INFOTEXT="$INFOTEXT
701    Using arch:      $ARCH"
702
703    INFOTEXT="$INFOTEXT
704
705 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
706
707    dialog --title "$PN" --no-collapse \
708           --yesno "$INFOTEXT" 0 0
709    [ $? -eq 0 ] || bailout 0
710
711 else # if not running automatic installation display configuration and prompt for execution:
712    einfo "$PN [${VERSION}] - Please recheck configuration before execution:"
713    echo
714    echo "   Target:          $TARGET"
715
716    # do not display if MNTPOINT is the default one
717    case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo "   Mount point:     $MNTPOINT" ;; esac
718
719    if [ -n "$VIRTUAL" ] ; then
720       echo "   Install grub:    yes"
721    else
722      [ -n "$GRUB" ]     && echo "   Install grub:    $GRUB" || echo "   Install grub:    no"
723    fi
724
725    [ -n "$RELEASE" ]  && echo "   Using release:   $RELEASE"
726    [ -n "$HOSTNAME" ] && echo "   Using hostname:  $HOSTNAME"
727    [ -n "$MIRROR" ]   && echo "   Using mirror:    $MIRROR"
728    [ -n "$ISO" ]      && echo "   Using ISO:       $ISO"
729    [ -n "$ARCH" ]     && echo "   Using arch:      $ARCH"
730    if [ -n "$VIRTUAL" ] ; then
731       echo "   Deploying as Virtual Machine."
732       [ -n "$VMSIZE" ] && echo "   Using Virtual Disk file with size of ${VMSIZE}."
733    fi
734
735    if [ ! -t 0 -a -z "$ROOTPASSWORD" -a -z "$NOPASSWORD" ] ; then
736       echo
737       echo "   You do not have a TTY allocated, your password will be shown in"
738       echo "   plaintext on the terminal! If you are using SSH, try its -t option!"
739    fi
740
741    echo
742    echo "   Important! Continuing will delete all data from ${TARGET}!"
743
744    if [ -n "$FORCE" ] ; then
745      einfo "Skip user acknowledgement as requested via --force option."
746    else
747      echo
748      einfon "Is this ok for you? [y/N] "
749      read a
750      if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
751         eerror "Exiting as requested." ; eend 1
752         bailout 1
753      fi
754    fi
755 fi
756 }
757 # }}}
758
759 # interactive mode {{{
760 interactive_mode()
761 {
762   check4progs dialog || bailout 1
763
764   welcome_dialog
765
766   prompt_for_release
767
768   prompt_for_swraid
769
770   prompt_for_target
771
772   prompt_for_bootmanager
773
774   prompt_for_hostname
775
776   prompt_for_password
777
778   prompt_for_mirror
779 }
780
781 # run interactive mode if we didn't get the according configuration yet
782 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
783    # only target might be unset, so make sure the INTERACTIVE flag is set as well
784    INTERACTIVE=1
785    interactive_mode
786 fi
787 # }}}
788
789 # architecture setup {{{
790 if [ -n "$ARCH" ] ; then
791    ARCHCMD="--arch $ARCH"
792    ARCHINFO=" (${ARCH})"
793 else
794    ARCH="$(dpkg --print-architecture)"
795    ARCHCMD="--arch $ARCH"
796    ARCHINFO=" (${ARCH})"
797 fi
798 # }}}
799
800 checkconfiguration
801
802 # finally make sure at least $TARGET is set [the partition for the new system] {{{
803 if [ -n "$TARGET" ] ; then
804    SHORT_TARGET="${TARGET##*/}"
805 else
806    eerror "Please adjust $CONFFILES/config or..."
807    eerror "... use the interactive version for configuration before running ${0}" ; eend 1
808    bailout 1
809 fi
810 # }}}
811
812 # stages setup {{{
813 if [ -z "$STAGES" ] ; then
814    STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
815    [ -d "$STAGES" ] || mkdir -p "$STAGES"
816 fi
817
818 if [ -r "$STAGES"/grml-debootstrap ] ; then
819    if grep -q done $STAGES/grml-debootstrap ; then
820       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
821       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
822    fi
823 fi
824 # }}}
825
826 # partition handling {{{
827 PARTITION=''
828 DIRECTORY=''
829
830 set_target_directory(){
831     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
832     DIRECTORY=1
833     MNTPOINT="$TARGET"
834     MKFS=''
835     TUNE2FS=''
836     FSCK=''
837     GRUB=''
838     # make sure we normalise the path to an absolute directory name so something like:
839     #  mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
840     # works
841     TARGET="$(readlink -f $TARGET)"
842 }
843
844 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
845     PARTITION=1
846 else
847     set_target_directory
848 fi
849 # }}}
850
851 # make sure we have the right syntax when using an iso image {{{
852 if [ -n "$ISO" ] ; then
853    case $ISO in
854       file*) # do nothing
855       ;;
856       *)
857       ISO=file:$ISO
858       ;;
859    esac
860 fi
861 ISODIR=${ISO##file:}
862 ISODIR=${ISODIR%%/}
863 # }}}
864
865 # Debian ISOs do not contain signed Release files {{{
866 if [ -n "$ISO" ] ; then
867     DEBOOTSTRAP_OPT="$DEBOOTSTRAP_OPT --no-check-gpg"
868 fi
869 # }}}
870
871 # create filesystem {{{
872 mkfs() {
873   if [ -n "$DIRECTORY" ] ; then
874      einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
875   else
876     if grep -q "$TARGET" /proc/mounts ; then
877       eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
878       bailout 1
879     fi
880
881     if [ -n "$MKFS" ] ; then
882        einfo "Running $MKFS on $TARGET"
883        $MKFS $TARGET ; RC=$?
884
885        # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
886        # will fail to detect the uuid in the chroot
887        if echo "$TARGET" | grep -q "/dev/md" ; then
888          blockdev --rereadpt "${TARGET}"
889        elif ! [ -n "$VIRTUAL" ] ; then
890          blockdev --rereadpt "${TARGET%%[0-9]*}"
891        fi
892        # give the system 2 seconds, otherwise we might run into
893        # race conditions :-/
894        sleep 2
895
896        eval $(blkid -o udev $TARGET 2>/dev/null)
897        [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
898
899        eend $RC
900     fi
901
902   fi
903 }
904 # }}}
905
906 # modify filesystem settings {{{
907 tunefs() {
908   if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
909      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
910      $TUNE2FS $TARGET
911      eend $?
912   fi
913 }
914 # }}}
915
916 # mount the new partition or if it's a directory do nothing at all {{{
917 mount_target() {
918   if [ -n "$DIRECTORY" ] ; then
919      einfo "Running grml-debootstrap on a directory, nothing to mount."
920   else
921      if grep -q $TARGET /proc/mounts ; then
922         ewarn "$TARGET already mounted, continuing anyway." ; eend 0
923      else
924        if ! [ -d "${MNTPOINT}" ] ; then
925           [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
926        fi
927        einfo "Mounting $TARGET to $MNTPOINT"
928        mkdir -p "$MNTPOINT"
929        mount -o rw,suid,dev $TARGET $MNTPOINT
930        eend $?
931      fi
932   fi
933   if [ -n "$ISODIR" ] ; then
934      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
935      mkdir -p "$MNTPOINT/$ISODIR"
936      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
937      eend $?
938   fi
939 }
940 # }}}
941
942 # prepare VM image for usage with debootstrap {{{
943 prepare_vm() {
944   if [ -z "$VIRTUAL" ] ; then
945      return 0 # be quite by intention
946   fi
947
948   if [ -b "$TARGET" ] ; then
949      eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
950      eend 1
951      bailout 1
952   fi
953
954   ORIG_TARGET="$TARGET" # store for later reuse
955
956   qemu-img create -f raw "${TARGET}" "${VMSIZE}"
957   echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
958   dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
959   parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
960
961   # if dm-mod isn't available then kpartx will fail with
962   # "Is device-mapper driver missing from kernel? [...]"
963   if ! kpartx -av $TARGET >/dev/null 2>&1 || ! grep -q device-mapper /proc/misc >/dev/null 2>&1 ; then
964     einfo "Device-mapper not ready yet, trying to load dm-mod module."
965     modprobe dm-mod ; eend $?
966   fi
967
968   # make sure loop module is present
969   if ! losetup -f >/dev/null 2>&1; then
970     einfo "Can not find a usable loop device, retrying after loading loop module."
971     modprobe loop
972     if losetup -f >/dev/null 2>&1; then
973       einfo "Found a usable loop device now, continuing."
974     else
975       eerror "Error finding usable loop device" ; eend 1
976       bailout 1
977     fi
978   fi
979
980   DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
981   if [ -z "${DEVINFO}" ] ; then
982     eerror "Error setting up loopback device." ; eend 1
983     bailout 1
984   fi
985
986   # hopefully this always works as expected
987   LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
988   BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/')   # '253:0'
989   LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
990   export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
991
992   blockdev --rereadpt "${LOOP}"
993
994   if [ -z "$TARGET" ] ; then
995      eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
996      bailout 1
997   fi
998 }
999 # }}}
1000
1001 # make VM image bootable and unmount it {{{
1002 finalize_vm() {
1003   if [ -z "${VIRTUAL}" ] ; then
1004      return 0
1005   fi
1006
1007   if ! mount "${TARGET}" "${MNTPOINT}" ; then
1008     eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
1009     bailout 1
1010   fi
1011
1012   einfo "Installing Grub as bootloader."
1013   mount -t proc none "${MNTPOINT}"/proc
1014   mount -t sysfs none "${MNTPOINT}"/sys
1015   mount --bind /dev "${MNTPOINT}"/dev
1016
1017   mkdir -p "${MNTPOINT}/boot/grub"
1018   if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
1019      eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
1020      bailout 1
1021   fi
1022
1023   cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
1024   chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
1025   dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
1026   rm -f "${MNTPOINT}/tmp/core.img"
1027
1028   einfo "Updating grub configuration file."
1029   if [ -n "$BOOT_APPEND" ] ; then
1030      sed -i "/GRUB_CMDLINE_LINUX_DEFAULT/ s#\"\$# ${BOOT_APPEND}\"#" "${MNTPOINT}"/etc/default/grub
1031   fi
1032   chroot "${MNTPOINT}" update-grub
1033
1034   umount "${MNTPOINT}"/proc
1035   umount "${MNTPOINT}"/sys
1036   umount "${MNTPOINT}"/dev
1037
1038   einfo "Adjusting grub.cfg for successful boot sequence."
1039   # ugly but needed to boot grub acordingly
1040   sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
1041   sed -i "s;root=[^ ]\+;root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
1042
1043   umount "${MNTPOINT}"
1044   kpartx -d "${ORIG_TARGET}" >/dev/null
1045 }
1046 # }}}
1047
1048 # install main chroot {{{
1049 debootstrap_system() {
1050   if [ "$_opt_nodebootstrap" ]; then
1051      einfo "Skipping debootstrap as requested."
1052      return
1053   fi
1054
1055   if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
1056     :
1057   else
1058     eerror "Error: $MNTPOINT not mounted, can not continue."
1059     eend 1 ; exit 1
1060   fi
1061
1062   if [ -n "$ISO" ] ; then
1063     einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${ISO}"
1064     einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
1065     $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
1066     RC=$?
1067   else
1068     einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}"
1069     einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
1070     $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
1071     RC=$?
1072   fi
1073
1074   if [ $RC -ne 0 ] ; then
1075     if [ -r "$MNTPOINT/debootstrap/debootstrap.log" ] && \
1076       [ -s "$MNTPOINT/debootstrap/debootstrap.log" ] ; then
1077       einfo "Presenting last ten lines of debootstrap.log:"
1078       tail -10 $MNTPOINT/debootstrap/debootstrap.log
1079       einfo "End of debootstrap.log"
1080     fi
1081   fi
1082
1083   eend $RC
1084 }
1085 # }}}
1086
1087 # prepare chroot via chroot-script {{{
1088 preparechroot() {
1089   einfo "Preparing chroot system"
1090
1091   # provide variables to chroot system
1092   CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
1093   touch $CHROOT_VARIABLES
1094   chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
1095   echo "# Configuration of ${PN}"                              > $CHROOT_VARIABLES
1096   [ -n "$ARCH" ]                && echo "ARCH=\"$ARCH\""                               >> $CHROOT_VARIABLES
1097   [ -n "$BACKPORTREPOS" ]       && echo "BACKPORTREPOS=\"$BACKPORTREPOS\""             >> $CHROOT_VARIABLES
1098   [ -n "$CHROOT_SCRIPTS" ]      && echo "CHROOT_SCRIPTS=\"$CHROOT_SCRIPTS\""           >> $CHROOT_VARIABLES
1099   [ -n "$CONFFILES" ]           && echo "CONFFILES=\"$CONFFILES\""                     >> $CHROOT_VARIABLES
1100   [ -n "$DEBCONF" ]             && echo "DEBCONF=\"$DEBCONF\""                         >> $CHROOT_VARIABLES
1101   [ -n "$DEBIAN_FRONTEND" ]     && echo "DEBIAN_FRONTEND=\"$DEBIAN_FRONTEND\""         >> $CHROOT_VARIABLES
1102   [ -n "$DEBOOTSTRAP" ]         && echo "DEBOOTSTRAP=\"$DEBOOTSTRAP\""                 >> $CHROOT_VARIABLES
1103   [ -n "$EXTRAPACKAGES" ]       && echo "EXTRAPACKAGES=\"$EXTRAPACKAGES\""             >> $CHROOT_VARIABLES
1104   [ -n "$FALLBACK_MIRROR" ]     && echo "FALLBACK_MIRROR=\"$FALLBACK_MIRROR\""         >> $CHROOT_VARIABLES
1105   [ -n "$FORCE" ]               && echo "FORCE=\"$FORCE\""                             >> $CHROOT_VARIABLES
1106   [ -n "$GRMLREPOS" ]           && echo "GRMLREPOS=\"$GRMLREPOS\""                     >> $CHROOT_VARIABLES
1107   [ -n "$GRUB" ]                && echo "GRUB=\"$GRUB\""                               >> $CHROOT_VARIABLES
1108   [ -n "$HOSTNAME" ]            && echo "HOSTNAME=\"$HOSTNAME\""                       >> $CHROOT_VARIABLES
1109   [ -n "$INITRD" ]              && echo "INITRD=\"$INITRD\""                           >> $CHROOT_VARIABLES
1110   [ -n "$INSTALL_NOTES" ]       && echo "INSTALL_NOTES=\"$INSTALL_NOTES\""             >> $CHROOT_VARIABLES
1111   [ -n "$ISODIR" ]              && echo "ISODIR=\"$ISO\""                              >> $CHROOT_VARIABLES
1112   [ -n "$ISO" ]                 && echo "ISO=\"$ISO\""                                 >> $CHROOT_VARIABLES
1113   [ -n "$KEEP_SRC_LIST" ]       && echo "KEEP_SRC_LIST=\"$KEEP_SRC_LIST\""             >> $CHROOT_VARIABLES
1114   [ -n "$LOCALES" ]             && echo "LOCALES=\"$LOCALES\""                         >> $CHROOT_VARIABLES
1115   [ -n "$MIRROR" ]              && echo "MIRROR=\"$MIRROR\""                           >> $CHROOT_VARIABLES
1116   [ -n "$MKFS" ]                && echo "MKFS=\"$MKFS\""                               >> $CHROOT_VARIABLES
1117   [ -n "$NOPASSWORD" ]          && echo "NOPASSWORD=\"true\""                          >> $CHROOT_VARIABLES
1118   [ -n "$PACKAGES" ]            && echo "PACKAGES=\"$PACKAGES\""                       >> $CHROOT_VARIABLES
1119   [ -n "$PRE_SCRIPTS" ]         && echo "PRE_SCRIPTS=\"$PRE_SCRIPTS\""                 >> $CHROOT_VARIABLES
1120   [ -n "$RECONFIGURE" ]         && echo "RECONFIGURE=\"$RECONFIGURE\""                 >> $CHROOT_VARIABLES
1121   [ -n "$RELEASE" ]             && echo "RELEASE=\"$RELEASE\""                         >> $CHROOT_VARIABLES
1122   [ -n "$RM_APTCACHE" ]         && echo "RM_APTCACHE=\"$RM_APTCACHE\""                 >> $CHROOT_VARIABLES
1123   [ -n "$ROOTPASSWORD" ]        && echo "ROOTPASSWORD=\"$ROOTPASSWORD\""               >> $CHROOT_VARIABLES
1124   [ -n "$SCRIPTS" ]             && echo "SCRIPTS=\"$SCRIPTS\""                         >> $CHROOT_VARIABLES
1125   [ -n "$SECURE" ]              && echo "SECURE=\"$SECURE\""                           >> $CHROOT_VARIABLES
1126   [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=\"$SELECTED_PARTITIONS\"" >> $CHROOT_VARIABLES
1127   [ -n "$TARGET" ]              && echo "TARGET=\"$TARGET\""                           >> $CHROOT_VARIABLES
1128   [ -n "$UPGRADE_SYSTEM" ]      && echo "UPGRADE_SYSTEM=\"$UPGRADE_SYSTEM\""           >> $CHROOT_VARIABLES
1129   [ -n "$TARGET_UUID" ]         && echo "TARGET_UUID=\"$TARGET_UUID\""                 >> $CHROOT_VARIABLES
1130   [ -n "$TIMEZONE" ]            && echo "TIMEZONE=\"$TIMEZONE\""                       >> $CHROOT_VARIABLES
1131   [ -n "$TUNE2FS" ]             && echo "TUNE2FS=\"$TUNE2FS\""                         >> $CHROOT_VARIABLES
1132   [ -n "$VMSIZE" ]              && echo "VMSIZE=\"$VMSIZE\""                           >> $CHROOT_VARIABLES
1133
1134   cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
1135   chmod 755 $MNTPOINT/bin/chroot-script
1136   [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1137
1138   # make sure we have our files for later use via chroot-script
1139   cp $VERBOSE $CONFFILES/config    $MNTPOINT/etc/debootstrap/
1140   # make sure we adjust the configuration variables accordingly:
1141   sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
1142   sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#"    $MNTPOINT/etc/debootstrap/config
1143   sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#"          $MNTPOINT/etc/debootstrap/config
1144
1145   # install notes:
1146   if [ -n "$INSTALL_NOTES" ] ; then
1147      [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
1148   fi
1149
1150   # package selection:
1151   cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
1152     $MNTPOINT/etc/debootstrap/packages
1153
1154   # debconf preseeding:
1155   _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1156   [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
1157     cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
1158
1159   # copy scripts that should be executed inside the chroot:
1160   _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1161   [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
1162     mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
1163     cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
1164   }
1165
1166   # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1167   cp $VERBOSE $CHROOT_VARIABLES  $MNTPOINT/etc/debootstrap/variables
1168
1169   cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
1170
1171   # make sure we can access network [relevant for cdebootstrap]
1172   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
1173
1174   # provide system's /etc/hosts to the target:
1175   if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1176      cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
1177   fi
1178
1179   # setup default locales
1180   [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen  $MNTPOINT/etc/locale.gen
1181
1182   # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1183   ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1184
1185   # copy any existing files to chroot
1186   [ -d $CONFFILES/bin   ] && cp $VERBOSE -a -L $CONFFILES/bin/*   $MNTPOINT/bin/
1187   [ -d $CONFFILES/boot  ] && cp $VERBOSE -a -L $CONFFILES/boot/*  $MNTPOINT/boot/
1188   [ -d $CONFFILES/etc   ] && cp $VERBOSE -a -L $CONFFILES/etc/*   $MNTPOINT/etc/
1189   [ -d $CONFFILES/sbin  ] && cp $VERBOSE -a -L $CONFFILES/sbin/*  $MNTPOINT/sbin/
1190   [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1191   [ -d $CONFFILES/usr   ] && cp $VERBOSE -a -L $CONFFILES/usr/*   $MNTPOINT/usr/
1192   [ -d $CONFFILES/var   ] && cp $VERBOSE -a -L $CONFFILES/var/*   $MNTPOINT/var/
1193
1194   # copy local network setup to chroot
1195   if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1196      [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1197      cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1198   fi
1199
1200   # install config file providing some example entries
1201   if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1202      cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1203   fi
1204
1205   eend 0
1206 }
1207 # }}}
1208
1209 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1210 execute_pre_scripts() {
1211    # make sure we have $MNTPOINT available for our scripts
1212    export MNTPOINT
1213    if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1214       [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1215       for script in ${pre_scripts}/* ; do
1216          if [ -x "$script" ] ; then
1217             einfo "Executing pre-script $script"
1218             $script ; eend $?
1219          fi
1220       done
1221    fi
1222 }
1223 # }}}
1224
1225 # execute all scripts in /etc/debootstrap/scripts/ {{{
1226 execute_scripts() {
1227    # make sure we have $MNTPOINT available for our scripts
1228    export MNTPOINT
1229    if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1230       [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1231       for script in ${scripts}/* ; do
1232          if [ -x "$script" ] ; then
1233             einfo "Executing script $script"
1234             $script ; eend $?
1235          fi
1236       done
1237    fi
1238 }
1239 # }}}
1240
1241 # execute chroot-script {{{
1242 chrootscript() {
1243   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1244     mount_target
1245   fi
1246
1247   if ! [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1248     eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1249     eend 1
1250   else
1251     einfo "Executing chroot-script now"
1252     mount --bind /dev "$MNTPOINT"/dev
1253     chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1254     umount "$MNTPOINT"/dev
1255     eend $RC
1256   fi
1257
1258   # finally get rid of chroot-script again, there's no good reason to
1259   # keep it on the installed system
1260   if grep -q GRML_CHROOT_SCRIPT_MARKER "${MNTPOINT}/bin/chroot-script" ; then
1261     einfo "Removing chroot-script again"
1262     rm -f "${MNTPOINT}/bin/chroot-script"
1263     eend $?
1264   else
1265     einfo "Keeping chroot-script as string GRML_CHROOT_SCRIPT_MARKER could not be found"
1266     eend 0
1267   fi
1268 }
1269 # }}}
1270
1271 # unmount $MNTPOINT {{{
1272 umount_chroot() {
1273
1274   # display installation notes:
1275   if [ -n "$INSTALL_NOTES" ] ; then
1276      [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1277   fi
1278
1279   if [ -n "$ISODIR" ] ; then
1280      if grep -q "$ISODIR" /proc/mounts ; then
1281         einfo "Unmount $MNTPOINT/$ISODIR"
1282         umount "$MNTPOINT/$ISODIR"
1283         eend $?
1284      fi
1285   fi
1286
1287   if grep -q "$MNTPOINT" /proc/mounts ; then
1288      if [ -n "$PARTITION" ] ; then
1289         einfo "Unmount $MNTPOINT"
1290         umount $MNTPOINT
1291         eend $?
1292      fi
1293   fi
1294 }
1295 # }}}
1296
1297 # execute filesystem check {{{
1298 fscktool() {
1299  if [ -n "$VIRTUAL" ] ; then
1300    einfo "Skipping filesystem check because we deploy a virtual machine."
1301    return 0
1302  fi
1303
1304  if [ "$FSCK" = 'yes' ] ; then
1305    [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1306    einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1307    $FSCKTOOL $TARGET
1308    eend $?
1309  fi
1310 }
1311 # }}}
1312
1313 # now execute all the functions {{{
1314 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1315          preparechroot execute_pre_scripts chrootscript execute_scripts \
1316          umount_chroot finalize_vm fscktool ; do
1317     if stage "${i}" ; then
1318        $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "$i"
1319     fi
1320 done
1321
1322 cleanup
1323 # }}}
1324
1325 # end dialog of autoinstallation {{{
1326 if [ -n "$AUTOINSTALL" ] ; then
1327    if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1328 Automatically rebooting in 10 seconds.
1329
1330 Choose Cancel to skip rebooting." 10 60 10 ; then
1331      noeject noprompt reboot
1332   fi
1333 else
1334    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1335 fi
1336 # }}}
1337
1338 ## END OF FILE #################################################################
1339 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=2