Ce contenu n'est pas disponible dans la langue sélectionnée.

2.15. 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.14. 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
Retour au début
Red Hat logoGithubredditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance. Découvrez nos récentes mises à jour.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez le Blog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

Theme

© 2025 Red Hat