Class: Libvirt::Domain
- Inherits:
-
Object
- Object
- Libvirt::Domain
- Defined in:
- lib/libvirt/domain.rb
Class Method Summary collapse
Instance Method Summary collapse
- #auto_start ⇒ Boolean
- #free_domain ⇒ Object
-
#get_metadata(type: :ELEMENT, uri: nil, flags: :AFFECT_CURRENT) ⇒ String
Retrieves metadata.
- #get_state ⇒ Object
-
#initialize(dom_ptr) ⇒ Domain
constructor
A new instance of Domain.
- #max_memory ⇒ Object
- #max_vcpus ⇒ Object
- #name ⇒ Object
- #persistent? ⇒ Boolean
- #power_off(flags = 0) ⇒ Object
- #reboot(flags = 0) ⇒ Object
- #reset(flags = 0) ⇒ Object
- #resume ⇒ Object
-
#save_memory(flags = :PAUSED) ⇒ Object
After save_memory(:PAUSED) you need to call #start and #resume to move domain to the running state.
- #screenshot(stream, display = 0) ⇒ Object
- #set_auto_start(value) ⇒ Object
-
#set_metadata(metadata, type: :ELEMENT, key: nil, uri: nil, flags: :AFFECT_CURRENT) ⇒ Object
Sets metadata.
- #shutdown(flags = :ACPI_POWER_BTN) ⇒ Object
- #start(flags = 0) ⇒ Object
- #suspend ⇒ Object
- #to_ptr ⇒ Object
-
#undefine(options_or_flags = nil) ⇒ Object
Undefine a domain.
- #uuid ⇒ Object
-
#vcpus ⇒ Object
def vcpus # 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.
- #xml_desc(flags = 0) ⇒ Object
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
Instance Method Details
#auto_start ⇒ Boolean
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_domain ⇒ Object
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
196 197 198 199 200 201 |
# File 'lib/libvirt/domain.rb', line 196 def (type: :ELEMENT, uri: nil, flags: :AFFECT_CURRENT) result = FFI::Domain.virDomainGetMetadata(@dom_ptr, type, uri, flags) raise Errors::LibError, "Couldn't get domain metadata" if result.nil? result end |
#get_state ⇒ Object
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_memory ⇒ Object
89 90 91 |
# File 'lib/libvirt/domain.rb', line 89 def max_memory FFI::Domain.virDomainGetMaxMemory(@dom_ptr) end |
#max_vcpus ⇒ Object
55 56 57 |
# File 'lib/libvirt/domain.rb', line 55 def max_vcpus FFI::Domain.virDomainGetMaxVcpus(@dom_ptr) end |
#name ⇒ Object
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
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
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
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
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 |
#resume ⇒ Object
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.
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
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
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
182 183 184 185 |
# File 'lib/libvirt/domain.rb', line 182 def (, type: :ELEMENT, key: nil, uri: nil, flags: :AFFECT_CURRENT) result = FFI::Domain.virDomainSetMetadata(@dom_ptr, type, , key, uri, flags) raise Errors::LibError, "Couldn't set domain metadata" if result.negative? end |
#shutdown(flags = :ACPI_POWER_BTN) ⇒ Object
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
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 |
#suspend ⇒ Object
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_ptr ⇒ Object
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.
156 157 158 159 160 |
# File 'lib/libvirt/domain.rb', line 156 def undefine( = nil) flags = Util.parse_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 |
#uuid ⇒ Object
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 |
#vcpus ⇒ Object
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 |