+ exit "$2"
+}
+trap 'bailout ${LINENO} $?' ERR
+
+## helper functions
+virtualbox_setup() {
+ case "$DEBIAN_VERSION" in
+ lenny)
+ echo "* Debian lenny doesn't support Virtualbox Guest Additions, skipping."
+ return 0
+ ;;
+ esac
+
+ if ! mountpoint "${TARGET}" &>/dev/null ; then
+ echo "* Mounting target system"
+ mount "${INSTALL_TARGET}" "${TARGET}"
+ fi
+
+ echo "* Installing packages for Virtualbox Guest Additions"
+ chroot ${TARGET} apt-get -y install make gcc dkms
+
+ echo "* Installing Virtualbox Guest Additions"
+ isofile="${HOME}/VBoxGuestAdditions.iso"
+
+ KERNELHEADERS=$(basename $(find $TARGET/usr/src/ -maxdepth 1 -name linux-headers\* ! -name \*common) | sort -u -r -V | head -1)
+ if [ -z "$KERNELHEADERS" ] ; then
+ echo "Error: no kernel headers found for building the VirtualBox Guest Additions kernel module." >&2
+ exit 1
+ fi
+
+ KERNELVERSION=${KERNELHEADERS##linux-headers-}
+ if [ -z "$KERNELVERSION" ] ; then
+ echo "Error: no kernel version could be identified." >&2
+ exit 1
+ fi
+
+ cp /tmp/fake-uname.so "${TARGET}/tmp/fake-uname.so"
+ mkdir -p "${TARGET}/media/cdrom"
+ mountpoint "${TARGET}/media/cdrom" >/dev/null && umount "${TARGET}/media/cdrom"
+ mount -t iso9660 $isofile "${TARGET}/media/cdrom/"
+ UTS_RELEASE=$KERNELVERSION LD_PRELOAD=/tmp/fake-uname.so grml-chroot "$TARGET" /media/cdrom/VBoxLinuxAdditions.run --nox11 || true
+ if grep -q "make: .*vboxguest.*Error 2" "${TARGET}/var/log/vboxadd-install.log" ; then
+ echo "Error: looks like a fatal error happened during installation of VirtualBox Guest Additions." >&2
+ exit 1
+ fi
+ [ -r "${TARGET}/var/log/VBoxGuestAdditions.log" ] && tail -10 "${TARGET}/var/log/VBoxGuestAdditions.log"
+ [ -r "${TARGET}/var/log/vboxadd-install.log" ] && tail -10 "${TARGET}/var/log/vboxadd-install.log"
+ [ -r "${TARGET}/var/log/vboxadd-setup.log" ] && tail -10 "${TARGET}/var/log/vboxadd-setup.log"
+ umount "${TARGET}/media/cdrom/"
+
+ # work around bug in VirtualBox 4.3.18 which leaves process behind,
+ # causing unmount of "$TARGET" to fail
+ grml-chroot "$TARGET" /etc/init.d/vboxadd-service stop || true
+ # left behind by VBoxService
+ umount "$TARGET"/dev || true
+
+ # work around regression in virtualbox-guest-additions-iso 4.3.10
+ if [ -d ${TARGET}/opt/VBoxGuestAdditions-4.3.10 ] ; then
+ ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions ${TARGET}/usr/lib/VBoxGuestAdditions
+ fi
+
+ if mountpoint "${TARGET}" &>/dev/null ; then
+ echo "* Unmounting target system"
+ umount "${TARGET}"
+ fi