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