Class: Libvirt::Domain
- Inherits:
-
Object
- Object
- Libvirt::Domain
- Defined in:
- lib/libvirt/domain.rb
Overview
Represents a domain within libvirt, which is a single virtual machine or environment, typically.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Provide a meaningful equality check so that two domains can easily be checked for equality.
-
#active? ⇒ Boolean
Returns boolean of whether the domain is active (running) or not.
-
#autostart=(value) ⇒ Boolean
Sets the autostart status.
-
#autostart? ⇒ Boolean
Returns boolean of whether the domain autostarts on boot.
-
#cpu_time_used ⇒ Integer
Returns the CPU time used in nanoseconds.
-
#create ⇒ Boolean
(also: #start)
Starts the domain (moves it from the inactive to running state), and returns a boolean of whether the call succeeded or not.
-
#destroy ⇒ Boolean
(also: #stop)
Stops a running domain and returns a boolean of whether the call succeeded or not.
-
#id ⇒ Integer
Returns the hypervisor ID number for this domain.
-
#initialize(pointer = nil) ⇒ Domain
constructor
Initializes a new Domain object.
-
#max_memory ⇒ Integer
Returns the maximum memory (in KB) allowed on this domain.
-
#max_memory=(value) ⇒ Boolean
Sets the maximum memory (in KB) allowed on this domain.
-
#max_virtual_cpus ⇒ Integer
Returns the maximum number of virtual CPUs supported for this guest VM.
-
#memory ⇒ Integer
Returns the memory (in KB) currently allocated to this domain.
-
#memory=(value) ⇒ Boolean
Sets the memory (in KB) on an active domain.
-
#name ⇒ String
Returns the name of the domain as a string.
-
#os_type ⇒ String
Returns the OS type of the domain.
-
#persistent? ⇒ Boolean
Returns boolean of whether the domain is persistent, or whether it will still exist after it is shut down.
-
#reboot ⇒ Boolean
Reboots the domain.
-
#resume ⇒ Boolean
Resumes a suspended domain, returns a boolean of whether the call succeeded or not.
-
#shutdown ⇒ Boolean
Shutdown a domain, stopping the domain OS.
-
#spec ⇒ Libvirt::Spec::Domain
Returns the Spec::Domain object representing this domain.
-
#state ⇒ Symbol
Returns the current state this domain is in.
-
#suspend ⇒ Boolean
Suspends an active domain, the process is frozen but the memory is still allocated.
-
#to_ptr ⇒ FFI::Pointer
Provides the pointer to the domain.
-
#undefine ⇒ Boolean
Undefine a domain.
-
#uuid ⇒ String
Returns the UUID of the domain as a string.
-
#virtual_cpus ⇒ Integer
Returns the number of virtual CPUs for this domain.
-
#virtual_cpus=(value) ⇒ Boolean
Sets the number of virtual CPUs for this domain.
-
#xml ⇒ String
Returns the XML description of this domain.
Constructor Details
#initialize(pointer = nil) ⇒ Domain
Initializes a new Libvirt::Domain object. If you're calling this directly,
omit the pointer
argument, since that is meant for internal use.
7 8 9 10 |
# File 'lib/libvirt/domain.rb', line 7 def initialize(pointer=nil) @pointer = pointer if pointer.is_a?(FFI::Pointer) ObjectSpace.define_finalizer(self, method(:finalize)) end |
Instance Method Details
#==(other) ⇒ Boolean
Provide a meaningful equality check so that two domains can easily be checked for equality. This works by comparing UUIDs.
229 230 231 |
# File 'lib/libvirt/domain.rb', line 229 def ==(other) other.is_a?(Domain) && other.uuid == uuid end |
#active? ⇒ Boolean
Returns boolean of whether the domain is active (running) or not.
125 126 127 128 129 |
# File 'lib/libvirt/domain.rb', line 125 def active? result = FFI::Libvirt.virDomainIsActive(self) return nil if result == -1 result == 1 end |
#autostart=(value) ⇒ Boolean
Sets the autostart status. This assignment sets the value immediately on the domain.
155 156 157 158 |
# File 'lib/libvirt/domain.rb', line 155 def autostart=(value) FFI::Libvirt.virDomainSetAutostart(self, value ? 1 : 0) value end |
#autostart? ⇒ Boolean
Returns boolean of whether the domain autostarts on boot.
144 145 146 147 148 |
# File 'lib/libvirt/domain.rb', line 144 def autostart? output_ptr = FFI::MemoryPointer.new(:int) return nil if FFI::Libvirt.virDomainGetAutostart(self, output_ptr) < 0 output_ptr.read_int == 1 end |
#cpu_time_used ⇒ Integer
Returns the CPU time used in nanoseconds.
102 103 104 |
# File 'lib/libvirt/domain.rb', line 102 def cpu_time_used domain_info[:cpuTime] end |
#create ⇒ Boolean Also known as: start
Starts the domain (moves it from the inactive to running state), and returns a boolean of whether the call succeeded or not.
164 165 166 167 |
# File 'lib/libvirt/domain.rb', line 164 def create return true if active? FFI::Libvirt.virDomainCreate(self) == 0 end |
#destroy ⇒ Boolean Also known as: stop
Stops a running domain and returns a boolean of whether the call succeeded or not.
174 175 176 |
# File 'lib/libvirt/domain.rb', line 174 def destroy FFI::Libvirt.virDomainDestroy(self) == 0 end |
#id ⇒ Integer
Returns the hypervisor ID number for this domain.
31 32 33 |
# File 'lib/libvirt/domain.rb', line 31 def id FFI::Libvirt.virDomainGetID(self) end |
#max_memory ⇒ Integer
Returns the maximum memory (in KB) allowed on this domain.
52 53 54 |
# File 'lib/libvirt/domain.rb', line 52 def max_memory domain_info[:maxMem] end |
#max_memory=(value) ⇒ Boolean
Sets the maximum memory (in KB) allowed on this domain.
59 60 61 |
# File 'lib/libvirt/domain.rb', line 59 def max_memory=(value) FFI::Libvirt.virDomainSetMaxMemory(self, value) == 0 end |
#max_virtual_cpus ⇒ Integer
Returns the maximum number of virtual CPUs supported for this guest VM.
95 96 97 |
# File 'lib/libvirt/domain.rb', line 95 def max_virtual_cpus FFI::Libvirt.virDomainGetMaxVcpus(self) end |
#memory ⇒ Integer
Returns the memory (in KB) currently allocated to this domain.
66 67 68 |
# File 'lib/libvirt/domain.rb', line 66 def memory domain_info[:memory] end |
#memory=(value) ⇒ Boolean
Sets the memory (in KB) on an active domain.
73 74 75 |
# File 'lib/libvirt/domain.rb', line 73 def memory=(value) FFI::Libvirt.virDomainSetMemory(self, value) == 0 end |
#name ⇒ String
Returns the name of the domain as a string.
15 16 17 |
# File 'lib/libvirt/domain.rb', line 15 def name FFI::Libvirt.virDomainGetName(self) end |
#os_type ⇒ String
Returns the OS type of the domain.
38 39 40 |
# File 'lib/libvirt/domain.rb', line 38 def os_type FFI::Libvirt.virDomainGetOSType(self) end |
#persistent? ⇒ Boolean
Returns boolean of whether the domain is persistent, or whether it will still exist after it is shut down.
135 136 137 138 139 |
# File 'lib/libvirt/domain.rb', line 135 def persistent? result = FFI::Libvirt.virDomainIsPersistent(self) return nil if result == -1 result == 1 end |
#reboot ⇒ Boolean
Reboots the domain.
199 200 201 |
# File 'lib/libvirt/domain.rb', line 199 def reboot FFI::Libvirt.virDomainReboot(self, 0) == 0 end |
#resume ⇒ Boolean
Resumes a suspended domain, returns a boolean of whether the call succeeded or not.
191 192 193 194 |
# File 'lib/libvirt/domain.rb', line 191 def resume return true if active? FFI::Libvirt.virDomainResume(self) == 0 end |
#shutdown ⇒ Boolean
Shutdown a domain, stopping the domain OS.
206 207 208 |
# File 'lib/libvirt/domain.rb', line 206 def shutdown FFI::Libvirt.virDomainShutdown(self) == 0 end |
#spec ⇒ Libvirt::Spec::Domain
Returns the Spec::Domain object representing this domain.
118 119 120 |
# File 'lib/libvirt/domain.rb', line 118 def spec Spec::Domain.new(xml) end |
#state ⇒ Symbol
Returns the current state this domain is in.
45 46 47 |
# File 'lib/libvirt/domain.rb', line 45 def state domain_info[:state] end |
#suspend ⇒ Boolean
Suspends an active domain, the process is frozen but the memory is still allocated. Returns a boolean of whether the call succeeded or not.
183 184 185 |
# File 'lib/libvirt/domain.rb', line 183 def suspend FFI::Libvirt.virDomainSuspend(self) == 0 end |
#to_ptr ⇒ FFI::Pointer
Provides the pointer to the domain. This allows this object to be used
directly with the FFI layer which expects a virDomainPtr
.
221 222 223 |
# File 'lib/libvirt/domain.rb', line 221 def to_ptr @pointer end |
#undefine ⇒ Boolean
Undefine a domain. This will not stop it if it is running.
213 214 215 |
# File 'lib/libvirt/domain.rb', line 213 def undefine FFI::Libvirt.virDomainUndefine(self) == 0 end |
#uuid ⇒ String
Returns the UUID of the domain as a string.
22 23 24 25 26 |
# File 'lib/libvirt/domain.rb', line 22 def uuid output_ptr = FFI::MemoryPointer.new(:char, 36) FFI::Libvirt.virDomainGetUUIDString(self, output_ptr) output_ptr.read_string end |
#virtual_cpus ⇒ Integer
Returns the number of virtual CPUs for this domain.
80 81 82 |
# File 'lib/libvirt/domain.rb', line 80 def virtual_cpus domain_info[:nrVirtCpu] end |