Index: /trunk/src/VBox/Main/UnattendedTemplates/debian_postinstall.sh
===================================================================
--- /trunk/src/VBox/Main/UnattendedTemplates/debian_postinstall.sh	(revision 68074)
+++ /trunk/src/VBox/Main/UnattendedTemplates/debian_postinstall.sh	(revision 68075)
@@ -3,5 +3,5 @@
 # Post installation script template for debian-like distros.
 #
-# This script expects to be running chroot'ed into /target.
+# This script expects to be running w/o chroot.
 #
 
@@ -18,5 +18,8 @@
 #
 
-MY_LOGFILE="/var/log/vboxpostinstall.log"
+#MY_DEBUG="yes"
+MY_DEBUG=""
+MY_TARGET="/target"
+MY_LOGFILE="${MY_TARGET}/var/log/vboxpostinstall.log"
 MY_EXITCODE=0
 
@@ -24,5 +27,7 @@
 log_command()
 {
-    echo "Executing: $*" >> "${MY_LOGFILE}"
+    echo "--------------------------------------------------" >> "${MY_LOGFILE}"
+    echo "** Date:      `date -R`" >> "${MY_LOGFILE}"
+    echo "** Executing: $*" >> "${MY_LOGFILE}"
     "$@" 2>&1 | tee -a "${MY_LOGFILE}"
     if [ "${PIPESTATUS[0]}" != "0" ]; then
@@ -32,27 +37,132 @@
 }
 
+log_command_in_target()
+{
+    #
+    # We should be using in-target here, however we don't get any stderr output
+    # from it because of log-output. We can get stdout by --pass-stdout, but
+    # that's not helpful for failures.
+    #
+    # So, we try do the chroot prepping that in-target does at the start of the
+    # script (see below) and just use chroot here.
+    #
+    # Also, GA installer and in-root/log-output doesn't seem to get along.
+    #
+    log_command chroot "${MY_TARGET}" "$@"
+    # log_command in-target --pass-stdout "$@" # No stderr output... :-(
+}
 
-echo "Started: $*" >> "${MY_LOGFILE}"
-echo "Date:    `date -R`" >> "${MY_LOGFILE}"
 
-echo ''
-echo 'Installing packages for building kernel modules...'
-log_command apt-get -y install build-essential
-log_command apt-get -y install linux-headers-$(uname -r)
+#
+# Header.
+#
+echo "******************************************************************************" >> "${MY_LOGFILE}"
+echo "** VirtualBox Unattended Guest Installation - Late installation actions" >> "${MY_LOGFILE}"
+echo "** Date:    `date -R`" >> "${MY_LOGFILE}"
+echo "** Started: $0 $*" >> "${MY_LOGFILE}"
 
+#
+# Setup the target jail ourselves since in-target steals all the output.
+#
+if [ -f /lib/chroot-setup.sh ]; then
+    MY_HAVE_CHROOT_SETUP="yes"
+    . /lib/chroot-setup.sh
+    if chroot_setup; then
+        echo "** chroot_setup: done" | tee -a "${MY_LOGFILE}"
+    else
+        echo "** chroot_setup: failed $?" | tee -a "${MY_LOGFILE}"
+    fi
+else
+    MY_HAVE_CHROOT_SETUP=""
+fi
+
+#
+# Debug
+#
+if [ "${MY_DEBUG}" = "yes" ]; then
+    log_command id
+    log_command df
+    log_command mount
+    log_command_in_target df
+    log_command_in_target mount
+    log_command_in_target ls -Rla /cdrom
+    log_command_in_target ls -Rla /media
+    log_command find /
+    MY_EXITCODE=0
+fi
+
+# We want the ISO available inside the target jail.
+if [ -f "${MY_TARGET}/cdrom/vboxpostinstall.sh" ]; then
+    MY_UNMOUNT_TARGET_CDROM=
+    echo "** binding cdrom into jail: already done" | tee -a "${MY_LOGFILE}"
+else
+    MY_UNMOUNT_TARGET_CDROM="yes"
+    log_command mount -o bind /cdrom "${MY_TARGET}/cdrom"
+    if [ -f "${MY_TARGET}/cdrom/vboxpostinstall.sh" ]; then
+        echo "** binding cdrom into jail: success"  | tee -a "${MY_LOGFILE}"
+    else
+        echo "** binding cdrom into jail: failed"   | tee -a "${MY_LOGFILE}"
+    fi
+    if [ "${MY_DEBUG}" = "yes" ]; then
+        log_command find "${MY_TARGET}/cdrom"
+    fi
+fi
+
+#
+# Packages needed for GAs.
+#
+echo '** Installing packages for building kernel modules...' | tee -a "${MY_LOGFILE}"
+log_command_in_target apt-get -y install build-essential
+log_command_in_target apt-get -y install linux-headers-$(uname -r)
+
+#
+# GAs
+#
 @@VBOX_COND_IS_INSTALLING_ADDITIONS@@
