Re: [PATCH v4 5/8] screenshot: Expose the new API in virsh

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, May 12, 2011 at 06:29:12PM +0200, Michal Privoznik wrote:
> * tools/virsh.c: Add screenshot command
> * tools/virsh.pod: Document new command
> ---
>  tools/virsh.c   |   97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  tools/virsh.pod |    6 +++
>  2 files changed, 103 insertions(+), 0 deletions(-)
> 
> diff --git a/tools/virsh.c b/tools/virsh.c
> index fbeb7c8..9aa20ed 100644
> --- a/tools/virsh.c
> +++ b/tools/virsh.c
> @@ -1873,6 +1873,102 @@ cmdDump(vshControl *ctl, const vshCmd *cmd)
>      return ret;
>  }
>  
> +static const vshCmdInfo info_screenshot[] = {
> +    {"help", N_("take a screenshot of a current domain console and store it "
> +                "into a file")},
> +    {"desc", N_("screenshot of a current domain console")},
> +    {NULL, NULL}
> +};
> +
> +static const vshCmdOptDef opts_screenshot[] = {
> +    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
> +    {"file", VSH_OT_DATA, VSH_OFLAG_REQ, N_("where to store the screenshot")},
> +    {"screen", VSH_OT_INT, 0, N_("ID of a screen to take screenshot of")},
> +    {NULL, 0, 0, NULL}
> +};
> +
> +static int cmdScreenshotSink(virStreamPtr st ATTRIBUTE_UNUSED,
> +                             const char *bytes, size_t nbytes, void *opaque)
> +{
> +    int *fd = opaque;
> +
> +    return safewrite(*fd, bytes, nbytes);
> +}
> +
> +static bool
> +cmdScreenshot(vshControl *ctl, const vshCmd *cmd) {
> +    virDomainPtr dom;
> +    const char *name = NULL;
> +    const char *file = NULL;
> +    int fd = -1;
> +    virStreamPtr st = NULL;
> +    unsigned int screen = 0;
> +    unsigned int flags = 0; /* currently unused */
> +    int ret = false;
> +    bool created = true;
> +    char *mime = NULL;
> +
> +    if (!vshConnectionUsability(ctl, ctl->conn))
> +        return false;
> +
> +    if (vshCommandOptString(cmd, "file", &file) < 0) {
> +        vshError(ctl, "%s", _("file must not be empty"));
> +        return false;
> +    }
> +
> +    if (vshCommandOptInt(cmd, "screen", (int*) &screen) < 0) {
> +        vshError(ctl, "%s", _("invalid screen ID"));
> +        return false;
> +    }
> +
> +    if (!(dom = vshCommandOptDomain(ctl, cmd, &name)))
> +        return false;
> +
> +    if ((fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
> +        created = false;
> +        if (errno != EEXIST ||
> +            (fd = open(file, O_WRONLY|O_TRUNC, 0666)) < 0) {
> +            vshError(ctl, _("cannot create file %s"), file);
> +            goto cleanup;
> +        }
> +    }
> +
> +    st = virStreamNew(ctl->conn, 0);
> +
> +    mime = virDomainScreenshot(dom, st, screen, flags);
> +    if (mime == NULL) {
> +        vshError(ctl, _("could not take a screenshot of %s"), name);
> +        goto cleanup;
> +    }
> +
> +    if (virStreamRecvAll(st, cmdScreenshotSink, &fd) < 0) {
> +        vshError(ctl, _("could not receive data from domain %s"), name);
> +        goto cleanup;
> +    }
> +
> +    if (VIR_CLOSE(fd) < 0) {
> +        vshError(ctl, _("cannot close file %s"), file);
> +        goto cleanup;
> +    }
> +
> +    if (virStreamFinish(st) < 0) {
> +        vshError(ctl, _("cannot close stream on domain %s"), name);
> +        goto cleanup;
> +    }
> +
> +    vshPrint(ctl, _("Screenshot saved to %s it's type is %s"), file, mime);
> +    ret = true;
> +
> +cleanup:
> +    if (ret == false && created)
> +        unlink(file);
> +    virDomainFree(dom);
> +    if (st)
> +        virStreamFree(st);
> +    VIR_FORCE_CLOSE(fd);
> +    return ret;
> +}
> +
>  /*
>   * "resume" command
>   */
> @@ -10751,6 +10847,7 @@ static const vshCmdDef domManagementCmds[] = {
>      {"resume", cmdResume, opts_resume, info_resume},
>      {"save", cmdSave, opts_save, info_save},
>      {"schedinfo", cmdSchedinfo, opts_schedinfo, info_schedinfo},
> +    {"screenshot", cmdScreenshot, opts_screenshot, info_screenshot},
>      {"setmaxmem", cmdSetmaxmem, opts_setmaxmem, info_setmaxmem},
>      {"setmem", cmdSetmem, opts_setmem, info_setmem},
>      {"setvcpus", cmdSetvcpus, opts_setvcpus, info_setvcpus},
> diff --git a/tools/virsh.pod b/tools/virsh.pod
> index f317c57..c4990f9 100644
> --- a/tools/virsh.pod
> +++ b/tools/virsh.pod
> @@ -588,6 +588,12 @@ Therefore, -1 is a useful shorthand for 262144.
>  B<Note>: The weight and cap parameters are defined only for the
>  XEN_CREDIT scheduler and are now I<DEPRECATED>.
>  
> +=item B<screenshot> I<domain-id> I<imagefilepath> optional I<screenID>
> +
> +Takes a screenshot of a current domain console and stores it into a file.
> +Optionally, if hypervisor supports more displays for a domain, I<screenID>
> +allows to specify from which should be the screenshot taken.
> +

I think it would be better if 'screenID' was a flag instead of a
positional parameter, eg

      screenshot --screen 1 myguest imagefile.png

That would then allow us to make the filename optional too. eg

   screenshot --screen 1 myguest

could automatically create a filename, based on domain name,
date + mimetype:

     myguest-s1-2011-04-20-10:16:10.png


Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]