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