Move grub_install from grml-debootstrap to chroot-script.
[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 set -e # exit on any error
11
12 # variables {{{
13 PN="$(basename $0)"
14 VERSION="$(dpkg --list $PN 2>/dev/null| awk '/^i/ {print $3}')"
15 VERSION="${VERSION:-unknown}"
16 MNTPOINT="/mnt/debootstrap.$$"
17
18 # inside the chroot system locales might not be available, so use minimum:
19 export LANG=C
20 export LC_ALL=C
21
22 # make sure interactive mode is only executed when
23 # using an empty configuration file or option --interactive
24 INTERACTIVE=''
25 # }}}
26
27 # source core functions {{{
28 . /etc/grml/lsb-functions
29 . /etc/grml/script-functions
30 # }}}
31
32 # help text {{{
33 usage() {
34   echo "$PN - wrapper around debootstrap for installing Debian
35
36 Usage: $PN [options]
37
38 Bootstrap options:
39
40   -m, --mirror <URL>     Mirror which should be used for apt-get/aptitude.
41   -i, --iso <mnt>        Mountpoint where a Debian ISO is mounted to, for use
42                            instead of fetching packages from a mirror.
43   -r, --release <name>   Release of new Debian system (default: lenny).
44   -t, --target <target>  Target partition (/dev/...) or directory where the
45                          system should be installed to.
46   -p, --mntpoint <mnt>   Mountpoint used for mounting the target system,
47                          has no effect if -t is given and represents a directory.
48       --debopt <params>  Extra parameters passed to the debootstrap command.
49       --interactive      Use interactive mode (frontend).
50       --nodebootstrap    Skip debootstrap, only do configuration to the target.
51       --grub <device>    Target for grub installation. Usage example: /dev/sda
52       --arch <arch>      Architecture to use. Currently only i386 is supported.
53       --filesystem <fs>  Filesystem that should be used when target is a partition.
54       --insecure         Do not download and check Release file signatures.
55
56 Configuration options:
57
58   -c, --config <file>      Use specified configuration file, defaults to
59                              /etc/debootstrap/config
60   -d, --confdir <path>     Place of config files for debootstrap, defaults
61                              to /etc/debootstrap
62       --packages <file>    Install packages defined in specified list file.
63       --nopackages         Skip installation of packages defined in
64                              /etc/debootstrap/packages
65       --debconf <file>     Pre-seed packages using specified pre-seed db file.
66       --keep_src_list      Do not overwrite user provided apt sources.list.
67       --hostname <name>    Hostname of Debian system.
68       --password <pwd>     Use specified password as password for user root.
69       --bootappend <line>  Add specified appendline to kernel whilst booting.
70       --chroot-scripts <d> Execute chroot scripts from specified directory.
71       --pre-scripts <dir>  Execute scripts from specified directory (before chroot-scripts).
72       --scripts <dir>      Execute scripts from specified directory (after chroot-scripts).
73
74 Other options:
75
76   -v, --verbose            Increase verbosity.
77   -h, --help               Print this usage information and exit.
78   -V, --version            Show summary of options and exit.
79
80 Usage examples can be found in the grml-debootstrap manpage.
81 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
82 "
83 }
84
85 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
86    usage
87    echo 'Please notice that this script requires root permissions!'
88    exit 0
89 fi
90 # }}}
91
92 # make sure we have what we need {{{
93 check4progs debootstrap dialog || exit 1
94 # }}}
95
96 # source main configuration file {{{
97 if [ -r /etc/debootstrap/config ] ; then
98   . /etc/debootstrap/config
99 fi
100 # }}}
101
102 # cmdline handling {{{
103 # source external command line parameter-processing script
104 if [ -r ./cmdlineopts.clp ] ; then
105    . ./cmdlineopts.clp
106 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
107    . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
108 else
109    echo "Error: cmdline function file not found, exiting.">&2
110    exit 1
111 fi
112
113 # == business-logic of command line parameter-processing
114
115 # source configuration file in <confdir> if supplied. {{{
116 [ "$_opt_confdir" ] && {
117   CONFFILES=$_opt_confdir
118   einfo "Using config files under $CONFFILES/."
119   if ! [ -r "$CONFFILES/config" ] ; then
120     eerror "Error: config file $CONFFILES/config not found."; eend 1; exit 1
121   fi
122   if ! . "$CONFFILES/config" ; then
123     eerror "Error reading config file $CONFFILES/config" ; eend 1 ; exit 1
124   fi
125   # restore the command line parameter value
126   CONFFILES=$_opt_confdir
127 }
128 # }}}
129
130 [ "$_opt_mirror" ]              && MIRROR=$_opt_mirror
131 [ "$_opt_iso" ]                 && ISO=$_opt_iso
132 [ "$_opt_release" ]             && RELEASE=$_opt_release
133 [ "$_opt_target" ]              && TARGET=$_opt_target
134 [ "$_opt_mntpoint" ]            && MNTPOINT=$_opt_mntpoint
135 [ "$_opt_debopt" ]              && DEBOOTSTRAP_OPT=$_opt_debopt
136 [ "$_opt_interactive" ]         && INTERACTIVE=1
137 [ "$_opt_config" ]              && CONFIGFILE=$_opt_config
138 [ "$_opt_filesystem" ]          && MKFS="mkfs.$_opt_filesystem"
139 [ "$_opt_packages_set" ]        && PACKAGES='yes'
140 [ "$_opt_nopackages" ]          && PACKAGES=''
141 [ "$_opt_debconf_set" ]         && DEBCONF='yes'
142 [ "$_opt_scripts_set" ]         && SCRIPTS='yes'
143 [ "$_opt_pre_scripts_set" ]     && PRE_SCRIPTS='yes'
144 [ "$_opt_chroot_scripts_set" ]  && CHROOT_SCRIPTS='yes'
145 [ "$_opt_keep_src_list" ]       && KEEP_SRC_LIST='yes'
146 [ "$_opt_hostname" ]            && HOSTNAME=$_opt_hostname
147 [ "$_opt_password" ]            && ROOTPASSWORD=$_opt_password
148 [ "$_opt_bootappend" ]          && BOOT_APPEND=$_opt_bootappend
149 [ "$_opt_grub" ]                && GRUB=$_opt_grub
150 [ "$_opt_arch" ]                && ARCH=$_opt_arch
151 [ "$_opt_insecure" ]            && SECURE='false'
152 [ "$_opt_verbose" ]             && VERBOSE="-v"
153
154 [ "$_opt_help" ] && {
155   usage ; eend 0
156   eend 0
157   exit 0
158 }
159
160 [ "$_opt_version" ] && {
161   einfo "$PN - version $VERSION"
162   einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
163   eend 0
164   exit 0
165 }
166 # }}}
167
168 # check for root permissions {{{
169 if ! check4root ; then
170    echo "For usage instructions please execute '$PN --help'."
171    exit 1
172 fi
173 # }}}
174
175 # source specified configuration file {{{
176 if [ -n "$CONFIGFILE" ] ; then
177    einfo "Reading specified config file $CONFIGFILE."
178    if ! . "$CONFIGFILE" ; then
179       eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
180    fi
181 fi
182 # }}}
183
184 # backwards compability checks {{{
185 if [ -n "$GROOT" ] ; then
186    echo "Error: you seem to have \$GROOT configured." >&2
187    echo "This variable is no longer supported, please visit the" >&2
188    echo "grml-debootstrap documentation for details." >&2
189    exit 1
190 fi
191
192 if echo "$GRUB" | grep -q '^hd' ; then
193    echo "Error: this syntax for the grub configuration variable is no longer supported." >&2
194    echo "Please do not use hd... any longer but /dev/sdX instead." >&2
195    exit 1
196 fi
197 # }}}
198
199 # welcome screen {{{
200 welcome_dialog()
201 {
202    dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
203 Do you want to continue installing Debian using this frontend?" 0 0
204 }
205 # }}}
206
207 # ask for target {{{
208 prompt_for_target()
209 {
210   AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
211                sed 's/*//' | \
212                grep -v 'Extended$' | \
213                gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
214
215   if [ -z "$AVAILABLE_PARTITIONS" ] ; then
216      dialog --title "$PN" --trim \
217      --msgbox "Sorry, no partitions found. Please configure your
218      harddisks (see /proc/partitions) using a tool like fdisk,
219      cfdisk, gpart, gparted,..." 0 0
220      exit 0
221   fi
222
223   PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
224                        echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
225                    done)
226
227   TARGET=$(dialog --title "$PN" --single-quoted --stdout \
228          --menu "Please select the target partition:" 0 0 0 \
229          $PARTITION_LIST)
230 }
231 # }}}
232
233 # ask for bootmanager {{{
234 prompt_for_bootmanager()
235 {
236   ADDITIONAL_PARAMS=""
237
238   if echo "$TARGET" | grep -q "/dev/md" ; then
239      MBRPART="all disks of Software RAID $TARGET"
240   else
241      # figure out whole disk device
242      found=
243      for device in /dev/disk/by-id/*
244      do
245         [ $(readlink -f $device) = ${TARGET} ] || continue
246         found=1
247         break
248      done
249      [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
250      if [ -e "$MBRDISK" ]; then
251         MBRDISK=$(readlink -f $MBRDISK)
252      else
253         # fall back to old behaviour
254         MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
255      fi
256
257      MBRPART="MBR of $MBRDISK"
258   fi
259
260   for device in cciss/c0d0 sda hda; do
261     if [ /dev/$device != ${MBRDISK} ]; then
262       grep -q $device /proc/partitions && \
263       ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
264     fi
265   done
266   ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
267
268   OIFS="$IFS"; IFS=:
269
270   GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
271           --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
272             mbr       "install bootmanager into $MBRPART" \
273             nowhere   "do not install bootmanager at all" \
274           ${ADDITIONAL_PARAMS})
275   [ $? -eq 0 ] || bailout 3
276   IFS="$OIFS"
277
278   case "$GETMBR" in
279     mbr)
280       # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
281       if echo "$TARGET" | grep -q "/dev/md" ; then
282          # using sw-raid:
283          if [ -n "$SELECTED_PARTITIONS" ] ; then
284             GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
285          else
286             GRUB="$TARGET"
287          fi
288       else
289         GRUB="$MBRDISK"
290       fi
291       ;;
292     hda)
293       GRUB="/dev/hda"
294       ;;
295     sda)
296       GRUB="/dev/sda"
297       ;;
298     nowhere)
299       GRUB=''
300       ;;
301   esac
302 }
303 # }}}
304
305 # ask for Debian release {{{
306 prompt_for_release()
307 {
308   [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='lenny'
309   RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
310             "Please enter the Debian release you would like to use for installation:" \
311             0 50 3 \
312             lenny    Debian/stable \
313             squeeze  Debian/testing \
314             sid      Debian/unstable)"
315 }
316 # }}}
317
318 # ask for hostname {{{
319 prompt_for_hostname()
320 {
321   HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
322             "Please enter the hostname you would like to use for installation:" \
323             0 0 $HOSTNAME)"
324 }
325 # }}}
326
327 # ask for password {{{
328 prompt_for_password()
329 {
330      ROOTPW1='PW1'
331      ROOTPW2='PW2'
332      while [ "$ROOTPW1" != "$ROOTPW2" ]; do
333        ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
334        "Please enter the password for the root account:" 10 60)
335        ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
336        "Please enter the password for the root account again for \
337        confirmation:" 10 60)
338
339        if [ "$ROOTPW1" != "$ROOTPW2" ]; then
340          $(dialog --stdout --title "${PN}" --ok-label \
341          "Retry" --msgbox "Passwords do not match!" 10 60)
342        fi
343      done
344      ROOTPASSWORD="$ROOTPW1"
345 }
346 # }}}
347
348 # ask for Debian mirror {{{
349 prompt_for_mirror()
350 {
351   [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
352
353   CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
354           --menu "Where do you want to install from?" 0 0 0 \
355             net   "install via network (downloading from mirror)" \
356             local "install from local directory/mirror"
357           )
358
359   if [ "$CHOOSE_MIRROR" = 'net' ] ; then
360      [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
361      MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
362                "Please enter Debian mirror you would like to use for installing packages." \
363                0 0 $MIRROR)"
364   else # CHOOSE_MIRROR == local
365      [ -n "$ISO" ] || ISO='/mnt/mirror'
366      ISO="$(dialog --stdout --title "${PN}" --inputbox \
367                "Please enter directory name you would like to use for installing packages." \
368                0 0 $ISO)"
369   fi
370 }
371 # }}}
372
373 # software raid setup {{{
374 config_swraid_setup()
375 {
376 TMPFILE=$(mktemp)
377
378 # Currently we support only raid1:
379 RAIDLEVEL='raid1'
380
381 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
382 #                   --menu "Which RAID level do you want to use?" 0 0 0 \
383 #                     raid1 "Software RAID level 1" \
384 #                     raid5 "Software RAID level 5")
385 #[ $? -eq 0 ] || bailout 20
386
387 MD_LIST=$(for i in $(seq 0 9) ; do
388             awk '{print $4}' /proc/partitions | grep -q md$i || \
389             echo "/dev/md$i /dev/md$i"
390           done)
391
392 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
393 --menu "Which device do you want to use for ${RAIDLEVEL}?
394
395 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 \
396 $MD_LIST)
397 [ $? -eq 0 ] || bailout 20
398
399 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
400              sed 's/*//' | \
401              grep -v 'Extended$' | \
402              gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
403 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
404 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
405                      echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
406                  done)
407
408 dialog --title "$PN" --separate-output \
409        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
410        $PARTITION_LIST 2>$TMPFILE
411 RETVAL=$?
412 SELECTED_PARTITIONS="$(cat $TMPFILE)"
413
414 NUM_PARTITIONS=0
415 for i in $(cat $TMPFILE) ; do
416    NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
417 done
418
419 # force metadata version 0.90 for lenny so old grub can boot from this array.
420 METADATA_VERSION=""
421 if [ $RELEASE = "lenny" ]; then
422    METADATA_VERSION="-e0"
423 fi
424
425 ERRORFILE=$(mktemp)
426 set +e
427 # TODO: better error handling?
428 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
429       --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
430 RC=$?
431 set -e
432
433 if [ "$RC" = 0 ] ; then
434    dialog --title "$PN" --msgbox \
435    "Creating $TARGET was successful." 0 0
436    rm -f "$TMPFILE" "$ERRORFILE"
437 else
438    dialog --title "$PN" --msgbox \
439    "There was an error setting up $TARGET:
440
441 $(cat $ERRORFILE)
442
443 Exiting." 0 0
444    rm -f "$TMPFILE" "$ERRORFILE"
445    exit 1
446 fi
447
448 }
449
450 prompt_for_swraid()
451 {
452 if dialog --stdout --title "$PN" \
453           --defaultno --yesno "Do you want to configure Software RAID?
454
455 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
456   config_swraid_setup
457 fi
458 }
459 # }}}
460
461 # user should recheck his configuration {{{
462 # support full automatic installation:
463 checkforrun() {
464    dialog --timeout 10 --title "$PN" \
465           --yesno "Do you want to stop at this stage?
466
467 Notice: you are running ${PN} in non-interactive mode.
468 ${PN} will install Debian ${RELEASE} on ${TARGET}.
469 Last chance to quit. Timeout of 10 seconds running....
470
471 Do you want to stop now?" 0 0 2>/dev/null
472 }
473
474 # make sure the user is aware of the used configuration {{{
475 checkconfiguration()
476 {
477 if [ -n "$AUTOINSTALL" ] ; then
478    if checkforrun ; then
479       eerror "Exiting as requested" ; eend 0
480       exit 1
481    fi
482 elif [ -n "$INTERACTIVE" ] ; then
483
484    INFOTEXT="Please recheck configuration before execution:
485    "
486    [ -n "$TARGET" ]  && INFOTEXT="$INFOTEXT
487    Target:          $TARGET"
488    [ -n "$GRUB" ]    && INFOTEXT="$INFOTEXT
489    Install grub:    $GRUB"
490    [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
491    Using release:   $RELEASE"
492    [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
493    Using hostname   $HOSTNAME"
494    [ -n "$MIRROR" ]  && INFOTEXT="$INFOTEXT
495    Using mirror:    $MIRROR"
496    [ -n "$ISO" ]  && INFOTEXT="$INFOTEXT
497    Using ISO:       $ISO"
498
499    INFOTEXT="$INFOTEXT
500
501 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
502
503    dialog --title "$PN" --no-collapse \
504           --yesno "$INFOTEXT" 0 0
505
506 else # if not running automatic installation display configuration and prompt for execution:
507    einfo "$PN - Please recheck configuration before execution:"
508    echo
509    echo "   Target:          $TARGET"
510
511    # do not display if MNTPOINT is the default one
512    case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo "   Mount point:     $MNTPOINT" ;; esac
513
514    [ -n "$GRUB" ]     && echo "   Install grub:    $GRUB" || echo "   Install grub:    no"
515    [ -n "$RELEASE" ]  && echo "   Using release:   $RELEASE"
516    [ -n "$MIRROR" ]   && echo "   Using mirror:    $MIRROR"
517    [ -n "$HOSTNAME" ] && echo "   Using hostname:  $HOSTNAME"
518    [ -n "$ISO" ]      && echo "   Using ISO:       $ISO"
519
520    echo "   Important! Continuing will delete all data from ${TARGET}!"
521
522    echo
523    einfon "Is this ok for you? [y/N] "
524    read a
525    if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
526       eerror "Exiting as requested." ; eend 1
527       exit 1
528    fi
529 fi
530 }
531 # }}}
532
533 # interactive mode {{{
534 interactive_mode()
535 {
536   welcome_dialog
537
538   prompt_for_release
539
540   prompt_for_swraid
541
542   prompt_for_target
543
544   prompt_for_bootmanager
545
546   prompt_for_hostname
547
548   prompt_for_password
549
550   prompt_for_mirror
551 }
552
553 # run interactive mode if we didn't get the according configuration yet
554 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
555    # only target might be unset, so make sure the INTERACTIVE flag is set as well
556    INTERACTIVE=1
557    interactive_mode
558 fi
559 # }}}
560
561 checkconfiguration
562
563 # finally make sure at least $TARGET is set [the partition for the new system] {{{
564 if [ -n "$TARGET" ] ; then
565    SHORT_TARGET="${TARGET##*/}"
566 else
567    eerror "Please adjust $CONFFILES/config or..."
568    eerror "... use the interactive version for configuration before running ${0}" ; eend 1
569    exit 1
570 fi
571 # }}}
572
573 # stages setup {{{
574 if [ -z "$STAGES" ] ; then
575    STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
576    [ -d "$STAGES" ] || mkdir -p "$STAGES"
577 fi
578
579 if [ -r "$STAGES"/grml-debootstrap ] ; then
580    if grep -q done $STAGES/grml-debootstrap ; then
581       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
582       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
583    fi
584 fi
585 # }}}
586
587 # partition handling {{{
588 PARTITION=''
589 DIRECTORY=''
590
591 set_target_directory(){
592     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
593     DIRECTORY=1
594     MNTPOINT="$TARGET"
595     MKFS=''
596     TUNE2FS=''
597     FSCK=''
598     GRUB=''
599     # make sure we normalise the path to an absolute directory name so something like:
600     #  mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
601     # works
602     TARGET="$(readlink -f $TARGET)"
603 }
604
605 if [ -b "$TARGET" ] ; then
606     PARTITION=1
607 else
608     set_target_directory
609 fi
610 # }}}
611
612 # architecture setup {{{
613 if [ -n "$ARCH" ] ; then
614    ARCHCMD="--arch $ARCH"
615    ARCHINFO=" (${ARCH})"
616 else
617    ARCH="$(dpkg --print-architecture)"
618    ARCHCMD="--arch $ARCH"
619    ARCHINFO=" (${ARCH})"
620 fi
621 # }}}
622
623 # keyring setupt {{{
624 KEYRING=""
625 if [ "$SECURE" = 'yes' ] ; then
626    if [ -e '/etc/apt/trusted.gpg' ] ; then
627       KEYRING="--keyring /etc/apt/trusted.gpg"
628    else
629       eerror "Could not find /etc/apt/trusted.gpg."
630    fi
631 else
632    ewarn "Not checking Release signatures!"
633 fi
634 # }}}
635
636 # make sure we have the right syntax when using an iso image {{{
637 if [ -n "$ISO" ] ; then
638    case $ISO in
639       file*) # do nothing
640       ;;
641       *)
642       ISO=file:$1
643       ;;
644    esac
645 fi
646 ISODIR=${ISO##file:}
647 ISODIR=${ISODIR%%/}
648 # }}}
649
650 # helper functions {{{
651 # we want to exit smoothly and clean:
652 bailout(){
653   # make sure $TARGET is not mounted when exiting grml-debootstrap
654   if [ -n "$MNTPOINT" ] ; then
655      if grep -q $MNTPOINT /proc/mounts ; then
656         # make sure nothing is left inside chroot so we can unmount it
657         [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
658         [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
659         # ugly, but make sure we really don't leave anything (/proc /proc is intended)
660         for ARG in /sys /proc /proc ; do
661           [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
662         done
663         umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
664
665         [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
666
667         if [ -n "$DIRECTORY" ] ; then
668           einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
669         else
670           einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
671         fi
672
673         if [ -n "$STAGES" ] ; then
674            echo -n "Removing stages directory ${STAGES}: "
675            rm -rf "$STAGES" && echo done
676         fi
677
678         # remove directory only if we used the default with process id inside the name
679         if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
680            einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
681         fi
682      fi
683   fi
684
685   [ -n "$1" ] && EXIT="$1" || EXIT="1"
686   [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
687
688   exit "$EXIT"
689 }
690 trap bailout HUP INT QUIT TERM
691
692 # we want to execute all the functions only once, simple check for it:
693 stage() {
694   if [ -n "$2" ] ; then
695      echo "$2" > "${STAGES}/${1}"
696      return 0
697   elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
698      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
699      eindent
700        ewarn "To reexecute it clean up the according directory inside $STAGES" ; eend 0
701      eoutdent
702      return 1
703   fi
704 }
705 # }}}
706
707 # create filesystem {{{
708 mkfs() {
709   if [ -n "$DIRECTORY" ] ; then
710      einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
711   else
712     if grep -q "$TARGET" /proc/mounts ; then
713       eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
714       exit 1
715     fi
716
717     if [ -n "$MKFS" ] ; then
718        einfo "Running $MKFS on $TARGET"
719        $MKFS $TARGET ; RC=$?
720
721        # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
722        # will fail to detect the uuid in the chroot
723        if echo "$TARGET" | grep -q "/dev/md" ; then
724          blockdev --rereadpt "${TARGET}"
725        else
726          blockdev --rereadpt "${TARGET%%[0-9]*}"
727        fi
728        # give the system 2 seconds, otherwise we might run into
729        # race conditions :-/
730        sleep 2
731
732        eval $(blkid -o udev $TARGET 2>/dev/null)
733        [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
734
735        eend $RC
736     fi
737
738   fi
739 }
740 # }}}
741
742 # modify filesystem settings {{{
743 tunefs() {
744   if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
745      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
746      $TUNE2FS $TARGET
747      eend $?
748   fi
749 }
750 # }}}
751
752 # mount the new partition or if it's a directory do nothing at all {{{
753 mount_target() {
754   if [ -n "$DIRECTORY" ] ; then
755      einfo "Running grml-debootstrap on a directory, nothing to mount."
756   else
757      if grep -q $TARGET /proc/mounts ; then
758         ewarn "$TARGET already mounted, continuing anyway." ; eend 0
759      else
760        [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
761        einfo "Mounting $TARGET to $MNTPOINT"
762        mount -o rw,suid,dev $TARGET $MNTPOINT
763        eend $?
764      fi
765   fi
766   if [ -n "$ISODIR" ] ; then
767      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
768      mkdir -p "$MNTPOINT/$ISODIR"
769      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
770      eend $?
771   fi
772 }
773 # }}}
774
775 # install main chroot {{{
776 debootstrap_system() {
777   if [ "$_opt_nodebootstrap" ]; then
778      einfo "Skipping debootstrap as requested."
779      return
780   fi
781
782   if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
783      einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
784      if [ -n "$MIRROR" ] ; then
785         einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
786         $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
787      else
788         einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
789         $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
790      fi
791      eend $?
792   else
793      eerror "Error: $MNTPOINT not mounted, can not continue."
794      eend 1
795   fi
796 }
797 # }}}
798
799 # prepare chroot via chroot-script {{{
800 preparechroot() {
801   einfo "Preparing chroot system"
802
803   # provide variables to chroot system
804   CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
805   touch $CHROOT_VARIABLES
806   chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
807   echo "# Configuration of ${PN}"                              > $CHROOT_VARIABLES
808   [ -n "$ARCH" ]          && echo "ARCH=$ARCH"                 >> $CHROOT_VARIABLES
809   [ -n "$GRUB" ]          && echo "GRUB=$GRUB"                 >> $CHROOT_VARIABLES
810   [ -n "$HOSTNAME" ]      && echo "HOSTNAME=$HOSTNAME"         >> $CHROOT_VARIABLES
811   [ -n "$INSTALL_NOTES" ] && echo "INSTALL_NOTES=$INSTALL_NOTES" >> $CHROOT_VARIABLES
812   [ -n "$ISODIR" ]        && echo "ISODIR=$ISO"                >> $CHROOT_VARIABLES
813   [ -n "$ISO" ]           && echo "ISO=$ISO"                   >> $CHROOT_VARIABLES
814   [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
815   [ -n "$MIRROR" ]        && echo "MIRROR=$MIRROR"             >> $CHROOT_VARIABLES
816   [ -n "$PACKAGES" ]      && echo "PACKAGES=$PACKAGES"         >> $CHROOT_VARIABLES
817   [ -n "$RM_APTCACHE" ]   && echo "RM_APTCACHE=$RM_APTCACHE"   >> $CHROOT_VARIABLES
818   [ -n "$ROOTPASSWORD" ]  && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
819   [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=$SELECTED_PARTITIONS" >> $CHROOT_VARIABLES
820   [ -n "$TARGET" ]        && echo "TARGET=$TARGET"             >> $CHROOT_VARIABLES
821   [ -n "$TARGET_UUID" ]   && echo "TARGET_UUID=$TARGET_UUID"   >> $CHROOT_VARIABLES
822
823   cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
824   chmod 755 $MNTPOINT/bin/chroot-script
825   [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
826
827   # make sure we have our files for later use via chroot-script
828   cp $VERBOSE $CONFFILES/config    $MNTPOINT/etc/debootstrap/
829   # make sure we adjust the configuration variables accordingly:
830   sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
831   sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#"    $MNTPOINT/etc/debootstrap/config
832   sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#"          $MNTPOINT/etc/debootstrap/config
833
834   # install notes:
835   if [ -n "$INSTALL_NOTES" ] ; then
836      [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
837   fi
838
839   # package selection:
840   cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
841     $MNTPOINT/etc/debootstrap/packages
842
843   # debconf preseeding:
844   _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
845   [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
846     cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
847
848   # copy scripts that should be executed inside the chroot:
849   _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
850   [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
851     mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
852     cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
853   }
854
855   # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
856   cp $VERBOSE $CHROOT_VARIABLES  $MNTPOINT/etc/debootstrap/variables
857
858   cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
859
860   # make sure we can access network [relevant for cdebootstrap]
861   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
862
863   # provide system's /etc/hosts to the target:
864   if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
865      cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
866   fi
867
868   # setup default locales
869   [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen  $MNTPOINT/etc/locale.gen
870
871   # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
872   ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
873
874   # copy any existing files to chroot
875   [ -d $CONFFILES/bin   ] && cp $VERBOSE -a -L $CONFFILES/bin/*   $MNTPOINT/bin/
876   [ -d $CONFFILES/boot  ] && cp $VERBOSE -a -L $CONFFILES/boot/*  $MNTPOINT/boot/
877   [ -d $CONFFILES/etc   ] && cp $VERBOSE -a -L $CONFFILES/etc/*   $MNTPOINT/etc/
878   [ -d $CONFFILES/sbin  ] && cp $VERBOSE -a -L $CONFFILES/sbin/*  $MNTPOINT/sbin/
879   [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
880   [ -d $CONFFILES/usr   ] && cp $VERBOSE -a -L $CONFFILES/usr/*   $MNTPOINT/usr/
881   [ -d $CONFFILES/var   ] && cp $VERBOSE -a -L $CONFFILES/var/*   $MNTPOINT/var/
882
883   # copy local network setup to chroot
884   if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
885      [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
886      cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
887   fi
888
889   # install config file providing some example entries
890   if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
891      cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
892   fi
893
894   eend 0
895 }
896 # }}}
897
898 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
899 execute_pre_scripts() {
900    # make sure we have $MNTPOINT available for our scripts
901    export MNTPOINT
902    if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
903       [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
904       for script in ${pre_scripts}/* ; do
905          if [ -x "$script" ] ; then
906             einfo "Executing pre-script $script"
907             $script ; eend $?
908          fi
909       done
910    fi
911 }
912 # }}}
913
914 # execute all scripts in /etc/debootstrap/scripts/ {{{
915 execute_scripts() {
916    # make sure we have $MNTPOINT available for our scripts
917    export MNTPOINT
918    if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
919       [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
920       for script in ${scripts}/* ; do
921          if [ -x "$script" ] ; then
922             einfo "Executing script $script"
923             $script ; eend $?
924          fi
925       done
926    fi
927 }
928 # }}}
929
930 # execute chroot-script {{{
931 chrootscript() {
932   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
933      mount_target
934   fi
935
936   if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
937      einfo "Executing chroot-script now"
938      mount --bind /dev "$MNTPOINT"/dev
939      chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
940      umount "$MNTPOINT"/dev
941      eend $RC
942   else
943      eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
944      eend 1
945   fi
946 }
947 # }}}
948
949 # unmount $MNTPOINT {{{
950 umount_chroot() {
951
952   # display installation notes:
953   if [ -n "$INSTALL_NOTES" ] ; then
954      [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
955   fi
956
957   if [ -n "$ISODIR" ] ; then
958      if grep -q "$ISODIR" /proc/mounts ; then
959         einfo "Unmount $MNTPOINT/$ISODIR"
960         umount "$MNTPOINT/$ISODIR"
961         eend $?
962      fi
963   fi
964
965   if grep -q "$MNTPOINT" /proc/mounts ; then
966      if [ -n "$PARTITION" ] ; then
967         einfo "Unmount $MNTPOINT"
968         umount $MNTPOINT
969         eend $?
970      fi
971   fi
972 }
973 # }}}
974
975 # execute filesystem check {{{
976 fscktool() {
977   if [ "$FSCK" = 'yes' ] ; then
978      [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
979      einfo "Checking filesystem on $TARGET using $FSCKTOOL"
980      $FSCKTOOL $TARGET
981      eend $?
982   fi
983 }
984 # }}}
985
986 # now execute all the functions {{{
987 for i in mkfs tunefs mount_target debootstrap_system preparechroot \
988          execute_pre_scripts chrootscript execute_scripts umount_chroot   \
989          fscktool ; do
990     if stage "${i}" ; then
991        $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
992     fi
993 done
994 # }}}
995
996 # finalize {{{
997 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
998 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
999
1000 # Remove temporary mountpoint again
1001 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
1002    einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
1003 fi
1004 # }}}
1005
1006 # end dialog of autoinstallation {{{
1007 if [ -n "$AUTOINSTALL" ] ; then
1008    dialog --title "$PN" --msgbox \
1009           "Finished execution of ${PN}. Enjoy your Debian system." 0 0
1010 else
1011    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1012 fi
1013 # }}}
1014
1015 ## END OF FILE #################################################################
1016 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3