Add backwards compability checks and NEWS file
[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.32'
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
53 Configuration options:
54
55   -c, --config <file>      Use specified configuration file, defaults to
56                              /etc/debootstrap/config
57   -d, --confdir <path>     Place of config files for debootstrap, defaults
58                              to /etc/debootstrap
59       --packages <file>    Install packages defined in specified list file.
60       --debconf <file>     Pre-seed packages using specified pre-seed db file.
61       --keep_src_list      Do not overwrite user provided apt sources.list.
62       --hostname <name>    Hostname of Debian system.
63       --password <pwd>     Use specified password as password for user root.
64       --bootappend <line>  Add specified appendline to kernel whilst booting.
65       --chroot-scripts <d> Execute chroot scripts from specified directory.
66       --scripts <dir>      Execute scripts from specified directory.
67
68 Other options:
69
70   -v, --verbose            Increase verbosity.
71   -h, --help               Print this usage information and exit.
72   -V, --version            Show summary of options and exit.
73
74 Usage examples can be found in the grml-debootstrap manpage.
75 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
76 "
77 }
78
79 if [ "$1" = '-h' ] || [ "$1" = '-help' ] ; then
80    usage
81    echo 'Please notice that this script requires root permissions!'
82    exit 0
83 fi
84 # }}}
85
86 # make sure we have what we need {{{
87 check4progs debootstrap dialog || exit 1
88 check4root || exit 1
89 # }}}
90
91 # source configuration file {{{
92 if [ -r /etc/debootstrap/config ] ; then
93    if [ -n "$CONFIGFILE" ] ; then
94       einfo "Using config file $CONFIGFILE."
95       if ! . "$CONFIGFILE" ; then
96          eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
97       fi
98    else
99       . /etc/debootstrap/config
100    fi
101 fi
102 # }}}
103
104 # cmdline handling {{{
105 # source external command line parameter-processing script
106 if [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
107    . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
108 elif [ -r ./cmdlineopts.clp ] ; then
109    . ./cmdlineopts.clp
110 else
111    echo "Error: cmdline function file not found, exiting.">&2
112    exit 1
113 fi
114
115 # == business-logic of command line parameter-processing
116
117 # source configuration file in <confdir> if supplied. {{{
118 [ "$_opt_confdir" ] && {
119   CONFFILES=$_opt_confdir
120   einfo "Using config files under $CONFFILES/."
121   if ! [ -r "$CONFFILES/config" ] ; then
122     eerror "Error: config file $CONFFILES/config not found."; eend 1; exit 1
123   fi
124   if ! . "$CONFFILES/config" ; then
125     eerror "Error reading config file $CONFFILES/config" ; eend 1 ; exit 1
126   fi
127   # restore the command line parameter value
128   CONFFILES=$_opt_confdir
129 }
130 # }}}
131
132 [ "$_opt_mirror" ]              && MIRROR=$_opt_mirror
133 [ "$_opt_iso" ]                 && ISO=$_opt_iso
134 [ "$_opt_release" ]             && RELEASE=$_opt_release
135 [ "$_opt_target" ]              && TARGET=$_opt_target
136 [ "$_opt_mntpoint" ]            && MNTPOINT=$_opt_mntpoint
137 [ "$_opt_debopt" ]              && DEBOOTSTRAP_OPT=$_opt_debopt
138 [ "$_opt_interactive" ]         && INTERACTIVE=1
139 [ "$_opt_config" ]              && CONFIGFILE=$_opt_config
140 [ "$_opt_packages_set" ]        && PACKAGES='yes'
141 [ "$_opt_debconf_set" ]         && DEBCONF='yes'
142 [ "$_opt_scripts_set" ]         && SCRIPTS='yes'
143 [ "$_opt_chroot_scripts_set" ]  && CHROOT_SCRIPTS='yes'
144 [ "$_opt_keep_src_list" ]       && KEEP_SRC_LIST='yes'
145 [ "$_opt_hostname" ]            && HOSTNAME=$_opt_hostname
146 [ "$_opt_password" ]            && ROOTPASSWORD=$_opt_password
147 [ "$_opt_bootappend" ]          && BOOT_APPEND=$_opt_bootappend
148 [ "$_opt_grub" ]                && GRUB=$_opt_grub
149 [ "$_opt_arch" ]                && ARCH=$_opt_arch
150 [ "$_opt_verbose" ]             && VERBOSE="-v"
151
152 [ "$_opt_help" ] && {
153   usage ; eend 0
154   eend 0
155   exit 0
156 }
157
158 [ "$_opt_version" ] && {
159   einfo "$PN - version $VERSION"
160   einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
161   eend 0
162   exit 0
163 }
164 # }}}
165
166 # backwards compability checks {{{
167 if [ -n "$GROOT" ] ; then
168    echo "Error: you seem to have \$GROOT configured." >&2
169    echo "This variable is no longer supported, please visit the" >&2
170    echo "grml-debootstrap documentation for details." >&2
171    exit 1
172 fi
173
174 if echo "$GRUB" | grep -q '^hd' ; then
175    echo "Error: this syntax for the grub configuration variable is no longer supported." >&2
176    echo "Please do not use hd... any longer but /dev/sdX instead." >&2
177    exit 1
178 fi
179 # }}}
180
181 # welcome screen {{{
182 welcome_dialog()
183 {
184    dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
185 Do you want to continue installing Debian using this frontend?" 0 0
186 }
187 # }}}
188
189 # ask for target {{{
190 prompt_for_target()
191 {
192   AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
193                sed 's/*//' | \
194                grep -v 'Extended$' | \
195                gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
196
197   if [ -z "$AVAILABLE_PARTITIONS" ] ; then
198      dialog --title "$PN" --trim \
199      --msgbox "Sorry, no partitions found. Please configure your
200      harddisks (see /proc/partitions) using a tool like fdisk,
201      cfdisk, gpart, gparted,..." 0 0
202      exit 0
203   fi
204
205   PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
206                        echo "$i $(vol_id --type $i 2>/dev/null || echo [no_filesystem_yet])"
207                    done)
208
209   TARGET=$(dialog --title "$PN" --single-quoted --stdout \
210          --menu "Please select the target partition:" 0 0 0 \
211          $PARTITION_LIST)
212 }
213 # }}}
214
215 # ask for bootmanager {{{
216 prompt_for_bootmanager()
217 {
218   ADDITIONAL_PARAMS=""
219
220   if echo "$TARGET" | grep -q "/dev/md" ; then
221      MBRPART="all disks of Software RAID $TARGET"
222   else
223      # figure out whole disk device
224      found=
225      for device in /dev/disk/by-id/*
226      do
227         [ $(readlink -f $device) = ${TARGET} ] || continue
228         found=1
229         break
230      done
231      [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
232      if [ -e "$MBRDISK" ]; then
233         MBRDISK=$(readlink -f $MBRDISK)
234      else
235         # fall back to old behaviour
236         MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$/')
237      fi
238
239      MBRPART="MBR of $MBRDISK"
240   fi
241
242   for device in cciss/c0d0 sda hda; do
243     if [ /dev/$device != ${MBRDISK} ]; then
244       grep -q $device /proc/partitions && \
245       ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
246     fi
247   done
248   ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
249
250   OIFS="$IFS"; IFS=:
251
252   GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
253           --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
254             mbr       "install bootmanager into $MBRPART" \
255             partition "install bootmanager into partition $TARGET" \
256             nowhere   "do not install bootmanager at all" \
257           ${ADDITIONAL_PARAMS})
258   [ $? -eq 0 ] || bailout 3
259   IFS="$OIFS"
260
261   case "$GETMBR" in
262     mbr)
263       # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
264       if echo "$TARGET" | grep -q "/dev/md" ; then
265          # using sw-raid:
266          if [ -n "$SELECTED_PARTITIONS" ] ; then
267             GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
268          else
269             GRUB="$TARGET"
270          fi
271       else
272         GRUB="$MBRDISK"
273       fi
274       ;;
275     partition)
276       GRUB="$TARGET"
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 case $TARGET in
572   /dev/*)
573     PARTITION=1
574     ;;
575   *)
576     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
577     DIRECTORY=1
578     MNTPOINT="$TARGET"
579     MKFS=''
580     TUNE2FS=''
581     FSCK=''
582     GRUB=''
583     ;;
584 esac
585 # }}}
586
587 # architecture setup {{{
588 if [ -n "$ARCH" ] ; then
589    ARCHCMD="--arch $ARCH"
590    ARCHINFO=" (${ARCH})"
591 else
592    ARCH="$(dpkg --print-architecture)"
593    ARCHCMD="--arch $ARCH"
594    ARCHINFO=" (${ARCH})"
595 fi
596 # }}}
597
598 # make sure we have the right syntax when using an iso image {{{
599 if [ -n "$ISO" ] ; then
600    case $ISO in
601       file*) # do nothing
602       ;;
603       *)
604       ISO=file:$1
605       ;;
606    esac
607 fi
608 ISODIR=${ISO##file:}
609 ISODIR=${ISODIR%%/}
610 # }}}
611
612 # helper functions {{{
613 # we want to exit smoothly and clean:
614 bailout(){
615   # make sure $TARGET is not mounted when exiting grml-debootstrap
616   if [ -n "$MNTPOINT" ] ; then
617      if grep -q $MNTPOINT /proc/mounts ; then
618         # make sure nothing is left inside chroot so we can unmount it
619         [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
620         [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
621         # ugly, but make sure we really don't leav anything
622         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /sys  1>/dev/null 2>&1
623         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a    1>/dev/null 2>&1
624         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
625         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
626         [ -d "$MNTPOINT/$ISODIR" ]    && umount "$MNTPOINT/$ISODIR" 1>/dev/null 2>&1
627
628         if [ -n "$DIRECTORY" ] ; then
629           einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
630         else
631           einfo "Unmounting $MNTPOINT"   ; umount "$MNTPOINT" ; eend $?
632         fi
633
634         if [ -n "$STAGES" ] ; then
635            echo -n "Removing stages directory ${STAGES}: "
636            rm -rf "$STAGES" && echo done
637         fi
638
639         # remove directory only if we used the default with process id inside the name
640         if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
641            einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
642         fi
643      fi
644   fi
645
646   [ -n "$1" ] && EXIT="$1" || EXIT="1"
647   [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
648
649   exit "$EXIT"
650 }
651 trap bailout HUP INT QUIT TERM
652
653 # we want to execute all the functions only once, simple check for it:
654 stage() {
655   if [ -n "$2" ] ; then
656      echo "$2" > "${STAGES}/${1}"
657      return 0
658   elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
659      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
660      eindent
661        ewarn "To reexecute it clean up the according directory inside $STAGES" ; eend 0
662      eoutdent
663      return 1
664   fi
665 }
666 # }}}
667
668 # create filesystem {{{
669 mkfs() {
670   if [ -n "$DIRECTORY" ] ; then
671      einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
672   else
673     if grep -q "$TARGET" /proc/mounts ; then
674       eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
675       exit 1
676     fi
677
678     if [ -n "$MKFS" ] ; then
679        einfo "Running $MKFS on $TARGET"
680        $MKFS $TARGET ; RC=$?
681
682        # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
683        # will fail to detect the uuid in the chroot
684        blockdev --rereadpt "${TARGET%%[0-9]*}"
685        # give the system 2 seconds, otherwise we might run into
686        # race conditions :-/
687        sleep 2
688
689        eval $(blkid -o udev $TARGET 2>/dev/null)
690        [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
691
692        eend $RC
693     fi
694
695   fi
696 }
697 # }}}
698
699 # modify filesystem settings {{{
700 tunefs() {
701   if [ -n "$TUNE2FS" ] ; then
702      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
703      $TUNE2FS $TARGET
704      eend $?
705   fi
706 }
707 # }}}
708
709 # mount the new partition or if it's a directory do nothing at all {{{
710 mount_target() {
711   if [ -n "$DIRECTORY" ] ; then
712      einfo "Running grml-debootstrap on a directory, nothing to mount."
713   else
714      if grep -q $TARGET /proc/mounts ; then
715         ewarn "$TARGET already mounted, continuing anyway." ; eend 0
716      else
717        [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
718        einfo "Mounting $TARGET to $MNTPOINT"
719        mount -o rw,suid,dev $TARGET $MNTPOINT
720        eend $?
721      fi
722   fi
723   if [ -n "$ISODIR" ] ; then
724      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
725      mkdir -p "$MNTPOINT/$ISODIR"
726      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
727      eend $?
728   fi
729 }
730 # }}}
731
732 # install main chroot {{{
733 debootstrap_system() {
734   if [ "$_opt_nodebootstrap" ]; then
735      einfo "Skipping debootstrap as requested."
736      return
737   fi
738
739   if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
740      einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
741      if [ -n "$MIRROR" ] ; then
742         $DEBOOTSTRAP $ARCHCMD $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
743      else
744         $DEBOOTSTRAP $ARCHCMD $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
745      fi
746      eend $?
747   else
748      eerror "Error: $MNTPOINT not mounted, can not continue."
749      eend 1
750   fi
751 }
752 # }}}
753
754 # prepare chroot via chroot-script {{{
755 preparechroot() {
756   einfo "Preparing chroot system"
757
758   # provide variables to chroot system
759   CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
760   touch $CHROOT_VARIABLES
761   chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
762   echo "# Configuration of ${PN}"                              > $CHROOT_VARIABLES
763   [ -n "$ARCH" ]          && echo "ARCH=$ARCH"                 >> $CHROOT_VARIABLES
764   [ -n "$GRUB" ]          && echo "GRUB=$GRUB"                 >> $CHROOT_VARIABLES
765   [ -n "$HOSTNAME" ]      && echo "HOSTNAME=$HOSTNAME"         >> $CHROOT_VARIABLES
766   [ -n "$INSTALL_NOTES" ] && echo "INSTALL_NOTES=$INSTALL_NOTES" >> $CHROOT_VARIABLES
767   [ -n "$ISODIR" ]        && echo "ISODIR=$ISO"                >> $CHROOT_VARIABLES
768   [ -n "$ISO" ]           && echo "ISO=$ISO"                   >> $CHROOT_VARIABLES
769   [ -n "$MIRROR" ]        && echo "MIRROR=$MIRROR"             >> $CHROOT_VARIABLES
770   [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
771   [ -n "$PACKAGES" ]      && echo "PACKAGES=$PACKAGES"         >> $CHROOT_VARIABLES
772   [ -n "$ROOTPASSWORD" ]  && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
773   [ -n "$TARGET" ]        && echo "TARGET=$TARGET"             >> $CHROOT_VARIABLES
774   [ -n "$TARGET_UUID" ]   && echo "TARGET_UUID=$TARGET_UUID"   >> $CHROOT_VARIABLES
775
776   cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
777   chmod 755 $MNTPOINT/bin/chroot-script
778   [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
779
780   # make sure we have our files for later use via chroot-script
781   cp $VERBOSE $CONFFILES/config    $MNTPOINT/etc/debootstrap/
782   # make sure we adjust the configuration variables accordingly:
783   sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
784   sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#"    $MNTPOINT/etc/debootstrap/config
785   sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#"          $MNTPOINT/etc/debootstrap/config
786
787   # install notes:
788   if [ -n "$INSTALL_NOTES" ] ; then
789      [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
790   fi
791
792   # package selection:
793   cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
794     $MNTPOINT/etc/debootstrap/packages
795
796   # debconf preseeding:
797   _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
798   [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
799     cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
800
801   # copy scripts that should be executed inside the chroot:
802   _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
803   [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
804     mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
805     cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
806   }
807
808   # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
809   cp $VERBOSE $CHROOT_VARIABLES  $MNTPOINT/etc/debootstrap/variables
810
811   cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
812
813   # make sure we can access network [relevant for cdebootstrap]
814   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
815
816   # provide system's /etc/hosts to the target:
817   if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
818      cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
819   fi
820
821   # setup default locales
822   [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen  $MNTPOINT/etc/locale.gen
823
824   # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
825   ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
826
827   # copy any existing files to chroot
828   [ -d $CONFFILES/bin   ] && cp $VERBOSE -a -L $CONFFILES/bin/*   $MNTPOINT/bin/
829   [ -d $CONFFILES/boot  ] && cp $VERBOSE -a -L $CONFFILES/boot/*  $MNTPOINT/boot/
830   [ -d $CONFFILES/etc   ] && cp $VERBOSE -a -L $CONFFILES/etc/*   $MNTPOINT/etc/
831   [ -d $CONFFILES/sbin  ] && cp $VERBOSE -a -L $CONFFILES/sbin/*  $MNTPOINT/sbin/
832   [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
833   [ -d $CONFFILES/usr   ] && cp $VERBOSE -a -L $CONFFILES/usr/*   $MNTPOINT/usr/
834   [ -d $CONFFILES/var   ] && cp $VERBOSE -a -L $CONFFILES/var/*   $MNTPOINT/var/
835
836   # copy local network setup to chroot
837   if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
838      [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
839      cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
840   fi
841
842   eend 0
843 }
844 # }}}
845
846 # execute all scripts in /etc/debootstrap/scripts/ {{{
847 execute_scripts() {
848    # make sure we have $MNTPOINT available for our scripts
849    export MNTPOINT
850    if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
851       [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
852       for script in ${scripts}/* ; do
853          if [ -x "$script" ] ; then
854             einfo "Executing script $script"
855             $script ; eend $?
856          fi
857       done
858    fi
859 }
860 # }}}
861
862 # execute chroot-script {{{
863 chrootscript() {
864   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
865      mount_target
866   fi
867
868   if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
869      einfo "Executing chroot-script now"
870      mount --bind /dev "$MNTPOINT"/dev
871      chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
872      umount "$MNTPOINT"/dev
873      eend $RC
874   else
875      eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
876      eend 1
877   fi
878 }
879 # }}}
880
881 # install booloader grub {{{
882 grub_install() {
883   if [ -z "$GRUB" ] ; then
884      echo "Notice: \$GRUB not defined, will not install grub therefore."
885      return 0
886   fi
887
888   if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
889      for device in $SELECTED_PARTITIONS ; do
890         GRUB="${device%%[0-9]}"
891         einfo "Installing grub on ${GRUB}:"
892         [ -x /usr/sbin/grub-install ] && GRUBINSTALL="/usr/sbin/grub-install --no-floppy" || GRUBINSTALL="/sbin/grub-install --no-floppy"
893         $GRUBINSTALL --root-directory="$MNTPOINT" "$GRUB"
894         eend $?
895      done
896   else
897      einfo "Installing grub on ${GRUB}:"
898      [ -x /usr/sbin/grub-install ] && GRUBINSTALL="/usr/sbin/grub-install --no-floppy" || GRUBINSTALL="/sbin/grub-install --no-floppy"
899      $GRUBINSTALL --root-directory="$MNTPOINT" "$GRUB"
900      eend $?
901   fi
902 }
903 # }}}
904
905 # unmount $MNTPOINT {{{
906 umount_chroot() {
907
908   # display installation notes:
909   if [ -n "$INSTALL_NOTES" ] ; then
910      [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
911   fi
912
913   if [ -n "$ISODIR" ] ; then
914      if grep -q "$ISODIR" /proc/mounts ; then
915         einfo "Unmount $MNTPOINT/$ISODIR"
916         umount "$MNTPOINT/$ISODIR"
917         eend $?
918      fi
919   fi
920
921   if grep -q "$MNTPOINT" /proc/mounts ; then
922      if [ -n "$PARTITION" ] ; then
923         einfo "Unmount $MNTPOINT"
924         umount $MNTPOINT
925         eend $?
926      fi
927   fi
928 }
929 # }}}
930
931 # execute filesystem check {{{
932 fscktool() {
933   if [ "$FSCK" = 'yes' ] ; then
934      [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
935      einfo "Checking filesystem on $TARGET using $FSCKTOOL"
936      $FSCKTOOL $TARGET
937      eend $?
938   fi
939 }
940 # }}}
941
942 # now execute all the functions {{{
943 for i in mkfs tunefs mount_target debootstrap_system preparechroot \
944          chrootscript execute_scripts grub_install umount_chroot   \
945          fscktool ; do
946     if stage "${i}" ; then
947        $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
948     fi
949 done
950 # }}}
951
952 # finalize {{{
953 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
954 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
955
956 # Remove temporary mountpoint again
957 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
958    einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
959 fi
960 # }}}
961
962 # end dialog of autoinstallation {{{
963 if [ -n "$AUTOINSTALL" ] ; then
964    dialog --title "$PN" --msgbox \
965           "Finished execution of ${PN}. Enjoy your Debian system." 0 0
966 else
967    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
968 fi
969 # }}}
970
971 ## END OF FILE #################################################################
972 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3