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