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)
- 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.
- See VM storage#CreateHardDisksonHypervisorDatastores.
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
We will walk through the following API request process with examples of the requests and data objects.
1. 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.
curl -X GET "https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualmachines?has=743bfe9e" \ -H "Accept: application/vnd.abiquo.virtualmachines+json;version=4.2" \ -u user:password | jq .
2. 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.
curl --verbose -X GET "https://mjsabiquo.bcn.abiquo.com/api/cloud/virtualdatacenters/3" \ -H "Accept: application/vnd.abiquo.virtualdatacenter+json;version=4.2" \ -u user:password | jq .
This is the disks link.
https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/3/disks
By default your new disk will use the same datasatore tier as the existing disk.
3. Create a data object for the hard disk
Create a hard disk entity.
{{ "links":[], "sizeInMb":64, "sequence":2, "allocation":"THIN", "diskControllerType":"SCSI", "label":"test disk 04", "diskController":"lsilogic" }
4. 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.
curl --verbose -X POST "https://example.com:443/api/cloud/virtualdatacenters/3/disks" \ -H "Accept: application/vnd.abiquo.harddisk+json;version=4.2" \ -H "Content-Type: application/vnd.abiquo.harddisk+json;version=4.2" \ -u user:password -k \ -d '{ "links":[ ], "sizeInMb":64, "sequence":2, "allocation":"THIN", "diskControllerType":"SCSI", "label":"test disk 04", "diskController":"lsilogic" }' | jq .
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.
5. 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.
curl --verbose -X PUT "https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/3/virtualappliances/4/virtualmachines/182/state" \ -H "Content-Type: application/vnd.abiquo.virtualmachinestate+json;version=4.2" \ -d '{"state": "OFF", "gracefulShutdown": true}' \ -u user:password -k
6. 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.
https://mjsabiquo.bcn.abiquo.com.com:443/api/cloud/virtualdatacenters/3/virtualappliances/4/virtualmachines/182
7. 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.
{ "diskAllocation": "THIN", "diskControllerType": "IDE", "diskLabel": "Hard disk 1", "length": "64", "title": "bbda1534-5e6c-4d47-8482-c282f383fabd", "rel": "disk0", "type": "application/vnd.abiquo.harddisk+json", "href": "https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/3/disks/6" },
And this is the edit link of our hard disk.
{ "diskAllocation": "THIN", "diskController": "lsilogic", "diskControllerType": "SCSI", "diskLabel": "test disk 04", "rel": "edit", "type": "application/vnd.abiquo.harddisk+json", "href": "https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/3/disks/13" }
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:
{ "diskAllocation": "THIN", "diskController": "lsilogic", "diskControllerType": "SCSI", "diskLabel": "test disk 04", "rel": "disk3", "type": "application/vnd.abiquo.harddisk+json", "href": "https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/3/disks/13" }
Next we will add this link to the VM.
8. 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!
9. 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.
curl --verbose -X PUT "https://example.com:443/api/cloud/virtualdatacenters/3/virtualappliances/4/virtualmachines/182" \ -H "Content-Type: application/vnd.abiquo.virtualmachine+json;version=4.2" \ -d @vmwithhd.json \ -u user:password -k | jq .
10. Check the result in your VM
Get the VM to check that the disk has been added.
curl --verbose -X GET "https://example.com:443/api/cloud/virtualdatacenters/3/virtualappliances/4/virtualmachines/182" \ -H "Accept: application/vnd.abiquo.virtualmachine+json;version=4.2" \ -u user:password -k | jq .
The disk was added successfully because this disk3 link can be found in the following VM object.
{ "diskAllocation": "THIN", "diskController": "lsilogic", "diskControllerType": "SCSI", "diskLabel": "test disk 04", "rel": "disk3", "type": "application/vnd.abiquo.harddisk+json", "href": "https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/3/disks/13" },