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