public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Chris Webb <chris@arachsys.com>
To: Freddie Cash <fjwcash@gmail.com>
Cc: kvm@vger.kernel.org
Subject: qemu-send.c (was Re: Since we're sharing, here's my kvmctl script)
Date: Thu, 12 Jun 2008 01:42:01 +0100	[thread overview]
Message-ID: <20080612004201.GA7622@arachsys.com> (raw)
In-Reply-To: <b269bc570806111552s465281fl1e24050c32f1d892@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1510 bytes --]

Freddie Cash <fjwcash@gmail.com> writes:

> For everyone's viewing (and critiquing, I guess) pleasure, I present
> my version of a kvmctl script.

Hi. I have a small 'qemu-send' utility for talking to a running qemu/kvm
process whose monitor console listens on a filesystem socket, which I think
might be a useful building block when extending these kinds of script to do
things like migratation, pausing, and so on. The source is attached.

It's careful to extract and pass on the correct command output from kvm and
waits for the (qemu) prompt to return before exiting, so you can do stuff
like

  qemu-send /var/run/vm.ctl migrate file:///var/statedumps/foo
  do-something-with-the-file /var/statedumps/foo

without any race problem, and

  qemu-send /tmp/vm.ctl 'info blockstats'

will list the right info without any extraneous command echo or prompt text
leaking out.

Any comments or feedback welcome, and please feel free to incorporate it
where useful.

> The only thing I haven't been able to figure out, is how to send a
> "shutdown" command from the host OS to the guest OS, such that the
> guest OS will do a proper shutdown sequence.  You have to switch to
> the VM console and manually tell it to shutdown.  :(

If you have the qemu monitor listening on a unix socket /var/run/vm.ctl,
i.e. if you've started the kvm with an argument like

  -monitor unix:/var/run/vm.ctl,server,nowait

you could send a graceful shutdown using

  qemu-send /var/run/vm.ctl system_powerdown

Best wishes,

Chris.

[-- Attachment #2: qemu-send.c --]
[-- Type: text/x-csrc, Size: 2378 bytes --]

#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>

const char *prompt = "(qemu) ";

void echo(char *s, size_t n, int *skip) {
  char *p;

  for (p = s; *skip != 0 && p < n + s; p++, (*skip)--)
    if ((p = memchr(p, '\n', n + s - p)) == NULL)
      return;

  if (p < n + s)
    write(STDOUT_FILENO, p, n + s - p);
}

void getprompt(int fd, int skip, int eof) {
  char s[PIPE_BUF];
  int n, sl = 0;

  do {
    if ((n = read(fd, s + sl, sizeof(s) - sl)) < 0) {
      perror("read");
      exit(EXIT_FAILURE);
    } else
      sl += n;

    if (n == 0) {
      echo(s, sl, &skip);
      exit(eof);
    }

    if (sl > strlen(prompt)) {
      echo(s, sl - strlen(prompt), &skip);
      memmove(s, s + sl - strlen(prompt), strlen(prompt));
      sl = strlen(prompt);
    }
  } while (memcmp(s, prompt, strlen(prompt)));
}

void usage(char *progname) {
  fprintf(stderr, "\
Usage: %1$s [-n] [-q] SOCKET COMMAND\n\
       %1$s -t SOCKET\n\
Options:\n\
  -n    do not wait for command to finish before returning\n\
  -q    do not echo output from kvm/qemu to stdout\n\
  -t    test if kvm/qemu is listening on SOCKET without issuing a command\n\
", progname);
  exit(EXIT_FAILURE);
}

int main(int argc, char **argv) {
  int n, sock, quiet = 0, wait = 1, test = 0;
  struct sockaddr_un sockaddr;

  while ((n = getopt(argc, argv, "nqt")) > 0)
    switch (n) {
      case 'n':
        wait = 0;
        break;
      case 'q':
        quiet = 1;
        break;
      case 't':
        test = 1;
        break;
      default:
        usage(argv[0]);
    }
  if ((argc -= optind) != (test ? 1 : 2))
    usage(argv[0]);
  argv += optind;

  if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return EXIT_FAILURE;
  }

  sockaddr.sun_family = AF_UNIX;
  strcpy(sockaddr.sun_path, argv[0]);
  n = strlen(sockaddr.sun_path) + sizeof(sockaddr.sun_family);
  if (connect(sock, (struct sockaddr *) &sockaddr, n) < 0) {
    if (test == 0)
      perror("connect");
    return EXIT_FAILURE;
  }

  getprompt(sock, -1, EXIT_FAILURE);
  if (test == 0) {
    write(sock, argv[1], strlen(argv[1]));
    write(sock, "\n", 1);
    if (wait)
      /* always discard first line because of command echo */
      getprompt(sock, quiet ? -1 : 1, EXIT_SUCCESS);
  }
  return EXIT_SUCCESS;
}

  parent reply	other threads:[~2008-06-12  0:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-11 22:52 Since we're sharing, here's my kvmctl script Freddie Cash
2008-06-11 23:04 ` Freddie Cash
2008-06-12 15:15   ` Freddie Cash
2008-06-12  0:42 ` Chris Webb [this message]
2008-06-12  2:10   ` qemu-send.c (was Re: Since we're sharing, here's my kvmctl script) Javier Guerra Giraldez
2008-06-12  8:35     ` Chris Webb
2008-06-12  2:07 ` Since we're sharing, here's my kvmctl script Javier Guerra Giraldez
2008-06-12  8:38   ` William Boughton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080612004201.GA7622@arachsys.com \
    --to=chris@arachsys.com \
    --cc=fjwcash@gmail.com \
    --cc=kvm@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox