class Pennyworth::Libvirt

Constants

LIBVIRT_NET_NAME
LIBVIRT_POOL_NAME

Public Class Methods

ensure_libvirt_env_started() click to toggle source
# File lib/pennyworth/libvirt.rb, line 24
def ensure_libvirt_env_started
  # The check here is unnecessary for technical reasons ("sysctl start" does
  # not fail if the service is already running), but it avoids an unnecessary
  # sudo password prompt.
  libvirtd_start unless libvirtd_active?

  libvirt_net_start unless libvirt_net_active?

  # As default pool is not always available but gets sometimes created by
  # tools like virt-manager, we need to create it ourself if it does not yet
  # exist.
  libvirt_pool_create unless libvirt_pool_exists?
end

Private Class Methods

libvirt_net_active?() click to toggle source
# File lib/pennyworth/libvirt.rb, line 56
def libvirt_net_active?
  output = with_c_locale do
    Cheetah.run "virsh", "-c", "qemu:///system", "net-list", "--all", :stdout => :capture
  end

  # The output looks like this:
  #
  #    Name                 State      Autostart     Persistent
  #   ----------------------------------------------------------
  #    default              active     no            yes
  #    vagrant0             active     yes           yes
  #
  output.split("\n")[2..-1].find do |line|
    line =~ /^\s*#{LIBVIRT_NET_NAME}\s+active/
  end
end
libvirt_net_start() click to toggle source
# File lib/pennyworth/libvirt.rb, line 52
def libvirt_net_start
  Cheetah.run "virsh", "-c", "qemu:///system", "net-start", LIBVIRT_NET_NAME
end
libvirt_pool_create() click to toggle source
# File lib/pennyworth/libvirt.rb, line 73
def libvirt_pool_create
  pool_config_file = File.dirname(__FILE__) + "/../../files/pool-default.xml"
  Cheetah.run "virsh", "-c", "qemu:///system", "pool-create", pool_config_file
end
libvirt_pool_exists?() click to toggle source
# File lib/pennyworth/libvirt.rb, line 78
def libvirt_pool_exists?
  output = with_c_locale do
    Cheetah.run "virsh", "-c", "qemu:///system", "pool-list", "--all", :stdout => :capture
  end

  # The output looks like this:
  #
  #    Name                 State      Autostart
  #   -----------------------------------------
  #    default              active     no
  #
  output.split("\n")[2..-1].detect { |line| line =~ /^\s*#{LIBVIRT_POOL_NAME}/ }
end
libvirtd_active?() click to toggle source
# File lib/pennyworth/libvirt.rb, line 46
def libvirtd_active?
  output = Cheetah.run "systemctl", "show", "--property", "ActiveState", "libvirtd", :stdout => :capture

  output == "ActiveState=active"
end
libvirtd_start() click to toggle source
# File lib/pennyworth/libvirt.rb, line 40
def libvirtd_start
  Cheetah.run "sudo", "systemctl", "start", "libvirtd"

  sleep 0.1 until File.exists?("/var/run/libvirt/libvirt-sock")
end