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