diff --git a/bootstrapvz/providers/virtualbox/assets/install_guest_additions.sh b/bootstrapvz/providers/virtualbox/assets/install_guest_additions.sh new file mode 100644 index 0000000..3774c5d --- /dev/null +++ b/bootstrapvz/providers/virtualbox/assets/install_guest_additions.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +function uname { + if [[ $1 == '-r' ]]; then + echo "KERNEL_VERSION" + return 0 + elif [[ $1 == '-m' ]]; then + echo "KERNEL_ARCH" + return 0 + else + $(which uname) $@ + fi +} +export -f uname + +INSTALL_SCRIPT --nox11 diff --git a/bootstrapvz/providers/virtualbox/tasks/guest_additions.py b/bootstrapvz/providers/virtualbox/tasks/guest_additions.py index e4e3890..55aed80 100644 --- a/bootstrapvz/providers/virtualbox/tasks/guest_additions.py +++ b/bootstrapvz/providers/virtualbox/tasks/guest_additions.py @@ -2,6 +2,9 @@ from bootstrapvz.base import Task from bootstrapvz.common import phases from bootstrapvz.common.tasks.packages import InstallPackages from bootstrapvz.common.exceptions import TaskError +import os + +assets = os.path.normpath(os.path.join(os.path.dirname(__file__), '../assets')) class CheckGuestAdditionsPath(Task): @@ -10,7 +13,6 @@ class CheckGuestAdditionsPath(Task): @classmethod def run(cls, info): - import os.path guest_additions_path = info.manifest.provider['guest_additions'] if not os.path.exists(guest_additions_path): msg = 'The file {file} does not exist.'.format(file=guest_additions_path) @@ -27,12 +29,19 @@ class AddGuestAdditionsPackages(Task): info.packages.add('bzip2') info.packages.add('build-essential') info.packages.add('dkms') + kernel_headers_pkg = 'linux-headers-' if info.manifest.system['architecture'] == 'i386': + arch = 'i686' kernel_headers_pkg += '686-pae' else: + arch = 'x86_64' kernel_headers_pkg += 'amd64' info.packages.add(kernel_headers_pkg) + info.kernel = { + 'arch': arch, + 'headers_pkg': kernel_headers_pkg, + } class InstallGuestAdditions(Task): @@ -42,19 +51,35 @@ class InstallGuestAdditions(Task): @classmethod def run(cls, info): - import os + from bootstrapvz.common.tools import log_call, log_check_call + for line in log_check_call(['chroot', info.root, 'apt-cache', 'show', info.kernel['headers_pkg']]): + key, value = line.split(':') + if key.strip() == 'Depends': + kernel_version = value.strip().split('linux-headers-')[-1] + break + guest_additions_path = info.manifest.provider['guest_additions'] mount_dir = 'mnt/guest_additions' mount_path = os.path.join(info.root, mount_dir) os.mkdir(mount_path) root = info.volume.partition_map.root root.add_mount(guest_additions_path, mount_path, ['-o', 'loop']) + install_script = os.path.join('/', mount_dir, 'VBoxLinuxAdditions.run') + install_wrapper_name = 'install_guest_additions.sh' + install_wrapper = open(os.path.join(assets, install_wrapper_name)) \ + .read() \ + .replace("KERNEL_VERSION", kernel_version) \ + .replace("KERNEL_ARCH", info.kernel['arch']) \ + .replace("INSTALL_SCRIPT", install_script) + install_wrapper_path = os.path.join(info.root, install_wrapper_name) + with open(install_wrapper_path, 'w') as f: + f.write(install_wrapper + '\n') - install_script = os.path.join('/', mount_dir, 'VBoxLinuxAdditions.run') # Don't check the return code of the scripts here, because 1 not necessarily means they have failed - from bootstrapvz.common.tools import log_call - log_call(['chroot', info.root, install_script, '--nox11']) + log_call(['chroot', info.root, 'bash', '/' + install_wrapper_name]) + # VBoxService process could be running, as it is not affected by DisableDaemonAutostart log_call(['chroot', info.root, 'service', 'vboxadd-service', 'stop']) root.remove_mount(mount_path) os.rmdir(mount_path) + os.remove(install_wrapper_path)