Show Table of Contents
2.16. 例: Python を使用した仮想マシンへの ISO イメージのアタッチ
新規作成した仮想マシンにゲストオペレーティングシステムのインストールを開始するには、オペレーティングシステムのインストールメディアを格納した ISO ファイルをアタッチする必要があります。
例2.15 ISO イメージの特定
ISO イメージは、ISO ストレージドメインにアタッチされた
files コレクション内にあります。以下の例は、ISO ストレージドメイン上の files コレクションのコンテンツを一覧表示します。
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")
sd = api.storagedomains.get(name="iso1")
iso = sd.files.list()
for i in iso:
print "%s" % i.get_name()
except Exception as ex:
print "Unexpected error: %s" % ex
成功した場合には、スクリプトにより、
files コレクション内のファイルごとに以下の例のようなエントリーが出力されます。
RHEL6.3-Server-x86_64-DVD1.iso
ISO ドメイン上のファイルには必ず一意名が付けられるため、ファイルの
id および name 属性が共有される点に注意してください。
例2.16 Python を使用した仮想マシンへの ISO イメージのアタッチ
以下の Python の例では、
RHEL6.3-Server-x86_64-DVD1.iso の ISO イメージファイルを vm1 仮想マシンにアタッチします。イメージファイルを特定し、仮想マシンの cdroms コレクションの 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")
sd = api.storagedomains.get(name="iso1")
cd_iso = sd.files.get(name="RHEL6.3-Server-x86_64-DVD1.iso")
cd_vm = api.vms.get(name="vm1")
cd_params = params.CdRom(file=cd_iso)
try:
cd_vm.cdroms.add(cd_params)
print "Attached CD to '%s'." % cd_vm.get_name()
except Exception as ex:
print "Failed to attach CD to '%s': %s" % (cd_vm.get_name(), ex)
api.disconnect()
except Exception as ex:
print "Unexpected error: %s" % ex
add 要求が成功した場合には、スクリプトにより以下のような出力が表示されます。
Attached CD to 'vm1'.
注記
上記の例は、ステータスが
Down の仮想マシンに ISO をアタッチするための手順です。ステータスが Up の仮想マシンに ISO をアタッチするには、第 2 の try ステートメントを以下のように変更してください。
try: cdrom=cd_vm.cdroms.get(id="00000000-0000-0000-0000-000000000000") cdrom.set_file(cd_iso) cdrom.update(current=True) print "Attached CD to '%s'." % cd_vm.get_name() except: print "Failed to attach CD to '%s': %s" % (cd_vm.get_name(), ex)
例2.17 Python を使用する、仮想マシンからの CD-ROM の取り出し
仮想マシンの
cdrom コレクションから ISO を取り出します。
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")
sd = api.storagedomains.get(name="iso1")
vm = api.vms.get(name="vm1")
try:
vm.cdroms.get(id="00000000-0000-0000-0000-000000000000").delete()
print "Removed CD from '%s'." % vm.get_name()
except Exception as ex:
print "Failed to remove CD from '%s': %s" % (vm.get_name(), ex)
api.disconnect()
except Exception as ex:
print "Unexpected error: %s" % ex
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.