From 0be1fc017a348f283b7f7a1f5efd49930425ea5a Mon Sep 17 00:00:00 2001 From: Guto Andreollo Date: Thu, 14 Jan 2016 19:49:39 -0200 Subject: [PATCH 1/3] Added basic support for CPU features The following option was added: * cpu_feature - Defaults to unset, needs two options: "name" and "policy", as interpreted by libvirt This only adds support for creating a VM with specific CPU features defined, not for changing them after the VM was created --- lib/vagrant-libvirt/action/create_domain.rb | 4 ++++ lib/vagrant-libvirt/config.rb | 18 ++++++++++++++++++ lib/vagrant-libvirt/templates/domain.xml.erb | 3 +++ 3 files changed, 25 insertions(+) diff --git a/lib/vagrant-libvirt/action/create_domain.rb b/lib/vagrant-libvirt/action/create_domain.rb index deecf86..0c34674 100644 --- a/lib/vagrant-libvirt/action/create_domain.rb +++ b/lib/vagrant-libvirt/action/create_domain.rb @@ -33,6 +33,7 @@ module VagrantPlugins @name = env[:domain_name] @uuid = config.uuid @cpus = config.cpus.to_i + @cpu_features = config.cpu_features @cpu_mode = config.cpu_mode @loader = config.loader @machine_type = config.machine_type @@ -150,6 +151,9 @@ module VagrantPlugins end env[:ui].info(" -- Domain type: #{@domain_type}") env[:ui].info(" -- Cpus: #{@cpus}") + @cpu_features.each do |cpu_feature| + env[:ui].info(" -- CPU Feature: name=#{cpu_feature[:name]}, policy=#{cpu_feature[:policy]}") + end env[:ui].info(" -- Memory: #{@memory_size / 1024}M") env[:ui].info(" -- Management MAC: #{@management_network_mac}") env[:ui].info(" -- Loader: #{@loader}") diff --git a/lib/vagrant-libvirt/config.rb b/lib/vagrant-libvirt/config.rb index 27bfbea..66af3e8 100644 --- a/lib/vagrant-libvirt/config.rb +++ b/lib/vagrant-libvirt/config.rb @@ -58,6 +58,7 @@ module VagrantPlugins attr_accessor :memory attr_accessor :cpus attr_accessor :cpu_mode + attr_accessor :cpu_features attr_accessor :loader attr_accessor :boot_order attr_accessor :machine_type @@ -128,6 +129,7 @@ module VagrantPlugins @memory = UNSET_VALUE @cpus = UNSET_VALUE @cpu_mode = UNSET_VALUE + @cpu_features = UNSET_VALUE @loader = UNSET_VALUE @machine_type = UNSET_VALUE @machine_arch = UNSET_VALUE @@ -209,6 +211,21 @@ module VagrantPlugins raise 'Only four cdroms may be attached at a time' end + def cpu_feature(options={}) + if options[:name].nil? || options.[:policy].nil? + raise 'CPU Feature name AND policy must be specified' + end + + if @cpu_features == UNSET_VALUE + @cpu_features = [] + end + + @cpu_features.push({ + name: options[:name], + policy: options[:policy] + }) + end + def input(options={}) if options[:type].nil? || options[:bus].nil? raise 'Input type AND bus must be specified' @@ -390,6 +407,7 @@ module VagrantPlugins @memory = 512 if @memory == UNSET_VALUE @cpus = 1 if @cpus == UNSET_VALUE @cpu_mode = 'host-model' if @cpu_mode == UNSET_VALUE + @cpu_features = [] if @cpu_features == UNSET_VALUE @loader = nil if @loader == UNSET_VALUE @machine_type = nil if @machine_type == UNSET_VALUE @machine_arch = nil if @machine_arch == UNSET_VALUE diff --git a/lib/vagrant-libvirt/templates/domain.xml.erb b/lib/vagrant-libvirt/templates/domain.xml.erb index 27499f2..5c9cf29 100644 --- a/lib/vagrant-libvirt/templates/domain.xml.erb +++ b/lib/vagrant-libvirt/templates/domain.xml.erb @@ -12,6 +12,9 @@ <% end %> + <% @cpu_features.each do |cpu_feature| %> + + <% end %> <% end %> From c3f1e8806697f8385cd521a3776d2931361e70eb Mon Sep 17 00:00:00 2001 From: Gerben Meijer Date: Thu, 11 Feb 2016 14:40:28 +0100 Subject: [PATCH 2/3] Fix typo in policy option check --- lib/vagrant-libvirt/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vagrant-libvirt/config.rb b/lib/vagrant-libvirt/config.rb index 66af3e8..95db8bc 100644 --- a/lib/vagrant-libvirt/config.rb +++ b/lib/vagrant-libvirt/config.rb @@ -212,7 +212,7 @@ module VagrantPlugins end def cpu_feature(options={}) - if options[:name].nil? || options.[:policy].nil? + if options[:name].nil? || options[:policy].nil? raise 'CPU Feature name AND policy must be specified' end From 4554ceb6f0e1cdce01c0c491d298b33345c74bc9 Mon Sep 17 00:00:00 2001 From: Gerben Meijer Date: Thu, 11 Feb 2016 14:56:08 +0100 Subject: [PATCH 3/3] Update README on CPU features --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index af6ed54..ce509cf 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ welcome and can help a lot :-) - [CDROMs](#cdroms) - [Input](#input) - [PCI device passthrough](#pci-device-passthrough) +- [CPU Features](#cpu-features) - [No box and PXE boot](#no-box-and-pxe-boot) - [SSH Access To VM](#ssh-access-to-vm) - [Forwarded Ports](#forwarded-ports) @@ -542,6 +543,27 @@ Vagrant.configure("2") do |config| end ``` +## CPU features + +You can specify CPU feature policies via `libvirt.cpu_feature`. Available options are +listed below. Note that both options are required: + +* `name` - The name of the feature for the chosen CPU (see libvirts `cpu_map.xml`) +* `policy` - The policy for this feature (one of `force`, `require`, `optional`, `disable` and `forbid` - see libvirt documentation) + +```ruby +Vagrant.configure("2") do |config| + config.vm.provider :libvirt do |libvirt| + # The feature will not be supported by virtual CPU. + libvirt.cpu_feature :name => 'hypervisor', :policy => 'disable' + # Guest creation will fail unless the feature is supported by host CPU. + libvirt.cpu_feature :name => 'vmx', :policy => 'require' + # The virtual CPU will claim the feature is supported regardless of it being supported by host CPU. + libvirt.cpu_feature :name => 'pdpe1gb', :policy => 'force' + end +end +``` + ## USB device passthrough You can specify multiple USB devices to passthrough to the VM via `libvirt.usb`. The device can be specified by the following options: