Use API to remove specific VM disks and add new disk images

  • 19 March 2019
  • 3 replies

Badge +1
We are on 5.10. I want to leverage the API(s) to remove disks from a VM, and then add new disks to a VM (using images as the sources).

Which API should I use? v3 has "Update a existing VM" (sic) but I am unclear about how to (a) remove scsi:1 and scsi:2 and (b) add new disks (using images).

The v2 API has "Detach disks" functionality. If I detach a disk does it get deleted? If not how do I delete it after detaching? "Attach disks" is also available in v2 but I don't see a way to use an image as the source (which I can do using ACLI).

Thanks in advance for your responses.

Best answer by CPatterson 10 December 2019, 15:32

View original

This topic has been closed for comments

3 replies

Userlevel 4
Badge +5

Hello @CPatterson 

Sorry for the late reply. 

Regarding your queries
A) → Detach a disk from V2 API, deletes the vdisk also. 
B) → Attach a disk V2 API has few parameters regarding VM clone 

“"vm_disk_clone": {
        "disk_address": {
          "device_bus": "SCSI",
          "device_index": 0,
          "ndfs_filepath": "string",
          "vmdisk_uuid": "string",
          "volume_group_uuid": "string"

You can try playing around with them to attach a disk. 

In the meanwhile, I’ll search the V3 API docs to achieve this. 

Badge +1

Thanks for the response.  I figured it out.

I used the v2 API to “detach” (delete) the disks from the VM.  This is the Ansible syntax using the URI module to remove disks 1 and 2 (disk 0 is the boot disk so I leave it alone):

- name: Detach disks scsi:1 and scsi:2
url: "{{ base_urlv2 }}/vms/{{ vm_uuid }}/disks/detach"
validate_certs: no
force_basic_auth: yes
method: POST
status_code: 200,201
user: "{{ username }}"
password: "{{ password }}"
return_content: no
body_format: json
- disk_address:
device_bus: "SCSI"
device_index: 2
- disk_address:
device_bus: "SCSI"
device_index: 1
register: retval
ignore_errors: yes

… and then I clone disk images from the image store, mounting them as SCSI 1 and SCSI 2.  This takes several steps:
1. Determine the UUID of the VM you want to mount the images on.
2. Determine the VMDisk ID for each image you want to attach to the VM.
3. Clone/mount the disk images.

The Ansible YAML for this is fairly long so I won’t post it here unless someone specifically asks for it.

Userlevel 6
Badge +5

@CPatterson are you able to upload to GitHub and post a URL here maybe? I am sure your findings would be of great value to the community.