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