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