lago.providers.libvirt package

Submodules

lago.providers.libvirt.cpu module

class lago.providers.libvirt.cpu.CPU(spec, host_cpu)[source]

Bases: object

cpu_xml
generate_cpu_xml()[source]

Get CPU XML

Returns:cpu node
Return type:lxml.etree.Element
generate_custom(cpu, vcpu_num, fill_topology)[source]

Generate custom CPU model. This method attempts to convert the dict to XML, as defined by xmltodict.unparse method.

Parameters:
  • cpu (dict) – CPU spec
  • vcpu_num (int) – number of virtual cpus
  • fill_topology (bool) – if topology is not defined in cpu and vcpu was not set, will add CPU topology to the generated CPU.
Returns:

CPU XML node

Return type:

lxml.etree.Element

Raises:

LagoInitException – when failed to convert dict to XML

generate_exact(model, vcpu_num, host_cpu)[source]

Generate exact CPU model with nested virtualization CPU feature.

Parameters:
  • model (str) – libvirt supported CPU model
  • vcpu_num (int) – number of virtual cpus
  • host_cpu (lxml.etree.Element) – the host CPU model
Returns:

CPU XML node

Return type:

lxml.etree.Element

generate_feature(name, policy='require')[source]

Generate CPU feature element

Parameters:
  • name (str) – feature name
  • policy (str) – libvirt feature policy
Returns:

feature XML element

Return type:

lxml.etree.Element

generate_host_passthrough(vcpu_num)[source]

Generate host-passthrough XML cpu node

Parameters:vcpu_num (str) – number of virtual CPUs
Returns:CPU XML node
Return type:lxml.etree.Element
generate_numa(vcpu_num)[source]

Generate guest CPU <numa> XML child Configures 1, 2 or 4 vCPUs per cell.

Parameters:vcpu_num (str) – number of virtual CPUs
Returns:numa XML element
Return type:lxml.etree.Element
generate_topology(vcpu_num, cores=1, threads=1)[source]

Generate CPU <topology> XML child

Parameters:
  • vcpu_num (str) – number of virtual CPUs
  • cores (int) – number of cores
  • threads (int) – number of threads
Returns:

topology XML element

Return type:

lxml.etree.Element

generate_vcpu(vcpu_num)[source]

Generate <vcpu> domain XML child

Parameters:vcpu_num (str) – number of virtual cpus
Returns:vcpu XML element
Return type:lxml.etree.Element
generate_vcpu_xml(vcpu_num)[source]
Parameters:vcpu_num (int) – number of virtual cpus
Returns:vcpu XML node
Return type:lxml.etree.Element
model
validate()[source]

Validate CPU-related VM spec are compatible

Raises:LagoInitException – if both ‘cpu_model’ and ‘cpu_custom’ are defined.
vcpu_xml
vendor
class lago.providers.libvirt.cpu.LibvirtCPU[source]

Bases: object

Query data from /usr/share/libvirt/cpu_map.xml

classmethod get_cpu_props(family, arch='x86')[source]

Get CPU info XML

Parameters:
  • family (str) – CPU family
  • arch (str) – CPU arch
Returns:

CPU xml

Return type:

lxml.etree.Element

Raises:

LagoException – If no such CPU family exists

classmethod get_cpu_vendor(family, arch='x86')[source]

Get CPU vendor, if vendor is not available will return ‘generic’

Parameters:
  • family (str) – CPU family
  • arch (str) – CPU arch
Returns:

CPU vendor if found otherwise ‘generic’

Return type:

str

classmethod get_cpus_by_arch(arch)[source]

Get all CPUs info by arch

Parameters:arch (str) – CPU architecture
Returns:CPUs by arch XML
Return type:lxml.etree.element
Raises:LagoException – If no such ARCH is found
lago.providers.libvirt.cpu.create_xml_map(cpu_map_index_xml, cpu_map_dir)[source]

lago.providers.libvirt.network module

class lago.providers.libvirt.network.BridgeNetwork(env, spec, compat)[source]

Bases: lago.providers.libvirt.network.Network

_libvirt_xml()[source]
start()[source]

Start the network, will check if the network is active attempts times, waiting timeout between each attempt.

