qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Corey Bryant <coreyb@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, aliguori@us.ibm.com,
	stefanha@linux.vnet.ibm.com, libvir-list@redhat.com,
	eblake@redhat.com
Subject: [Qemu-devel] [RFC PATCH 4/4] Example -filefd and getfd_file server
Date: Mon, 21 May 2012 16:19:58 -0400	[thread overview]
Message-ID: <1337631598-30639-5-git-send-email-coreyb@linux.vnet.ibm.com> (raw)
In-Reply-To: <1337631598-30639-1-git-send-email-coreyb@linux.vnet.ibm.com>

This example demonstrates use of the -filefd command to open two
disk drives at start-up time.  It also demonstrates hot attaching
a third disk drive with the getfd_file monitor command.  I still
have some learning to do with regards to QMP, so the example is
using a not-so-program-friendly HMP method.

Usage:
./test-fd-passing /path/hda.img /path/hdb.img /path/hdc.img

Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
---
 test-fd-passing.c |  224 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 224 insertions(+), 0 deletions(-)
 create mode 100644 test-fd-passing.c

diff --git a/test-fd-passing.c b/test-fd-passing.c
new file mode 100644
index 0000000..d568198
--- /dev/null
+++ b/test-fd-passing.c
@@ -0,0 +1,224 @@
+/*
+ * QEMU -filefd and getfd_file test server
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Stefan Hajnoczi   <stefanha@linux.vnet.ibm.com>
+ *  Corey Bryant      <coreyb@linux.vnet.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ * gcc -Wall -o test-fd-passing test-fd-passing.c
+ */
+
+#define _GNU_SOURCE
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <spawn.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+static int openQemuMonitor(const char *monitor)
+{
+    int i;
+    int ret;
+    struct sockaddr_un addr;
+    int monfd = 0;
+
+    if ((monfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+        perror("socket");
+        goto error;
+    }
+
+    memset(&addr, 0, sizeof(addr));
+    addr.sun_family = AF_UNIX;
+    strcpy(addr.sun_path, monitor);
+
+    for (i = 0; i < 100; i++) {
+        ret = connect(monfd, (struct sockaddr *) &addr, sizeof(addr));
+        if (ret == 0) {
+            break;
+        }
+        usleep(.2 * 1000000);
+    }
+
+    if (ret != 0) {
+        fprintf(stderr, "no monitor socket");
+        goto error;
+    }
+
+    return monfd;
+
+error:
+    close(monfd);
+    return -1;
+}
+
+static int issueHMPCmdFD(int monfd,const char *data, size_t len, int fd)
+{
+    int ret;
+    struct msghdr msg;
+    struct iovec iov[1];
+    char control[CMSG_SPACE(sizeof(int))];
+    struct cmsghdr *cmsg;
+
+    memset(&msg, 0, sizeof(msg));
+
+    iov[0].iov_base = (void *)data;
+    iov[0].iov_len = len;
+
+    msg.msg_iov = iov;
+    msg.msg_iovlen = 1;
+
+    msg.msg_control = control;
+    msg.msg_controllen = sizeof(control);
+
+    cmsg = CMSG_FIRSTHDR(&msg);
+    cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+    cmsg->cmsg_level = SOL_SOCKET;
+    cmsg->cmsg_type = SCM_RIGHTS;
+    memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
+
+    do {
+        ret = sendmsg(monfd, &msg, 0);
+    } while (ret < 0 && errno == EINTR);
+
+    return ret;
+}
+
+int main(int argc, char *argv[]) {
+    int rc;
+    int fd1, fd2, hotfd, monfd=-1;
+    int flags = O_RDWR;
+    int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
+    pid_t child_pid;
+    char *drive_str_1 = NULL;;
+    char *drive_str_2 = NULL;;
+    char *filefd_str_1 = NULL;
+    char *filefd_str_2 = NULL;
+    char *getfd_file_str = NULL;
+    char *drive_add_str = NULL;
+    char *device_add_str = NULL;
+
+    if (argc != 4) {
+        fprintf(stderr, "usage: %s <boot-image-file-1> <boot-image-file-2> <attach-image-file>\n", argv[0]);
+        goto error;
+    }
+
+    fd1 = open(argv[1], flags, mode);
+    if (fd1 == -1) {
+        perror("open");
+        goto error;
+    }
+
+    fd2 = open(argv[2], flags, mode);
+    if (fd2 == -1) {
+        perror("open");
+        goto error;
+    }
+
+    hotfd = open(argv[3], flags, mode);
+    if (hotfd == -1) {
+        perror("open");
+        goto error;
+    }
+
+    asprintf(&drive_str_1, "file=%s,if=none,id=drive-virtio-disk0", argv[1]);
+    asprintf(&filefd_str_1, "file=%s,fd=%d", argv[1], fd1);
+    asprintf(&drive_str_2, "file=%s,if=none,id=drive-virtio-disk1", argv[2]);
+    asprintf(&filefd_str_2, "file=%s,fd=%d", argv[2], fd2);
+
+    char *child_argv[] = {
+        "qemu-system-x86_64",
+        "-enable-kvm",
+        "-m", "1024",
+        "-chardev",
+        "socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL62.monitor,server,nowait",
+        "-mon",
+        "chardev=charmonitor,id=monitor,mode=readline",
+        "-drive", drive_str_1,
+        "-device",
+        "virtio-blk-pci,bus=pci.0,addr=0x4,drive=drive-virtio-disk0,id=virtio-disk0",
+        "-drive", drive_str_2,
+        "-device",
+        "virtio-blk-pci,bus=pci.0,addr=0x6,drive=drive-virtio-disk1,id=virtio-disk1",
+        "-filefd", filefd_str_1,
+        "-filefd", filefd_str_2,
+        "-vnc", ":0",
+        NULL,
+    };
+
+    if (posix_spawn(&child_pid, "/usr/local/bin/qemu-system-x86_64",
+                    NULL, NULL, child_argv, environ) != 0) {
+        perror("posix_spawn\n");
+        goto error;
+    }
+
+    monfd = openQemuMonitor("/var/lib/libvirt/qemu/RHEL62.monitor");
+    if (monfd == -1) {
+        goto error;
+    }
+
+    asprintf(&getfd_file_str, "getfd_file %s\r\n", argv[3]);
+    rc = issueHMPCmdFD(monfd, getfd_file_str,
+                       strlen(getfd_file_str), hotfd);
+    if (rc < 0) {
+        perror("issueHMPCmdFD");
+        goto error;
+    }
+
+    sleep(1);
+    asprintf(&drive_add_str, "drive_add data_drive file=%s,%s", argv[3],
+             "if=none,id=drive-virtio-disk2,cache=writethrough\r\n");
+    rc = write(monfd, drive_add_str, strlen(drive_add_str));
+    if (rc < 0) {
+        perror("write");
+        goto error;
+    }
+
+    sleep(1);
+    asprintf(&device_add_str, "device_add virtio-blk-pci,bus=pci.0,%s",
+             "addr=0x8,drive=drive-virtio-disk2,id=virtio-disk2\r\n");
+    rc = write(monfd, device_add_str, strlen(device_add_str));
+    if (rc < 0) {
+        perror("write");
+        goto error;
+    }
+
+error:
+    if (drive_str_1) {
+        free(drive_str_1);
+    }
+    if (drive_str_2) {
+        free(drive_str_2);
+    }
+    if (filefd_str_1) {
+        free(filefd_str_1);
+    }
+    if (filefd_str_2) {
+        free(filefd_str_2);
+    }
+    if (getfd_file_str) {
+        free(getfd_file_str);
+    }
+    if (drive_add_str) {
+        free(drive_add_str);
+    }
+    if (device_add_str) {
+        free(device_add_str);
+    }
+    if (monfd != -1) {
+        close(monfd);
+    }
+    return -1;
+}
-- 
1.7.7.6

  parent reply	other threads:[~2012-05-21 20:20 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-21 20:19 [Qemu-devel] [RFC PATCH 0/4] block: file descriptor passing using -filefd and getfd_file Corey Bryant
2012-05-21 20:19 ` [Qemu-devel] [RFC PATCH 1/4] qemu-options: Add -filefd command line option Corey Bryant
2012-05-21 21:40   ` Eric Blake
2012-05-22 13:25     ` Corey Bryant
2012-05-22 13:38       ` Kevin Wolf
2012-05-22 14:26         ` Stefan Hajnoczi
2012-05-22 14:39           ` Kevin Wolf
2012-05-21 20:19 ` [Qemu-devel] [RFC PATCH 2/4] qmp/hmp: Add getfd_file monitor command Corey Bryant
2012-05-21 21:48   ` Eric Blake
2012-05-22 13:37     ` Corey Bryant
2012-05-22  9:18   ` Stefan Hajnoczi
2012-05-22 14:13     ` Corey Bryant
2012-05-22 19:06     ` Luiz Capitulino
2012-05-22 20:02       ` Corey Bryant
2012-05-22 20:26         ` Luiz Capitulino
2012-05-22 22:34           ` Corey Bryant
2012-05-23 13:33             ` Luiz Capitulino
2012-05-23 13:45               ` Corey Bryant
2012-05-21 20:19 ` [Qemu-devel] [RFC PATCH 3/4] block: Enable QEMU to retrieve passed fd before attempting open Corey Bryant
2012-05-21 21:50   ` Eric Blake
2012-05-22 14:06     ` Corey Bryant
2012-05-21 20:19 ` Corey Bryant [this message]
2012-05-22  8:18 ` [Qemu-devel] [RFC PATCH 0/4] block: file descriptor passing using -filefd and getfd_file Kevin Wolf
2012-05-22 12:02   ` Eric Blake
2012-05-22 12:08     ` Kevin Wolf
2012-05-22 14:30   ` Corey Bryant
2012-05-22 14:45     ` Kevin Wolf
2012-05-22 15:01       ` Eric Blake
2012-05-22 15:24         ` Kevin Wolf
2012-05-22 15:29       ` Corey Bryant
2012-05-22 15:39         ` Kevin Wolf
2012-05-22 16:02           ` Corey Bryant
2012-05-22 16:15         ` Eric Blake
2012-05-22 17:17           ` Corey Bryant

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=1337631598-30639-5-git-send-email-coreyb@linux.vnet.ibm.com \
    --to=coreyb@linux.vnet.ibm.com \
    --cc=aliguori@us.ibm.com \
    --cc=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=libvir-list@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@linux.vnet.ibm.com \
    /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;
as well as URLs for NNTP newsgroup(s).