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