Show Table of Contents
2.10. 示例:使用 Python 创建 NFS ISO 存储
要创建虚拟机,则需要为虚拟机操作系统提供安装介质。可在 Red Hat Virtualization 环境中将安装介质保存在 ISO 存储域中。
注意
这个示例中所提供的代码假设,远程 NFS 共享已经为在 Red Hat Virtualization 中使用进行了预配置。请参阅 Red Hat Virtualization 管理指南 来获得关于准备 NFS 共享的信息。
例 2.9. 使用 Python 创建 NFS ISO 存储
这个 Python 示例在
storagedomains 集合中添加了一个 NFS ISO 域。使用 Python 添加 NFS 存储域可以分为以下几步:
- 使用
datacenters集合的get方法指定存储必须被附加到的数据中心。dc = api.datacenters.get( name="Default" )
- 使用
hosts集合的get方法指定用来附加存储的主机。h = api.hosts.get(name="Atlantic")
- 指定 NFS 存储域的
Storage参数。在这个示例中,NFS 的位置是192.0.43.10/storage/iso。s = params.Storage(address="192.0.43.10", path="/storage/iso", type_="nfs")
- 使用
storagedomains集合的add方法请求创建存储域。除了Storage参数,还需要提供以下信息:- 存储域的名称。
- 从
datacenters集合获得的数据中心项。 - 从
hosts集合获得的主机项。 - 添加的存储域类型(
data、iso或export)。 - 使用的存储格式(
v1、v2或v3)。
以上的步骤被组合为:
from ovirtsdk.api import API
from ovirtsdk.xml import params
try:
api = API (url="https://HOST",
username="USER@DOMAIN",
password="PASS",
ca_file="ca.crt")
dc = api.datacenters.get(name="Default")
h = api.hosts.get(name="Atlantic")
s = params.Storage(address="192.0.43.10", path="/storage/iso", type_="nfs")
sd_params = params.StorageDomain(name="iso1", data_center=dc, host=h, type_="iso", storage_format="v3", storage=s)
try:
sd = api.storagedomains.add(sd_params)
print "Storage Domain '%s' added (%s)." % (sd.get_name())
except Exception as ex:
print "Adding storage domain failed: %s" % ex
api.disconnect()
except Exception as ex:
print "Unexpected error: %s" % ex
如果
add 方法调用成功,脚本会输出以下信息:
Storage Domain 'iso1' added (789814a7-7b90-4a39-a1fd-f6a98cc915d8).

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.