[libvirt] for testing the new qemu specific regiater API

Eric Blake eblake at redhat.com
Sat Feb 4 00:09:28 UTC 2012


On 12/16/2011 09:59 AM, shaohef at linux.vnet.ibm.com wrote:
> From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
> 
> this patch is just used to the new register API
> 
> add two command, register-event and deregister-event in virsh in order to test the new API.
> 
> there will come an  "RESUME" events when qemu domains are started.
> 
> virish starts a qemu domain and to catch "RESUME" event.
> virsh# register-event RESUME
> virsh# start domain

I'd name this qemu-register-event, to make it obvious that it depends on
libvirt-qemu.

> +void printRegistEvent(virConnectPtr conn,
> +                      virDomainPtr dom,
> +                      const char *eventName, /* The JSON event name */
> +                      const char *eventArgs, /* The JSON string of args */
> +                      void *opaque){
> +    char *uriName = virConnectGetURI (conn);
> +    fputc('\n', stdout);
> +    if (uriName != NULL)
> +       fprintf(stdout, "connect URI: %s, ",uriName);
> +
> +    if (dom != NULL)
> +       fprintf(stdout, "dom: %s(%d) receive an event:\n", dom->name, dom->id);
> +
> +    if (eventName != NULL)
> +        fprintf(stdout, "{ event: %s", eventName);
> +    if (eventArgs != NULL)
> +        fprintf(stdout, ", data: %s }\n", eventArgs);
> +    else
> +        fprintf(stdout, " }\n");
> +    fprintf(stdout, "----------------------------------------------");
> +    fputc('\n', stdout);
> +}

Hmm, looks like it does as much as can be expected when receiving an event.

> +
> +static bool
> +cmdRegisterEvent(vshControl *ctl, const vshCmd *cmd)
> +{
> +    const char *name = NULL;
> +    virDomainPtr dom;
> +    bool ret = false;
> +    int eventID = -1;
> +
> +    if (!vshConnectionUsability(ctl, ctl->conn))
> +        return false;
> +
> +    if (vshCommandOptString(cmd, "event", &name) < 0) {
> +        vshError(ctl, "%s", _("Please specify valid event name"));
> +        return false;
> +    }
> +
> +    dom = NULL;
> +    eventID = virConnectDomainQemuEventRegister(ctl->conn, dom, name, printRegistEvent, NULL, NULL);
> +    if (eventID < 0) {
> +       fprintf(stdout, "%s event register error.\n", name);
> +       return false;
> +    }
> +    fprintf(stdout, "%s event call back ID is %d, you can use it to deregister the event.\n", name, eventID);
> +    ret = true;
> + cleanup:
> +    return ret;
> +}

What happens if virsh dies without deregistering the event?  I'm
guessing this is the sort of task where you'd either have to run virsh
in command mode (not batch mode).

Perhaps it might also be worth setting up a mode where you can do:

virsh qemu-register-event --wait domain RESUME &

which then blocks that virsh instance until a RESUME event arrives, so
that you can use backgrounded virsh tasks as a one-shot view into an
event firing.

> @@ -15363,6 +15467,8 @@ static const vshCmdDef domManagementCmds[] = {
>      {"vcpupin", cmdVcpuPin, opts_vcpupin, info_vcpupin, 0},
>      {"version", cmdVersion, opts_version, info_version, 0},
>      {"vncdisplay", cmdVNCDisplay, opts_vncdisplay, info_vncdisplay, 0},
> +    {"register-event", cmdRegisterEvent, opts_regevent, info_regevent, 0},
> +    {"deregister-event", cmdDeregisterEvent, opts_deregevent, info_deregevent, 0},
>      {NULL, NULL, NULL, NULL, 0}

Sort these new commands with the other qemu-* commands.

-- 
Eric Blake   eblake at redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 620 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20120203/2df626f2/attachment-0001.sig>


More information about the libvir-list mailing list