Parameters:
  • attempts (int) – number of attempts to check the network is active
  • timeout (int) – timeout for each attempt
Returns:

None

Raises:
  • RuntimeError – if network creation failed, or failed to verify it is
  • active.
stop()[source]
class lago.providers.libvirt.network.NATNetwork(env, spec, compat)[source]

Bases: lago.providers.libvirt.network.Network

_generate_dns_disable()[source]
_generate_dns_forward(forward_ip)[source]
_generate_main_dns(records, subnet, forward_plain='no')[source]
_ipv6_prefix(subnet, const='fd8f:1391:3a82:')[source]
_libvirt_xml()[source]
class lago.providers.libvirt.network.Network(env, spec, compat)[source]

Bases: object

_libvirt_name()[source]
_libvirt_xml()[source]
add_mapping(name, ip, save=True)[source]
add_mappings(mappings)[source]
alive()[source]
gw()[source]
is_management()[source]
mapping()[source]
mtu()[source]
name()[source]
resolve(name)[source]
save()[source]
spec
start(attempts=5, timeout=2)[source]

Start the network, will check if the network is active attempts times, waiting timeout between each attempt.

Parameters:
  • attempts (int) – number of attempts to check the network is active
  • timeout (int) – timeout for each attempt
Returns:

None

Raises:
  • RuntimeError – if network creation failed, or failed to verify it is
  • active.
stop()[source]

lago.providers.libvirt.utils module

lago.providers.libvirt.utils.DOMAIN_STATES = {<sphinx.ext.autodoc.importer._MockObject object>: 'no state', <sphinx.ext.autodoc.importer._MockObject object>: 'running', <sphinx.ext.autodoc.importer._MockObject object>: 'blocked', <sphinx.ext.autodoc.importer._MockObject object>: 'paused', <sphinx.ext.autodoc.importer._MockObject object>: 'begin shut down', <sphinx.ext.autodoc.importer._MockObject object>: 'shut off', <sphinx.ext.autodoc.importer._MockObject object>: 'crashed', <sphinx.ext.autodoc.importer._MockObject object>: 'suspended'}

Mapping of domain statuses values to human readable strings

class lago.providers.libvirt.utils.Domain[source]

Bases: object

Class to namespace libvirt domain related helpers

static resolve_state(state_number)[source]

Get a nice description from a domain state number

Parameters:state_number (list of int) – State number as returned by libvirt.virDomain.state()
Returns:
small human readable description of the domain state, unknown
if the state is not in the known list
Return type:str
lago.providers.libvirt.utils.LIBVIRT_CONNECTIONS = {}

Singleton with the cached opened libvirt connections

lago.providers.libvirt.utils.auth_callback(credentials, user_data)[source]
lago.providers.libvirt.utils.dict_to_xml(spec, full_document=False)[source]

Convert dict to XML

Parameters:
  • spec (dict) – dict to convert
  • full_document (bool) – whether to add XML headers
Returns:

XML tree

Return type:

lxml.etree.Element

lago.providers.libvirt.utils.get_domain_template(distro, libvirt_ver, **kwargs)[source]

Get a rendered Jinja2 domain template

Parameters:
  • distro (str) – domain distro
  • libvirt_ver (int) – libvirt version
  • kwargs (dict) – args for template render
Returns:

rendered template

Return type:

str

lago.providers.libvirt.utils.get_libvirt_connection(name)[source]
lago.providers.libvirt.utils.get_template(basename)[source]

Load a file as a string from the templates directory

Parameters:basename (str) – filename
Returns:string representation of the file
Return type:str
lago.providers.libvirt.utils.libvirt_callback(userdata, err)[source]

lago.providers.libvirt.vm module

exception lago.providers.libvirt.vm.LagoLocalLibvirtVMProviderException[source]

Bases: lago.utils.LagoException

class lago.providers.libvirt.vm.LocalLibvirtVMProvider(vm)[source]

Bases: lago.plugins.vm.VMProviderPlugin

_createXML(dom_xml, flags=0)[source]
_create_dead_snapshot(name)[source]
_create_live_snapshot(name)[source]
_get_domain()[source]