-echo ''
-echo 'Installing VirtualBox Guest Additions...'
-log_command /bin/bash /cdrom/VBoxAdditions/VBoxLinuxAdditions.run
-log_command usermod -a -G vboxsf "@@VBOX_INSERT_USER_LOGIN@@"
+echo '** Installing VirtualBox Guest Additions...' | tee -a "${MY_LOGFILE}"
+log_command_in_target /bin/bash /cdrom/vboxadditions/VBoxLinuxAdditions.run --nox11
+log_command_in_target usermod -a -G vboxsf "@@VBOX_INSERT_USER_LOGIN@@"
 @@VBOX_COND_END@@
 
+#
+# Testing.
+#
 @@VBOX_COND_IS_INSTALLING_TEST_EXEC_SERVICE@@
-echo ''
-echo 'Installing Test Execution Service...'
+echo '** Installing Test Execution Service...' | tee -a "${MY_LOGFILE}"
+log_command_in_target test "/cdrom/linux/@@VBOX_INSERT_OS_ARCH@@/TestExecService"
 ## @todo fix this
 @@VBOX_COND_END@@
 
-echo "Final exit code: ${MY_EXITCODE}" >> "${MY_LOGFILE}"
+#
+# Run user command.
+#
+@@VBOX_COND_HAS_POST_INSTALL_COMMAND@@
+echo '** Running custom user command ...'      | tee -a "${MY_LOGFILE}"
+log_command @@VBOX_INSERT_POST_INSTALL_COMMAND@@
+@@VBOX_COND_END@@
+
+#
+# Unmount the cdrom if we bound it and clean up the chroot if we set it up.
+#
+if [ -n "${MY_UNMOUNT_TARGET_CDROM}" ]; then
+    echo "** unbinding cdrom from jail..." | tee -a "${MY_LOGFILE}"
+    log_command umount "${MY_TARGET}/cdrom"
+fi
+if [ -n "${MY_HAVE_CHROOT_SETUP}" ]; then
+    if chroot_cleanup; then
+        echo "** chroot_cleanup: done"      | tee -a "${MY_LOGFILE}"
+    else
+        echo "** chroot_cleanup: failed $?" | tee -a "${MY_LOGFILE}"
+    fi
+fi
+
+#
+# Footer.
+#
+echo "******************************************************************************" >> "${MY_LOGFILE}"
+echo "** Date:            `date -R`" >> "${MY_LOGFILE}"
+echo "** Final exit code: ${MY_EXITCODE}" >> "${MY_LOGFILE}"
+echo "******************************************************************************" >> "${MY_LOGFILE}"
+
 exit ${MY_EXITCODE}
 
Index: /trunk/src/VBox/Main/UnattendedTemplates/debian_preseed.cfg
===================================================================
--- /trunk/src/VBox/Main/UnattendedTemplates/debian_preseed.cfg	(revision 68074)
+++ /trunk/src/VBox/Main/UnattendedTemplates/debian_preseed.cfg	(revision 68075)
@@ -64,4 +64,4 @@
 d-i preseed/late_command string cp /cdrom/vboxpostinstall.sh /target/root/vboxpostinstall.sh \
  && chmod +x /target/root/vboxpostinstall.sh \
- && chroot /target /bin/bash /root/vboxpostinstall.sh
+ && /bin/bash /root/vboxpostinstall.sh
 
Index: /trunk/src/VBox/Main/UnattendedTemplates/ubuntu_preseed.cfg
===================================================================
--- /trunk/src/VBox/Main/UnattendedTemplates/ubuntu_preseed.cfg	(revision 68074)
+++ /trunk/src/VBox/Main/UnattendedTemplates/ubuntu_preseed.cfg	(revision 68075)
@@ -64,9 +64,9 @@
 d-i preseed/late_command string cp /cdrom/vboxpostinstall.sh /target/root/vboxpostinstall.sh \
  && chmod +x /target/root/vboxpostinstall.sh \
- && chroot /target /bin/bash /root/vboxpostinstall.sh
+ && /bin/bash /root/vboxpostinstall.sh
 
 # Same as above, but for ubiquity.
 ubiquity ubiquity/success_command string cp /cdrom/vboxpostinstall.sh /target/root/vboxpostinstall.sh \
  && chmod +x /target/root/vboxpostinstall.sh \
- && chroot /target /bin/bash /root/vboxpostinstall.sh
+ && /bin/bash /target/root/vboxpostinstall.sh
 
