libvirt创建一个仅主机host网络请写出一个示例
下面是一个示例,演示了如何使用libvirt创建一个仅主机(host-only)网络。
import libvirt
# 连接到本地的QEMU/KVM hypervisor
conn = libvirt.open("qemu:///system")
# 定义网络XML配置
network_xml = """
<network>
<name>host-only</name>
<forward mode="none"/>
<bridge name="virbr0" stp="off" delay="0"/>
<ip address="192.168.100.1" netmask="255.255.255.0">
<dhcp>
<range start="192.168.100.2" end="192.168.100.254"/>
</dhcp>
</ip>
</network>
"""
# 创建网络
network = conn.networkCreateXML(network_xml)
# 启动网络
network.create()
# 关闭连接
conn.close()
在上面的示例中,我们首先使用libvirt.open()函数连接到本地的QEMU/KVM hypervisor。然后,我们定义了一个XML配置,其中<name>元素指定了网络的名称为"host-only",<bridge>元素定义了网络的桥接接口为"virbr0",<ip>元素定义了网络的IP地址和子网掩码,并使用<dhcp>元素配置了IP地址的动态分配范围。
接下来,我们使用conn.networkCreateXML()函数创建网络,并使用create()方法启动网络。最后,我们使用conn.close()关闭连接。
请注意,要运行此示例,您需要安装libvirt Python绑定,并且具有适当的权限来执行这些操作。
原文地址: https://www.cveoy.top/t/topic/iysj 著作权归作者所有。请勿转载和采集!