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