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

daggs
2019-01-07T12:49:18Z

Greetings Peter,

> Sent: Monday, January 07, 2019 at 11:06 AM
> From: "Peter Krempa" <pkrempa@redhat.com>
> To: daggs <daggs@gmx.com>
> Cc: libvirt-users@redhat.com
> Subject: Re: [libvirt-users] script called from qemu hook freezes.
>
> On Fri, Jan 04, 2019 at 17:27:48 +0100, daggs wrote:
> > Greetings Peter,
> >
> > > Sent: Friday, January 04, 2019 at 4:47 PM
> > > From: "Peter Krempa" <pkrempa@redhat.com>
> > > To: daggs <daggs@gmx.com>
> > > Cc: libvirt-users@redhat.com
> > > Subject: Re: [libvirt-users] script called from qemu hook
freezes.
> > >
> > > 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'>
> > > >
> > > >    <address bus='${busnum}'
device='${devnum}' />
> > > >
> > > > </hostdev>
> > > > END
> > > >
> > > > 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.
> > >
> >
> > I see, is there another way to do what I need (on startup usb hotplug)
or maybe optional hotplug (e.g. don't fail the device isn't present)?
>
> If you need programatic logic to decide which device to plug in you
> either use the above suggestion to use a daemon using libvirt API or
> fork off a separate process or do the logic prior to start by
> pre-constructing the XML and using 'virsh create xmlfile' which
will be
> used as the running config without modifying the persistent one.
>

Laine's suggestion was the missing part, now it is a udev issue as it seems
that not all the usb dev have settled when the rule is executed.

Dagg.