Class: PackerFiles::Virtual::KVM

Inherits:
Utils::HashSerializer show all
Includes:
Utils::Size
Defined in:
lib/PackerFiles/Virtual/KVM.rb

Overview

Abstraction for KVM(QEMU) builder in Packer. Refer to Packer.io documentation on more details on the various variables for QEMU ISO Builder.

Instance Method Summary collapse

Methods included from Utils::Size

#Bytes, #MiB

Methods inherited from Utils::HashSerializer

hash_attributes, hash_variable, #merge_hs, #to_hash

Constructor Details

#initialize {|_self| ... } ⇒ KVM

Constructor. Supports the standard block semantics via yield

Yields:

  • (_self)

Yield Parameters:



55
56
57
58
# File 'lib/PackerFiles/Virtual/KVM.rb', line 55

def initialize(&block)
   self.type = 'qemu'
   yield self if block_given?
end

Instance Method Details

#Hypervisor(hyper, amd64 = true) ⇒ Object

Given an Hypervisor object, convert into a list of qemuargs commands



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/PackerFiles/Virtual/KVM.rb', line 61

def Hypervisor(hyper, amd64=true)

  # Allocate a new array to store values.
  value = Array.new

  # If CPU hot plug is enabled, then current cpu count < max cpu count
  count = hyper.cpu_count.to_s 
  if (hyper.cpu_hot_plug)
     value << ["-smp", "cpus=#{count},maxcpus=255"]
  else
     value << ["-smp", "cpus=#{count},maxcpus=#{count}"]
  end
 
  # NOTE: CPU Execution Cap is not implemented in KVM
  # NOTE: Video RAM Size is not implemented in KVM

  # RAM Size is always translated into MiB
  value << ["-m", MiB(hyper.ram_size).to_s]

  # Memory Balloon size is ignored by KVM. It is just enabling or
  # disabling a feature.
  if MiB(hyper.guest_balloon_size) > 0
     value << ["-balloon", "none"]
  else
     value << ["-balloon", "virtio"]
  end

  # ACPI is by default enabled on KVM. We just have to handle the 
  # disabled case.
  if !hyper.acpi_enabled
     value << ["-no-acpi"]
  end

  # We have to use EFI firmware for virtual machines, if it has been
  # asked for.
  if hyper.use_efi
     value << ["-bios", "/usr/share/ovmf/OVMF.fd"]
  end

  # KVM works very differently w.r.t. CPU features. First a base CPU 
  # (64 bit or 32 bit) has to be selected and then more features need
  # to be added.
  cpu_features  =   (amd64)?'qemu64':'qemu32'
  cpu_features  +=  ',+pae'   if hyper.pae_enabled 
  cpu_features  +=  ',+vmx'   if hyper.hw_virt_enabled
  cpu_features  +=  ',+npt'   if hyper.hw_nested_pages
  cpu_features  +=  ',+pse36' if hyper.use_large_pages
  cpu_features  +=  ',+hypervisor' if hyper.unrestricted_guest_mode
  value << ["-cpu", cpu_features]

  # Set the value for QEMU Args
  self.qemuargs = value
end