diff --git a/examples/makebuildserver.config.py b/examples/makebuildserver.config.py index 4ee73fbc..b6c0183d 100644 --- a/examples/makebuildserver.config.py +++ b/examples/makebuildserver.config.py @@ -48,3 +48,11 @@ # about the timeout, extend the timeout here. (default: 600 seconds) # # boot_timeout = 1200 + +# By default, this whole process uses VirtualBox as the provider, but +# QEMU+KVM is also supported via the libvirt plugin to vagrant. If +# this is run within a KVM guest, then libvirt's QEMU+KVM will be used +# automatically. It can also be manually enabled by uncommenting +# below: +# +# vm_provider = 'libvirt' diff --git a/makebuildserver b/makebuildserver index 3a26bf51..19010137 100755 --- a/makebuildserver +++ b/makebuildserver @@ -64,8 +64,20 @@ config = { 'cpus': 1, 'memory': 1024, 'hwvirtex': 'off', + 'vm_provider': 'virtualbox', } +if os.path.isfile('/usr/bin/systemd-detect-virt'): + try: + virt = subprocess.check_output('/usr/bin/systemd-detect-virt').strip().decode('utf-8') + except subprocess.CalledProcessError as e: + virt = 'none' + if virt == 'qemu' or virt == 'kvm': + print('Running in a VM guest, defaulting to QEMU/KVM via libvirt') + config['vm_provider'] = 'libvirt' + elif virt != 'none': + print('Running in an unsupported VM guest (' + virt + ')!') + # load config file, if present if os.path.exists('makebuildserver.config.py'): exec(compile(open('makebuildserver.config.py').read(), 'makebuildserver.config.py', 'exec'), config) @@ -346,6 +358,7 @@ elif os.path.exists('/proc/cpuinfo'): vf = os.path.join(serverdir, 'Vagrantfile.yaml') writevf = True if os.path.exists(vf): + print('Halting', serverdir) vagrant(['halt'], serverdir) with open(vf, 'r', encoding='utf-8') as f: oldconfig = yaml.load(f) @@ -361,6 +374,33 @@ if writevf: with open(vf, 'w', encoding='utf-8') as f: yaml.dump(config, f) +if config['vm_provider'] == 'libvirt': + returncode, out = vagrant(['box', 'list'], serverdir, printout=options.verbose) + found_basebox = False + needs_mutate = False + for line in out.splitlines(): + if line.startswith(config['basebox']): + found_basebox = True + if line.split('(')[1].split(',')[0] != 'libvirt': + needs_mutate = True + continue + if not found_basebox: + if isinstance(config['baseboxurl'], str): + baseboxurl = config['baseboxurl'] + else: + baseboxurl = config['baseboxurl'][0] + print('Adding', config['basebox'], 'from', baseboxurl) + vagrant(['box', 'add', '--name', config['basebox'], baseboxurl], + serverdir, printout=options.verbose) + needs_mutate = True + if needs_mutate: + print('Converting', config['basebox'], 'to libvirt format') + vagrant(['mutate', config['basebox'], 'libvirt'], + serverdir, printout=options.verbose) + print('Removing virtualbox format copy of', config['basebox']) + vagrant(['box', 'remove', '--provider', 'virtualbox', config['basebox']], + serverdir, printout=options.verbose) + print("Configuring build server VM") returncode, out = vagrant(['up', '--provision'], serverdir, printout=True) with open(os.path.join(serverdir, 'up.log'), 'w') as log: