dbf975943f8a0544018a041cfbe0f26ee30f08bc
[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.30'
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='http://cdn.debian.net/debian'
332      MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
333                "Please enter Debian mirror you would like to use for installing packages." \
334                0 0 $MIRROR)"
335   else # CHROOT_VARIABLES == local
336      [ -n "$ISO" ] || ISO='/mnt/mirror'
337      ISO="$(dialog --stdout --title "${PN}" --inputbox \
338                "Please enter directory name you would like to use for installing packages." \
339                0 0 $ISO)"
340   fi
341 }
342 # }}}
343
344 # 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=$(mktemp)
354
355   # create device.map
356   if which grub >/dev/null 2>&1 ; then
357      echo 'quit' | grub --device-map="$device_map" >/dev/null 2>&1
358   elif which grub-mkdevicemap >/dev/null 2>&1 ; then
359      grub-mkdevicemap --device-map="$device_map" >/dev/null 2>&1
360   else
361     echo "Error: neither grub nor grub-mkdevicemap command found." >&2
362     return 1
363   fi
364
365   # based on code from d-i's trunk/packages/arch/i386/grub-installer/grub-installer:
366   tmp_disk=$(echo "$device" | sed -e 's%\(\(s\|h\|v\|xv\)d[a-z]\)[0-9]*$%\1%' \
367                     -e 's%\(fd[0-9]*\)$%\1%' \
368                     -e 's%/part[0-9]*$%/disc%' \
369                     -e 's%\(c[0-7]d[0-9]*\).*$%\1%' \
370                     -e 's%\(mmcblk[0-9]d[0-9]*\).*$%\1%' \
371                     -e 's%\(/mapper/mpath[0-9]\+\)-part[0-9]\+$%\1%')
372   tmp_part=$(echo "$device" | sed -e 's%.*/\(s\|h\|v\|xv\)d[a-z]\([0-9]*\)$%\2%' \
373                     -e 's%.*/fd[0-9]*$%%' \
374                     -e 's%.*/floppy/[0-9]*$%%' \
375                     -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
376                     -e 's%.*c[0-7]d[0-9]*p*%%' \
377                     -e 's%.*mmcblk[0-9]d[0-9]*p%%' \
378                     -e 's%.*/mapper/mpath[0-9]\+-part\([0-9]\+\)%\1%')
379   tmp_drive=$(grep -v '^#' $device_map | grep "$tmp_disk *$" | sed 's%.*\([hf]d[0-9][a-g0-9,]*\).*%\1%')
380
381   if [ -z "$tmp_drive" ]; then
382     echo "Error: $device does not have any corresponding BIOS drive." >&2
383     return 1
384   fi
385
386   case $1 in
387      /dev/[sh]d[a-z]) # we expect something like 'hd0'
388         echo "$tmp_drive"
389         ;;
390       *) # we expect something like 'hd0,0'
391         echo "$tmp_drive" | sed "s%$%,$(expr $tmp_part - 1)%" # FIXME => md0
392         ;;
393   esac
394   rm -f "$device_map"
395 }
396 # }}}
397
398 # software raid setup {{{
399 config_swraid_setup()
400 {
401 TMPFILE=$(mktemp)
402
403 # Currently we support only raid1:
404 RAIDLEVEL='raid1'
405
406 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
407 #                   --menu "Which RAID level do you want to use?" 0 0 0 \
408 #                     raid1 "Software RAID level 1" \
409 #                     raid5 "Software RAID level 5")
410 #[ $? -eq 0 ] || bailout 20
411
412 MD_LIST=$(for i in $(seq 0 9) ; do
413             awk '{print $4}' /proc/partitions | grep -q md$i || \
414             echo "/dev/md$i /dev/md$i"
415           done)
416
417 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
418 --menu "Which device do you want to use for ${RAIDLEVEL}?
419
420 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 \
421 $MD_LIST)
422 [ $? -eq 0 ] || bailout 20
423
424 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
425              sed 's/*//' | \
426              grep -v 'Extended$' | \
427              gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
428 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
429 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
430                      echo "$i $(vol_id --type $i 2>/dev/null || echo [no_filesystem_yet]) off"
431                  done)
432
433 dialog --title "$PN" --separate-output \
434        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
435        $PARTITION_LIST 2>$TMPFILE
436 RETVAL=$?
437 SELECTED_PARTITIONS="$(cat $TMPFILE)"
438
439 NUM_PARTITIONS=0
440 for i in $(cat $TMPFILE) ; do
441    NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
442 done
443
444 ERRORFILE=$(mktemp)
445 set +e
446 # TODO: better error handling?
447 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
448       --raid-devices="${NUM_PARTITIONS}" ${SELECTED_PARTITIONS} 1>/dev/null 2>$ERRORFILE
449 RC=$?
450 set -e
451
452 if [ "$RC" = 0 ] ; then
453    dialog --title "$PN" --msgbox \
454    "Creating $TARGET was successful." 0 0
455    rm -f "$TMPFILE" "$ERRORFILE"
456 else
457    dialog --title "$PN" --msgbox \
458    "There was an error setting up $TARGET:
459
460 $(cat $ERRORFILE)
461
462 Exiting." 0 0
463    rm -f "$TMPFILE" "$ERRORFILE"
464    exit 1
465 fi
466
467 }
468
469 prompt_for_swraid()
470 {
471 if dialog --stdout --title "$PN" \
472           --defaultno --yesno "Do you want to configure Software RAID?
473
474 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
475   config_swraid_setup
476 fi
477 }
478 # }}}
479
480 # user should recheck his configuration {{{
481 # support full automatic installation:
482 checkforrun() {
483    dialog --timeout 10 --title "$PN" \
484           --yesno "Do you want to stop at this stage?
485
486 Notice: you are running ${PN} in non-interactive mode.
487 ${PN} will install Debian ${RELEASE} on ${TARGET}.
488 Last chance to quit. Timeout of 10 seconds running....
489
490 Do you want to stop now?" 0 0 2>/dev/null
491 }
492
493 # make sure the user is aware of the used configuration {{{
494 checkconfiguration()
495 {
496 if [ -n "$AUTOINSTALL" ] ; then
497    if checkforrun ; then
498       eerror "Exiting as requested" ; eend 0
499       exit 1
500    fi
501 elif [ -n "$INTERACTIVE" ] ; then
502
503    INFOTEXT="Please recheck configuration before execution:
504    "
505    [ -n "$TARGET" ]  && INFOTEXT="$INFOTEXT
506    Target:          $TARGET"
507    [ -n "$GRUB" ]    && INFOTEXT="$INFOTEXT
508    Install grub:    $GRUB"
509    [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
510    Using release:   $RELEASE"
511    [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
512    Using hostname   $HOSTNAME"
513    [ -n "$MIRROR" ]  && INFOTEXT="$INFOTEXT
514    Using mirror:    $MIRROR"
515    [ -n "$ISO" ]  && INFOTEXT="$INFOTEXT
516    Using ISO:       $ISO"
517
518    INFOTEXT="$INFOTEXT
519
520 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
521
522    dialog --title "$PN" --no-collapse \
523           --yesno "$INFOTEXT" 0 0
524
525 else # if not running automatic installation display configuration and prompt for execution:
526    einfo "$PN - Please recheck configuration before execution:"
527    echo
528    echo "   Target:          $TARGET"
529
530    # do not display if MNTPOINT is the default one
531    case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo "   Mount point:     $MNTPOINT" ;; esac
532
533    [ -n "$GRUB" ]     && echo "   Install grub:    $GRUB" || echo "   Install grub:    no"
534    [ -n "$RELEASE" ]  && echo "   Using release:   $RELEASE"
535    [ -n "$MIRROR" ]   && echo "   Using mirror:    $MIRROR"
536    [ -n "$HOSTNAME" ] && echo "   Using hostname:  $HOSTNAME"
537    [ -n "$ISO" ]      && echo "   Using ISO:       $ISO"
538
539    echo "   Important! Continuing will delete all data from ${TARGET}!"
540
541    echo
542    einfon "Is this ok for you? [y/N] "
543    read a
544    if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
545       eerror "Exiting as requested." ; eend 1
546       exit 1
547    fi
548 fi
549 }
550 # }}}
551
552 # interactive mode {{{
553 interactive_mode()
554 {
555   welcome_dialog
556
557   prompt_for_swraid
558
559   prompt_for_target
560
561   prompt_for_bootmanager
562
563   prompt_for_release
564
565   prompt_for_hostname
566
567   prompt_for_password
568
569   # use first disk of sw-raid for grub by default, install grub on
570   # all involved disks later on
571   if echo "$TARGET" | grep -q '/dev/md' ; then
572      if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
573         # use hdX and not hdX,Y for $GRUB
574         TMPDEVICE=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
575         GRUB="$(grubdevice ${TMPDEVICE%%[0-9]})" # like: hd0
576         GROOT="$(grubdevice ${TMPDEVICE})"       # like: hd0,0
577      fi
578   else
579      [ -n "$BOOT_PARTITION" ] && GRUB="$(grubdevice $BOOT_PARTITION)"
580      [ -n "$TARGET" ]         && GROOT="$(grubdevice $TARGET)"
581   fi
582
583   prompt_for_mirror
584 }
585
586 # run interactive mode if we didn't get the according configuration yet
587 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
588    # only target might be unset, so make sure the INTERACTIVE flag is set as well
589    INTERACTIVE=1
590    interactive_mode
591 fi
592 # }}}
593
594 checkconfiguration
595
596 # finally make sure at least $TARGET is set [the partition for the new system] {{{
597 if [ -n "$TARGET" ] ; then
598    SHORT_TARGET="${TARGET##*/}"
599 else
600    eerror "Please adjust $CONFFILES/config or..."
601    eerror "... use the interactive version for configuration before running ${0}" ; eend 1
602    exit 1
603 fi
604 # }}}
605
606 # stages setup {{{
607 if [ -z "$STAGES" ] ; then
608    STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
609    [ -d "$STAGES" ] || mkdir -p "$STAGES"
610 fi
611
612 if [ -r "$STAGES"/grml-debootstrap ] ; then
613    if grep -q done $STAGES/grml-debootstrap ; then
614       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
615       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
616    fi
617 fi
618 # }}}
619
620 # partition handling {{{
621 PARTITION=''
622 DIRECTORY=''
623
624 case $TARGET in
625   /dev/*)
626     PARTITION=1
627     ;;
628   *)
629     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
630     DIRECTORY=1
631     MNTPOINT="$TARGET"
632     MKFS=''
633     TUNE2FS=''
634     FSCK=''
635     GRUB=''
636     GROOT=''
637     ;;
638 esac
639 # }}}
640
641 # architecture setup {{{
642 if [ -n "$ARCH" ] ; then
643    ARCHCMD="--arch $ARCH"
644    ARCHINFO=" (${ARCH})"
645 else
646    ARCH="$(dpkg --print-architecture)"
647    ARCHCMD="--arch $ARCH"
648    ARCHINFO=" (${ARCH})"
649 fi
650 # }}}
651
652 # make sure we have the right syntax when using an iso image {{{
653 if [ -n "$ISO" ] ; then
654    case $ISO in
655       file*) # do nothing
656       ;;
657       *)
658       ISO=file:$1
659       ;;
660    esac
661 fi
662 ISODIR=${ISO##file:}
663 ISODIR=${ISODIR%%/}
664 # }}}
665
666 # helper functions {{{
667 # we want to exit smoothly and clean:
668 bailout(){
669   # make sure $TARGET is not mounted when exiting grml-debootstrap
670   if [ -n "$MNTPOINT" ] ; then
671      if grep -q $MNTPOINT /proc/mounts ; then
672         # make sure nothing is left inside chroot so we can unmount it
673         [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
674         [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
675         # ugly, but make sure we really don't leav anything
676         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /sys  1>/dev/null 2>&1
677         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a    1>/dev/null 2>&1
678         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
679         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
680         [ -d "$MNTPOINT/$ISODIR" ]    && umount "$MNTPOINT/$ISODIR" 1>/dev/null 2>&1
681
682         if [ -n "$DIRECTORY" ] ; then
683           einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
684         else
685           einfo "Unmounting $MNTPOINT"   ; umount "$MNTPOINT" ; eend $?
686         fi
687
688         if [ -n "$STAGES" ] ; then
689            echo -n "Removing stages directory ${STAGES}: "
690            rm -rf "$STAGES" && echo done
691         fi
692
693         # remove directory only if we used the default with process id inside the name
694         if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
695            einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
696         fi
697      fi
698   fi
699
700   [ -n "$1" ] && EXIT="$1" || EXIT="1"
701   [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
702
703   exit "$EXIT"
704 }
705 trap bailout HUP INT QUIT TERM
706
707 # we want to execute all the functions only once, simple check for it:
708 stage() {
709   if [ -n "$2" ] ; then
710      echo "$2" > "${STAGES}/${1}"
711      return 0
712   elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
713      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
714      eindent
715        ewarn "To reexecute it clean up the according directory inside $STAGES" ; eend 0
716      eoutdent
717      return 1
718   fi
719 }
720 # }}}
721
722 # create filesystem {{{
723 mkfs() {
724   if [ -n "$DIRECTORY" ] ; then
725      einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
726   else
727     if grep -q "$TARGET" /proc/mounts ; then
728       eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
729       exit 1
730     fi
731
732     if [ -n "$MKFS" ] ; then
733        einfo "Running $MKFS on $TARGET"
734        $MKFS $TARGET
735        TARGET_UUID="$(vol_id -u $TARGET 2>/dev/null || echo '')"
736        eend $?
737     fi
738
739   fi
740 }
741 # }}}
742
743 # modify filesystem settings {{{
744 tunefs() {
745   if [ -n "$TUNE2FS" ] ; then
746      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
747      $TUNE2FS $TARGET
748      eend $?
749   fi
750 }
751 # }}}
752
753 # mount the new partition or if it's a directory do nothing at all {{{
754 mount_target() {
755   if [ -n "$DIRECTORY" ] ; then
756      einfo "Running grml-debootstrap on a directory, nothing to mount."
757   else
758      if grep -q $TARGET /proc/mounts ; then
759         ewarn "$TARGET already mounted, continuing anyway." ; eend 0
760      else
761        [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
762        einfo "Mounting $TARGET to $MNTPOINT"
763        mount -o rw,suid,dev $TARGET $MNTPOINT
764        eend $?
765      fi
766   fi
767   if [ -n "$ISODIR" ] ; then
768      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
769      mkdir -p "$MNTPOINT/$ISODIR"
770      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
771      eend $?
772   fi
773 }
774 # }}}
775
776 # install main chroot {{{
777 debootstrap_system() {
778   if [ "$_opt_nodebootstrap" ]; then
779      einfo "Skipping debootstrap as requested."
780      return
781   fi
782
783   if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
784      einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
785      if [ -n "$MIRROR" ] ; then
786         $DEBOOTSTRAP $ARCHCMD $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
787      else
788         $DEBOOTSTRAP $ARCHCMD $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
789      fi
790      eend $?
791   else
792      eerror "Error: $MNTPOINT not mounted, can not continue."
793      eend 1
794   fi
795 }
796 # }}}
797
798 # prepare chroot via chroot-script {{{
799 preparechroot() {
800   einfo "Preparing chroot system"
801
802   # provide variables to chroot system
803   CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
804   touch $CHROOT_VARIABLES
805   chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
806   echo "# Configuration of ${PN}"                              > $CHROOT_VARIABLES
807   [ -n "$ARCH" ]         && echo "ARCH=$ARCH"                 >> $CHROOT_VARIABLES
808   [ -n "$GROOT" ]        && echo "GROOT=$GROOT"               >> $CHROOT_VARIABLES
809   [ -n "$GRUB" ]         && echo "GRUB=$GRUB"                 >> $CHROOT_VARIABLES
810   [ -n "$HOSTNAME" ]     && echo "HOSTNAME=$HOSTNAME"         >> $CHROOT_VARIABLES
811   [ -n "$ISODIR" ]       && echo "ISODIR=$ISO"                >> $CHROOT_VARIABLES
812   [ -n "$ISO" ]          && echo "ISO=$ISO"                   >> $CHROOT_VARIABLES
813   [ -n "$MIRROR" ]       && echo "MIRROR=$MIRROR"             >> $CHROOT_VARIABLES
814   [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
815   [ -n "$PACKAGES" ]     && echo "PACKAGES=$PACKAGES"         >> $CHROOT_VARIABLES
816   [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
817   [ -n "$TARGET" ]       && echo "TARGET=$TARGET"             >> $CHROOT_VARIABLES
818   [ -n "$TARGET_UUID" ]  && echo "TARGET_UUID=$TARGET_UUID"   >> $CHROOT_VARIABLES
819
820   cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
821   chmod 755 $MNTPOINT/bin/chroot-script
822   [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
823
824   # make sure we have our files for later use via chroot-script
825   cp $VERBOSE $CONFFILES/config    $MNTPOINT/etc/debootstrap/
826   # make sure we adjust the configuration variables accordingly:
827   sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
828   sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#"    $MNTPOINT/etc/debootstrap/config
829   sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#"          $MNTPOINT/etc/debootstrap/config
830   sed -i "s#GROOT=.*#GROOT=\"$GROOT\"#"       $MNTPOINT/etc/debootstrap/config
831
832   # package selection:
833   cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
834     $MNTPOINT/etc/debootstrap/packages
835
836   # debconf preseeding:
837   _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
838   [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
839     cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
840
841   # copy scripts that should be executed inside the chroot:
842   _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
843   [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
844     mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
845     cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
846   }
847
848   # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
849   cp $VERBOSE $CHROOT_VARIABLES  $MNTPOINT/etc/debootstrap/variables
850
851   cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
852
853   # make sure we can access network [relevant for cdebootstrap]
854   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
855
856   # provide system's /etc/hosts to the target:
857   if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
858      cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
859   fi
860
861   # setup default locales
862   [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen  $MNTPOINT/etc/locale.gen
863
864   # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
865   ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
866
867   # copy any existing files to chroot
868   [ -d $CONFFILES/bin   ] && cp $VERBOSE -a -L $CONFFILES/bin/*   $MNTPOINT/bin/
869   [ -d $CONFFILES/boot  ] && cp $VERBOSE -a -L $CONFFILES/boot/*  $MNTPOINT/boot/
870   [ -d $CONFFILES/etc   ] && cp $VERBOSE -a -L $CONFFILES/etc/*   $MNTPOINT/etc/
871   [ -d $CONFFILES/sbin  ] && cp $VERBOSE -a -L $CONFFILES/sbin/*  $MNTPOINT/sbin/
872   [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
873   [ -d $CONFFILES/usr   ] && cp $VERBOSE -a -L $CONFFILES/usr/*   $MNTPOINT/usr/
874   [ -d $CONFFILES/var   ] && cp $VERBOSE -a -L $CONFFILES/var/*   $MNTPOINT/var/
875
876   # copy local network setup to chroot
877   if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
878      [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
879      cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
880   fi
881
882   eend 0
883 }
884 # }}}
885
886 # execute all scripts in /etc/debootstrap/scripts/ {{{
887 execute_scripts() {
888    # make sure we have $MNTPOINT available for our scripts
889    export MNTPOINT
890    if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
891       [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
892       for script in ${scripts}/* ; do
893          if [ -x "$script" ] ; then
894             einfo "Executing script $script"
895             $script ; eend $?
896          fi
897       done
898    fi
899 }
900 # }}}
901
902 # execute chroot-script {{{
903 chrootscript() {
904   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
905      mount_target
906   fi
907   if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
908      einfo "Executing chroot-script now"
909      chroot "$MNTPOINT" /bin/chroot-script
910      eend $?
911   else
912      eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
913      eend 1
914   fi
915 }
916 # }}}
917
918 # install booloader grub {{{
919 grub_install() {
920   if [ -z "$GRUB" -o -z "$GROOT" ] ; then
921      echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
922   elif [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
923      for device in $SELECTED_PARTITIONS ; do
924         # TMPDEVICE=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}')
925         # GRUB="$(grubdevice ${TMPDEVICE})"
926         # GRUB="$(grubdevice ${TMPDEVICE%%[0-9]})"
927         # GRUB=$(grubdevice $device)
928         GRUB="$(grubdevice ${device%%[0-9]})"
929         einfo "Installing grub on ${GRUB}:"
930         [ -x /usr/sbin/grub-install ] && GRUBINSTALL="/usr/sbin/grub-install --no-floppy" || GRUBINSTALL="/sbin/grub-install --no-floppy"
931         $GRUBINSTALL --root-directory="$MNTPOINT" "(${GRUB})"
932         eend $?
933      done
934   else
935      einfo "Installing grub on ${GRUB}:"
936      [ -x /usr/sbin/grub-install ] && GRUBINSTALL="/usr/sbin/grub-install --no-floppy" || GRUBINSTALL="/sbin/grub-install --no-floppy"
937      $GRUBINSTALL --root-directory="$MNTPOINT" "(${GRUB})"
938      eend $?
939   fi
940 }
941 # }}}
942
943 # unmount $MNTPOINRT {{{
944 umount_chroot() {
945   if [ -n "$ISODIR" ] ; then
946      if grep -q "$ISODIR" /proc/mounts ; then
947         einfo "Unmount $MNTPOINT/$ISODIR"
948         umount "$MNTPOINT/$ISODIR"
949         eend $?
950      fi
951   fi
952
953   if grep -q "$MNTPOINT" /proc/mounts ; then
954      if [ -n "$PARTITION" ] ; then
955         einfo "Unmount $MNTPOINT"
956         umount $MNTPOINT
957         eend $?
958      fi
959   fi
960 }
961 # }}}
962
963 # execute filesystem check {{{
964 fscktool() {
965   if [ "$FSCK" = 'yes' ] ; then
966      [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
967      einfo "Checking filesystem on $TARGET using $FSCKTOOL"
968      $FSCKTOOL $TARGET
969      eend $?
970   fi
971 }
972 # }}}
973
974 # now execute all the functions {{{
975 for i in mkfs tunefs mount_target debootstrap_system preparechroot \
976          chrootscript execute_scripts grub_install umount_chroot   \
977          fscktool ; do
978     if stage "${i}" ; then
979        $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
980     fi
981 done
982 # }}}
983
984 # finalize {{{
985 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
986 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
987
988 # Remove temporary mountpoint again
989 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
990    einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
991 fi
992 # }}}
993
994 # end dialog of autoinstallation {{{
995 if [ -n "$AUTOINSTALL" ] ; then
996    dialog --title "$PN" --msgbox \
997           "Finished execution of ${PN}. Enjoy your Debian system." 0 0
998 else
999    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1000 fi
1001 # }}}
1002
1003 ## END OF FILE #################################################################
1004 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3