bac2cd51fab2e919a36ea064630b6549c53c29da
[grml-debootstrap.git] / grml-debootstrap
1 #!/bin/bash
2 # Filename:      grml-debootstrap
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 # variables {{{
11 PN="$(basename $0)"
12 VERSION="$(dpkg --list $PN 2>/dev/null| awk '/^i/ {print $3}')"
13 VERSION="${VERSION:-unknown}"
14 MNTPOINT="/mnt/debootstrap.$$"
15
16 # defaults
17 CHROOT_SCRIPTS='yes'
18 CONFFILES='/etc/debootstrap'
19 DEBCONF='yes'
20 DEBIAN_FRONTEND='noninteractive'
21 DEBOOTSTRAP='debootstrap'
22 EXTRAPACKAGES='yes'
23 FORCE=''
24 HOSTNAME='grml'
25 INITRD='yes'
26 INSTALL_NOTES='/etc/debootstrap/install_notes'
27 LOCALES='yes'
28 MIRROR='http://cdn.debian.net/debian'
29 MKFS='mkfs.ext3'
30 PACKAGES='yes'
31 PRE_SCRIPTS='yes'
32 RECONFIGURE='console-data'
33 RELEASE='squeeze'
34 RM_APTCACHE='yes'
35 SCRIPTS='yes'
36 SECURE='yes'
37 TIMEZONE='Europe/Vienna'
38 TUNE2FS='tune2fs -c0 -i0'
39 UPGRADE_SYSTEM='yes'
40 VMSIZE="2G"
41
42 # inside the chroot system locales might not be available, so use minimum:
43 export LANG=C
44 export LC_ALL=C
45
46 # make sure interactive mode is only executed when
47 # using an empty configuration file or option --interactive
48 INTERACTIVE=''
49 # }}}
50
51 # help text {{{
52 usage() {
53   echo "$PN - wrapper around debootstrap for installing Debian
54
55 Usage: $PN [options]
56
57 Bootstrap options:
58
59   -m, --mirror <URL>     Mirror which should be used for apt-get/aptitude.
60   -i, --iso <mnt>        Mountpoint where a Debian ISO is mounted to, for use
61                          instead of fetching packages from a mirror.
62   -r, --release <name>   Release of new Debian system (default: squeeze).
63   -t, --target <target>  Target partition (/dev/...) or directory where the
64                          system should be installed to.
65   -p, --mntpoint <mnt>   Mountpoint used for mounting the target system,
66                          has no effect if -t is given and represents a directory.
67       --debopt <params>  Extra parameters passed to the debootstrap command.
68       --interactive      Use interactive mode (frontend).
69       --nodebootstrap    Skip debootstrap, only do configuration to the target.
70       --grub <device>    Target for grub installation. Usage example: /dev/sda
71       --arch <arch>      Set target architecture, use for installing i386 on amd64.
72       --filesystem <fs>  Filesystem that should be used when target is a partition
73                          or Virtual Machine (see --vmfile).
74       --force            Do not prompt for user acknowledgement.
75
76 Options for Virtual Machine deployment:
77
78       --vmfile           Set up a Virtual Machine instead of installing to
79                          a partition or directory, to be combined with --target,
80                          like: --vmfile --target /mnt/sda1/qemu.img
81       --vmsize <size>    Use specified size for size of VM file (default: 2G).
82                          Syntax as supported by qemu-img, like: --vmsize 3G
83
84 Configuration options:
85
86   -c, --config <file>      Use specified configuration file, defaults to
87                              /etc/debootstrap/config
88   -d, --confdir <path>     Place of config files for debootstrap, defaults
89                              to /etc/debootstrap
90       --packages <file>    Install packages defined in specified list file
91                              instead of using /etc/debootstrap/packages.
92       --nopackages         Skip installation of packages defined in
93                              /etc/debootstrap/packages
94       --debconf <file>     Pre-seed packages using specified pre-seed db file.
95       --grmlrepos          Enable Grml's Debian repository (deb.grml.org).
96       --keep_src_list      Do not overwrite user provided apt sources.list.
97       --hostname <name>    Hostname of Debian system.
98       --nopassword         Do not prompt for the root password.
99       --password <pwd>     Use specified password as password for user root.
100       --bootappend <line>  Add specified appendline to kernel whilst booting.
101       --chroot-scripts <d> Execute chroot scripts from specified directory.
102       --pre-scripts <dir>  Execute scripts from specified directory (before chroot-scripts).
103       --scripts <dir>      Execute scripts from specified directory (after chroot-scripts).
104
105 Other options:
106
107   -v, --verbose            Increase verbosity.
108   -h, --help               Print this usage information and exit.
109   -V, --version            Show summary of options and exit.
110
111 Usage examples can be found in the grml-debootstrap manpage.
112 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
113 "
114 }
115
116 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
117    usage
118    echo 'Please notice that this script requires root permissions!'
119    exit 0
120 fi
121 # }}}
122
123 # early helper functions {{{
124 GOOD='\e[32;01m'
125 WARN='\e[33;01m'
126 BAD='\e[31;01m'
127 NORMAL='\e[0m'
128 HILITE='\e[36;01m'
129 BRACKET='\e[34;01m'
130
131 einfo() {
132   einfon "$1\n"
133   return 0
134 }
135
136 einfon() {
137   [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
138   printf " ${GOOD}*${NORMAL} $*"
139   LAST_E_CMD=einfon
140   return 0
141 }
142
143 eerror() {
144   [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
145   printf " ${BAD}*${NORMAL} $*\n" >&2
146   LAST_E_CMD=eerror
147   return 0
148 }
149
150 eend() {
151   local retval="${1:-0}"
152   shift
153   if [ $retval -gt 0 ]; then
154     printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
155   fi
156   return $retval
157 }
158
159 check4root(){
160   if [ "$(id -u 2>/dev/null)" != 0 ] ; then
161     echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
162   fi
163 }
164
165 check4progs(){
166   local RC=''
167   for arg in $* ; do
168     which $arg >/dev/null 2>&1 || RC="$arg"
169   done
170   if [ -n "$RC" ] ; then
171      echo "$RC not installed"
172      return 1
173   fi
174 }
175 # }}}
176
177 # helper functions {{{
178 cleanup() {
179   if [ -n "$CHROOT_VARIABLES" ] ; then
180     einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
181   fi
182
183   if [ -n "$STAGES" ] ; then
184     einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
185   fi
186
187   # Remove temporary mountpoint again
188   if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
189     rmdir "$MNTPOINT" 2>/dev/null
190   fi
191
192   # make sure $TARGET is not mounted when exiting grml-debootstrap
193   if [ -n "$MNTPOINT" ] ; then
194     if grep -q "$MNTPOINT" /proc/mounts ; then
195       # make sure nothing is left inside chroot so we can unmount it
196       [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
197       [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
198
199       [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a >/dev/null 2>&1
200
201       # ugly, but make sure we really don't leave anything (/proc /proc and
202       # /dev /dev are intended, trying to work around timing issues, see #657023)
203       for ARG in /sys /proc /proc /dev /dev ; do
204         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1
205         umount "$MNTPOINT"/$ARG >/dev/null 2>&1
206       done
207
208       if [ -n "$ISODIR" ] ; then
209         [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1
210       fi
211
212       if [ -n "$DIRECTORY" ] ; then
213         einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
214       else
215         einfo "Unmounting $MNTPOINT"
216         umount "$MNTPOINT"
217         eend $?
218       fi
219
220       if [ -n "$STAGES" ] ; then
221         echo -n "Removing stages directory ${STAGES}: "
222         rm -rf "$STAGES" && echo done
223       fi
224
225       # remove directory only if we used the default with process id inside the name
226       if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
227         einfo "Removing directory ${MNTPOINT}"
228         rmdir "$MNTPOINT"
229         eend $?
230       fi
231     fi
232   fi
233
234   if [ -n "${ORIG_TARGET}" ] ; then
235     einfo "Removing loopback mount of file ${ORIG_TARGET}."
236     kpartx -d "${ORIG_TARGET}" ; eend $?
237   fi
238 }
239
240 # we want to exit smoothly and clean:
241 bailout(){
242
243   cleanup
244
245   [ -n "$1" ] && EXIT="$1" || EXIT="1"
246   [ -n "$2" ] && einfo "Notice: remove $STAGES/$2 to reexecute the stage"
247
248   exit "$EXIT"
249 }
250 trap bailout HUP INT QUIT TERM
251
252 # we want to execute all the functions only once, simple check for it:
253 stage() {
254   if [ -n "$2" ] ; then
255      echo "$2" > "${STAGES}/${1}"
256      return 0
257   elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
258      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
259      ewarn "  To reexecute it clean up the according directory inside $STAGES" ; eend 0
260      return 1
261   fi
262 }
263 # }}}
264
265 # make sure we have what we need {{{
266 check4progs debootstrap || bailout 1
267 # }}}
268
269 # source main configuration file {{{
270 if [ -r /etc/debootstrap/config ] ; then
271   . /etc/debootstrap/config
272 fi
273 # }}}
274
275 # cmdline handling {{{
276 # source external command line parameter-processing script
277 if [ -r ./cmdlineopts.clp ] ; then
278    . ./cmdlineopts.clp
279 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
280    . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
281 else
282    eerror "Error: cmdline function file not found, exiting."
283    eend 1
284    bailout 1
285 fi
286
287 # == business-logic of command line parameter-processing
288
289 # source configuration file in <confdir> if supplied. {{{
290 [ "$_opt_confdir" ] && {
291   CONFFILES=$_opt_confdir
292   einfo "Using config files under $CONFFILES/."
293   if ! [ -r "$CONFFILES/config" ] ; then
294     eerror "Error: config file $CONFFILES/config not found."; eend 1; bailout 1
295   fi
296   if ! . "$CONFFILES/config" ; then
297     eerror "Error reading config file $CONFFILES/config" ; eend 1 ; bailout 1
298   fi
299   # restore the command line parameter value
300   CONFFILES=$_opt_confdir
301 }
302 # }}}
303
304 [ "$_opt_mirror" ]              && MIRROR=$_opt_mirror
305 [ "$_opt_iso" ]                 && ISO=$_opt_iso
306 [ "$_opt_release" ]             && RELEASE=$_opt_release
307 [ "$_opt_target" ]              && TARGET=$_opt_target
308 [ "$_opt_vmfile" ]              && VIRTUAL=1
309 [ "$_opt_vmsize" ]              && VMSIZE=$_opt_vmsize
310 [ "$_opt_mntpoint" ]            && MNTPOINT=$_opt_mntpoint
311 [ "$_opt_debopt" ]              && DEBOOTSTRAP_OPT=$_opt_debopt
312 [ "$_opt_interactive" ]         && INTERACTIVE=1
313 [ "$_opt_config" ]              && CONFIGFILE=$_opt_config
314 [ "$_opt_filesystem" ]          && MKFS="mkfs.$_opt_filesystem"
315 [ "$_opt_packages_set" ]        && PACKAGES='yes'
316 [ "$_opt_nopackages" ]          && PACKAGES=''
317 [ "$_opt_debconf_set" ]         && DEBCONF='yes'
318 [ "$_opt_scripts_set" ]         && SCRIPTS='yes'
319 [ "$_opt_pre_scripts_set" ]     && PRE_SCRIPTS='yes'
320 [ "$_opt_chroot_scripts_set" ]  && CHROOT_SCRIPTS='yes'
321 [ "$_opt_keep_src_list" ]       && KEEP_SRC_LIST='yes'
322 [ "$_opt_grmlrepos" ]           && GRMLREPOS='yes'
323 [ "$_opt_hostname" ]            && HOSTNAME=$_opt_hostname
324 [ "$_opt_password" ]            && ROOTPASSWORD=$_opt_password
325 [ "$_opt_nopassword" ]          && NOPASSWORD='yes'
326 [ "$_opt_bootappend" ]          && BOOT_APPEND=$_opt_bootappend
327 [ "$_opt_grub" ]                && GRUB=$_opt_grub
328 [ "$_opt_arch" ]                && ARCH=$_opt_arch
329 [ "$_opt_insecure" ]            && echo "Warning: --insecure is deprecated, continuing anyway."
330 [ "$_opt_force" ]               && FORCE=$_opt_force
331 [ "$_opt_verbose" ]             && VERBOSE="-v"
332
333 [ "$_opt_help" ] && {
334   usage ; eend 0
335   eend 0
336   exit 0
337 }
338
339 [ "$_opt_version" ] && {
340   einfo "$PN - version $VERSION"
341   einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
342   eend 0
343   exit 0
344 }
345 # }}}
346
347 # check for root permissions {{{
348 if ! check4root ; then
349    echo "For usage instructions please execute '$PN --help'."
350    bailout 1
351 fi
352 # }}}
353
354 # make sure we have what we need {{{
355 if [ -n "$VIRTUAL" ] ; then
356   check4progs kpartx mksh parted qemu-img || bailout 1
357 fi
358 # }}}
359
360 # source specified configuration file {{{
361 if [ -n "$CONFIGFILE" ] ; then
362    einfo "Reading specified config file $CONFIGFILE."
363    if ! . "$CONFIGFILE" ; then
364       eerror "Error reading config file $CONFIGFILE" ; eend 1 ; bailout 1
365    fi
366 fi
367 # }}}
368
369 # backwards compability checks {{{
370 if [ -n "$GROOT" ] ; then
371    eerror "Error: you seem to have \$GROOT configured."
372    eerror "This variable is no longer supported, please visit the"
373    eerror "grml-debootstrap documentation for details."
374    eend 1
375    bailout 1
376 fi
377
378 if echo "$GRUB" | grep -q '^hd' ; then
379    eerror "Error: this syntax for the grub configuration variable is no longer supported."
380    eerror "Please do not use hd... any longer but /dev/sdX instead."
381    eend 1
382    bailout 1
383 fi
384 # }}}
385
386 # welcome screen {{{
387 welcome_dialog()
388 {
389    dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
390 Do you want to continue installing Debian using this frontend?" 0 0 || bailout 0
391 }
392 # }}}
393
394 # ask for target {{{
395 prompt_for_target()
396 {
397   AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
398                sed 's/*//' | \
399                grep -v 'Extended$' | \
400                gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
401
402   if [ -z "$AVAILABLE_PARTITIONS" ] ; then
403      dialog --title "$PN" --trim \
404      --msgbox "Sorry, no partitions found. Please configure your
405      harddisks (see /proc/partitions) using a tool like fdisk,
406      cfdisk, gpart, gparted,..." 0 0
407      bailout 1
408   fi
409
410   PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
411                        echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
412                    done)
413
414   TARGET=$(dialog --title "$PN" --single-quoted --stdout \
415          --menu "Please select the target partition:" 0 0 0 \
416          $PARTITION_LIST)
417   [ $? -eq 0 ] || bailout 1
418 }
419 # }}}
420
421 # ask for bootmanager {{{
422 prompt_for_bootmanager()
423 {
424   ADDITIONAL_PARAMS=""
425
426   if echo "$TARGET" | grep -q "/dev/md" ; then
427      MBRPART="all disks of Software RAID $TARGET"
428   else
429      # figure out whole disk device
430      found=
431      for device in /dev/disk/by-id/*
432      do
433         [ $(readlink -f $device) = ${TARGET} ] || continue
434         found=1
435         break
436      done
437      [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
438      if [ -e "$MBRDISK" ]; then
439         MBRDISK=$(readlink -f $MBRDISK)
440      else
441         # fall back to old behaviour
442         MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
443      fi
444
445      MBRPART="MBR of $MBRDISK"
446   fi
447
448   for device in cciss/c0d0 sda hda; do
449     if [ /dev/$device != ${MBRDISK} ]; then
450       grep -q $device /proc/partitions && \
451       ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
452     fi
453   done
454   ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
455
456   OIFS="$IFS"; IFS=:
457
458   GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
459           --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
460             mbr       "install bootmanager into $MBRPART" \
461             nowhere   "do not install bootmanager at all" \
462           ${ADDITIONAL_PARAMS})
463   [ $? -eq 0 ] || bailout 3
464   IFS="$OIFS"
465
466   case "$GETMBR" in
467     mbr)
468       # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
469       if echo "$TARGET" | grep -q "/dev/md" ; then
470          # using sw-raid:
471          if [ -n "$SELECTED_PARTITIONS" ] ; then
472             GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
473          else
474             GRUB="$TARGET"
475          fi
476       else
477         GRUB="$MBRDISK"
478       fi
479       ;;
480     hda)
481       GRUB="/dev/hda"
482       ;;
483     sda)
484       GRUB="/dev/sda"
485       ;;
486     nowhere)
487       GRUB=''
488       ;;
489   esac
490 }
491 # }}}
492
493 # ask for Debian release {{{
494 prompt_for_release()
495 {
496   [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='squeeze'
497   RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
498             "Please enter the Debian release you would like to use for installation:" \
499             0 50 4 \
500             lenny    Debian/old-stable \
501             squeeze  Debian/stable \
502             wheezy   Debian/testing \
503             sid      Debian/unstable)"
504   [ $? -eq 0 ] || bailout
505 }
506 # }}}
507
508 # ask for hostname {{{
509 prompt_for_hostname()
510 {
511   HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
512             "Please enter the hostname you would like to use for installation:" \
513             0 0 $HOSTNAME)"
514   [ $? -eq 0 ] || bailout
515 }
516 # }}}
517
518 # ask for password {{{
519 prompt_for_password()
520 {
521   if [ "$_opt_nopassword" ] ; then
522     einfo "Skip asking for root password as requested."
523     return 0
524   fi
525
526   ROOTPW1='PW1'
527   ROOTPW2='PW2'
528   while [ "$ROOTPW1" != "$ROOTPW2" ]; do
529     ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
530     "Please enter the password for the root account:" 10 60)
531     [ $? -eq 0 ] || bailout
532     ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
533     "Please enter the password for the root account again for \
534     confirmation:" 10 60)
535     [ $? -eq 0 ] || bailout
536
537     if [ "$ROOTPW1" != "$ROOTPW2" ]; then
538       $(dialog --stdout --title "${PN}" --ok-label \
539       "Retry" --msgbox "Passwords do not match!" 10 60)
540     fi
541   done
542   ROOTPASSWORD="$ROOTPW1"
543 }
544 # }}}
545
546 # ask for Debian mirror {{{
547 prompt_for_mirror()
548 {
549   [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
550
551   CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
552           --menu "Where do you want to install from?" 0 0 0 \
553             net   "install via network (downloading from mirror)" \
554             local "install from local directory/mirror"
555           )
556   [ $? -eq 0 ] || bailout
557
558   if [ "$CHOOSE_MIRROR" = 'net' ] ; then
559      [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
560      MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
561                "Please enter Debian mirror you would like to use for installing packages." \
562                0 0 $MIRROR)"
563      [ $? -eq 0 ] || bailout
564   else # CHOOSE_MIRROR == local
565      [ -n "$ISO" ] || ISO='/mnt/mirror'
566      ISO="$(dialog --stdout --title "${PN}" --inputbox \
567                "Please enter directory name you would like to use for installing packages." \
568                0 0 $ISO)"
569      [ $? -eq 0 ] || bailout
570   fi
571 }
572 # }}}
573
574 # software raid setup {{{
575 config_swraid_setup()
576 {
577 TMPFILE=$(mktemp)
578
579 # Currently we support only raid1:
580 RAIDLEVEL='raid1'
581
582 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
583 #                   --menu "Which RAID level do you want to use?" 0 0 0 \
584 #                     raid1 "Software RAID level 1" \
585 #                     raid5 "Software RAID level 5")
586 #[ $? -eq 0 ] || bailout 20
587
588 MD_LIST=$(for i in $(seq 0 9) ; do
589             awk '{print $4}' /proc/partitions | grep -q md$i || \
590             echo "/dev/md$i /dev/md$i"
591           done)
592
593 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
594 --menu "Which device do you want to use for ${RAIDLEVEL}?
595
596 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 \
597 $MD_LIST)
598 [ $? -eq 0 ] || bailout 20
599
600 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
601              sed 's/*//' | \
602              grep -v 'Extended$' | \
603              gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
604 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
605 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
606                      echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
607                  done)
608
609 dialog --title "$PN" --separate-output \
610        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
611        $PARTITION_LIST 2>$TMPFILE
612 [ $? -eq 0 ] || bailout
613 RETVAL=$?
614 SELECTED_PARTITIONS="$(cat $TMPFILE)"
615
616 NUM_PARTITIONS=0
617 for i in $(cat $TMPFILE) ; do
618    NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
619 done
620
621 # force metadata version 0.90 for lenny so old grub can boot from this array.
622 METADATA_VERSION=""
623 if [ $RELEASE = "lenny" ]; then
624    METADATA_VERSION="-e0"
625 fi
626
627 ERRORFILE=$(mktemp)
628 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
629       --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
630 RC=$?
631 if [ "$RC" = 0 ] ; then
632    dialog --title "$PN" --msgbox \
633    "Creating $TARGET was successful." 0 0
634    rm -f "$TMPFILE" "$ERRORFILE"
635 else
636    dialog --title "$PN" --msgbox \
637    "There was an error setting up $TARGET:
638
639 $(cat $ERRORFILE)
640
641 Exiting." 0 0
642    rm -f "$TMPFILE" "$ERRORFILE"
643    bailout 1
644 fi
645
646 }
647
648 prompt_for_swraid()
649 {
650 if dialog --stdout --title "$PN" \
651           --defaultno --yesno "Do you want to configure Software RAID?
652
653 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
654   config_swraid_setup
655 fi
656 }
657 # }}}
658
659 # user should recheck his configuration {{{
660 # support full automatic installation:
661 checkforrun() {
662    dialog --timeout 10 --title "$PN" \
663           --yesno "Do you want to stop at this stage?
664
665 Notice: you are running ${PN} in non-interactive mode.
666 ${PN} will install Debian ${RELEASE} on ${TARGET}.
667 Last chance to quit. Timeout of 10 seconds running....
668
669 Do you want to stop now?" 0 0 2>/dev/null
670 }
671 # }}}
672
673 # make sure the user is aware of the used configuration {{{
674 checkconfiguration()
675 {
676 if [ -n "$AUTOINSTALL" ] ; then
677    if checkforrun ; then
678       eerror "Exiting as requested" ; eend 0
679       bailout 1
680    fi
681 elif [ -n "$INTERACTIVE" ] ; then
682
683    INFOTEXT="Please recheck configuration before execution:
684    "
685    [ -n "$TARGET" ]  && INFOTEXT="$INFOTEXT
686    Target:          $TARGET"
687    [ -n "$GRUB" ]    && INFOTEXT="$INFOTEXT
688    Install grub:    $GRUB"
689    [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
690    Using release:   $RELEASE"
691    [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
692    Using hostname:  $HOSTNAME"
693    [ -n "$MIRROR" ]  && INFOTEXT="$INFOTEXT
694    Using mirror:    $MIRROR"
695    [ -n "$ISO" ]  && INFOTEXT="$INFOTEXT
696    Using ISO:       $ISO"
697    [ -n "$ARCH" ]  && INFOTEXT="$INFOTEXT
698    Using arch:      $ARCH"
699
700    INFOTEXT="$INFOTEXT
701
702 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
703
704    dialog --title "$PN" --no-collapse \
705           --yesno "$INFOTEXT" 0 0
706    [ $? -eq 0 ] || bailout 0
707
708 else # if not running automatic installation display configuration and prompt for execution:
709    einfo "$PN [${VERSION}] - Please recheck configuration before execution:"
710    echo
711    echo "   Target:          $TARGET"
712
713    # do not display if MNTPOINT is the default one
714    case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo "   Mount point:     $MNTPOINT" ;; esac
715
716    if [ -n "$VIRTUAL" ] ; then
717       echo "   Install grub:    yes"
718    else
719      [ -n "$GRUB" ]     && echo "   Install grub:    $GRUB" || echo "   Install grub:    no"
720    fi
721
722    [ -n "$RELEASE" ]  && echo "   Using release:   $RELEASE"
723    [ -n "$HOSTNAME" ] && echo "   Using hostname:  $HOSTNAME"
724    [ -n "$MIRROR" ]   && echo "   Using mirror:    $MIRROR"
725    [ -n "$ISO" ]      && echo "   Using ISO:       $ISO"
726    [ -n "$ARCH" ]     && echo "   Using arch:      $ARCH"
727    if [ -n "$VIRTUAL" ] ; then
728       echo "   Deploying as Virtual Machine."
729       [ -n "$VMSIZE" ] && echo "   Using Virtual Disk file with size of ${VMSIZE}."
730    fi
731
732    echo
733    echo "   Important! Continuing will delete all data from ${TARGET}!"
734
735    if [ -n "$FORCE" ] ; then
736      einfo "Skip user acknowledgement as requested via --force option."
737    else
738      echo
739      einfon "Is this ok for you? [y/N] "
740      read a
741      if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
742         eerror "Exiting as requested." ; eend 1
743         bailout 1
744      fi
745    fi
746 fi
747 }
748 # }}}
749
750 # interactive mode {{{
751 interactive_mode()
752 {
753   check4progs dialog || bailout 1
754
755   welcome_dialog
756
757   prompt_for_release
758
759   prompt_for_swraid
760
761   prompt_for_target
762
763   prompt_for_bootmanager
764
765   prompt_for_hostname
766
767   prompt_for_password
768
769   prompt_for_mirror
770 }
771
772 # run interactive mode if we didn't get the according configuration yet
773 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
774    # only target might be unset, so make sure the INTERACTIVE flag is set as well
775    INTERACTIVE=1
776    interactive_mode
777 fi
778 # }}}
779
780 # architecture setup {{{
781 if [ -n "$ARCH" ] ; then
782    ARCHCMD="--arch $ARCH"
783    ARCHINFO=" (${ARCH})"
784 else
785    ARCH="$(dpkg --print-architecture)"
786    ARCHCMD="--arch $ARCH"
787    ARCHINFO=" (${ARCH})"
788 fi
789 # }}}
790
791 checkconfiguration
792
793 # finally make sure at least $TARGET is set [the partition for the new system] {{{
794 if [ -n "$TARGET" ] ; then
795    SHORT_TARGET="${TARGET##*/}"
796 else
797    eerror "Please adjust $CONFFILES/config or..."
798    eerror "... use the interactive version for configuration before running ${0}" ; eend 1
799    bailout 1
800 fi
801 # }}}
802
803 # stages setup {{{
804 if [ -z "$STAGES" ] ; then
805    STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
806    [ -d "$STAGES" ] || mkdir -p "$STAGES"
807 fi
808
809 if [ -r "$STAGES"/grml-debootstrap ] ; then
810    if grep -q done $STAGES/grml-debootstrap ; then
811       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
812       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
813    fi
814 fi
815 # }}}
816
817 # partition handling {{{
818 PARTITION=''
819 DIRECTORY=''
820
821 set_target_directory(){
822     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
823     DIRECTORY=1
824     MNTPOINT="$TARGET"
825     MKFS=''
826     TUNE2FS=''
827     FSCK=''
828     GRUB=''
829     # make sure we normalise the path to an absolute directory name so something like:
830     #  mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
831     # works
832     TARGET="$(readlink -f $TARGET)"
833 }
834
835 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
836     PARTITION=1
837 else
838     set_target_directory
839 fi
840 # }}}
841
842 # make sure we have the right syntax when using an iso image {{{
843 if [ -n "$ISO" ] ; then
844    case $ISO in
845       file*) # do nothing
846       ;;
847       *)
848       ISO=file:$ISO
849       ;;
850    esac
851 fi
852 ISODIR=${ISO##file:}
853 ISODIR=${ISODIR%%/}
854 # }}}
855
856 # create filesystem {{{
857 mkfs() {
858   if [ -n "$DIRECTORY" ] ; then
859      einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
860   else
861     if grep -q "$TARGET" /proc/mounts ; then
862       eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
863       bailout 1
864     fi
865
866     if [ -n "$MKFS" ] ; then
867        einfo "Running $MKFS on $TARGET"
868        $MKFS $TARGET ; RC=$?
869
870        # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
871        # will fail to detect the uuid in the chroot
872        if echo "$TARGET" | grep -q "/dev/md" ; then
873          blockdev --rereadpt "${TARGET}"
874        elif ! [ -n "$VIRTUAL" ] ; then
875          blockdev --rereadpt "${TARGET%%[0-9]*}"
876        fi
877        # give the system 2 seconds, otherwise we might run into
878        # race conditions :-/
879        sleep 2
880
881        eval $(blkid -o udev $TARGET 2>/dev/null)
882        [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
883
884        eend $RC
885     fi
886
887   fi
888 }
889 # }}}
890
891 # modify filesystem settings {{{
892 tunefs() {
893   if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
894      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
895      $TUNE2FS $TARGET
896      eend $?
897   fi
898 }
899 # }}}
900
901 # mount the new partition or if it's a directory do nothing at all {{{
902 mount_target() {
903   if [ -n "$DIRECTORY" ] ; then
904      einfo "Running grml-debootstrap on a directory, nothing to mount."
905   else
906      if grep -q $TARGET /proc/mounts ; then
907         ewarn "$TARGET already mounted, continuing anyway." ; eend 0
908      else
909        if ! [ -d "${MNTPOINT}" ] ; then
910           [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
911        fi
912        einfo "Mounting $TARGET to $MNTPOINT"
913        mkdir -p "$MNTPOINT"
914        mount -o rw,suid,dev $TARGET $MNTPOINT
915        eend $?
916      fi
917   fi
918   if [ -n "$ISODIR" ] ; then
919      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
920      mkdir -p "$MNTPOINT/$ISODIR"
921      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
922      eend $?
923   fi
924 }
925 # }}}
926
927 # prepare VM image for usage with debootstrap {{{
928 prepare_vm() {
929   if [ -z "$VIRTUAL" ] ; then
930      return 0 # be quite by intention
931   fi
932
933   if [ -b "$TARGET" ] ; then
934      eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
935      eend 1
936      bailout 1
937   fi
938
939   ORIG_TARGET="$TARGET" # store for later reuse
940
941   qemu-img create -f raw "${TARGET}" "${VMSIZE}"
942   echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
943   dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
944   parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
945
946   # if dm-mod isn't available then kpartx will fail with
947   # "Is device-mapper driver missing from kernel? [...]"
948   if ! kpartx -av $TARGET >/dev/null 2>&1 || ! grep -q device-mapper /proc/misc >/dev/null 2>&1 ; then
949     einfo "Device-mapper not ready yet, trying to load dm-mod module."
950     modprobe dm-mod ; eend $?
951   fi
952
953   # make sure loop module is present
954   if ! losetup -f >/dev/null 2>&1; then
955     einfo "Can not find a usable loop device, retrying after loading loop module."
956     modprobe loop
957     if losetup -f >/dev/null 2>&1; then
958       einfo "Found a usable loop device now, continuing."
959     else
960       eerror "Error finding usable loop device" ; eend 1
961       bailout 1
962     fi
963   fi
964
965   DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
966   if [ -z "${DEVINFO}" ] ; then
967     eerror "Error setting up loopback device." ; eend 1
968     bailout 1
969   fi
970
971   # hopefully this always works as expected
972   LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
973   BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/')   # '253:0'
974   LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
975   export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
976
977   blockdev --rereadpt "${LOOP}"
978
979   if [ -z "$TARGET" ] ; then
980      eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
981      bailout 1
982   fi
983 }
984 # }}}
985
986 # make VM image bootable and unmount it {{{
987 finalize_vm() {
988   if [ -z "${VIRTUAL}" ] ; then
989      return 0
990   fi
991
992   if ! mount "${TARGET}" "${MNTPOINT}" ; then
993     eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
994     bailout 1
995   fi
996
997   einfo "Installing Grub as bootloader."
998   mount -t proc none "${MNTPOINT}"/proc
999   mount -t sysfs none "${MNTPOINT}"/sys
1000   mount --bind /dev "${MNTPOINT}"/dev
1001
1002   mkdir -p "${MNTPOINT}/boot/grub"
1003   if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
1004      eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
1005      bailout 1
1006   fi
1007
1008   cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
1009   chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
1010   dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
1011   rm -f "${MNTPOINT}/tmp/core.img"
1012
1013   einfo "Updating grub configuration file."
1014   chroot "${MNTPOINT}" update-grub
1015
1016   umount "${MNTPOINT}"/proc
1017   umount "${MNTPOINT}"/sys
1018   umount "${MNTPOINT}"/dev
1019
1020   einfo "Adjusting grub.cfg for successful boot sequence."
1021   # ugly but needed to boot grub acordingly
1022   sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
1023   sed -i "s;root=[^ ]\+;root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
1024
1025   umount "${MNTPOINT}"
1026   kpartx -d "${ORIG_TARGET}" >/dev/null
1027 }
1028 # }}}
1029
1030 # install main chroot {{{
1031 debootstrap_system() {
1032   if [ "$_opt_nodebootstrap" ]; then
1033      einfo "Skipping debootstrap as requested."
1034      return
1035   fi
1036
1037   if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
1038     :
1039   else
1040     eerror "Error: $MNTPOINT not mounted, can not continue."
1041     eend 1 ; exit 1
1042   fi
1043
1044   einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
1045   if [ -n "$MIRROR" ] ; then
1046     einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
1047     $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
1048     RC=$?
1049   else
1050     einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
1051     $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
1052     RC=$?
1053   fi
1054
1055   if [ $RC -ne 0 ] ; then
1056     if [ -r "$MNTPOINT/debootstrap/debootstrap.log" ] && \
1057       [ -s "$MNTPOINT/debootstrap/debootstrap.log" ] ; then
1058       einfo "Presenting last ten lines of debootstrap.log:"
1059       tail -10 $MNTPOINT/debootstrap/debootstrap.log
1060       einfo "End of debootstrap.log"
1061     fi
1062   fi
1063
1064   eend $RC
1065 }
1066 # }}}
1067
1068 # prepare chroot via chroot-script {{{
1069 preparechroot() {
1070   einfo "Preparing chroot system"
1071
1072   # provide variables to chroot system
1073   CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
1074   touch $CHROOT_VARIABLES
1075   chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
1076   echo "# Configuration of ${PN}"                              > $CHROOT_VARIABLES
1077   [ -n "$ARCH" ]                && echo "ARCH=\"$ARCH\""                               >> $CHROOT_VARIABLES
1078   [ -n "$CHROOT_SCRIPTS" ]      && echo "CHROOT_SCRIPTS=\"$CHROOT_SCRIPTS\""           >> $CHROOT_VARIABLES
1079   [ -n "$CONFFILES" ]           && echo "CONFFILES=\"$CONFFILES\""                     >> $CHROOT_VARIABLES
1080   [ -n "$DEBCONF" ]             && echo "DEBCONF=\"$DEBCONF\""                         >> $CHROOT_VARIABLES
1081   [ -n "$DEBIAN_FRONTEND" ]     && echo "DEBIAN_FRONTEND=\"$DEBIAN_FRONTEND\""         >> $CHROOT_VARIABLES
1082   [ -n "$DEBOOTSTRAP" ]         && echo "DEBOOTSTRAP=\"$DEBOOTSTRAP\""                 >> $CHROOT_VARIABLES
1083   [ -n "$EXTRAPACKAGES" ]       && echo "EXTRAPACKAGES=\"$EXTRAPACKAGES\""             >> $CHROOT_VARIABLES
1084   [ -n "$FORCE" ]               && echo "FORCE=\"$FORCE\""                             >> $CHROOT_VARIABLES
1085   [ -n "$GRMLREPOS" ]           && echo "GRMLREPOS=\"$GRMLREPOS\""                     >> $CHROOT_VARIABLES
1086   [ -n "$GRUB" ]                && echo "GRUB=\"$GRUB\""                               >> $CHROOT_VARIABLES
1087   [ -n "$HOSTNAME" ]            && echo "HOSTNAME=\"$HOSTNAME\""                       >> $CHROOT_VARIABLES
1088   [ -n "$INITRD" ]              && echo "INITRD=\"$INITRD\""                           >> $CHROOT_VARIABLES
1089   [ -n "$INSTALL_NOTES" ]       && echo "INSTALL_NOTES=\"$INSTALL_NOTES\""             >> $CHROOT_VARIABLES
1090   [ -n "$ISODIR" ]              && echo "ISODIR=\"$ISO\""                              >> $CHROOT_VARIABLES
1091   [ -n "$ISO" ]                 && echo "ISO=\"$ISO\""                                 >> $CHROOT_VARIABLES
1092   [ -n "$KEEP_SRC_LIST" ]       && echo "KEEP_SRC_LIST=\"$KEEP_SRC_LIST\""             >> $CHROOT_VARIABLES
1093   [ -n "$LOCALES" ]             && echo "LOCALES=\"$LOCALES\""                         >> $CHROOT_VARIABLES
1094   [ -n "$MIRROR" ]              && echo "MIRROR=\"$MIRROR\""                           >> $CHROOT_VARIABLES
1095   [ -n "$MKFS" ]                && echo "MKFS=\"$MKFS\""                               >> $CHROOT_VARIABLES
1096   [ -n "$NOPASSWORD" ]          && echo "NOPASSWORD=\"true\""                          >> $CHROOT_VARIABLES
1097   [ -n "$PACKAGES" ]            && echo "PACKAGES=\"$PACKAGES\""                       >> $CHROOT_VARIABLES
1098   [ -n "$PRE_SCRIPTS" ]         && echo "PRE_SCRIPTS=\"$PRE_SCRIPTS\""                 >> $CHROOT_VARIABLES
1099   [ -n "$RECONFIGURE" ]         && echo "RECONFIGURE=\"$RECONFIGURE\""                 >> $CHROOT_VARIABLES
1100   [ -n "$RELEASE" ]             && echo "RELEASE=\"$RELEASE\""                         >> $CHROOT_VARIABLES
1101   [ -n "$RM_APTCACHE" ]         && echo "RM_APTCACHE=\"$RM_APTCACHE\""                 >> $CHROOT_VARIABLES
1102   [ -n "$ROOTPASSWORD" ]        && echo "ROOTPASSWORD=\"$ROOTPASSWORD\""               >> $CHROOT_VARIABLES
1103   [ -n "$SCRIPTS" ]             && echo "SCRIPTS=\"$SCRIPTS\""                         >> $CHROOT_VARIABLES
1104   [ -n "$SECURE" ]              && echo "SECURE=\"$SECURE\""                           >> $CHROOT_VARIABLES
1105   [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=\"$SELECTED_PARTITIONS\"" >> $CHROOT_VARIABLES
1106   [ -n "$TARGET" ]              && echo "TARGET=\"$TARGET\""                           >> $CHROOT_VARIABLES
1107   [ -n "$UPGRADE_SYSTEM" ]      && echo "UPGRADE_SYSTEM=\"$UPGRADE_SYSTEM\""           >> $CHROOT_VARIABLES
1108   [ -n "$TARGET_UUID" ]         && echo "TARGET_UUID=\"$TARGET_UUID\""                 >> $CHROOT_VARIABLES
1109   [ -n "$TIMEZONE" ]            && echo "TIMEZONE=\"$TIMEZONE\""                       >> $CHROOT_VARIABLES
1110   [ -n "$TUNE2FS" ]             && echo "TUNE2FS=\"$TUNE2FS\""                         >> $CHROOT_VARIABLES
1111   [ -n "$VMSIZE" ]              && echo "VMSIZE=\"$VMSIZE\""                           >> $CHROOT_VARIABLES
1112
1113   cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
1114   chmod 755 $MNTPOINT/bin/chroot-script
1115   [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1116
1117   # make sure we have our files for later use via chroot-script
1118   cp $VERBOSE $CONFFILES/config    $MNTPOINT/etc/debootstrap/
1119   # make sure we adjust the configuration variables accordingly:
1120   sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
1121   sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#"    $MNTPOINT/etc/debootstrap/config
1122   sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#"          $MNTPOINT/etc/debootstrap/config
1123
1124   # install notes:
1125   if [ -n "$INSTALL_NOTES" ] ; then
1126      [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
1127   fi
1128
1129   # package selection:
1130   cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
1131     $MNTPOINT/etc/debootstrap/packages
1132
1133   # debconf preseeding:
1134   _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1135   [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
1136     cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
1137
1138   # copy scripts that should be executed inside the chroot:
1139   _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1140   [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
1141     mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
1142     cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
1143   }
1144
1145   # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1146   cp $VERBOSE $CHROOT_VARIABLES  $MNTPOINT/etc/debootstrap/variables
1147
1148   cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
1149
1150   # make sure we can access network [relevant for cdebootstrap]
1151   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
1152
1153   # provide system's /etc/hosts to the target:
1154   if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1155      cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
1156   fi
1157
1158   # setup default locales
1159   [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen  $MNTPOINT/etc/locale.gen
1160
1161   # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1162   ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1163
1164   # copy any existing files to chroot
1165   [ -d $CONFFILES/bin   ] && cp $VERBOSE -a -L $CONFFILES/bin/*   $MNTPOINT/bin/
1166   [ -d $CONFFILES/boot  ] && cp $VERBOSE -a -L $CONFFILES/boot/*  $MNTPOINT/boot/
1167   [ -d $CONFFILES/etc   ] && cp $VERBOSE -a -L $CONFFILES/etc/*   $MNTPOINT/etc/
1168   [ -d $CONFFILES/sbin  ] && cp $VERBOSE -a -L $CONFFILES/sbin/*  $MNTPOINT/sbin/
1169   [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1170   [ -d $CONFFILES/usr   ] && cp $VERBOSE -a -L $CONFFILES/usr/*   $MNTPOINT/usr/
1171   [ -d $CONFFILES/var   ] && cp $VERBOSE -a -L $CONFFILES/var/*   $MNTPOINT/var/
1172
1173   # copy local network setup to chroot
1174   if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
1175      [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
1176      cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
1177   fi
1178
1179   # install config file providing some example entries
1180   if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1181      cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1182   fi
1183
1184   eend 0
1185 }
1186 # }}}
1187
1188 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1189 execute_pre_scripts() {
1190    # make sure we have $MNTPOINT available for our scripts
1191    export MNTPOINT
1192    if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1193       [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1194       for script in ${pre_scripts}/* ; do
1195          if [ -x "$script" ] ; then
1196             einfo "Executing pre-script $script"
1197             $script ; eend $?
1198          fi
1199       done
1200    fi
1201 }
1202 # }}}
1203
1204 # execute all scripts in /etc/debootstrap/scripts/ {{{
1205 execute_scripts() {
1206    # make sure we have $MNTPOINT available for our scripts
1207    export MNTPOINT
1208    if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1209       [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1210       for script in ${scripts}/* ; do
1211          if [ -x "$script" ] ; then
1212             einfo "Executing script $script"
1213             $script ; eend $?
1214          fi
1215       done
1216    fi
1217 }
1218 # }}}
1219
1220 # execute chroot-script {{{
1221 chrootscript() {
1222   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1223     mount_target
1224   fi
1225
1226   if ! [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1227     eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1228     eend 1
1229   else
1230     einfo "Executing chroot-script now"
1231     mount --bind /dev "$MNTPOINT"/dev
1232     chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1233     umount "$MNTPOINT"/dev
1234     eend $RC
1235   fi
1236
1237   # finally get rid of chroot-script again, there's no good reason to
1238   # keep it on the installed system
1239   if grep -q GRML_CHROOT_SCRIPT_MARKER "${MNTPOINT}/bin/chroot-script" ; then
1240     einfo "Removing chroot-script again"
1241     rm -f "${MNTPOINT}/bin/chroot-script"
1242     eend $?
1243   else
1244     einfo "Keeping chroot-script as string GRML_CHROOT_SCRIPT_MARKER could not be found"
1245     eend 0
1246   fi
1247 }
1248 # }}}
1249
1250 # unmount $MNTPOINT {{{
1251 umount_chroot() {
1252
1253   # display installation notes:
1254   if [ -n "$INSTALL_NOTES" ] ; then
1255      [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1256   fi
1257
1258   if [ -n "$ISODIR" ] ; then
1259      if grep -q "$ISODIR" /proc/mounts ; then
1260         einfo "Unmount $MNTPOINT/$ISODIR"
1261         umount "$MNTPOINT/$ISODIR"
1262         eend $?
1263      fi
1264   fi
1265
1266   if grep -q "$MNTPOINT" /proc/mounts ; then
1267      if [ -n "$PARTITION" ] ; then
1268         einfo "Unmount $MNTPOINT"
1269         umount $MNTPOINT
1270         eend $?
1271      fi
1272   fi
1273 }
1274 # }}}
1275
1276 # execute filesystem check {{{
1277 fscktool() {
1278  if [ -n "$VIRTUAL" ] ; then
1279    einfo "Skipping filesystem check because we deploy a virtual machine."
1280    return 0
1281  fi
1282
1283  if [ "$FSCK" = 'yes' ] ; then
1284    [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1285    einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1286    $FSCKTOOL $TARGET
1287    eend $?
1288  fi
1289 }
1290 # }}}
1291
1292 # now execute all the functions {{{
1293 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1294          preparechroot execute_pre_scripts chrootscript execute_scripts \
1295          umount_chroot finalize_vm fscktool ; do
1296     if stage "${i}" ; then
1297        $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "$i"
1298     fi
1299 done
1300
1301 cleanup
1302 # }}}
1303
1304 # end dialog of autoinstallation {{{
1305 if [ -n "$AUTOINSTALL" ] ; then
1306    if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1307 Automatically rebooting in 10 seconds.
1308
1309 Choose Cancel to skip rebooting." 10 60 10 ; then
1310      noeject noprompt reboot
1311   fi
1312 else
1313    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1314 fi
1315 # }}}
1316
1317 ## END OF FILE #################################################################
1318 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=2