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