[libvirt-users] script called from qemu hook freezes.

Peter Krempa pkrempa at redhat.com
Fri Jan 4 14:47:10 UTC 2019

On Thu, Jan 03, 2019 at 18:07:58 +0100, daggs wrote:
> Greetings,
> I'm executing an external script when the qemu hook is called with start or release, the script is rather simple, upon start it iterates over the output of lsusb -t and for each device, it looks if it should be added to the vm we started, if so, it attaches it to the vm as follows:
> virsh --connect qemu:///system "${cmd}" "${domain}" /dev/stdin << END
> <hostdev mode='subsystem' type='usb'>
>   <source>
>    <address bus='${busnum}' device='${devnum}' />
>   </source>
> </hostdev>
> where cmd is attach-device, domain is the vm's name, busnum and devnum come from the output of the lsusb -t.
> my issue is that upon the first attach attempt, the cmd hangs, I need to kill it and after than I cannot preform any virsh cmd, I must restart the host.
> if I try to execute the same cmd after the vm is up, it works great.
> why the attach process gets stuck? do I need to execute it under different stage?

Hook scripts shall never call any libvirt API (even through virsh). At
the point when the hook script is called the VM startup process is
paused until the script returns. If the script attempts to modify the VM
it gets stuck as the VM is locked at that point.

You either need to add the device prior to startup, but AFAIK that is
not possible with a hook script or after but the script needs to return.

So you either fork off a process which will wait for the startup to
finish from the hook script or write an APP using the libvirt API which
will wait for the VM start event and then execute what's necessary.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvirt-users/attachments/20190104/73adf024/attachment.sig>

More information about the libvirt-users mailing list