Skip to main content

Python package to orchestrate libvirt API from yaml declaration file

Project description

What is pylibvirt

Pylibvirt is a tools based on python-libvirt api. The goal are to create virtual machines, networks, volumes and storage more easily using yaml description file.

How to create template file

The template must contain at least the provider.

After you can declare just what you need.

If you just want to create a Storage on your target you can just add storage element.

All properties used in the yaml are the same as those used in libvirt

Provider

Currently the only tested provider is Qemu/Kvm

The URI format is the same as libvirt api.

provider:
    uri: qemu+ssh://root@ip/system

network

Mode is the type of network, values can be nat, route, open, hostdev and False for isolated network

network:
    - network_name:
          mode: nat
          domain: network
          ip4_cidr: ipv4_format_cidr
          dhcp_start: first_ip_to_use
          dhcp_stop: last_ip_to_use

storage

Only dir storage have been tested

storage:
    - storage_name:
          pool_type: dir
          path: storage_path

volume

pool is the storage name declared in libvirt or previously in yaml.

name is the end name set to the volume. It’s this name that must used in domain section.

The _key_ has no effect you can set what you want.

If you want to clone an existing volume or a previouslly create volume you can use the clone argument with the name of the volume. The two volumes must be in the same pool.

volume:
  - _key_:
      disk_format: qcow2
      capacity: 30
      size_unit: G
      pool: storage_name
      name: volume_name

domain

_key_ is the name of the VM, choose what you want.

Except the following key [‘memory’, ‘boot_order’, ‘DiskDevice’, ‘domain_type’,

‘Feature’, ‘Os’]

all other key must be the name of a class in pylibvirt/modules/devices

The class is called dynamically with the parameters. Example if you want to add Rng module add an item call RngDevice (class in pylibvirt/modules/devices/rng.py)

RngDevice:
    - first_rng_device:
        - arg_class: value
    - second_rng_device
        - model: virtio
        - backend_model: random
        - host_device: /dev/my_custom_random
domain:
  - _key_:
      boot_order:
        - cdrom
        - hd
      memory:
        mem_unit: G
        max_memory: 4
      Os:
        arch: x86_64
        machine: q35
        os_type: hvm
      Feature: # Features list: https://libvirt.org/formatdomain.html#hypervisor-features
        - acpi
        - kvm:
            hidden:
              state: 'on'
            poll-control:
              state: 'on'
      CpuDevice:
        cpu_model: host
        model_args:
          fallback: allow
        vcpu: 2
        vcpu_args:
          placement: static
      GraphicDevice:
        - spice_server:
            graphic_type: spice
      VideoDevice:
        - screen:
            model_type: qxl
            ram: 66500
      DiskDevice:
        - disk:
            volume: debian-10-2.qcow2
            driver: qemu
            bus: scsi
            pool: data
        - cdrom:
            volume: debian-10.10.0-amd64-netinst.iso
            pool: data

      NetworkInterfaceDevice:
        - default:
            net_interface: default
            net_type: network
            model: e1000e

How to install

Requirements

You need to install the following packages on your system to install python-libvirt in a virtualenv

Fedora

RPM dependencies

dnf install python3-devel pkgconfig libvirt-devel
pip install pylibvirt

Debian

DEB dependencies

apt install python3-dev pkg-config libvirt-dev
pip install pylibvirt

How to use

Cli usage

pylibvirt -t /path/to/template.yml

Use in python code

To use pylibvirt in your python code you can do:

call manager and set file path

import pylibvirt
pylibvirt.Manager(template='path_to_file')

or call manager and directly pass template object

import pylibvirt
pylibvirt.Manager(template=[yaml object])

Create your external module

If you want to create your own pylibvirt modules you must create a class that inherits from

from pylibvirt.modules.devices import Device

and implement method generate_data(self)

In your setup.py you must register it

from setuptools import setup

setup(
    name='cute_snek',
    entry_points={
        'pylibvirt_modules': [
            'ClassName = ModuleName',
        ],
    }
)

If you use poetry configure your toml like this

[tool.poetry.plugins."pylibvirt_modules"]
ClassName = 'ModuleName'

You can find example of modules in pylibvirt/modules/devices

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylibvirt-0.0.12.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pylibvirt-0.0.12-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

Details for the file pylibvirt-0.0.12.tar.gz.

File metadata

  • Download URL: pylibvirt-0.0.12.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for pylibvirt-0.0.12.tar.gz
Algorithm Hash digest
SHA256 88e008bdc8d345f495dd8071b35dba2f5fa43fa44569b3873b1a4716491ec10a
MD5 5359a546b465db3f8210c0bdcdd5e6bc
BLAKE2b-256 f8eda48c9dc1424d4421e0c2cdb6cc0319cf3f83530306f81d0b4de8b62a3c0a

See more details on using hashes here.

File details

Details for the file pylibvirt-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: pylibvirt-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 27.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for pylibvirt-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 2a0b3b8b513bc4198f817f45d286123c3a02a96927270406e9a6c4166770407e
MD5 eb445020ef11a1616d44f0fca38f1bf7
BLAKE2b-256 a14f0eebb148cb7d8b318cfe89f13184ec54aec3979bceb4416df7aa511f3493

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page