杰瑞科技汇

libvirt python rpm

Of course! Here is a comprehensive guide to the libvirt-python RPM, covering what it is, why you need it, how to install it, and how to use it.

libvirt python rpm-图1
(图片来源网络,侵删)

What is libvirt-python?

libvirt-python is the Python binding for the libvirt virtualization management API.

In simple terms, it's a software package that allows Python applications to communicate with and control a libvirt daemon. The libvirt daemon acts as a unified API and management layer for various virtualization technologies (like KVM, Xen, VMware, LXC, etc.).

Think of it this way:

  • libvirt (the daemon): The engine room that knows how to talk to different hypervisors (KVM, etc.).
  • libvirt-python (the RPM): The toolkit that gives Python programs the ability to send commands to the engine room.
  • Your Python Script: The captain that uses the toolkit to give orders (e.g., "start this VM," "check its memory usage").

Why Do You Need It?

You need to install the libvirt-python RPM on any machine where you plan to:

libvirt python rpm-图2
(图片来源网络,侵删)
  1. Write Python scripts to manage virtual machines (create, start, stop, delete, migrate, get console logs).
  2. Run applications that use Python to interact with virtual machines, such as:
    • OpenStack Nova (the compute component)
    • oVirt/RHEV (the management server and agents)
    • Proxmox VE (uses libvirt in its backend)
    • Cockpit (the web-based management interface)
    • Various custom automation and monitoring tools.

How to Install libvirt-python

The installation process depends on your Linux distribution.

On RHEL / CentOS / Fedora (using dnf or yum)

This is the most common scenario. The package is available in the standard repositories.

# For RHEL/CentOS 8+ and Fedora
sudo dnf install libvirt-python
# For older RHEL/CentOS 7
sudo yum install libvirt-python

Verification: After installation, you can verify that the package is installed and check its version.

# Check if the package is installed
rpm -q libvirt-python
# Example output:
# libvirt-python-8.0.0-1.el8.x86_64
# Check the Python module version
python3 -c "import libvirt; print(libvirt.getVersion())"
# Example output:
# (8, 0, 0)

On Ubuntu / Debian (using apt)

The package name is slightly different on Debian-based systems.

libvirt python rpm-图3
(图片来源网络,侵删)
sudo apt update
sudo apt install python3-libvirt

Note: On older Ubuntu/Debian systems, you might find python-libvirt (for Python 2). For any modern development, you should use python3-libvirt.

Prerequisites

To actually use libvirt-python, you also need the core libvirt packages and a hypervisor.

# Install the libvirt daemon and client tools
sudo dnf install libvirt libvirt-client
# Install KVM (the most common hypervisor for RHEL/Fedora)
sudo dnf install qemu-kvm
# Enable and start the libvirt service
sudo systemctl enable --now libvirtd

How to Use libvirt-python (A Simple Example)

Here is a basic Python script that connects to the local libvirt daemon, lists all running virtual machines, and prints their names.

Prerequisites for the Script

  1. You must have the libvirt service running (sudo systemctl start libvirtd).
  2. Your user must be in the libvirt group to connect without sudo. If not, run:
    sudo usermod -aG libvirt $USER
    # You will need to log out and log back in for this to take effect.

The Python Script

Save this code as list_vms.py:

#!/usr/bin/env python3
import libvirt
import sys
def main():
    # The default URI for a local libvirt connection
    # 'qemu:///system' connects to the system-wide QEMU/KVM instance
    conn = None
    try:
        # Connect to the hypervisor
        # For a remote connection, you would use: conn = libvirt.open('qemu+ssh://user@hostname/system')
        conn = libvirt.open('qemu:///system')
        if conn is None:
            print('Failed to open connection to qemu:///system', file=sys.stderr)
            return 1
        print(f'Successfully connected to libvirt, version: {conn.getVersion()}')
        print('-----------------------------------------')
        # List all defined (but not necessarily running) domains
        domains = conn.listAllDomains()
        if not domains:
            print('No domains (VMs) found.')
            return 0
        print('Found the following domains (VMs):')
        for domain in domains:
            # Get the domain's state
            # state: 0 = running, 1 = blocked, 2 = paused, 3 = shutdown, 4 = shutoff, 5 = crashed
            state, _ = domain.state()
            state_str = ['running', 'blocked', 'paused', 'shutdown', 'shutoff', 'crashed'][state]
            print(f'  - Name: {domain.name()} | ID: {domain.ID()} | State: {state_str}')
    except libvirt.libvirtError as e:
        print(f'Libvirt error: {e}', file=sys.stderr)
        return 1
    finally:
        # Always close the connection
        if conn is not None:
            conn.close()
    return 0
if __name__ == '__main__':
    sys.exit(main())

Running the Script

# Make the script executable
chmod +x list_vms.py
# Run it
./list_vms.py

Example Output:

Successfully connected to libvirt, version: 8000
-----------------------------------------
Found the following domains (VMs):
  - Name: fedora36 | ID: 1 | State: running
  - Name: centos8 | ID: -1 | State: shutoff

Key Takeaways

  • libvirt-python is an RPM that provides the Python bindings for the libvirt API.
  • Install it on any system where you need to write Python code to manage VMs.
  • The core installation command on RHEL/CentOS/Fedora is sudo dnf install libvirt-python.
  • It's a prerequisite for many major virtualization platforms like OpenStack and oVirt.
  • Always ensure your user is in the libvirt group and the libvirtd service is running to use it without sudo.
分享:
扫描分享到社交APP
上一篇
下一篇