3.15. 创建虚拟机磁盘
为确保新创建的虚拟机可以访问持久性存储,您必须创建并附加磁盘。
例 3.13. 创建虚拟机磁盘
这个示例创建了一个 8 GB VirtIO 磁盘,并将其附加到虚拟机 vm1。这个磁盘有以下要求:
-
存储在名为
data1的存储域中。 - 大小为 8 GB。
-
系统类型磁盘(而不是数据)。 -
VirtIO存储设备. -
COW格式。 - 标记为可用的引导设备。
V4
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Locate the virtual machines service and use it to find the virtual
# machine:
vms_service = connection.system_service().vms_service()
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the disk attachments of the virtual
# machine:
disk_attachments_service = vms_service.vm_service(vm.id).disk_attachments_service()
# Use the "add" method of the disk attachments service to add the disk.
# Note that the size of the disk, the `provisioned_size` attribute, is
# specified in bytes, so to create a disk of 10 GiB the value should
# be 10 * 2^30.
disk_attachment = disk_attachments_service.add(
types.DiskAttachment(
disk=types.Disk(
format=types.DiskFormat.COW,
provisioned_size=8*1024*1024,
storage_domains=[
types.StorageDomain(
name='data1',
),
],
),
interface=types.DiskInterface.VIRTIO,
bootable=True,
active=True,
),
)
# Wait until the disk status is OK:
disks_service = connection.system_service().disks_service()
disk_service = disks_service.disk_service(disk_attachment.disk.id)
while True:
time.sleep(5)
disk = disk_service.get()
if disk.status == types.DiskStatus.OK:
break
print("Disk '%s' added to '%s'." % (disk.name(), vm.name()))
# Close the connection to the server:
connection.close()
如果 添加 请求成功,则输出文本的示例:
Disk 'vm1_Disk1' added to 'vm1'.