Return the object representation of this provider VM.

Returns:Libvirt domain object
Return type:libvirt.virDomain
Raises:exc:`~lago.plugins.vm.LagoFailedToGetVMStateError: If the VM exist, but the query returned an error.
_get_qemu_kvm_path()[source]
_libvirt_name()[source]
_libvirt_xml()[source]
_load_xml()[source]
_reclaim_disk(path)[source]
_reclaim_disks()[source]
_shutdown(libvirt_cmd, ssh_cmd, msg)[source]

Choose the invoking method (using libvirt or ssh) to shutdown / poweroff the domain.

If acpi is defined in the domain use libvirt, otherwise use ssh.

Parameters:
  • libvirt_cmd (function) – Libvirt function the invoke
  • ssh_cmd (list of str) – Shell command to invoke on the domain
  • msg (str) – Name of the command that should be inserted to the log message.
Returns
None
Raises:RuntimeError – If acpi is not configured an ssh isn’t available
alive()[source]
bootstrap()[source]

Does any actions needed to get the domain ready to be used, ran on prefix init. :returns: None

caps
cpu
cpu_model

VM CPU model

Returns:CPU model
Return type:str
cpu_vendor

VM CPU Vendor

Returns:CPU vendor
Return type:str
create_snapshot(name)[source]

Take any actions needed to create a snapshot :param name: Name for the snapshot, will be used as key to retrieve

it later
Returns:None
export_disks(standalone, dst_dir, compress, collect_only=False, with_threads=True, *args, **kwargs)[source]

Export all the disks of self.

Parameters:
  • standalone (bool) – if true, merge the base images and the layered image into a new file (Supported only in qcow2 format)
  • dst_dir (str) – dir to place the exported disks
  • compress (bool) – if true, compress each disk.
  • collect_only (bool) – If true, return only a dict which maps between the name of the vm to the paths of the disks that will be exported (don’t export anything).
  • with_threads (bool) – If True, export disks in parallel
Returns:

which maps between the name of the vm to the paths of the disks that will be exported

Return type:

(dict)

extract_paths(paths, ignore_nopath)[source]

Extract the given paths from the domain

Attempt to extract all files defined in paths with the method defined in extract_paths(), if it fails, and guestfs is available it will try extracting the files with guestfs.

Parameters:
  • paths (list of tuples) – files to extract in [(src1, dst1), (src2, dst2)…] format.
  • ignore_nopath (boolean) – if True will ignore none existing paths.
Returns:

None

Raises:
extract_paths_dead(paths, ignore_nopath)[source]

Extract the given paths from the domain using guestfs. Using guestfs can have side-effects and should be used as a second option, mainly when SSH is not available.

Parameters:
  • paths (list of str) – paths to extract
  • ignore_nopath (boolean) – if True will ignore none existing paths.
Returns:

None

Raises:
interactive_console()[source]

Opens an interactive console

Returns:result of the virsh command execution
Return type:lago.utils.CommandStatus
libvirt_ver
raw_state()[source]

Return the state of the domain in Libvirt’s terms

Retruns:
tuple of ints: The state and its reason
Raises:
  • LagoVMDoesNotExistError – If the VM of this provider doesn’t exist.
  • exc:`~lago.plugins.vm.LagoFailedToGetVMStateError: If the VM exist, but the query returned an error.
reboot(*args, **kwargs)[source]

Reboot a domain :returns: None

revert_snapshot(name)[source]

Take any actions needed to revert/restore a snapshot :param name: Name for the snapshot, same that was set on creation :type name: str

Returns:None
running()[source]

Returns: (bool): True if the VM is running

shutdown(*args, **kwargs)[source]

Shutdown a domain :returns: None

start()[source]

Start a domain :returns: None

state()[source]

Return a small description of the current status of the domain

Returns:small description of the domain status, ‘down’ if it’s not found at all.
Return type:str
stop()[source]

Stop a domain :returns: None

lago.providers.libvirt.vm.log_task(task, level='info', propagate_fail=True, uuid=None)

Parameterized decorator to wrap a function in a log task

Example

>>> @log_task('mytask')
... def do_something():
...     pass