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