Freddie Cash <[EMAIL PROTECTED]> 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.
#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; }