How to add a hard disk to a VM via API
Introduction
This page describes how to add a hard disk to a VM.
You will need the following resources.
A VM in a VDC that supports hard disks (private cloud or vCloud Director).Â
If deployed without hot-reconfigure, you will need to power off the VM (you can do this with the API)Â
Potential data loss
Abiquo destroys hard disks when you detach them from the VM or when you undeploy or delete the VM
Always be careful when working with VM objects and put requests. If you accidentally remove a link, this could delete a disk or network interface card, for example
Your user role must have at least the following privileges:
ROLE_VDC_MANAGE_STORAGE
ROLE_VDC_MANAGE_STORAGE_CONTROLLER
ROLE_MANAGE_HARD_DISKS
Feature description and UI documentation:
Abiquo hard disks are non-persistent disks that are created on the hypervisor datastore.Â
Â
To add a hard disk to a VM through the API, the main steps are as follows.
Create a hard disk in the virtual datacenter
Update the VM to add the hard disk link in the disk sequenceÂ
This may appear to be different to how you add a hard disk through the UI, where you work directly in the VM. But behind the scenes, the UI uses the API to create the hard disk in the virtual datacenter and then adds it to the VM.
Summary diagram
The steps in this diagram link to the pages of the API reference guide for resources and data entities.
Â
Â
Detailed steps
To add a hard disk to a VM via API
Send a GET request to the API login resource to obtain an authorization token
curl --verbose -X GET "https://nardo40.bcn.abiquo.com:443/api/login" -u adminuser:password -k | jq .
 Use basic authentication and get the token from the X-Abiquo-Token header.
Get VMs from the cloud and keep the VM entity and links
Filter the VMs by vmlabel, for example, as shown in the following cURL. If the request is successful, the status code is 200. This request returns a collection of VMs, so you will need to get the VM from within the collection or use the edit link to obtain the VM by itself.Â
curl --verbose -k -X GET "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualmachines?vmlabel=yVM_addDisk" \ -H "Accept: application/vnd.abiquo.virtualmachines+json;version=5.0" \ -H "Authorization: Token XXXX..." | jq .
Keep the VM entity to create the VM object for the update. As well as the VM edit link, you will also need the harddisks, datastore tiers, diskX, and virtualdatacenter links.
VM edit link
{ "title": "ABQ_f5067776-9b43-4105-bb5a-4478ecf9b96c", "rel": "edit", "type": "application/vnd.abiquo.virtualmachine+json", "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19477" },
harddisks link
diskX links for all hard disks and volumes that are already attached to the VM. In this case, there is one HD in position 0 of the boot order and one volume in position 1. Add the new disk in the next position.
datastoretierX for hard disks
virtualdatacenter
Reference:Â https://wiki.abiquo.com/api/latest/AllVirtualMachinesResource.html#list-virtual-machines-of-the-user
Use the VDC link from the VM to get the VDC
For a successful request, the status code is 200.Â
From the virtual datacenter save the link with a rel value of "disks"
Create a data object for the hard disk
You can also specify a datastore tier with a link. Note that the datastore tier must be compatible with the datastore tier of the boot disk
Reference:Â https://wiki.abiquo.com/api/latest/harddisk.html
Create the hard disk in the VDC
Send a POST request to the virtual datacenter disks link with the disk data object, for example, with cURL
If the request is successful, the status code will be 201.Â
Keep the edit link to use to assign the hard disk to the VM
Create the disk link to add to the VM entity
Change the edit link to diskX with the next number in the disk sequence. In this case, for the third disk, we will use "disk2"
If the VM is deployed without hot reconfigure, power off the VM
Send a PUT request to the VM state link from the VM object. To power off, set the state attribute to "OFF". To shut down the VM, add the "gracefulShutdown" attribute and set it to "true". Note that the graceful shutdown may require VM tools.
If the request returns a status code of 202, that means it was accepted. However, you should always check that the VM is really powered off
Reference:Â https://wiki.abiquo.com/api/latest/VirtualMachinesResource.html#change-the-state-of-a-virtual-machine
Get the VM again using the VM edit link
For example, with cURL.
Ensure that your VM object is up to date and check that the VM is in the OFF state!
Reference:Â https://wiki.abiquo.com/api/latest/VirtualMachinesResource.html#get-a-virtual-machineAdd the disk link to the VM data object
Add the disk link to the VM object in the links section (usually at the end of the links, and being careful to add a comma between the previous link and the disk link). See #Sample VM data object to update the VM. Save the VM data object in a file, for example, virtualMachineHD.json.
Update the VM
For example, using cURL, and with the VM data object in a file named "virtualMachineHD.json".
Reference:Â https://wiki.abiquo.com/api/latest/VirtualMachinesResource.html#update-a-virtual-machine
A successful request will return a 204 no content link if the VM is not deployed. And if the VM is deployed, it will return a 202 - accepted request with a link to follow the progress of the operation.
When you check your VM, it should contain the new hard disk. See #Check that the hard disk is attached
Sample hard disk data object
This is an example of a harddisk data object after you create a hard disk in the VDC
Sample VM data object to update the VM
This is an example of a VM data object that includes the new hard disk link. You can save this data object to a file and reference the file in a cURL request using an @ character.
Check that the hard disk is attached
To check that the hard disk is attached, get the VM object again and check that the disk link is correct. The disk should also have a datastore tier.
Remove an auxiliary hard disk from a VM
To remove the hard disk from the VM, destroying the hard disk and the data on it, remove the link to the hard disk and update the VM.
Copyright © 2006-2024, Abiquo Holdings SL. All rights reserved