Adding an Image to template during a CREATE Hook

I have a CREATE VM hook that gets the template data and based on User templates and VM template it creates an DATABLOCK image, and uploads it to Opennebula. I have the VM ID and IMAGE ID (In ready state), and I want to add it to the VM before it boots.
I have tried:

onevm disk-attach "$VMID" --image "$IMAGEID"

and get

[VirtualMachineAttach] VM is not running in any host

Any suggestions?

hooks are asynchronous, you cannot guarantee that the datastore will be created before that. However I think you can accomplish what you want by using a VOLATILE disk.
http://docs.opennebula.org/5.2/operation/references/template.html#volatile-disks

Thanks Jaime, I got this working, my next question is, is there a way to get the total number of nic cards attached to a VM using Hooks? Someting like:

NIC=$XPATH -b $T64 TEMPLATE/NIC`

Doesn’t working

Any thoughts, thanks

Hello Greg,

I think this is what you are looking for:

[oneadmin]$ onevm show 34 -x | /var/lib/one/remotes/datastore/xpath.rb -s %m%TEMPLATE/NIC/MAC
02:00:0a:02:08:80 02:00:0a:c7:c7:01

Then count the returned elements…

Kind Regards,
Anton Todorov