Este conteúdo não está disponível no idioma selecionado.

2.16. Example: Creating a Virtual Machine Storage Disk using Python


To ensure a newly created virtual machine has access to persistent storage you must create and attach a disk.

Example 2.15. Creating a virtual machine storage disk using Python

This Python example creates an 8 GB virtio disk drive and attaches it to the virtual machine named vm1. The disk in this example:
  • must be stored on the storage domain named data1,
    disk_storage_domain = params.StorageDomains(storage_domain=[api.storagedomains.get(name="data1")])
    Copy to Clipboard Toggle word wrap
  • must be 8 GB in size,
    disk_size = 8*1024*1024
    Copy to Clipboard Toggle word wrap
  • must be a system type disk (as opposed to data),
    disk_type = "system"
    Copy to Clipboard Toggle word wrap
  • must be virtio storage device,
    disk_interface = "virtio"
    Copy to Clipboard Toggle word wrap
  • must be stored in cow format, and
    disk_format = "cow"
    Copy to Clipboard Toggle word wrap
  • must be marked as a usable boot device.
    disk_bootable = True
    Copy to Clipboard Toggle word wrap
These options are combined into a disk parameter object, before using the add method of the virtual machine's disks collection to create the disk itself.
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
Copy to Clipboard Toggle word wrap
If the add request is successful then the script will output:
Disk 'vm1_Disk1' added to 'vm1'.
Copy to Clipboard Toggle word wrap
Voltar ao topo
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2025 Red Hat