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