packer: explicitly set Bash as default shell for Vagrant user
[grml-debootstrap.git] / chroot-script
1 #!/bin/bash
2 # Filename:      /etc/debootstrap/chroot-script
3 # Purpose:       script executed in chroot when installing Debian via grml-debootstrap
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 # GRML_CHROOT_SCRIPT_MARKER - do not remove this line unless you want to keep
9 # this script as /bin/chroot-script on your new installed system
10 ################################################################################
11
12 # error_handler {{{
13 if [ "$REPORT_TRAP_ERR" = "yes" ] || [ "$FAIL_TRAP_ERR" = "yes" ]; then
14    set -E
15    set -o pipefail
16    trap "error_handler" ERR
17 fi
18 # }}}
19
20 . /etc/debootstrap/config    || exit 1
21 . /etc/debootstrap/variables || exit 1
22
23 [ -r /proc/1 ] || mount -t proc none /proc
24 [ -r /sys/kernel ] || mount -t sysfs none /sys
25
26 # variable checks {{{
27
28 # use aptitude only if it's available
29 if [ -x /usr/bin/aptitude ] ; then
30    APTUPDATE="aptitude update $DPKG_OPTIONS"
31    # Debian ISOs do not contain signed Release files
32    if [ -n "$ISO" ] ; then
33       APTINSTALL="aptitude -y --allow-untrusted --without-recommends install $DPKG_OPTIONS"
34       APTUPGRADE="aptitude -y --allow-untrusted safe-upgrade $DPKG_OPTIONS"
35    else
36       APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
37       APTUPGRADE="aptitude -y safe-upgrade $DPKG_OPTIONS"
38    fi
39 else
40    APTINSTALL="apt-get -y --no-install-recommends install $DPKG_OPTIONS"
41    APTUPDATE="apt-get update $DPKG_OPTIONS"
42    APTUPGRADE="apt-get -y upgrade $DPKG_OPTIONS"
43 fi
44
45 if [ -z "$STAGES" ] ; then
46    STAGES='/etc/debootstrap/stages'
47    [ -d "$STAGES" ] || mkdir -p "$STAGES"
48 fi
49 # }}}
50
51 # helper functions {{{
52 stage() {
53   if [ -n "$2" ] ; then
54      echo "$2" > "$STAGES/$1"
55      return 0
56   elif grep -q 'done' "$STAGES/$1" 2>/dev/null ; then
57      echo "   [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
58      return 1
59   fi
60   echo "   Executing stage ${1}"
61   return 0
62 }
63
64 askpass() {
65   # read -s emulation for dash. result is in $resp.
66   set -o noglob
67   [ -t 0 ] && stty -echo
68   read resp
69   [ -t 0 ] && stty echo
70   set +o noglob
71 }
72 # }}}
73
74 # define chroot mirror {{{
75 chrootmirror() {
76   if [ -n "$KEEP_SRC_LIST" ] ; then
77     echo "KEEP_SRC_LIST has been set, skipping chrootmirror stage."
78     return
79   fi
80
81   if [ -z "$COMPONENTS" ] ; then
82     COMPONENTS='main'
83   fi
84   echo "Using repository components $COMPONENTS"
85
86   if [ -n "$ISO" ] ; then
87     echo "Adjusting sources.list for ISO (${ISO})."
88     echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
89
90     if [ -n "$MIRROR" ] ; then
91       echo "Adding mirror entry (${MIRROR}) to sources.list."
92       echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
93     fi
94   else
95     if [ -n "$MIRROR" ] ; then
96       echo "Adjusting sources.list for mirror (${MIRROR})."
97       echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
98     fi
99   fi
100
101   # LTS support
102   case "$RELEASE" in
103     squeeze)
104       if [ -n "$MIRROR" ] ; then
105         echo "Release matching $RELEASE - enabling LTS support in sources.list"
106         echo "deb $MIRROR ${RELEASE}-lts $COMPONENTS" >> /etc/apt/sources.list
107       fi
108       ;;
109   esac
110
111   # add security.debian.org:
112   case "$RELEASE" in
113     unstable|sid|lenny) ;;  # no security pool available
114     *)
115       echo "Adding security.debian.org to sources.list."
116       echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
117       ;;
118   esac
119 }
120 # }}}
121
122 # remove local chroot mirror {{{
123 remove_chrootmirror() {
124   if [ -n "$KEEP_SRC_LIST" ] ; then
125     echo "KEEP_SRC_LIST has been set, skipping remove_chrootmirror stage."
126     return
127   fi
128
129   if [ -n "$ISO" ] ; then
130     echo "Removing ISO (${ISO}) from sources.list."
131     TMP_ISO=$(echo "$ISO" |sed 's#/#\\/#g')
132     sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
133   else
134     if [ -n "$MIRROR" ] && echo "$MIRROR" | grep -q 'file:' ; then
135       echo "Removing local mirror (${MIRROR}) from sources.list."
136       TMP_MIRROR=$(echo "$MIRROR" |sed 's#/#\\/#g')
137       sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
138       echo "Adding fallback mirror entry (${FALLBACK_MIRROR}) to sources.list instead."
139       echo "deb $FALLBACK_MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
140     fi
141   fi
142 }
143 # }}}
144
145 # set up grml repository {{{
146 grmlrepos() {
147   if [ -n "$GRMLREPOS" ] ; then
148      # user might have provided their own apt sources.list
149      if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
150         cat >> /etc/apt/sources.list.d/grml.list << EOF
151 # grml: stable repository:
152   deb     http://deb.grml.org/ grml-stable  main
153   deb-src http://deb.grml.org/ grml-stable  main
154
155 # grml: testing/development repository:
156   deb     http://deb.grml.org/ grml-testing main
157   deb-src http://deb.grml.org/ grml-testing main
158 EOF
159      fi
160
161      if apt-get update $DPKG_OPTIONS; then
162        apt-get -y --allow-unauthenticated install grml-debian-keyring $DPKG_OPTIONS
163        apt-get update $DPKG_OPTIONS
164      else
165        # make sure we have the keys available for aptitude
166        gpg --keyserver subkeys.pgp.net --recv-keys 709BCE51568573EBC160E590F61E2E7CECDEA787
167        gpg --export 709BCE51568573EBC160E590F61E2E7CECDEA787 | apt-key add - || true # not yet sure
168        # why it's necessary, sometimes we get an error even though it works [mika]
169      fi
170
171      # make sure we install packages from Grml's pool only if not available
172      # from Debian!
173      if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
174         cat >> /etc/apt/preferences << EOF
175 // debian pool (default):
176 Package: *
177 Pin: release o=Debian
178 Pin-Priority: 996
179
180 // main grml-repository:
181 Package: *
182 Pin: origin deb.grml.org
183 Pin-Priority: 991
184 EOF
185      fi
186   fi
187 }
188 # }}}
189
190 # check available backports release version {{{
191 checkbackports() {
192   wget -q -O/dev/null "http://backports.debian.org/debian-backports/dists/${1}-backports/Release"
193 }
194 # }}}
195
196 # feature to provide Debian backports repos {{{
197 backportrepos() {
198     if [ -n "$BACKPORTREPOS" ] ; then
199         if ! checkbackports "$RELEASE" ; then
200             echo "Backports for ${RELEASE} are not available." >&2
201             exit 1
202         else
203             # user might have provided their own apt sources.list
204             if ! grep -q backports /etc/apt/sources.list.d/backports.list 2>/dev/null ; then
205                 cat >> /etc/apt/sources.list.d/backports.list << EOF
206 # debian backports: ${RELEASE}-backports repository:
207 deb     http://backports.debian.org/debian-backports ${RELEASE}-backports main
208 deb-src http://backports.debian.org/debian-backports ${RELEASE}-backports main
209 EOF
210             fi
211         fi
212     fi
213 }
214 # }}}
215
216 # set up kernel-img.conf {{{
217 kernelimg_conf() {
218   if ! [ -r /etc/kernel-img.conf ] ; then
219      echo "Setting up /etc/kernel-img.conf"
220      cat > /etc/kernel-img.conf << EOF
221 # Kernel Image management overrides
222 # See kernel-img.conf(5) for details
223 do_initrd = Yes
224 do_symlinks = Yes
225 EOF
226   fi
227 }
228 # }}}
229
230 # make sure services do not start up {{{
231 install_policy_rcd() {
232   if ! [ -r /usr/sbin/policy-rc.d ] ; then
233      export POLICYRCD=1
234      cat > /usr/sbin/policy-rc.d << EOF
235 #!/bin/sh
236 exit 101
237 EOF
238      chmod 775 /usr/sbin/policy-rc.d
239   fi
240 }
241 # }}}
242
243 # make sure we have an up2date system {{{
244 upgrade_system() {
245   if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
246     echo "Running update + upgrade"
247     $APTUPDATE
248     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
249   else
250     echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
251   fi
252 }
253
254 # }}}
255 # remove now useless apt cache {{{
256 remove_apt_cache() {
257   if [ "$RM_APTCACHE" = 'yes' ] ; then
258     echo "Cleaning apt cache."
259     apt-get clean $DPKG_OPTIONS
260   else
261     echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
262   fi
263 }
264 # }}}
265
266 # install additional packages {{{
267 packages() {
268   # Pre-seed the debconf database with answers. Each question will be marked
269   # as seen to prevent debconf from asking the question interactively.
270   [ -f /etc/debootstrap/debconf-selections ] && {
271     echo "Preseeding the debconf database, some lines might be skipped..."
272     debconf-set-selections < /etc/debootstrap/debconf-selections
273   }
274
275   if [ "$PACKAGES" = 'yes' ] ; then
276      if ! [ -r /etc/debootstrap/packages ] ; then
277        echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
278        exit 1
279      else
280        $APTUPDATE
281        DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
282      fi
283   fi
284 }
285 # }}}
286
287 # install extra packages {{{
288 extrapackages() {
289     if [ "$EXTRAPACKAGES" = 'yes' ] ; then
290         PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
291         if [ -n "$PACKAGELIST" ]; then
292             dpkg -i $PACKAGELIST
293             # run apt again to resolve any deps
294             DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
295         fi
296     fi
297 }
298 # }}}
299
300 # check if the specified Debian package exists
301 package_exists() {
302   output=$(apt-cache show "$1" 2>/dev/null)
303   [ -n "$output" ]
304   return $?
305 }
306
307
308 # determine the kernel version postfix
309 get_kernel_version() {
310   # do not override $KERNEL if set via config file
311   if [ -n "$KERNEL" ] ; then
312     echo "$KERNEL"
313     return 0
314   fi
315
316   local KARCH
317
318   case "$ARCH" in
319     i386)
320       case "$RELEASE" in
321         lenny|squeeze|wheezy) KARCH='686' ;;
322         # since jessie the linux-image-686 image doesn't exist any longer
323         *) KARCH='686-pae' ;;
324       esac
325       ;;
326     amd64)
327       KARCH='amd64'
328       ;;
329     *)
330       echo "Only i386 and amd64 are currently supported" >&2
331       return 1
332   esac
333
334   for KPREFIX in "" "2.6-" ; do  # iterate through the kernel prefixes,
335                                  # currently "" and "2.6-"
336     if package_exists linux-image-${KPREFIX}${KARCH} ; then
337       echo ${KPREFIX}${KARCH}
338       return 0
339     fi
340
341   done
342 }
343
344 # install kernel packages {{{
345 kernel() {
346   if [ -n "$NOKERNEL" ] ; then
347     echo "Skipping installation of kernel packages as requested via --nokernel"
348     return 0
349   fi
350
351   $APTUPDATE
352   KVER=$(get_kernel_version)
353   if [ -n "$KVER" ] ; then
354      # note: install busybox to be able to debug initramfs
355      KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free"
356      # only add firmware-linux if we have non-free as a component
357      if expr "$COMPONENTS" : '.*non-free' >/dev/null ; then
358        KERNELPACKAGES="$KERNELPACKAGES firmware-linux"
359      fi
360      DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
361   else
362      echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
363   fi
364 }
365 # }}}
366
367 # reconfigure packages {{{
368 reconfigure() {
369   if [ -n "$RECONFIGURE" ] ; then
370      for package in $RECONFIGURE ; do
371          if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
372            DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
373            echo "Warning: $package does not exist, can not reconfigure it."
374          fi
375      done
376   fi
377 }
378 # }}}
379
380 # set password of user root {{{
381 passwords()
382 {
383   if [ -n "$NOPASSWORD" ] ; then
384     echo "Skip setting root password as requested."
385     return 0
386   fi
387
388   echo "Activating shadow passwords."
389   shadowconfig on
390
391   CHPASSWD_OPTION=
392   if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
393      CHPASSWD_OPTION='-m'
394   fi
395
396   if [ -n "$ROOTPASSWORD" ] ; then
397      echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
398      export ROOTPASSWORD=''
399   else
400     a='1'
401     b='2'
402      echo "Setting password for user root:"
403      while [ "$a" != "$b" ] ; do
404        printf "Enter new UNIX password for user root: "
405        askpass
406        a="$resp"
407        unset resp
408        echo
409        printf "Retype new UNIX password for user root: "
410        askpass
411        b="$resp"
412        unset resp
413        echo
414        if [ "$a" != "$b" ] ; then
415          echo "Sorry, passwords do not match. Retry."
416          a='1'
417          b='2'
418        else
419          echo root:"$a" | chpasswd $CHPASSWD_OPTION
420          unset a
421          unset b
422        fi
423      done
424   fi
425 }
426 # }}}
427
428 # set up /etc/hosts {{{
429 hosts() {
430   if [ -f /etc/hosts ] ; then
431      sed -i "s#127.0.0.1 .*#127.0.0.1       localhost  $HOSTNAME#" /etc/hosts
432      [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
433   else
434      cat > /etc/hosts << EOF
435 127.0.0.1       localhost $HOSTNAME
436
437 #127.0.0.1       localhost
438 #127.0.1.1       $HOSTNAME.example.org $HOSTNAME
439
440 # The following lines are desirable for IPv6 capable hosts
441 #::1     ip6-localhost ip6-loopback $HOSTNAME
442 ::1     ip6-localhost ip6-loopback
443 fe00::0 ip6-localnet
444 ff00::0 ip6-mcastprefix
445 ff02::1 ip6-allnodes
446 ff02::2 ip6-allrouters
447 ff02::3 ip6-allhosts
448 EOF
449   fi
450 }
451 # }}}
452
453 # set default locales {{{
454 default_locales() {
455   if [ -n "$DEFAULT_LOCALES" ] ; then
456     if ! [ -x /usr/sbin/update-locale ] ; then
457       echo "Warning: update-locale executable not available (no locales package installed?)"
458       echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
459       return 0
460     fi
461
462     /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
463   fi
464 }
465 # }}}
466
467 # adjust timezone {{{
468 timezone() {
469   if [ -n "$TIMEZONE" ] ; then
470      echo "Adjusting /etc/localtime"
471      ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
472   fi
473 }
474 # }}}
475
476 # helper function for fstab() {{{
477 createfstab(){
478      echo "Setting up /etc/fstab"
479 if [ -n "$TARGET_UUID" ] ; then
480    echo "/dev/disk/by-uuid/${TARGET_UUID} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
481 else
482    echo "${TARGET} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
483 fi
484
485 if [ -n "$EFI" ] ; then
486   echo "UUID=$(blkid -o value -s UUID $EFI)  /boot/efi       vfat    umask=0077      0       1" >> /etc/fstab
487 fi
488
489 cat >> /etc/fstab << EOF
490 proc           /proc        proc    defaults                      0   0
491 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto                0   0
492 # some other examples:
493 # /dev/sda2       none         swap    sw,pri=0             0   0
494 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
495 # //1.2.3.4/pub   /smb/pub     cifs    user,noauto,uid=grml,gid=grml 0 0
496 # linux:/pub      /beer        nfs     defaults             0  0
497 # tmpfs           /tmp         tmpfs   size=300M            0  0
498 # /dev/sda5       none         swap    sw                   0  0
499 EOF
500 }
501 # }}}
502
503 # generate /etc/fstab {{{
504 fstab() {
505   # set up /etc/fstab if file is not present (cdebootstrap)
506   if [ ! -f /etc/fstab  ] ; then
507      createfstab
508   fi
509
510   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
511   if grep -q UNCONFIGURED /etc/fstab ; then
512      createfstab
513   fi
514 }
515 # }}}
516
517 # set up hostname {{{
518 hostname() {
519   if [ -n "$HOSTNAME" ] ; then
520      echo "Setting hostname to ${HOSTNAME}."
521      echo "$HOSTNAME" > /etc/hostname
522
523      # adjust postfix configuration
524      if [ -r /etc/postfix/main.cf ] ; then
525         # adjust hostname related options:
526         sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
527
528         # listen on loopback interface only:
529         sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
530         grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
531      fi
532      if [ -r /etc/mailname ] ; then
533         # adjust /etc/mailname
534         local etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
535         case "$HOSTNAME" in
536           *.*)
537             local mailname="$HOSTNAME"
538             ;;
539           *)
540             local mailname="${HOSTNAME}.${etc_mail_domain}"
541             ;;
542         esac
543         echo "Setting mailname to ${mailname}"
544         echo "$mailname" > /etc/mailname
545      fi
546   fi
547 }
548 # }}}
549
550 # generate initrd/initramfs {{{
551 initrd() {
552   # assume the first available kernel as our main kernel
553   KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
554   if [ -z "$KERNELIMG" ] ; then
555      echo 'No kernel image found, skipping initrd stuff.'>&2
556      return
557   fi
558
559   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
560
561   # generate initrd
562   if [ -n "$INITRD" ] ; then
563      echo "Generating initrd."
564      update-initramfs -c -t -k "$KERNELVER"
565   fi
566 }
567 # }}}
568
569 efi_setup() {
570   if [ -z "$EFI" ] ; then
571     return 0
572   fi
573
574   if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
575     echo "Notice: efi option set but no efibootmgr package, installing it therefore."
576     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
577   fi
578
579   mkdir -p /boot/efi
580   echo "Mounting $EFI on /boot/efi"
581   mount "$EFI" /boot/efi || return 1
582
583   echo "Invoking efibootmgr"
584   efibootmgr || return 1
585 }
586
587 # grub configuration/installation {{{
588 grub_install() {
589
590   if [ -z "$GRUB" ] ; then
591     echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
592     return 0
593   fi
594
595   efi_setup || return 1
596
597   if [ -n "$EFI" ] ; then
598     GRUB_PACKAGE=grub-efi-amd64
599   else
600     GRUB_PACKAGE=grub-pc
601   fi
602
603   # make sure this is pre-defined so we have sane settings for automated
604   # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
605   echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
606   echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect $GRUB" | debconf-set-selections
607
608   if ! dpkg --list ${GRUB_PACKAGE} 2>/dev/null | grep -q '^ii' ; then
609     echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
610     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL ${GRUB_PACKAGE}
611   fi
612
613   if ! [ -x "$(which grub-install)" ] ; then
614      echo "Error: grub-install not available. (Error while installing grub package?)" >&2
615      return 1
616   fi
617
618   if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
619      for device in $SELECTED_PARTITIONS ; do
620         GRUB="${device%%[0-9]}"
621         echo "Installing grub on ${GRUB}:"
622         grub-install --no-floppy "$GRUB"
623      done
624      rm -f /boot/grub/device.map
625   else
626      echo "Installing grub on ${GRUB}:"
627      case "$RELEASE" in
628        lenny|squeeze|wheezy)
629          grub-install --no-floppy "$(readlink -f "${GRUB}")"
630          rm -f /boot/grub/device.map
631          ;;
632        *)
633          echo "(hd0) ${GRUB}" > /boot/grub/device.map
634          grub-install "(hd0)"
635          rm /boot/grub/device.map
636          ;;
637      esac
638   fi
639
640   echo "Adjusting grub configuration for use on ${GRUB}."
641
642   # finally install grub
643   if [ -x /usr/sbin/update-grub ] ; then
644      UPDATEGRUB='/usr/sbin/update-grub'
645   elif [ -x /sbin/update-grub ] ; then
646      UPDATEGRUB='/sbin/update-grub'
647   else
648     echo "Error: update-grub not available, can not execute it." >&2
649     return 1
650   fi
651
652   mountpoint /boot/efi >/dev/null && umount /boot/efi
653
654   $UPDATEGRUB
655 }
656 # }}}
657
658 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
659 custom_scripts() {
660   [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
661
662   for script in /etc/debootstrap/chroot-scripts/* ; do
663       echo "Executing script $script"
664       $script && echo "done" || echo "failed"
665   done
666 }
667 # }}}
668
669 # make sure we don't have any running processes left {{{
670 services() {
671   for service in ssh mdadm mdadm-raid ; do
672     if [ -x /etc/init.d/"$service" ] ; then
673        /etc/init.d/"$service" stop || true
674     fi
675   done
676 }
677 # }}}
678
679 # unmount /proc and make sure nothing is left {{{
680 finalize() {
681   # make sure we don't leave any sensible data
682   rm -f /etc/debootstrap/variables
683
684   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
685
686   umount /sys >/dev/null 2>/dev/null || true
687   umount /proc >/dev/null 2>/dev/null || true
688 }
689 # }}}
690
691 # signal handler {{{
692 signal_handler() {
693   finalize
694   [ -n "$1" ] && EXIT="$1" || EXIT="1"
695   exit "$EXIT"
696 }
697 # }}}
698
699 # set signal handler {{{
700 trap signal_handler HUP INT QUIT TERM
701 # }}}
702
703 # execute the functions {{{
704
705  # always execute install_policy_rcd
706  install_policy_rcd
707
708  for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
709      kernel packages extrapackages reconfigure hosts \
710      default_locales timezone fstab hostname initrd grub_install passwords \
711      custom_scripts upgrade_system remove_apt_cache services \
712      remove_chrootmirror; do
713      if stage $i ; then
714        $i && stage $i 'done' || exit 1
715      fi
716   done
717   # always execute the finalize stage:
718   finalize
719 # }}}
720
721 # finally exit the chroot {{{
722   echo "Finished chroot installation, exiting."
723   exit 0
724 # }}}
725
726 ## END OF FILE #################################################################
727 # vim: ai tw=80 expandtab foldmethod=marker