[Users] Register a disk image via oVirt REST api

Juan Hernandez jhernand at redhat.com
Sat Jan 18 16:26:30 UTC 2014


On 01/17/2014 11:28 PM, Itamar Heim wrote:
> On 01/16/2014 07:21 PM, Satya Vempati wrote:
>> I have a disk image (i.e. a file) that I want to register as a disk
>> using the REST API.
>>
>> The REST API works with existing disks, but does the API work with disk
>> images?
> 
> can you pleas explain what do you mean by an image compared to a disk?
> 

I think that what you want is to take a file that you have that contains
the image of a disk and create a disk in the system with the same content.

As far as I know we don't have any direct way to do this. You will need
to create an empty disk in oVirt, and then attach it to a virtual
machine. Once it is attached to that virtual machine then you can write
to it, but always via the virtual machine. For example, you can use the
API as follows to create a new disk that isn't attached to any VM:

curl \
-k \
-X POST \
-H "Accept: application/xml" \
-H "Content-Type: application/xml" \
-d "
<disk>
  <name>newdisk</name>
  <size>1073741824</size>
  <format>raw</format>
  <interface>virtio</interface>
</disk>
" \
-u admin at internal:****** \
https://rhel.example.com/api/storagedomains/the_id_of_the_storage_domain/disks

Then prepare a VM that you will use to copy the contents of your file to
the new disk, and use the API to attach the new disk to this VM (you can
preserve this VM, and use multiple times for this purpose):

curl \
-k \
-X POST \
-H "Accept: application/xml" \
-H "Content-Type: application/xml" \
-d "
<disk id='the_id_of_the_disk'/>
" \
-u admin at internal:****** \
https://rhel.example.com/api/vms/the_id_of_the_vm/disks

(Note that in order to attach the disk you have to provide the disk id
returned by the API when you created it.)

Then activate the disk, so that the VM can see it:

curl \
-k \
-X POST \
-H "Accept: application/xml" \
-H "Content-Type: application/xml" \
-d "
<action/>
" \
-u admin at internal:****** \
https://rhel.example.com/api/vms/the_id_of_the_vm/disks/the_id_of_the_disk/activate

Now you will have to copy the contents of the file to the disk via the
VM. For example, assuming that you have SSH enabled on that VM and that
the disk device inside the VM is /dev/vdb (it won't allways be this,
depends on the number and order of attached disks) you can do something
like this:

ssh root at myvm 'cat > /dev/vdb' < myfile.img
ssh root at myvm 'sync'

Once the contents of the file have been copied you can deactivate the
disk and detach it from this intermediate VM, and maybe attach it to
another one.

Take into account that all these operations are lengthy ones, specially
the operation to create the disk, and that the RESTAPI will usually
return once the operation is initiated, so you will need to wait till
they are finished. For example, when creating the disk you should poll
the state of the VM till it is "ok".

-- 
Dirección Comercial: C/Jose Bardasano Baos, 9, Edif. Gorbea 3, planta
3ºD, 28016 Madrid, Spain
Inscrita en el Reg. Mercantil de Madrid – C.I.F. B82657941 - Red Hat S.L.



More information about the Users mailing list