Class: Xhyve::Guest

Inherits:
Object
  • Object
show all
Defined in:
lib/xhyve/guest.rb

Overview

An object to represent a guest that we can start and stop Effectively, it’s a command wrapper around xhyve to provide an object oriented interface to a hypervisor guest

Constant Summary collapse

PCI_BASE =
3
NULLDEV =
'/dev/null'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Guest

Returns a new instance of Guest.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/xhyve/guest.rb', line 18

def initialize(**opts)
  @kernel = opts.fetch(:kernel)
  @initrd = opts.fetch(:initrd)
  @cmdline = opts.fetch(:cmdline)
  @blockdevs = [opts[:blockdevs] || []].flatten
  @memory = opts[:memory] || '500M'
  @processors = opts[:processors] || '1'
  @uuid = opts[:uuid] || SecureRandom.uuid
  @serial = opts[:serial] || 'com1'
  @acpi = opts[:acpi] || true
  @networking = opts[:networking] || true
  @foreground = opts[:foreground] || false
  @command = build_command
  @mac = find_mac
end

Instance Attribute Details

#macObject (readonly)

Returns the value of attribute mac.



16
17
18
# File 'lib/xhyve/guest.rb', line 16

def mac
  @mac
end

#pidObject (readonly)

Returns the value of attribute pid.



16
17
18
# File 'lib/xhyve/guest.rb', line 16

def pid
  @pid
end

#uuidObject (readonly)

Returns the value of attribute uuid.



16
17
18
# File 'lib/xhyve/guest.rb', line 16

def uuid
  @uuid
end

Instance Method Details

#ipObject



53
54
55
# File 'lib/xhyve/guest.rb', line 53

def ip
  @ip ||= Xhyve::DHCP.get_ip_for_mac(@mac)
end

#running?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/xhyve/guest.rb', line 49

def running?
  (true if Process.kill(0, @pid) rescue false)
end

#startObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/xhyve/guest.rb', line 34

def start
  outfile, infile = redirection
  @pid = spawn(@command, [:out, :err] => outfile, in: infile)
  if @foreground
    Process.wait(@pid) 
    outfile.cooked!
    infile.cooked!
  end
  @pid
end

#stopObject



45
46
47
# File 'lib/xhyve/guest.rb', line 45

def stop
  Process.kill('KILL', @pid)
end