Drop cmdlineopts.sh references from build system
[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   fi
471 }
472 # }}}
473
474 # helper function for fstab() {{{
475 createfstab(){
476   echo "Setting up /etc/fstab"
477   if [ -n "$TARGET_UUID" ] ; then
478     echo "/dev/disk/by-uuid/${TARGET_UUID} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
479   else
480     echo "Warning: couldn't identify target UUID for rootfs, your /etc/fstab might be incomplete."
481   fi
482
483 if [ -n "$EFI" ] ; then
484   # shellcheck disable=SC2086
485   echo "UUID=$(blkid -o value -s UUID $EFI)  /boot/efi       vfat    umask=0077      0       1" >> /etc/fstab
486 fi
487
488 cat >> /etc/fstab << EOF
489 proc           /proc        proc    defaults                      0   0
490 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto                0   0
491 # some other examples:
492 # /dev/sda2       none         swap    sw,pri=0             0   0
493 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
494 # //1.2.3.4/pub   /smb/pub     cifs    user,noauto,uid=grml,gid=grml 0 0
495 # linux:/pub      /beer        nfs     defaults             0  0
496 # tmpfs           /tmp         tmpfs   size=300M            0  0
497 # /dev/sda5       none         swap    sw                   0  0
498 EOF
499 }
500 # }}}
501
502 # generate /etc/fstab {{{
503 fstab() {
504   # set up /etc/fstab if file is not present (cdebootstrap)
505   if [ ! -f /etc/fstab  ] ; then
506      createfstab
507   fi
508
509   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
510   if grep -q UNCONFIGURED /etc/fstab ; then
511      createfstab
512   fi
513 }
514 # }}}
515
516 # set up hostname {{{
517 hostname() {
518   if [ -n "$HOSTNAME" ] ; then
519      echo "Setting hostname to ${HOSTNAME}."
520      echo "$HOSTNAME" > /etc/hostname
521
522      # adjust postfix configuration
523      if [ -r /etc/postfix/main.cf ] ; then
524         # adjust hostname related options:
525         sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
526
527         # listen on loopback interface only:
528         sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
529         grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
530      fi
531      if [ -r /etc/mailname ] ; then
532         # adjust /etc/mailname
533         local etc_mail_domain
534         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   # shellcheck disable=SC2012
554   KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
555   if [ -z "$KERNELIMG" ] ; then
556      echo 'No kernel image found, skipping initrd stuff.'>&2
557      return
558   fi
559
560   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
561
562   # generate initrd
563   if [ -n "$INITRD" ] ; then
564      echo "Generating initrd."
565      update-initramfs -c -t -k "$KERNELVER"
566   fi
567 }
568 # }}}
569
570 efi_setup() {
571   if [ -z "$EFI" ] ; then
572     return 0
573   fi
574
575   if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
576     echo "Notice: efi option set but no efibootmgr package, installing it therefore."
577     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
578   fi
579
580   mkdir -p /boot/efi
581   echo "Mounting $EFI on /boot/efi"
582   mount "$EFI" /boot/efi || return 1
583
584   echo "Invoking efibootmgr"
585   efibootmgr || return 1
586 }
587
588 # grub configuration/installation {{{
589 grub_install() {
590
591   if [ -z "$GRUB" ] ; then
592     echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
593     return 0
594   fi
595
596   efi_setup || return 1
597
598   if [ -n "$EFI" ] ; then
599     GRUB_PACKAGE=grub-efi-amd64
600   else
601     GRUB_PACKAGE=grub-pc
602   fi
603
604   # make sure this is pre-defined so we have sane settings for automated
605   # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
606   echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
607   echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect $GRUB" | debconf-set-selections
608
609   if ! dpkg --list ${GRUB_PACKAGE} 2>/dev/null | grep -q '^ii' ; then
610     echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
611     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL ${GRUB_PACKAGE}
612   fi
613
614   if ! [ -x "$(command -v grub-install)" ] ; then
615      echo "Error: grub-install not available. (Error while installing grub package?)" >&2
616      return 1
617   fi
618
619   if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
620      for device in $SELECTED_PARTITIONS ; do
621         GRUB="${device%%[0-9]}"
622         echo "Installing grub on ${GRUB}:"
623         grub-install --no-floppy "$GRUB"
624      done
625      rm -f /boot/grub/device.map
626   else
627      echo "Installing grub on ${GRUB}:"
628      case "$RELEASE" in
629        lenny|squeeze|wheezy)
630          grub-install --no-floppy "$(readlink -f "${GRUB}")"
631          rm -f /boot/grub/device.map
632          ;;
633        *)
634          echo "(hd0) ${GRUB}" > /boot/grub/device.map
635          grub-install "(hd0)"
636          rm /boot/grub/device.map
637          ;;
638      esac
639   fi
640
641   echo "Adjusting grub configuration for use on ${GRUB}."
642
643   # finally install grub
644   if [ -x /usr/sbin/update-grub ] ; then
645      UPDATEGRUB='/usr/sbin/update-grub'
646   elif [ -x /sbin/update-grub ] ; then
647      UPDATEGRUB='/sbin/update-grub'
648   else
649     echo "Error: update-grub not available, can not execute it." >&2
650     return 1
651   fi
652
653   mountpoint /boot/efi &>/dev/null && umount /boot/efi
654
655   $UPDATEGRUB
656 }
657 # }}}
658
659 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
660 custom_scripts() {
661   [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
662
663   for script in /etc/debootstrap/chroot-scripts/* ; do
664       echo "Executing script $script"
665       $script && echo "done" || echo "failed"
666   done
667 }
668 # }}}
669
670 # make sure we don't have any running processes left {{{
671 services() {
672   for service in ssh mdadm mdadm-raid ; do
673     if [ -x /etc/init.d/"$service" ] ; then
674        /etc/init.d/"$service" stop || true
675     fi
676   done
677 }
678 # }}}
679
680 # unmount /proc and make sure nothing is left {{{
681 finalize() {
682   # make sure we don't leave any sensible data
683   rm -f /etc/debootstrap/variables
684
685   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
686
687   umount /sys >/dev/null 2>/dev/null || true
688   umount /proc >/dev/null 2>/dev/null || true
689 }
690 # }}}
691
692 # signal handler {{{
693 signal_handler() {
694   finalize
695   [ -n "$1" ] && EXIT="$1" || EXIT="1"
696   exit "$EXIT"
697 }
698 # }}}
699
700 # set signal handler {{{
701 trap signal_handler HUP INT QUIT TERM
702 # }}}
703
704 # execute the functions {{{
705
706  # always execute install_policy_rcd
707  install_policy_rcd
708
709  for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
710      kernel packages extrapackages reconfigure hosts \
711      default_locales timezone fstab hostname initrd grub_install passwords \
712      custom_scripts upgrade_system remove_apt_cache services \
713      remove_chrootmirror; do
714      if stage $i ; then
715        $i && stage $i 'done' || exit 1
716      fi
717   done
718   # always execute the finalize stage:
719   finalize
720 # }}}
721
722 # finally exit the chroot {{{
723   echo "Finished chroot installation, exiting."
724   exit 0
725 # }}}
726
727 ## END OF FILE #################################################################
728 # vim: ai tw=80 expandtab foldmethod=marker