2.15. 示例:使用 Python 创建虚拟机存储磁盘
为确保新创建的虚拟机有权访问持久性存储,您必须创建并附加磁盘。
例 2.14. 使用 Python 创建虚拟机存储磁盘
这个 Python 示例创建了一个 8 GB
在使用
virtio 磁盘驱动器,并将其附加到名为 vm1 的虚拟机。本例中的磁盘:
- 必须存储在名为
data1的存储域中,disk_storage_domain = params.StorageDomains(storage_domain=[api.storagedomains.get(name="data1")]) - 的大小必须为 8 GB,
disk_size = 8*1024*1024 - 必须是
系统类型磁盘(与数据相反)disk_type = "system" - 必须是
virtio存储设备,disk_interface = "virtio" - 必须以
cow格式存储,disk_format = "cow" - 必须标记为可用的引导设备。
disk_bootable = True
虚拟机磁盘集合的 add 方法来创建磁盘本身之前,这些选项将合并到磁盘 参数对象中。
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")
vm = api.vms.get(name="vm1")
sd = params.StorageDomains(storage_domain=[api.storagedomains.get(name="data1")])
disk_size = 8*1024*1024
disk_type = "system"
disk_interface = "virtio"
disk_format = "cow"
disk_bootable = True
disk_params = params.Disk(storage_domains=sd,
size=disk_size,
type_=disk_type,
interface=disk_interface,
format=disk_format,
bootable=disk_bootable)
try:
d = vm.disks.add(disk_params)
print "Disk '%s' added to '%s'." % (d.get_name(), vm.get_name())
except Exception as ex:
print "Adding disk to '%s' failed: %s" % (vm.get_name(), ex)
api.disconnect()
except Exception as ex:
print "Unexpected error: %s" % ex
如果
添加 请求成功,则脚本将输出:
Disk 'vm1_Disk1' added to 'vm1'.