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