Class: Libvirt::Domain

Inherits:
Object
  • Object
show all
Defined in:
lib/libvirt/domain.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dom_ptr) ⇒ Domain

Returns a new instance of Domain.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/libvirt/domain.rb', line 12

def initialize(dom_ptr)
  @dom_ptr = dom_ptr

  free = ->(obj_id) do
    Util.log(:debug) { "Finalize Libvirt::Domain 0x#{obj_id.to_s(16)} @dom_ptr=#{@dom_ptr}," }
    return unless @dom_ptr

    fr_result = FFI::Domain.virDomainFree(@dom_ptr)
    warn "Couldn't free Libvirt::Domain (0x#{obj_id.to_s(16)}) pointer #{@dom_ptr.address}" if fr_result.negative?
  end
  ObjectSpace.define_finalizer(self, free)
end

Class Method Details

.load_ref(dom_ptr) ⇒ Object

Raises:



5
6
7
8
9
10
# File 'lib/libvirt/domain.rb', line 5

def self.load_ref(dom_ptr)
  ref_result = FFI::Domain.virDomainRef(dom_ptr)
  raise Errors::LibError, "Couldn't retrieve domain reference" if ref_result.negative?

  new(dom_ptr)
end

Instance Method Details

#auto_startBoolean

Returns:

  • (Boolean)

Raises:



61
62
63
64
65
66
67
# File 'lib/libvirt/domain.rb', line 61

def auto_start
  value = ::FFI::MemoryPointer.new(:int)
  result = FFI::Domain.virDomainGetAutostart(@dom_ptr, value)
  raise Errors::LibError, "Couldn't get domain auto_start" if result.negative?

  value.read_int == 1
end

#free_domainObject

Raises:



107
108
109
110
111
112
# File 'lib/libvirt/domain.rb', line 107

def free_domain
  result = FFI::Domain.virDomainFree(@dom_ptr)
  raise Errors::LibError, "Couldn't free domain" if result.negative?

  @dom_ptr = nil
end

#get_metadata(type: :ELEMENT, uri: nil, flags: :AFFECT_CURRENT) ⇒ String

Retrieves metadata

Parameters:

  • type (Integer, Symbol) (defaults to: :ELEMENT)

    one of :ELEMENT, :TITLE, :DESCRIPTION

  • uri (String) (defaults to: nil)

    xml namespace (required for type element)

  • flags (Integer, Symbol) (defaults to: :AFFECT_CURRENT)

    one off AFFECT_CURRENT, AFFECT_CONFIG, AFFECT_LIVE AFFECT_CURRENT 0x0 - Affect current domain state. AFFECT_LIVE 0x1 - Affect running domain state. AFFECT_CONFIG 0x2 - Affect persistent domain state.

Returns:

  • (String)

    xml node, title, or description.

Raises:



196
197
198
199
200
201
# File 'lib/libvirt/domain.rb', line 196

def (type: :ELEMENT, uri: nil, flags: :AFFECT_CURRENT)
  result = FFI::Domain.(@dom_ptr, type, uri, flags)
  raise Errors::LibError, "Couldn't get domain metadata" if result.nil?

  result
end

#get_stateObject

Raises:



25
26
27
28
29
30
31
32
33
34
# File 'lib/libvirt/domain.rb', line 25

def get_state
  state = ::FFI::MemoryPointer.new(:int)
  reason = ::FFI::MemoryPointer.new(:int)
  result = FFI::Domain.virDomainGetState(@dom_ptr, state, reason, 0)
  raise Errors::LibError, "Couldn't get domain state" if result.negative?

  state_sym = FFI::Domain.enum_type(:state)[state.read_int]
  reason_sym = FFI::Domain.state_reason(state_sym, reason.read_int)
  [state_sym, reason_sym]
end

#max_memoryObject



89
90
91
# File 'lib/libvirt/domain.rb', line 89

def max_memory
  FFI::Domain.virDomainGetMaxMemory(@dom_ptr)
end

#max_vcpusObject



55
56
57
# File 'lib/libvirt/domain.rb', line 55

