Class: VagrantPlugins::ProviderKvm::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-kvm/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/vagrant-kvm/config.rb', line 89

def initialize
  @customizations   = []
  @name             = UNSET_VALUE
  @gui              = UNSET_VALUE
  @storage_pool     = UNSET_VALUE
  @image_type       = UNSET_VALUE
  @image_mode       = UNSET_VALUE
  @qemu_bin         = UNSET_VALUE
  @cpu_model        = UNSET_VALUE
  @memory_size      = UNSET_VALUE
  @core_number      = UNSET_VALUE
  @vnc_port         = UNSET_VALUE
  @vnc_autoport     = UNSET_VALUE
  @vnc_password     = UNSET_VALUE
  @machine_type     = UNSET_VALUE
  @network_model    = UNSET_VALUE
  @video_model      = UNSET_VALUE
  @disk_bus         = UNSET_VALUE
  @sound            = UNSET_VALUE
  @force_pause      = UNSET_VALUE
  @enable_virtfs    = UNSET_VALUE
  @virtio_rng       = UNSET_VALUE
end

Instance Attribute Details

#core_numberString

core number of cpu default: defined in box

Returns:

  • (String)


66
67
68
# File 'lib/vagrant-kvm/config.rb', line 66

def core_number
  @core_number
end

#cpu_modelString

cpu model

Returns:

  • (String)

    : x86_64/i386



54
55
56
# File 'lib/vagrant-kvm/config.rb', line 54

def cpu_model
  @cpu_model
end

#customizationsArray (readonly)

Returns:

  • (Array)


9
10
11
# File 'lib/vagrant-kvm/config.rb', line 9

def customizations
  @customizations
end

#disk_busString

disk bus type sata/virtio

Returns:

  • (String)


80
81
82
# File 'lib/vagrant-kvm/config.rb', line 80

def disk_bus
  @disk_bus
end

#force_pauseObject

Returns the value of attribute force_pause.



87
88
89
# File 'lib/vagrant-kvm/config.rb', line 87

def force_pause
  @force_pause
end

#guiBoolean

If set to ‘true`, then KVM/Qemu will be launched with a VNC console.

Returns:

  • (Boolean)


14
15
16
# File 'lib/vagrant-kvm/config.rb', line 14

def gui
  @gui
end

#image_backingBoolean (readonly)

VM image mode(clone or COW with backing file)

Returns:

  • (Boolean)


39
40
41
# File 'lib/vagrant-kvm/config.rb', line 39

def image_backing
  @image_backing
end

#image_modeString

VM image mode(clone or COW with backing file)

Returns:

  • (String)


44
45
46
# File 'lib/vagrant-kvm/config.rb', line 44

def image_mode
  @image_mode
end

#image_typeString

The VM image format

Returns:

  • (String)


34
35
36
# File 'lib/vagrant-kvm/config.rb', line 34

def image_type
  @image_type
end

#machine_typeObject

Returns the value of attribute machine_type.



70
71
72
# File 'lib/vagrant-kvm/config.rb', line 70

def machine_type
  @machine_type
end

#memory_sizeString

memory size in bytes default: defined in box

Returns:

  • (String)


60
61
62
# File 'lib/vagrant-kvm/config.rb', line 60

def memory_size
  @memory_size
end

#nameString

This should be set to the name of the VM

Returns:

  • (String)


19
20
21
# File 'lib/vagrant-kvm/config.rb', line 19

def name
  @name
end

#network_adaptersHash (readonly)

The defined network adapters.

Returns:

  • (Hash)


24
25
26
# File 'lib/vagrant-kvm/config.rb', line 24

def network_adapters
  @network_adapters
end

#network_modelObject

Returns the value of attribute network_model.



71
72
73
# File 'lib/vagrant-kvm/config.rb', line 71

def network_model
  @network_model
end

#qemu_binString

path of qemu binary

Returns:

  • (String)


49
50
51
# File 'lib/vagrant-kvm/config.rb', line 49

def qemu_bin
  @qemu_bin
end

#seclabelString

Security labelling default: off

Returns:

  • (String)


86
87
88
# File 'lib/vagrant-kvm/config.rb', line 86

def seclabel
  @seclabel
end

#soundObject

Returns the value of attribute sound.



73
74
75
# File 'lib/vagrant-kvm/config.rb', line 73

def sound
  @sound
end

#storage_poolString

The storage pool to use

Returns:

  • (String)


29
30
31
# File 'lib/vagrant-kvm/config.rb', line 29

def storage_pool
  @storage_pool
end

#video_modelObject

Returns the value of attribute video_model.



72
73
74
# File 'lib/vagrant-kvm/config.rb', line 72

def video_model
  @video_model
end

#virtio_rngObject

Returns the value of attribute virtio_rng.



74
75
76
# File 'lib/vagrant-kvm/config.rb', line 74

def virtio_rng
  @virtio_rng
end

#vnc_autoportObject

Returns the value of attribute vnc_autoport.



68
69
70
# File 'lib/vagrant-kvm/config.rb', line 68

def vnc_autoport
  @vnc_autoport
end

#vnc_passwordObject

Returns the value of attribute vnc_password.



69
70
71
# File 'lib/vagrant-kvm/config.rb', line 69

def vnc_password
  @vnc_password
end

#vnc_portObject

Returns the value of attribute vnc_port.



67
68
69
# File 'lib/vagrant-kvm/config.rb', line 67

def vnc_port
  @vnc_port
end

Instance Method Details

#customize(*command) ⇒ Object

Customize the VM by predefined actions.

When called multiple times, the customizations will be applied in the order given.

The special ‘:name` parameter in the command will be replaced with the unique ID or name of the virtual machine. This is useful for parameters to `modifyvm` and the like.

