Skip to content

Commit 1ff4cf4

Browse files
committed
Issue #270: Add control for cpu_model and cpu_feature.
Add configuration parameters for `cpu_model` and `cpu_feature` which control the setting of the following xml block. <cpu mode='custom' match='exact'> <model fallback='allow'>core2duo</model> <feature policy='require' name='vmx'/> </cpu> This allows a vagrant KVM guest to provide nested virtualization. NOTE: The `cpu_model` parameter was previously was listed to define the cpu architecture but it actually was not doing anything. This parameter has been reused to reflect the preferred guest cpu model.
1 parent 62cd4f1 commit 1ff4cf4

File tree

6 files changed

+66
-9
lines changed

6 files changed

+66
-9
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,16 @@ then you can simply run `vagrant up` to use the kvm provider.
176176

177177
There are some provider specific parameter to control VM definition.
178178

179-
* `cpu_model` - cpu architecture: 'i686' or 'x86\_64': default is x86\_64. Note
180-
that your base box should specify this.
179+
* `cpu_model` - CPU model to use for the guest. This parameter can be used to
180+
enable nested virtualization when paired with the `cpu_feature`
181+
parameter. Typical value might be "core2duo".
182+
* `cpu_feature` - Specify CPU features that you want in the guest. This can be
183+
used to enable nested virtualization when paired with the
184+
`cpu_model` parameter. This parameter takes two options; the
185+
`policy` and the `name` of the feature. You can specify the
186+
`cpu_feature` parameter many times to enable multiple features.
187+
This parameter is only used if the `cpu_model` parameter is
188+
defined.
181189
* `core_number` - number of cpu cores.
182190
* `memory_size` - memory size such as 512m, 1GiB, 100000KiB, unit is KiB if
183191
unspecified.
@@ -214,6 +222,15 @@ image. This is slower but allows multiple VMs to be booted at the same time.
214222
* `virtio_rng` - boolean for optional virtio device of random number generator.
215223
QEMU 1.3.0 and after support this device. default `false`
216224

225+
### Example KVM provider section of the Vagrantfile
226+
227+
```ruby
228+
config.vm.provider "kvm" do |k|
229+
k.cpu_model = "core2duo"
230+
k.cpu_feature :require, "vmx"
231+
k.cpu_feature :require, "ht"
232+
end
233+
```
217234

218235
## Comparison with [Vagrant-libvirt](https://github.com/pradels/vagrant-libvirt)
219236

lib/vagrant-kvm/action/import.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def call(env)
3333
:qemu_bin => provider_config.qemu_bin,
3434
:cpus => provider_config.core_number,
3535
:cpu_model => provider_config.cpu_model,
36+
:cpu_features => provider_config.cpu_features,
3637
:machine_type => provider_config.machine_type,
3738
:network_model => provider_config.network_model,
3839
:video_model => provider_config.video_model,

lib/vagrant-kvm/config.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,16 @@ class Config < Vagrant.plugin("2", :config)
4848
# @return [String]
4949
attr_accessor :qemu_bin
5050

51-
# cpu model
51+
# CPU model requested by the guest
5252
#
53-
# @return [String]: x86_64/i386
53+
# @return [String]
5454
attr_accessor :cpu_model
5555

56+
# An array of CPU features provided by the CPU model
57+
#
58+
# @return [Array]
59+
attr_reader :cpu_features
60+
5661
# memory size in bytes
5762
# default: defined in box
5863
#
@@ -95,6 +100,7 @@ def initialize
95100
@image_mode = UNSET_VALUE
96101
@qemu_bin = UNSET_VALUE
97102
@cpu_model = UNSET_VALUE
103+
@cpu_features = []
98104
@memory_size = UNSET_VALUE
99105
@core_number = UNSET_VALUE
100106
@vnc_port = UNSET_VALUE
@@ -126,6 +132,16 @@ def customize(*command)
126132
@customizations << [event, command]
127133
end
128134

135+
# Define CPU features desired when defining the cpu_model
136+
#
137+
# Call multiple times to add different features
138+
# @param policy The meaning of the feature depends on the policy
139+
# :force, :require, :optional, :disable, :forbid
140+
# @param name The name of the CPU feature.
141+
def cpu_feature(policy, name)
142+
@cpu_features << [policy, name]
143+
end
144+
129145
# This is the hook that is called to finalize the object before it
130146
# is put into use.
131147
def finalize!
@@ -150,9 +166,9 @@ def finalize!
150166
end
151167
# Search qemu binary with the default behavior
152168
@qemu_bin = nil if @qemu_bin == UNSET_VALUE
153-
# Default cpu model is x86_64, acceptable only x86_64/i686
154-
@cpu_model = 'x86_64' if @cpu_model == UNSET_VALUE
155-
@cpu_model = 'x86_64' unless @cpu_model =~ /^(i686|x86_64)$/
169+
# CPU model is unset by default.
170+
@cpu_model = nil if @cpu_model == UNSET_VALUE
171+
@cpu_features = [] if @cpu_features == UNSET_VALUE
156172
# Process memory size directive
157173
# accept the case
158174
# integer recgnized as KiB

lib/vagrant-kvm/util/vm_definition.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ def initialize(definition)
8787
if doc.elements["/domain/uuid"]
8888
update({:uuid => doc.elements["/domain/uuid"].text})
8989
end
90+
if doc.elements["/domain/cpu/model"]
91+
cpu_features = []
92+
doc.elements.each("/domain/cpu/feature") do |cpu_feature|
93+
cpu_features << [cpu_feature.attributes["policy"],
94+
cpu_feature.attributes["name"]]
95+
end
96+
update({:cpu_model => doc.elements["/domain/cpu/model"].text,
97+
:cpu_features => cpu_features})
98+
end
9099
# VNC
91100
if doc.elements["//devices/graphics"]
92101
attrs = doc.elements["//devices/graphics"].attributes

spec/vagrant-kvm/config_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616
end
1717

1818
describe "#cpu_model" do
19-
it "default to x86-64" do
20-
should_default(:cpu_model, 'x86_64')
19+
it "defaults to nil" do
20+
should_default(:cpu_model, nil)
21+
end
22+
end
23+
24+
describe "#cpu_features" do
25+
it "defaults to an empty array" do
26+
should_default(:cpu_features, [])
2127
end
2228
end
2329

templates/libvirt_domain.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
<type arch='<%= arch %>' machine='<%= machine_type %>'>hvm</type>
1818
<boot dev='hd'/>
1919
</os>
20+
<% if cpu_model %>
21+
<cpu match='exact'>
22+
<model><%= cpu_model %></model>
23+
<% cpu_features.each do |policy, name| %>
24+
<feature policy='<%= policy %>' name='<%= name %>'/>
25+
<% end %>
26+
</cpu>
27+
<% end %>
2028
<features>
2129
<acpi/>
2230
<apic/>

0 commit comments

Comments
 (0)