3.16. 将 ISO 镜像附加到虚拟机
要在新创建的虚拟机上安装客户端操作系统,您必须附加包含操作系统安装介质的 ISO 文件。要找到 ISO 文件,请参阅 列出 ISO 存储域中的文件。
例 3.14. 将 ISO 镜像附加到虚拟机
本示例将 my_iso_file.iso 附加到 vm1 虚拟机,并使用虚拟机的 cdroms 集合的 add 方法。
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',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Locate the service that manages the CDROM devices of the virtual machine:
cdroms_service = vm_service.cdroms_service()
# Get the first CDROM:
cdrom = cdroms_service.list()[0]
# Locate the service that manages the CDROM device found in previous step:
cdrom_service = cdroms_service.cdrom_service(cdrom.id)
# Change the CD of the VM to 'my_iso_file.iso'. By default the
# operation permanently changes the disk that is visible to the
# virtual machine after the next boot, but has no effect
# on the currently running virtual machine. If you want to change the
# disk that is visible to the current running virtual machine, change
# the `current` parameter's value to `True`.
cdrom_service.update(
cdrom=types.Cdrom(
file=types.File(
id='my_iso_file.iso'
),
),
current=False,
)
print("Attached CD to '%s'." % vm.name())
# Close the connection to the server:
connection.close()
如果 添加 请求成功,则输出文本的示例:
Attached CD to 'vm1'.
例 3.15. 从虚拟机中弹出一个 CDROM
本例从虚拟机的 cdrom 集合中弹出 ISO 镜像。
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',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Locate the service that manages the CDROM devices of the VM:
cdroms_service = vm_service.cdroms_service()
# Get the first found CDROM:
cdrom = cdroms_service.list()[0]
# Locate the service that manages the CDROM device found in previous step
# of the VM:
cdrom_service = cdroms_service.cdrom_service(cdrom.id)
cdrom_service.remove()
print("Removed CD from '%s'." % vm.name())
connection.close()
如果删除 请求成功,则示例输出文本:
或删除
Removed CD from 'vm1'.