Table of Contents |
---|
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)
Warningnote | |
---|---|
title | Potential data loss
|
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.
Tip |
---|
To add a hard disk to a VM through the API, the main steps are as follows.
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
We will walk through the following API request process with examples of the requests and data objects.
Table of Contents | ||||
---|---|---|---|---|
|
...
start-numbering-at | h4 |
---|
Get a VM by name
In the UI, copy the first part of the UUID field for the VM. Send a GET request to list all VMs with the "has" parameter to filter and only retrieve the VM with a name that matches the UUID.
Use the VDC link from the VM to get the VDC
Make a GET request to the virtualdatacenter link to obtain the virtual datacenter object. From the virtual datacenter object use the "disks" link. This is where you will create the disks.
This is the disks link.
By default your new disk will use the same datasatore tier as the existing disk.
Create a data object for the hard disk
...
The steps in this diagram link to the pages of the API reference guide for resources and data entities.
Mermaid | ||||||
---|---|---|---|---|---|---|
| ||||||
{"diagramDefinition":"graph TD\r\n\r\nZ(( ))\r\n\r\nstyle Z fill:#g1g,stroke:#100,stroke-width:20px\r\n\r\nZ-->A(Requires a VM in a VDC<br/> that supports hard disks)\r\n\r\nA -->B(<font color=0052cc>Log in and get token)\r\n\r\nclick B href \"https://wiki.abiquo.com/api/latest/LoginResource.html#login-and-get-the-current-user\" _blank\r\n\r\nB -->C(<font color=0052cc>Get VMs from the cloud)\r\n\r\nclick C href \"https://wiki.abiquo.com/api/latest/ |
...
Create a hard disk entity.
Create the hard disk in the VDC
To create the hard disk in the VDC, send a POST request to the virtual datacenter disks link with the disk data object.
In the response object, look for the edit link to the hard disk. You will need to modify this link and add it to the VM entity.
If necessary, power off the VM
If the VM is deployed and powered on, perform a PUT request to the VM state link, sending a VM state object to power it off. If your test platform supports "gracefulShutdown", you can use it as shown here. A status code of 202 means that the request was accepted. However, you should always check that the VM is really powered off.
Get the VM using the URL of the VM edit link
It's always best to get the VM again, just to check, and using the edit link from the VM object itself, instead of retrieving a list of one VM from the all VMs resource. This makes it easier to edit the VM object.
Modify the link to the hard disk to add it in the boot sequence of the VM
To add the hard disk to the VM, we will modify the VM entity and add a link to the hard disk. So the first step is to create a link to the hard disk.
First check if there are already any existing disks in the VM to determine the boot sequence. The disk "rel" (relation) link values are in the format "diskX", where "X" is the sequence number starting with 0 for the boot disk.
This is the boot disk of the VM taken from the VM object.
And this is the edit link of our hard disk.
So we should modify the edit link from the hard disk and change the "rel" link to specify that it should be included in the VM boot sequence as "disk3". So the link to send in the VM put request would look like this:
Next we will add this link to the VM.
Edit the VM data object to add the link to the hard disk
Add the link at the end of the links section. Don't forget to add a comma before the new link!
Modify the VM to add the hard disk
Now we need to send a PUT request to the VM to modify it and add the hard disk. The VM data object is enormous, so we saved it to a file and used the @ notation to specify the file. If the request is successful, for a VM that is not deployed, the status code returned will be a 204, with no content. If the VM is deployed, a successful request will return a status of 202 Accepted and an accepted request object, with a link to the reconfigure task.
Check the result in your VM
Get the VM to check that the disk has been added.
...
AllVirtualMachinesResource.html#list-virtual-machines-of-the-user\" _blank\r\n\r\nC-->D(<font color=0052cc>Get the virtual datacenter of the VM)\r\n\r\nclick D href \"https://wiki.abiquo.com/api/latest/VirtualDatacentersResource.html#get-a-virtual-datacenter\" _blank\r\n\r\nD-->E(<font color=0052cc>Create a data object<br/>for the hard disk)\r\n\r\nclick E href \"https://wiki.abiquo.com/api/latest/harddisk.html\" _blank\r\n\r\nE-->F(<font color=0052cc>Create a hard disk<br/>in a virtual datacenter)\r\n\r\nclick F href \"https://wiki.abiquo.com/api/latest/DisksManResource.html#create-a-hard-disk-in-a-virtual-datacenter\" _blank\r\n\r\nF-->G(<font color=0052cc>If necessary<br/>power off the VM)\r\n\r\nclick G href \"https://wiki.abiquo.com/api/latest/VirtualMachinesResource.html#change-the-state-of-a-virtual-machine\" _blank\r\n\r\nG-->H(<font color=0052cc>Get the VM again<br/>from its edit link)\r\n\r\nclick H href \"https://wiki.abiquo.com/api/latest/VirtualMachinesResource.html#get-a-virtual-machine\" _blank\r\n\r\nH-->I(Change the edit link of the<br/>hard disk to a diskX link<br/>and add to VM object)\r\n\r\nI-->J(<font color=0052cc>Update VM to add<br/>the hard disk)\r\n\r\nclick J href \"https://wiki.abiquo.com/api/latest/VirtualMachinesResource.html#update-a-virtual-machine\" _blank\r\n\r\nJ-->Q((<fontcolour=f3efe6> * ))\r\n\tstyle Q fill:#f3efe6,stroke:#030300,stroke-width:7px,color:#f3efe6"} |
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
Code Block 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.
Code Block 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
Code Block { "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
Code Block { "title": "disks", "rel": "harddisks", "type": "application/vnd.abiquo.harddisks+json", "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19477/storage/disks" },
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.
Code Block { "diskControllerType": "IDE", "length": "10", "title": "testvol1", "rel": "disk1", "type": "application/vnd.abiquo.volume+json", "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/locations/1/volumes/18087" }, { "diskControllerType": "IDE", "diskLabel": "Hard disk 1", "length": "64", "title": "2129870d-8f49-4486-90e9-c5f14f661d35", "rel": "disk0", "type": "application/vnd.abiquo.harddisk+json", "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/disks/18086" },
datastoretierX for hard disks
Code Block { "title": "datastoretierESXI6.0_2.29", "rel": "datastoretier0", "type": "application/vnd.abiquo.datastoretier+json", "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/locations/1/datastoretiers/58" },
virtualdatacenter
Code Block { "title": "vdc_api_tutorial", "rel": "virtualdatacenter", "type": "application/vnd.abiquo.virtualdatacenter+json", "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486" },
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
Code Block curl --verbose -k -X GET "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486" \ -H 'Accept: application/vnd.abiquo.virtualdatacenter+json;version=5.0' \ -H 'Authorization: Token XXXX' | jq .
For a successful request, the status code is 200.
From the virtual datacenter save the link with a rel value of "disks"
Code Block { "title": "disks", "rel": "disks", "type": "application/vnd.abiquo.harddisks+json", "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/disks" },
Create a data object for the hard disk
Code Block { "links":[], "sizeInMb":20, "sequence":2, "allocation":"THIN", "diskControllerType":"SCSI", "label":"test disk 04", "diskController":"lsilogic" }
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
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
Code Block curl --verbose -k -X POST "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/disks" \ -H "Accept: application/vnd.abiquo.harddisk+json;version=5.0" \ -H "Content-Type: application/vnd.abiquo.harddisk+json;version=5.0" \ -H "Authorization: Token XXXX" \ -d '{ "links":[ ], "sizeInMb":20, "sequence":2, "allocation":"THIN", "diskControllerType":"SCSI", "label":"testDisk02", "diskController":"lsilogic" }' | jq .
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
Code Block { "diskAllocation": "THIN", "diskController": "lsilogic", "diskControllerType": "SCSI", "diskLabel": "testDisk02", "rel": "edit", "type": "application/vnd.abiquo.harddisk+json", "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/disks/18092" }
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"
Code Block { "diskAllocation": "THIN", "diskController": "lsilogic", "diskControllerType": "SCSI", "diskLabel": "testDisk02", "rel": "disk2", "type": "application/vnd.abiquo.harddisk+json", "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/disks/18092" }
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.
Code Block curl --verbose -X PUT "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19477/state" \ -H "Content-Type: application/vnd.abiquo.virtualmachinestate+json;version=5.0" \ -d '{"state": "OFF", "gracefulShutdown": true}' \ -H "Authorization: Token XXXX" -k
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
Get the VM again using the VM edit link
For example, with cURL.
Code Block curl -X GET "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19477" \ -H "Accept: application/vnd.abiquo.virtualmachine+json;version=5.0" \ -u user:password | jq .
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".
Code Block curl --verbose -X PUT \ 'https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19477' \ -H 'Accept:application/vnd.abiquo.acceptedrequest+json;version=5.0' \ -H 'Content-type:application/vnd.abiquo.virtualmachine+json;version=5.0' \ -H 'Authorization: Token XXXX' \ -d @virtualMachineHD.json | jq .
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
Expand | ||
---|---|---|
| ||
|
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.
Expand | ||
---|---|---|
| ||
|
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.
Expand | ||
---|---|---|
| ||
|
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.