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