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