Merge branch 'chrootscripts'
[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.23'
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: stable).
43   -t, --target=target    Target partition (/dev/...) or directory.
44   -p, --mntpoint=mnt     Mountpoint used for mounting the target system.
45       --debopt=params    Extra parameters passed to the debootstrap.
46       --interactive      Use interactive mode (frontend).
47       --nodebootstrap    Skip debootstrap, only do configuration to the target.
48       --groot=device     Root device for usage in grub, corresponds with
49                            \$TARGET in grub syntax, like hd0,0 for /dev/sda1.
50       --grub=device      Target for grub installation. Use grub syntax for
51                            specifying, like hd0 for /dev/sda.
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[=f]       Install packages defined in <confdir>/packages.
60                              Option arg: alternative package list file.
61       --debconf[=f]        Pre-seed packages using <confdir>/debconf-selections.
62                              Option arg: alternative pre-seed db file.
63       --keep_src_list      Do not overwrite user provided apt sources.list.
64       --hostname=name      Hostname of Debian system.
65       --password=pwd       Use specified password as password for user root.
66       --bootappend=line    Add specified appendline to kernel whilst booting.
67       --chroot-scripts[=f] Execute scripts from <confdir>/chroot-scripts/
68                              Option arg: alternative chroot-scripts directory.
69       --scripts[=d]        Execute scripts from <confdir>/scripts/.
70                              Option arg: alternative scripts 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@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_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}')
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   for device in sda hda; do
209     if [ /dev/$device != ${TARGET%[0-9]} ]; then
210       grep -q $device /proc/partitions && \
211       ADDITIONAL_PARAMS=:$device:"install bootmanager grub into MBR of /dev/${device}"
212     fi
213   done
214   ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
215
216   OIFS="$IFS"; IFS=:
217
218   if echo $TARGET | grep -q "/dev/md" ; then
219      MBRPART="all disks of Software RAID $TARGET"
220   else
221      MBRPART="MBR of ${TARGET%[0-9]}"
222   fi
223
224   GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
225           --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
226             mbr       "install bootmanager into $MBRPART" \
227             partition "install bootmanager into partition $TARGET" \
228             nowhere   "do not install bootmanager at all" \
229           ${ADDITIONAL_PARAMS})
230   [ $? -eq 0 ] || bailout 3
231   IFS="$OIFS"
232
233   case "$GETMBR" in
234     mbr)
235       # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
236       if echo $TARGET | grep -q "*md*" ; then
237          BOOT_PARTITION="${TARGET}"
238       else
239          BOOT_PARTITION="${TARGET%[0-9]}"
240       fi
241       ;;
242     partition)
243       BOOT_PARTITION="$TARGET"
244       ;;
245     hda)
246       BOOT_PARTITION="/dev/hda"
247       ;;
248     sda)
249       BOOT_PARTITION="/dev/sda"
250       ;;
251     nowhere)
252       BOOT_PARTITION=''
253       ;;
254   esac
255 }
256 # }}}
257
258 # ask for Debian release {{{
259 prompt_for_release()
260 {
261   RELEASE="$(dialog --stdout --title "${PN}" --default-item etch --menu \
262             "Please enter the Debian release you would like to use for installation:" \
263             0 50 3 \
264             etch   Debian/stable \
265             lenny  Debian/testing \
266             sid    Debian/unstable)"
267 }
268 # }}}
269
270 # ask for hostname {{{
271 prompt_for_hostname()
272 {
273   HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
274             "Please enter the hostname you would like to use for installation:" \
275             0 0 grml)"
276 }
277 # }}}
278
279 # ask for password {{{
280 prompt_for_password()
281 {
282      ROOTPW1='PW1'
283      ROOTPW2='PW2'
284      while [ "$ROOTPW1" != "$ROOTPW2" ]; do
285        ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
286        "Please enter the password for the root account:" 10 60)
287        ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
288        "Please enter the password for the root account again for \
289        confirmation:" 10 60)
290
291        if [ "$ROOTPW1" != "$ROOTPW2" ]; then
292          $(dialog --stdout --title "${PN}" --ok-label \
293          "Retry" --msgbox "Passwords do not match!" 10 60)
294        fi
295      done
296      ROOTPASSWORD="$ROOTPW1"
297 }
298 # }}}
299
300 # ask for Debian mirror {{{
301 prompt_for_mirror()
302 {
303   MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
304             "Please enter Debian mirror you would like to use for installing packages." \
305             0 0 http://ftp.de.debian.org/debian)"
306 }
307 # }}}
308
309 # get grub's syntax for /dev/ice {{{
310 # usage example: 'grubdevice /dev/sda2' returns 'hd0,1'
311 grubdevice() {
312   if [ -z "$1" ] ; then
313      echo "Usage: grubdevice <device>">&2
314      return 1
315   fi
316
317   device="$1"
318   device_map=/boot/grub/device.map
319
320   # create device.map
321   if ! [ -f "$device_map" ] ; then
322      echo 'quit' | grub --device-map="$device_map" 1>/dev/null 2>&1
323   fi
324
325   # based on code from d-i's trunk/packages/arch/i386/grub-installer/grub-installer:
326   tmp_disk=`echo "$device" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
327                     -e 's%\(fd[0-9]*\)$%\1%' \
328                     -e 's%/part[0-9]*$%/disc%' \
329                     -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
330   tmp_part=`echo "$device" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
331                     -e 's%.*/fd[0-9]*$%%' \
332                     -e 's%.*/floppy/[0-9]*$%%' \
333                     -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
334                     -e 's%.*c[0-7]d[0-9]*p*%%'`
335   tmp_drive=$(grep -v '^#' $device_map | grep "$tmp_disk *$" | sed 's%.*\([hf]d[0-9][a-g0-9,]*\).*%\1%')
336
337   case $1 in
338      /dev/[sh]d[a-z]) # we expect something like 'hd0'
339         echo "$tmp_drive"
340         ;;
341       *) # we expect something like 'hd0,0'
342         echo "$tmp_drive" | sed "s%$%,`expr $tmp_part - 1`%" # FIXME => md0
343         ;;
344   esac
345 }
346 # }}}
347
348 # software raid setup {{{
349 config_swraid_setup()
350 {
351 TMPFILE=$(mktemp)
352
353 # Currently we support only raid1:
354 RAIDLEVEL='raid1'
355
356 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
357 #                   --menu "Which RAID level do you want to use?" 0 0 0 \
358 #                     raid1 "Software RAID level 1" \
359 #                     raid5 "Software RAID level 5")
360 #[ $? -eq 0 ] || bailout 20
361
362 MD_LIST=$(for i in $(seq 0 9) ; do
363             awk '{print $4}' /proc/partitions | grep -q md$i || \
364             echo "/dev/md$i /dev/md$i"
365           done)
366
367 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
368 --menu "Which device do you want to use for ${RAIDLEVEL}?
369
370 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 \
371 $MD_LIST)
372 [ $? -eq 0 ] || bailout 20
373
374 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
375              sed 's/*//' | \
376              grep -v 'Extended$' | \
377              gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
378 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
379 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
380                      echo "$i $(vol_id --type $i 2>/dev/null || echo [no_filesystem_yet]) off"
381                  done)
382
383 dialog --title "$PN" --separate-output \
384        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
385        $PARTITION_LIST 2>$TMPFILE
386 RETVAL=$?
387 SELECTED_PARTITIONS="$(cat $TMPFILE)"
388
389 NUM_PARTITIONS=0
390 for i in $(cat $TMPFILE) ; do
391    NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
392 done
393
394 ERRORFILE=$(mktemp)
395 set +e
396 # TODO: better error handling?
397 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
398       --raid-devices="${NUM_PARTITIONS}" ${SELECTED_PARTITIONS} 1>/dev/null 2>$ERRORFILE
399 RC=$?
400 set -e
401
402 if [ "$RC" = 0 ] ; then
403    dialog --title "$PN" --msgbox \
404    "Creating $TARGET was successful." 0 0
405    rm -f "$TMPFILE" "$ERRORFILE"
406 else
407    dialog --title "$PN" --msgbox \
408    "There was an error setting up $TARGET:
409
410 $(cat $ERRORFILE)
411
412 Exiting." 0 0
413    rm -f "$TMPFILE" "$ERRORFILE"
414    exit 1
415 fi
416
417 }
418
419 prompt_for_swraid()
420 {
421 if dialog --stdout --title "$PN" \
422           --defaultno --yesno "Do you want to configure Software RAID?
423
424 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
425   config_swraid_setup
426 fi
427 }
428 # }}}
429
430 # make sure the user is aware of the used configuration {{{
431 checkconfiguration()
432 {
433 if [ -n "$AUTOINSTALL" ] ; then
434    if checkforrun ; then
435       eerror "Exiting as requested" ; eend 0
436       exit 1
437    fi
438 elif [ -n "$INTERACTIVE" ] ; then
439
440    INFOTEXT="Please recheck configuration before execution:
441    "
442    [ -n "$TARGET" ]  && INFOTEXT="$INFOTEXT
443    Target:          $TARGET"
444    [ -n "$GRUB" ]    && INFOTEXT="$INFOTEXT
445    Install grub:    $GRUB"
446    [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
447    Using release:   $RELEASE"
448    [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
449    Using hostname   $HOSTNAME"
450    [ -n "$MIRROR" ]  && INFOTEXT="$INFOTEXT
451    Using mirror:    $MIRROR"
452
453    INFOTEXT="$INFOTEXT
454
455 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
456
457    dialog --title "$PN" --no-collapse \
458           --yesno "$INFOTEXT" 0 0
459
460 else # if not running automatic installation display configuration and prompt for execution:
461    einfo "$PN - Please recheck configuration before execution:"
462    echo
463    echo "   Target:          $TARGET"
464       case "$MNTPOINT" in "$TARGET") ;; *) echo "   Mount point:     $MNTPOINT" ;; esac
465       [ -n "$GRUB" ]    && echo "   Install grub:    $GRUB" || echo "   Install grub:    no"
466       [ -n "$RELEASE" ] && echo "   Using release:   $RELEASE"
467       [ -n "$MIRROR" ]  && echo "   Using mirror:    $MIRROR"
468       [ -n "$ISO" ]     && echo "   Using iso:       $ISO"
469       case "$MNTPOINT" in "$TARGET") ;; *) echo "   Important! Continuing will delete all data from ${TARGET}!" ;; esac
470       echo
471    einfon "Is this ok for you? [y/N] "
472    read a
473    if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
474       eerror "Exiting as requested." ; eend 1
475       exit 1
476    fi
477 fi
478 }
479 # }}}
480
481 # interactive mode {{{
482 interactive_mode()
483 {
484   welcome_dialog
485
486   prompt_for_swraid
487
488   # do not prompt for partition dialog if swraid has been configured already
489   [ -n "$TARGET" ] || prompt_for_target
490
491   prompt_for_bootmanager
492
493   prompt_for_release
494
495   prompt_for_hostname
496
497   prompt_for_password
498
499   # use first disk of sw-raid for grub by default, install grub on
500   # all involved disks later on
501   if echo "$TARGET" | grep -q '/dev/md' ; then
502      if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
503         # use hdX and not hdX,Y for $GRUB
504         TMPDEVICE=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
505         GRUB="$(grubdevice ${TMPDEVICE%%[0-9]})" # like: hd0
506         GROOT="$(grubdevice ${TMPDEVICE})"       # like: hd0,0
507         echo "debug: GRUB = $GRUB - GROOT = $GROOT" >/tmp/debug # FIXME
508      fi
509   else
510      [ -n "$BOOT_PARTITION" ] && GRUB="$(grubdevice $BOOT_PARTITION)"
511      [ -n "$TARGET" ]         && GROOT="$(grubdevice $TARGET)"
512   fi
513
514   prompt_for_mirror
515
516   checkconfiguration
517 }
518
519 # run interactive mode if we didn't get the according configuration yet
520 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
521    # only target might be unset, so make sure the INTERACTIVE flag is set as well
522    INTERACTIVE=1
523    interactive_mode
524 fi
525 # }}}
526
527 # finally make sure at least $TARGET is set [the partition for the new system] {{{
528 if [ -n "$TARGET" ] ; then
529    SHORT_TARGET="${TARGET##*/}"
530 else
531    eerror "Please adjust $CONFFILES/config or..."
532    eerror "... use the interactive version for configuration before running ${0}" ; eend 1
533    exit 1
534 fi
535 # }}}
536
537 # stages setup {{{
538 if [ -z "$STAGES" ] ; then
539    STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
540    [ -d "$STAGES" ] || mkdir -p "$STAGES"
541 fi
542
543 if [ -r "$STAGES"/grml-debootstrap ] ; then
544    if grep -q done $STAGES/grml-debootstrap ; then
545       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
546       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
547    fi
548 fi
549 # }}}
550
551 # partition handling {{{
552 PARTITION=''
553 DIRECTORY=''
554
555 case $TARGET in
556   /dev/*)
557     PARTITION=1
558     ;;
559   *)
560     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
561     DIRECTORY=1
562     MNTPOINT="$TARGET"
563     MKFS=''
564     TUNE2FS=''
565     FSCK=''
566     GRUB=''
567     GROOT=''
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         einfo "Unmounting $MNTPOINT"   ; umount "$MNTPOINT" ; eend $?
613
614         if [ -n "$STAGES" ] ; then
615            echo -n "Removing stages directory ${STAGES}: "
616            rm -rf "$STAGES" && echo done
617         fi
618
619         # remove directory only if we used the default with process id inside the name
620         if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
621            einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
622         fi
623      fi
624   fi
625
626   [ -n "$1" ] && EXIT="$1" || EXIT="1"
627   [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
628
629   exit "$EXIT"
630 }
631 trap bailout HUP INT QUIT TERM
632
633 # we want to execute all the functions only once, simple check for it:
634 stage() {
635   if [ -n "$2" ] ; then
636      echo "$2" > "${STAGES}/${1}"
637      return 0
638   elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
639      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
640      eindent
641        ewarn "To reexecute it clean up the according directory inside $STAGES" ; eend 0
642      eoutdent
643      return 1
644   fi
645 }
646 # }}}
647
648 # user should recheck his configuration {{{
649 # support full automatic installation:
650 checkforrun() {
651    dialog --timeout 10 --title "$PN" \
652           --yesno "Do you want to stop at this stage?
653
654 Notice: you are running ${PN} in non-interactive mode.
655 ${PN} will install Debian ${RELEASE} on ${TARGET}.
656 Last chance to quit. Timeout of 10 seconds running....
657
658 Do you want to stop now?" 0 0 2>/dev/null
659 }
660
661 # create filesystem {{{
662 mkfs() {
663   if [ -n "$MKFS" ] ; then
664      einfo "Running $MKFS on $TARGET"
665      $MKFS $TARGET
666      TARGET_UUID="$(vol_id -u $TARGET 2>/dev/null || echo '')"
667      eend $?
668   fi
669 }
670 # }}}
671
672 # modify filesystem settings {{{
673 tunefs() {
674   if [ -n "$TUNE2FS" ] ; then
675      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
676      $TUNE2FS $TARGET
677      eend $?
678   fi
679 }
680 # }}}
681
682 # mount the new partition or if it's a directory do nothing at all {{{
683 mount_target() {
684   if [ -n "$DIRECTORY" ] ; then
685      einfo "Running grml-debootstrap on a directory, nothing to mount."
686   else
687      if grep -q $TARGET /proc/mounts ; then
688         eerror "$TARGET already mounted, exiting."
689      else
690        [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
691        einfo "Mounting $TARGET to $MNTPOINT"
692        mount -o rw,suid,dev $TARGET $MNTPOINT
693        eend $?
694      fi
695   fi
696   if [ -n "$ISODIR" ] ; then
697      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
698      mkdir -p "$MNTPOINT/$ISODIR"
699      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
700      eend $?
701   fi
702 }
703 # }}}
704
705 # install main chroot {{{
706 debootstrap_system() {
707   if ! grep -q $MNTPOINT /proc/mounts ; then
708      mount_target
709   fi
710   if [ "$_opt_nodebootstrap" ]; then
711      einfo "Skipping debootstrap as requested."
712      return
713   fi
714   if grep -q $MNTPOINT /proc/mounts || [ -n "$DIRECTORY" ] ; then
715      einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
716      [ -n "$MIRROR" ] && $DEBOOTSTRAP $ARCHCMD $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR || \
717      $DEBOOTSTRAP $ARCHCMD $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
718      eend $?
719   else
720      eerror "Error: $MNTPOINT not mounted, can not continue."
721      eend 1
722   fi
723 }
724 # }}}
725
726 # prepare chroot via chroot-script {{{
727 preparechroot() {
728   einfo "Preparing chroot system"
729
730   # provide variables to chroot system
731   CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
732   touch $CHROOT_VARIABLES
733   chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
734   echo "# Configuration of ${PN}"                              > $CHROOT_VARIABLES
735   [ -n "$ARCH" ]         && echo "ARCH=$ARCH"                 >> $CHROOT_VARIABLES
736   [ -n "$GROOT" ]        && echo "GROOT=$GROOT"               >> $CHROOT_VARIABLES
737   [ -n "$GRUB" ]         && echo "GRUB=$GRUB"                 >> $CHROOT_VARIABLES
738   [ -n "$HOSTNAME" ]     && echo "HOSTNAME=$HOSTNAME"         >> $CHROOT_VARIABLES
739   [ -n "$ISODIR" ]       && echo "ISODIR=$ISO"                >> $CHROOT_VARIABLES
740   [ -n "$ISO" ]          && echo "ISO=$ISO"                   >> $CHROOT_VARIABLES
741   [ -n "$MIRROR" ]       && echo "CHROOTMIRROR=$MIRROR"       >> $CHROOT_VARIABLES
742   [ -n "$MIRROR" ]       && echo "MIRROR=$MIRROR"             >> $CHROOT_VARIABLES
743   [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
744   [ -n "$PACKAGES" ]     && echo "PACKAGES=$PACKAGES"         >> $CHROOT_VARIABLES
745   [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
746   [ -n "$TARGET" ]       && echo "TARGET=$TARGET"             >> $CHROOT_VARIABLES
747   [ -n "$TARGET_UUID" ]  && echo "TARGET_UUID=$TARGET_UUID"   >> $CHROOT_VARIABLES
748
749   cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
750   chmod 755 $MNTPOINT/bin/chroot-script
751   mkdir $MNTPOINT/etc/debootstrap/
752
753   # make sure we have our files for later use via chroot-script
754   cp $VERBOSE $CONFFILES/config    $MNTPOINT/etc/debootstrap/
755   # make sure we adjust the configuration variables accordingly:
756   sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
757   sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#"    $MNTPOINT/etc/debootstrap/config
758   sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#"          $MNTPOINT/etc/debootstrap/config
759   sed -i "s#GROOT=.*#GROOT=\"$GROOT\"#"       $MNTPOINT/etc/debootstrap/config
760
761   # package selection:
762   cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
763     $MNTPOINT/etc/debootstrap/packages
764
765   # debconf preseeding:
766   _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
767   [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
768     cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
769
770   # copy scripts that should be executed inside the chroot:
771   _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot_scripts/}
772   [ -f $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && \
773     cp -a $VERBOSE $_opt_chroot_scripts $MNTPOINT/etc/debootstrap/
774
775   # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
776   cp $VERBOSE $CHROOT_VARIABLES  $MNTPOINT/etc/debootstrap/variables
777
778   cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
779
780   # make sure we can access network [relevant for cdebootstrap]
781   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
782
783   # provide system's /etc/hosts to the target:
784   if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
785      cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
786      sed -i "s#127.0.0.1 .*#127.0.0.1       localhost  $HOSTNAME#" /etc/hosts
787   fi
788
789   # setup default locales
790   [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen  $MNTPOINT/etc/locale.gen
791
792   # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
793   ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
794
795   # copy any existing files to chroot
796   [ -d $CONFFILES/bin   ] && cp $VERBOSE -a -L $CONFFILES/bin/*   $MNTPOINT/bin/
797   [ -d $CONFFILES/boot  ] && cp $VERBOSE -a -L $CONFFILES/boot/*  $MNTPOINT/boot/
798   [ -d $CONFFILES/etc   ] && cp $VERBOSE -a -L $CONFFILES/etc/*   $MNTPOINT/etc/
799   [ -d $CONFFILES/sbin  ] && cp $VERBOSE -a -L $CONFFILES/sbin/*  $MNTPOINT/sbin/
800   [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
801   [ -d $CONFFILES/usr   ] && cp $VERBOSE -a -L $CONFFILES/usr/*   $MNTPOINT/usr/
802   [ -d $CONFFILES/var   ] && cp $VERBOSE -a -L $CONFFILES/var/*   $MNTPOINT/var/
803
804   # copy local network setup to chroot
805   if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
806      [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
807      cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
808   fi
809
810   eend 0
811 }
812 # }}}
813
814 # execute all scripts in /etc/debootstrap/scripts/ {{{
815 execute_scripts() {
816    if [ -f $_opt_scripts -a "$SCRIPTS" = 'yes' ] ; then
817       for script in /etc/debootstrap/scripts/* ; do
818          if [ -x "$script" ] ; then
819             einfo "Executing script $script"
820             $script ; eend $?
821          fi
822       done
823    fi
824 }
825 # }}}
826
827 # execute chroot-script {{{
828 chrootscript() {
829   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
830      mount_target
831   fi
832   if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
833      einfo "Executing chroot-script now"
834      chroot "$MNTPOINT" /bin/chroot-script
835      eend $?
836   else
837      eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
838      eend 1
839   fi
840 }
841 # }}}
842
843 # install booloader grub {{{
844 grub_install() {
845   if [ -z "$GRUB" -o -z "$GROOT" ] ; then
846      echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
847   elif [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
848      for device in $SELECTED_PARTITIONS ; do
849         # TMPDEVICE=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}')
850         # GRUB="$(grubdevice ${TMPDEVICE})"
851         # GRUB="$(grubdevice ${TMPDEVICE%%[0-9]})"
852         # GRUB=$(grubdevice $device)
853         GRUB="$(grubdevice ${device%%[0-9]})"
854         einfo "Installing grub on ${GRUB}:"
855         [ -x /usr/sbin/grub-install ] && GRUBINSTALL="/usr/sbin/grub-install --no-floppy" || GRUBINSTALL="/sbin/grub-install --no-floppy"
856         $GRUBINSTALL --root-directory="$MNTPOINT" "(${GRUB})"
857         eend $?
858      done
859   else
860      einfo "Installing grub on ${GRUB}:"
861      [ -x /usr/sbin/grub-install ] && GRUBINSTALL="/usr/sbin/grub-install --no-floppy" || GRUBINSTALL="/sbin/grub-install --no-floppy"
862      $GRUBINSTALL --root-directory="$MNTPOINT" "(${GRUB})"
863      eend $?
864   fi
865 }
866 # }}}
867
868 # unmount $MNTPOINRT {{{
869 umount_chroot() {
870   if [ -n "$ISODIR" ] ; then
871      if grep -q "$ISODIR" /proc/mounts ; then
872         einfo "Unmount $MNTPOINT/$ISODIR"
873         umount "$MNTPOINT/$ISODIR"
874         eend $?
875      fi
876   fi
877   if grep -q "$MNTPOINT" /proc/mounts ; then
878      if [ -n "$PARTITION" ] ; then
879         einfo "Unmount $MNTPOINT"
880         umount $MNTPOINT
881         eend $?
882      fi
883   fi
884 }
885 # }}}
886
887 # execute filesystem check {{{
888 fscktool() {
889   if [ "$FSCK" = 'yes' ] ; then
890      [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
891      einfo "Checking filesystem on $TARGET using $FSCKTOOL"
892      $FSCKTOOL $TARGET
893      eend $?
894   fi
895 }
896 # }}}
897
898 # now execute all the functions {{{
899 for i in mkfs tunefs mount_target debootstrap_system preparechroot \
900          chrootscript execute_scripts grub_install umount_chroot   \
901          fscktool ; do
902     if stage "${i}" ; then
903        $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
904     fi
905 done
906 # }}}
907
908 # finalize {{{
909 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
910 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
911
912 # Remove temporary mountpoint again
913 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
914    einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
915 fi
916 # }}}
917
918 # end dialog of autoinstallation {{{
919 if [ -n "$AUTOINSTALL" ] ; then
920    dialog --title "$PN" --msgbox \
921           "Finished execution of ${PN}. Enjoy your Debian system." 0 0
922 else
923    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
924 fi
925 # }}}
926
927 ## END OF FILE #################################################################
928 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3