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

- (Domain) initialize(pointer = nil)

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

- (Boolean) ==(other)

Provide a meaningful equality check so that two domains can easily be checked for equality. This works by comparing UUIDs.

Returns:

  • (Boolean)


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

def ==(other)
  other.is_a?(Domain) && other.uuid == uuid
end

- (Boolean) active?

Returns boolean of whether the domain is active (running) or not.

Returns:

  • (Boolean)


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

def active?
  result = FFI::Libvirt.virDomainIsActive(self)
  return nil if result == -1
  result == 1
end

- (Boolean) autostart=(value)

Sets the autostart status. This assignment sets the value immediately on the domain.

Parameters:

  • (Boolean) value

Returns:

  • (Boolean)

    The set value



147
148
149
150
# File 'lib/libvirt/domain.rb', line 147

def autostart=(value)
  FFI::Libvirt.virDomainSetAutostart(self, value ? 1 : 0)
  value
end

- (Boolean) autostart?

Returns boolean of whether the domain autostarts on boot.

Returns:

  • (Boolean)


136
137
138
139
140
# File 'lib/libvirt/domain.rb', line 136

def autostart?
  output_ptr = FFI::MemoryPointer.new(:int)
  return nil if FFI::Libvirt.virDomainGetAutostart(self, output_ptr) < 0
  output_ptr.read_int == 1
end

- (Integer) cpu_time_used

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

- (Boolean) create 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)


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

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

- (Boolean) destroy Also known as: stop

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

Returns:

  • (Boolean)


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

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

- (Integer) id

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

- (Integer) max_memory

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

- (Boolean) max_memory=(value)

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

- (Integer) max_virtual_cpus

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

- (Integer) memory

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

- (Boolean) memory=(value)

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

- (String) name

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

- (String) os_type

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

- (Boolean) persistent?

Returns boolean of whether the domain is persistent, or whether it will still exist after it is shut down.

Returns:

  • (Boolean)


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

def persistent?
  result = FFI::Libvirt.virDomainIsPersistent(self)
  return nil if result == -1
  result == 1
end

- (Boolean) reboot

Reboots the domain.

Returns:

  • (Boolean)


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

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

- (Boolean) resume

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

Returns:

  • (Boolean)


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

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

- (Boolean) shutdown

Shutdown a domain, stopping the domain OS.

Returns:

  • (Boolean)


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

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

- (Symbol) state

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

- (Boolean) suspend

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)


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

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

- (FFI::Pointer) to_ptr

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)


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

def to_ptr
  @pointer
end

- (Boolean) undefine

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

Returns:

  • (Boolean)


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

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

- (String) uuid

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

- (Integer) virtual_cpus

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

- (Boolean) virtual_cpus=(value)

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

- (String) xml

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