Naxi
Naxi

Reputation: 2044

How do I resize an already attached Disk to a VM using Ovirt

I am new to ovirt and am trying to increase the size of an already attached disk to my VM. Here is a good example for the same: Ovirt SDK example .

The only issue with this is that in this example, we are attaching the disk first and then resizing it. This way I have access to disk_attachment which is used later on to update the size. For me this is not an option as I am not attaching the disk myself as it happens automatically from the template.

//Attach disk first

disk_attachment = disk_attachments_service.add(
    types.DiskAttachment(
        disk=types.Disk(
            name='mydisk',
            description='my disk',
            format=types.DiskFormat.COW,
            provisioned_size=10 * 2**30,
            storage_domains=[
                types.StorageDomain(
                    name='bs-scsi-012',
                ),
            ],
        ),
        interface=types.DiskInterface.VIRTIO,
        bootable=False,
        active=True,
    ),
)

//update

# Find the service that manages the disk attachment that was added in the
# previous step:
disk_attachment_service = disk_attachments_service.attachment_service(disk_attachment.id)

Is there a way I can get my hands at disk_attachment.id so that I can fire the update operation or is there an alternate way to achieve the same?

Upvotes: 0

Views: 1624

Answers (1)

Shani
Shani

Reputation: 11

In case you need to find the disk attachment id, you can use this SDK example. This one lists the VM disks and some of their parameters, including their id.

Once you have the required disk id, you can use the following code (based on the example you've sheared):

# 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 disk attachments service and use it to find the revelant 
# disk attachment:
disk_attachments_service = vms_service.vm_service(vm.id).disk_attachments_service()
disk_attachment = disk_attachments_service.list(search='id=<the-disk-id>')[0]
disk_attachment_service = disk_attachments_service.attachment_service(disk_attachment.id)

# Extend the disk size to 3 GiB.
disk_attachment_service.update(
    types.DiskAttachment(
        disk=types.Disk(
            provisioned_size=3 * 2**30,
        ),
    ),
)

disks_service = connection.system_service().disks_service()
disk_service = disks_service.disk_service(disk_attachment.disk.id)

# Wait till the disk is OK:
while True:
    time.sleep(5)
    disk = disk_service.get()
    if disk.status == types.DiskStatus.OK:
        break

# Close the connection to the server:
connection.close()

I hope it helps.

Upvotes: 1

Related Questions