Parameters:

  • command (Array)

    An array of arguments to pass to



123
124
125
126
127
# File 'lib/vagrant-kvm/config.rb', line 123

def customize(*command)
  event   = command.first.is_a?(String) ? command.shift : "pre-boot"
  command = command[0]
  @customizations << [event, command]
end

#finalize!Object

This is the hook that is called to finalize the object before it is put into use.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/vagrant-kvm/config.rb', line 131

def finalize!
  # The default name is just nothing, and we default it
  @name = nil if @name == UNSET_VALUE
  # Default is to not show a GUI
  @gui = false if @gui == UNSET_VALUE
  # Default is a storage pool we create for vagrant
   = Etc.getlogin
  @storage_pool = 'vagrant-'+.to_s if @storage_pool == UNSET_VALUE
  # Default image type is a sparsed raw
  @image_type = 'qcow2' if @image_type == UNSET_VALUE
  case @image_mode
  when UNSET_VALUE
    @image_backing = true
  when 'clone'
    @image_backing = false
  when 'cow'
    @image_backing = true
  else
    @image_backing = true
  end
  # Search qemu binary with the default behavior
  @qemu_bin = nil if @qemu_bin == UNSET_VALUE
  # Default cpu model is x86_64, acceptable only x86_64/i686
  @cpu_model = 'x86_64' if @cpu_model == UNSET_VALUE
  @cpu_model = 'x86_64' unless @cpu_model =~ /^(i686|x86_64)$/
  # Process memory size directive
  # accept the case
  # integer recgnized as KiB
  # <num>KiB/KB/kb/MiB/MB/mb/GiB/GB/gb
  #
  case @memory_size
  when /^([0-9][0-9]*)(KiB|kib)$/
    @memory_size = ("#{$1}".to_i * 1024).to_s
  when /^([0-9][0-9]*)(KB|kb)$/
    @memory_size = ("#{$1}".to_i * 1000).to_s
  when /^([0-9][0-9]*)(m||MiB|mib|)$/
    @memory_size = ("#{$1}".to_i * 1048576).to_s
  when /^([0-9][0-9]*)(MB|mb|)$/
    @memory_size = ("#{$1}".to_i * 1000000).to_s
  when /^([0-9][0-9]*)(g||GiB|gib)$/
    @memory_size = ("#{$1}".to_i * 1073741824).to_s
  when /^([0-9][0-9]*)(GB|gb|)$/
    @memory_size = ("#{$1}".to_i * 1000000000).to_s
  when /^([0-9][0-9]*)$/
    @memory_size = ("#{$1}".to_i * 1024).to_s
  when UNSET_VALUE
    @memory_size = nil
  else
    @memory_size = nil
  end
  # Default core number is 1
  @core_number = 1 if @core_number == UNSET_VALUE

  @vnc_autoport = false if @vnc_autoport == UNSET_VALUE
  @vnc_password = nil if @vnc_password == UNSET_VALUE
  @vnc_port = -1 if @vnc_port == UNSET_VALUE
  @machine_type = "pc-1.2" if @machine_type == UNSET_VALUE
  @network_model = "virtio" if @network_model == UNSET_VALUE
  @video_model = "cirrus" if @video_model == UNSET_VALUE
  @disk_bus = nil if @disk_bus == UNSET_VALUE
  @sound = false if @sound == UNSET_VALUE
  @force_pause = false if @force_pause == UNSET_VALUE
  @enable_virtfs = false if @enable_virtfs == UNSET_VALUE
  @virtio_rng = nil if @virtio_rng == UNSET_VALUE
end

#validate(machine) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/vagrant-kvm/config.rb', line 197

def validate(machine)
  errors = []

  valid_events = ["pre-import", "pre-boot", "post-boot"]
  @customizations.each do |event, _|
    if !valid_events.include?(event)
      errors << I18n.t(
        "vagrant.kvm.config.invalid_event",
        event: event.to_s,
        valid_events: valid_events.join(", "))
    end
  end

  @customizations.each do |event, command|
    if event == "pre-import" && command.index(:id)
      errors << I18n.t("vagrant.kvm.config.id_in_pre_import")
    end
  end

  { "KVM Provider" => errors }
end