def max_vcpus
  FFI::Domain.virDomainGetMaxVcpus(@dom_ptr)
end

#nameObject

Raises:



48
49
50
51
52
53
# File 'lib/libvirt/domain.rb', line 48

def name
  result = FFI::Domain.virDomainGetName(@dom_ptr)
  raise Errors::LibError, "Couldn't retrieve storage pool name" if result.nil?

  result
end

#persistent?Boolean

Returns:

  • (Boolean)

Raises:



203
204
205
206
207
208
# File 'lib/libvirt/domain.rb', line 203

def persistent?
  result = FFI::Domain.virDomainIsPersistent(@dom_ptr)
  raise Errors::LibError, "Couldn't set domain metadata" if result.negative?

  result == 1
end

#power_off(flags = 0) ⇒ Object

Raises:



129
130
131
132
# File 'lib/libvirt/domain.rb', line 129

def power_off(flags = 0)
  result = FFI::Domain.virDomainDestroyFlags(@dom_ptr, flags)
  raise Errors::LibError, "Couldn't power off domain" if result.negative?
end

#reboot(flags = 0) ⇒ Object

Raises:



119
120
121
122
# File 'lib/libvirt/domain.rb', line 119

def reboot(flags = 0)
  result = FFI::Domain.virDomainReboot(@dom_ptr, flags)
  raise Errors::LibError, "Couldn't reboot domain" if result.negative?
end

#reset(flags = 0) ⇒ Object

Raises:



134
135
136
137
# File 'lib/libvirt/domain.rb', line 134

def reset(flags = 0)
  result = FFI::Domain.virDomainReset(@dom_ptr, flags)
  raise Errors::LibError, "Couldn't reset domain" if result.negative?
end

#resumeObject

Raises:



144
145
146
147
# File 'lib/libvirt/domain.rb', line 144

def resume
  result = FFI::Domain.virDomainResume(@dom_ptr)
  raise Errors::LibError, "Couldn't resume domain" if result.negative?
end

#save_memory(flags = :PAUSED) ⇒ Object

After save_memory(:PAUSED) you need to call #start and #resume to move domain to the running state.

Raises:



164
165
166
167
# File 'lib/libvirt/domain.rb', line 164

def save_memory(flags = :PAUSED)
  result = FFI::Domain.virDomainManagedSave(@dom_ptr, flags)
  raise Errors::LibError, "Couldn't save domain memory" if result.negative?
end

#screenshot(stream, display = 0) ⇒ Object

Raises:



97
98
99
100
101
102
103
104
105
# File 'lib/libvirt/domain.rb', line 97

def screenshot(stream, display = 0)
  dbg { "#screenshot stream=#{stream}, display=#{display}," }

  mime_type, pointer = FFI::Domain.virDomainScreenshot(@dom_ptr, stream.to_ptr, display, 0)
  raise Errors::LibError, "Couldn't attach domain screenshot" if pointer.null?

  # free pointer required
  mime_type
end

#set_auto_start(value) ⇒ Object

Parameters:

  • value (Boolean)

Raises:



71
72
73
74
75
# File 'lib/libvirt/domain.rb', line 71

def set_auto_start(value)
  value = value ? 1 : 0
  result = FFI::Domain.virDomainSetAutostart(@dom_ptr, value)
  raise Errors::LibError, "Couldn't set domain auto_start" if result.negative?
end

#set_metadata(metadata, type: :ELEMENT, key: nil, uri: nil, flags: :AFFECT_CURRENT) ⇒ Object

Sets metadata

Parameters:

  • metadata (String)

    xml node for element type, text for other types DESCRIPTION 0x0 - Operate on <description> TITLE 0x1 - Operate on <title> ELEMENT 0x2 - Operate on <metadata>

  • type (Integer, Symbol) (defaults to: :ELEMENT)

    one of :ELEMENT, :TITLE, :DESCRIPTION

  • key (String) (defaults to: nil)

    xml key (required for type element)

  • uri (String) (defaults to: nil)

    xml namespace (required for type element)

  • flags (Integer, Symbol) (defaults to: :AFFECT_CURRENT)

    one off AFFECT_CURRENT, AFFECT_CONFIG, AFFECT_LIVE AFFECT_CURRENT 0x0 - Affect current domain state. AFFECT_LIVE 0x1 - Affect running domain state. AFFECT_CONFIG 0x2 - Affect persistent domain state.

