- static const char *dir_template = "/tmp/libguestfsXXXXXX";
- int r, i;
- int wfd[2], rfd[2];
- int tries;
- /*const char *qemu = QEMU;*/ /* XXX */
- const char *qemu = "/usr/bin/qemu-system-x86_64";
- const char *kernel = "vmlinuz.fedora-10.x86_64";
- const char *initrd = "initramfs.fedora-10.x86_64.img";
- char unixsock[256];
- struct sockaddr_un addr;
-
- /* XXX Choose which qemu to run. */
- /* XXX Choose initrd, etc. */
-
- /* Configured? */
- if (!g->cmdline) {
- error (g, "you must call guestfs_add_drive before guestfs_launch");
- return -1;
- }
-
- if (g->state != CONFIG) {
- error (g, "qemu has already been launched");
- return -1;
- }
-
- /* Make the temporary directory containing the socket. */
- if (!g->tmpdir) {
- g->tmpdir = safe_strdup (g, dir_template);
- if (mkdtemp (g->tmpdir) == NULL) {
- perrorf (g, "%s: cannot create temporary directory", dir_template);
- return -1;
- }
- }
-
- snprintf (unixsock, sizeof unixsock, "%s/sock", g->tmpdir);
- unlink (unixsock);
-
- if (pipe (wfd) == -1 || pipe (rfd) == -1) {
- perrorf (g, "pipe");
- return -1;
- }
-
- r = fork ();
- if (r == -1) {
- perrorf (g, "fork");
- close (wfd[0]);
- close (wfd[1]);
- close (rfd[0]);
- close (rfd[1]);
- return -1;
- }
-
- if (r == 0) { /* Child (qemu). */
- char vmchannel[256];
- char append[256];
-
- /* Set up the full command line. Do this in the subprocess so we
- * don't need to worry about cleaning up.
- */
- g->cmdline[0] = (char *) qemu;
-
- /* Construct the -net channel parameter for qemu. */
- snprintf (vmchannel, sizeof vmchannel,
- "channel,%d:unix:%s,server,nowait",
- VMCHANNEL_PORT, unixsock);
-
- /* Linux kernel command line. */
- snprintf (append, sizeof append,
- "console=ttyS0 guestfs=%s:%d", VMCHANNEL_ADDR, VMCHANNEL_PORT);
-
- add_cmdline (g, "-m");
- add_cmdline (g, "384"); /* XXX Choose best size. */
- add_cmdline (g, "-no-kqemu"); /* Avoids a warning. */
- add_cmdline (g, "-kernel");
- add_cmdline (g, (char *) kernel);
- add_cmdline (g, "-initrd");
- add_cmdline (g, (char *) initrd);
- add_cmdline (g, "-append");
- add_cmdline (g, append);
- add_cmdline (g, "-nographic");
- add_cmdline (g, "-serial");
- add_cmdline (g, "stdio");
- add_cmdline (g, "-net");
- add_cmdline (g, vmchannel);
- add_cmdline (g, "-net");
- add_cmdline (g, "user,vlan=0");
- add_cmdline (g, "-net");
- add_cmdline (g, "nic,vlan=0");
- incr_cmdline_size (g);
- g->cmdline[g->cmdline_size-1] = NULL;
-
- if (g->verbose) {
- fprintf (stderr, "%s", qemu);
- for (i = 0; g->cmdline[i]; ++i)
- fprintf (stderr, " %s", g->cmdline[i]);
- fprintf (stderr, "\n");
- }
-
- /* Set up stdin, stdout. */
- close (0);
- close (1);
- close (wfd[1]);
- close (rfd[0]);
- dup (wfd[0]);
- dup (rfd[1]);
- close (wfd[0]);
- close (rfd[1]);
-
-#if 0
- /* Set up a new process group, so we can signal this process
- * and all subprocesses (eg. if qemu is really a shell script).
- */
- setpgid (0, 0);
-#endif
-
- execv (qemu, g->cmdline); /* Run qemu. */
- perror (qemu);
- _exit (1);
- }
-
- /* Parent (library). */
- g->pid = r;
-
- /* Start the clock ... */
- time (&g->start_t);
-
- /* Close the other ends of the pipe. */
- close (wfd[0]);
- close (rfd[1]);
-
- if (fcntl (wfd[1], F_SETFL, O_NONBLOCK) == -1 ||
- fcntl (rfd[0], F_SETFL, O_NONBLOCK) == -1) {
- perrorf (g, "fcntl");
- goto cleanup1;
- }
-
- g->fd[0] = wfd[1]; /* stdin of child */
- g->fd[1] = rfd[0]; /* stdout of child */
-
- /* Open the Unix socket. The vmchannel implementation that got
- * merged with qemu sucks in a number of ways. Both ends do
- * connect(2), which means that no one knows what, if anything, is
- * connected to the other end, or if it becomes disconnected. Even
- * worse, we have to wait some indeterminate time for qemu to create
- * the socket and connect to it (which happens very early in qemu's
- * start-up), so any code that uses vmchannel is inherently racy.
- * Hence this silly loop.
- */
- g->sock = socket (AF_UNIX, SOCK_STREAM, 0);
- if (g->sock == -1) {
- perrorf (g, "socket");
- goto cleanup1;
- }
-
- if (fcntl (g->sock, F_SETFL, O_NONBLOCK) == -1) {
- perrorf (g, "fcntl");
- goto cleanup2;
- }
-
- addr.sun_family = AF_UNIX;
- strncpy (addr.sun_path, unixsock, UNIX_PATH_MAX);
- addr.sun_path[UNIX_PATH_MAX-1] = '\0';
-
- tries = 100;
- while (tries > 0) {
- /* Always sleep at least once to give qemu a small chance to start up. */
- usleep (10000);
-
- r = connect (g->sock, (struct sockaddr *) &addr, sizeof addr);
- if ((r == -1 && errno == EINPROGRESS) || r == 0)
- goto connected;
-
- if (errno != ENOENT)
- perrorf (g, "connect");
- tries--;
- }
-
- error (g, "failed to connect to vmchannel socket");
- goto cleanup2;
-
- connected:
- /* Watch the file descriptors. */
- free (g->msg_in);
- g->msg_in = NULL;
- g->msg_in_size = g->msg_in_allocated = 0;
-
- free (g->msg_out);
- g->msg_out = NULL;
- g->msg_out_size = 0;
- g->msg_out_pos = 0;
-
- g->stdout_watch =
- main_loop.add_handle (g, g->fd[1],
- GUESTFS_HANDLE_READABLE,
- stdout_event, g);
- if (g->stdout_watch == -1) {
- error (g, "could not watch qemu stdout");
- goto cleanup3;
- }
-
- g->sock_watch =
- main_loop.add_handle (g, g->sock,
- GUESTFS_HANDLE_READABLE,
- sock_read_event, g);
- if (g->sock_watch == -1) {
- error (g, "could not watch daemon communications socket");
- goto cleanup3;
- }