Class: Libvirt::Domain

Inherits:
Object
  • Object
show all
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

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Parameters:

  • value (Boolean)

Returns:

  • (Boolean)

    The set value



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.

Returns:

  • (Boolean)


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_usedInteger

Returns the CPU time used in nanoseconds.

Returns:

  • (Integer)


102
103
104
# File 'lib/libvirt/domain.rb', line 102

def cpu_time_used
  domain_info[:cpuTime]
end

#createBoolean 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.

Returns:

  • (Boolean)


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

def create
  return true if active?
  FFI::Libvirt.virDomainCreate(self) == 0
end

#destroyBoolean Also known as: stop

Stops a running domain and returns a boolean of whether the call succeeded or not.

Returns:

  • (Boolean)


174
175
176
# File 'lib/libvirt/domain.rb', line 174

def destroy
  FFI::Libvirt.virDomainDestroy(self) == 0
end

#idInteger

Returns the hypervisor ID number for this domain.

Returns:

  • (Integer)


31
32
33
# File 'lib/libvirt/domain.rb', line 31

def id
  FFI::Libvirt.virDomainGetID(self)
end

#max_memoryInteger

Returns the maximum memory (in KB) allowed on this domain.

Returns:

  • (Integer)


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.

Returns:

  • (Boolean)

    Success of the command.



59
60
61
# File 'lib/libvirt/domain.rb', line 59

def max_memory=(value)
  FFI::Libvirt.virDomainSetMaxMemory(self, value) == 0
end

#max_virtual_cpusInteger

Returns the maximum number of virtual CPUs supported for this guest VM.

Returns:

  • (Integer)


95
96
97
# File 'lib/libvirt/domain.rb', line 95

def max_virtual_cpus
  FFI::Libvirt.virDomainGetMaxVcpus(self)
end

#memoryInteger

Returns the memory (in KB) currently allocated to this domain.

Returns:

  • (Integer)


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.

Returns:

  • (Boolean)

    Success of the command.



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

def memory=(value)
  FFI::Libvirt.virDomainSetMemory(self, value) == 0
end

#nameString

Returns the name of the domain as a string.

Returns:

  • (String)


15
16
17
# File 'lib/libvirt/domain.rb', line 15

def name
  FFI::Libvirt.virDomainGetName(self)
end

#os_typeString

Returns the OS type of the domain.

Returns:

  • (String)


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.

Returns:

  • (Boolean)


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

#rebootBoolean

Reboots the domain.

Returns:

  • (Boolean)


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

def reboot
  FFI::Libvirt.virDomainReboot(self, 0) == 0
end

#resumeBoolean

Resumes a suspended domain, returns a boolean of whether the call succeeded or not.

Returns:

  • (Boolean)


191
192
193
194
# File 'lib/libvirt/domain.rb', line 191

def resume
  return true if active?
  FFI::Libvirt.virDomainResume(self) == 0
end

#shutdownBoolean

Shutdown a domain, stopping the domain OS.

Returns:

  • (Boolean)


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

def shutdown
  FFI::Libvirt.virDomainShutdown(self) == 0
end

#specLibvirt::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

#stateSymbol

Returns the current state this domain is in.

Returns:

  • (Symbol)


45
46
47
# File 'lib/libvirt/domain.rb', line 45

def state
  domain_info[:state]
end

#suspendBoolean

Suspends an active domain, the process is frozen but the memory is still allocated. Returns a boolean of whether the call succeeded or not.

Returns:

  • (Boolean)


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

def suspend
  FFI::Libvirt.virDomainSuspend(self) == 0
end

#to_ptrFFI::Pointer

Provides the pointer to the domain. This allows this object to be used directly with the FFI layer which expects a virDomainPtr.

Returns:

  • (FFI::Pointer)


221
222
223
# File 'lib/libvirt/domain.rb', line 221

def to_ptr
  @pointer
end

#undefineBoolean

Undefine a domain. This will not stop it if it is running.

Returns:

  • (Boolean)


213
214
215
# File 'lib/libvirt/domain.rb', line 213

def undefine
  FFI::Libvirt.virDomainUndefine(self) == 0
end

#uuidString

Returns the UUID of the domain as a string.

Returns:

  • (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_cpusInteger

Returns the number of virtual CPUs for this domain.

Returns:

  • (Integer)


80
81
82
# File 'lib/libvirt/domain.rb', line 80

def virtual_cpus
  domain_info[:nrVirtCpu]
end

#virtual_cpus=(value) ⇒ Boolean

Sets the number of virtual CPUs for this domain.

Returns:

  • (Boolean)

    Success of the command.



87
88
89
# File 'lib/libvirt/domain.rb', line 87

def virtual_cpus=(value)
  FFI::Libvirt.virDomainSetVcpus(self, value) == 0
end

#xmlString

Returns the XML description of this domain.

Returns:

  • (String)


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

def xml
  # TODO: The flags in the 2nd parameter
  FFI::Libvirt.virDomainGetXMLDesc(self, 0)
end