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