Libvirt python 应用开发(三)- 连接

3.1 Libvirt 的 python 模块提供了三种打开连接的方式:

conn = libvirt.open(name)
conn = libvirt.openReadonly(name)
conn = libvirt.openAuth(name, auth, flags)

上面三种方式中,参数 name 表示 hypervisor 的 URI 。下面两小节讨论URI格式。

3.2 本地 URI 有四种格式:

driver:///system
driver:///session
driver+unix:///system
driver+unix:///session

除了这四种格式,其他格式都是被认为是远程 URI,即使要连接的是本地 hypervisor。

截止2016年,支持的 driver 有以下类型,最常用的当然就是 qemu 了:

Driver

Description

qemu

For managing qemu and KVM guests

xen

For managing old-style (Xen 3.1 and older) Xen guests

xenapi

For managing new-style Xen guests

uml

For managing UML guests

lxc

For managing Linux Containers

vbox

For managing VirtualBox guests

openvz

For managing OpenVZ containers

esx

For managing VMware ESX guests

one

For managing OpenNebula guests

phyp

For managing Power Hypervisor guests

下面做一个打开本地连接的例子,但是请注意权限,我是在 root 下执行的:

# example1.py
# open local connection
# coding: utf-8

from __future__ import print_function
import sys
import libvirt

conn = libvirt.open("qemu:///system")
if conn == None:
    print("Failed to connect to qemu:///system", file=sys.stderr)
    exit(1)
else:
    print("Succeeded to connect to qemu:///system")
conn.close()
exit(0)

3.3 远程URI的格式如下:

driver[+transport]://[username@][hostname][:port]/[path][?extraparameters]

各字段的含义如下:

Component

Description

driver

The name of the libvirt hypervisor driver to connect to. This is the same as that used in a local URI. Some examples are xenqemulxcopenvz, and test. As a special case, the pseudo driver name remote can be used, which will cause the remote daemon to probe for an active hypervisor and pick one to use. As a general rule if the application knows what hypervisor it wants, it should always specify the explicit driver name and not rely on automatic probing.

transport

The name of one of the data transports described earlier in this section. Possible values include tlstcpunix,ssh and ext. If omitted, it will default to tls if a hostname is provided, or unix if no hostname is provided.

username

When using the SSH data transport this allows choice of a username that differs from the client's current login name.

hostname

The fully qualified hostname of the remote machine. If using TLS with x509 certificates, or SASL with the GSSAPI/Keberos plug-in, it is critical that this hostname match the hostname used in the server's x509 certificates / Kerberos principle. Mis-matched hostnames will guarantee authentication failures.

port

Rarely needed, unless SSH or libvirtd has been configured to run on a non-standard TCP port. Defaults to22 for the SSH data transport, 16509 for the TCP data transport and 16514 for the TLS data transport.

path

The path should be the same path used for the hypervisor driver's local URIs. For Xen, this is always just /, while for QEMU this would be /system.

extraparameters

The URI query parameters provide the mean to fine tune some aspects of the remote connection, and are discussed in depth in the next section.

根据上面的表格,举几个远程 URI 的例子:

# Connect to a remote Xen hypervisor on host node.example.com using ssh tunneled data transport and ssh username root:
 xen+ssh://[email protected]/

# Connect to a remote QEMU hypervisor on host node.example.com using TLS with x509 certificates: 
 qemu://node.example.com/system

# Connect to a remote Xen hypervisor on host node.example.com using TLS, skipping verification of the server's x509 certificate (NB: this is compromising your security):
 xen://node.example.com/?no_verify=1

# Connect to the local QEMU instances over a non-standard Unix socket (the full path to the Unix socket is supplied explicitly in this case): 
 qemu+unix:///system?socket=/opt/libvirt/run/libvirt/libvirt-sock

# Connect to a libvirtd daemon offering unencrypted TCP/IP connections on an alternative TCP port 5000 and use the test driver with default configuration: 
 test+tcp://node.example.com:5000/default

同样,写一个远程连接的例子, 这里使用了 ssh 认证的方式 :

# coding: utf-8

from __future__ import print_function
import sys
import libvirt

conn = libvirt.open('qemu+ssh://host2/system')
if conn == None:
    print('Failed to open connection to qemu+ssh://host2/system', file=sys.stderr)
    exit(1)
else:
    print('Succeeded to open connection to qemu+ssh://host2/system')
conn.close()
exit(0)

猜你喜欢

转载自my.oschina.net/dianxiaoer/blog/1634279