Raises:



182
183
184
185
# File 'lib/libvirt/domain.rb', line 182

def (, type: :ELEMENT, key: nil, uri: nil, flags: :AFFECT_CURRENT)
  result = FFI::Domain.(@dom_ptr, type, , key, uri, flags)
  raise Errors::LibError, "Couldn't set domain metadata" if result.negative?
end

#shutdown(flags = :ACPI_POWER_BTN) ⇒ Object

Raises:



124
125
126
127
# File 'lib/libvirt/domain.rb', line 124

def shutdown(flags = :ACPI_POWER_BTN)
  result = FFI::Domain.virDomainShutdownFlags(@dom_ptr, flags)
  raise Errors::LibError, "Couldn't shutdown domain" if result.negative?
end

#start(flags = 0) ⇒ Object

Raises:



114
115
116
117
# File 'lib/libvirt/domain.rb', line 114

def start(flags = 0)
  result = FFI::Domain.virDomainCreateWithFlags(@dom_ptr, flags)
  raise Errors::LibError, "Couldn't start domain" if result.negative?
end

#suspendObject

Raises:



139
140
141
142
# File 'lib/libvirt/domain.rb', line 139

def suspend
  result = FFI::Domain.virDomainSuspend(@dom_ptr)
  raise Errors::LibError, "Couldn't suspend domain" if result.negative?
end

#to_ptrObject



36
37
38
# File 'lib/libvirt/domain.rb', line 36

def to_ptr
  @dom_ptr
end

#undefine(options_or_flags = nil) ⇒ Object

Undefine a domain. If the domain is running, it’s converted to transient domain, without stopping it. If the domain is inactive, the domain configuration is removed.

Parameters:

  • options_or_flags (Array<Symbol>, Hash{Symbol=>Boolean}, Integer, Symbol, nil) (defaults to: nil)

Raises:

See Also:



156
157
158
159
160
# File 'lib/libvirt/domain.rb', line 156

def undefine(options_or_flags = nil)
  flags = Util.parse_flags options_or_flags, FFI::Domain.enum_type(:undefine_flags_values)
  result = FFI::Domain.virDomainUndefineFlags(@dom_ptr, flags)
  raise Errors::LibError, "Couldn't resume domain" if result.negative?
end

#uuidObject

Raises:



40
41
42
43
44
45
46
# File 'lib/libvirt/domain.rb', line 40

def uuid
  buff = ::FFI::MemoryPointer.new(:char, Util::UUID_STRING_BUFLEN)
  result = FFI::Domain.virDomainGetUUIDString(@dom_ptr, buff)
  raise Errors::LibError, "Couldn't get domain uuid" if result.negative?

  buff.read_string
end

#vcpusObject

def vcpus

# https://github.com/libvirt/ruby-libvirt/blob/9f71ff5add1f57ffef7cf513b72638d92d9fd84f/ext/libvirt/domain.c#L787
# dominfo = virDomainGetInfo
# dominfo.nrVirtCpu
# maxcpus = ruby_libvirt_get_maxcpus(ruby_libvirt_connect_get(d));
# vcpu_infos_ptr
FFI::Domain.virDomainGetVcpus(@dom_ptr, vcpu_infos_ptr, maxinfo, cpumaps, maplen)

end



85
86
87
# File 'lib/libvirt/domain.rb', line 85

def vcpus
  OpenStruct.new(count: max_vcpus)
end

#xml_desc(flags = 0) ⇒ Object



93
94
95
# File 'lib/libvirt/domain.rb', line 93

def xml_desc(flags = 0)
  FFI::Domain.virDomainGetXMLDesc(@dom_ptr, flags)
end