vagrantvirtualboxkvmparavirtualization

Vagrant - paravirtualization "kvm" on VirtualBox version below 5.0


I have this vagrant settings below:

 12     config.vm.provider "virtualbox" do |v|
 13       v.customize ["modifyvm", :id, "--memory", "256"]
 14       v.customize ["modifyvm", :id, "--cpus", "1"]
 15       v.customize ["modifyvm", :id, "--paravirtprovider", "kvm"]     #this should only be applied to LINUX guests
 16     end

Found this on this link.

Case is, if VirtualBox version is >= 5.0, this will definitely should work. But on versions below 5.0, error should be raised.

Question: How am I able to check if --paravirtprovider is enabled in a VirtualBox version. If possible, I want this be done in the Vagrantfile itself. Thank you!


Solution

  • There might be better ways to do this, but one way is:

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    vbox_version = `VBoxManage --version`
    
    Vagrant.configure(2) do |config|
      config.vm.box = 'ubuntu/trusty64'
      config.vm.provider 'virtualbox' do |v|
        v.customize ['modifyvm', :id, '--memory', '256']
        v.customize ['modifyvm', :id, '--cpus', '1']
        if vbox_version.to_f >= 5.0
          v.customize ['modifyvm', :id, '--paravirtprovider', 'kvm']
        end
      end
    end
    

    vbox_version = `VBoxManage --version` has to happen outside of the configure block because it needs to be executed on the host system running vagrant and VirtualBox.