- /* Make the vmchannel socket. */
- snprintf (unixsock, sizeof unixsock, "%s/sock", g->tmpdir);
- unlink (unixsock);
-
- if (pipe (wfd) == -1 || pipe (rfd) == -1) {
- perrorf (g, "pipe");
- goto cleanup0;
- }
-
- r = fork ();
- if (r == -1) {
- perrorf (g, "fork");
- close (wfd[0]);
- close (wfd[1]);
- close (rfd[0]);
- close (rfd[1]);
- goto cleanup0;
- }
-
- if (r == 0) { /* Child (qemu). */
- char vmchannel[256];
- char append[256];
- char memsize_str[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] = g->qemu;
-
-#define LINUX_CMDLINE \
- "panic=1 " /* force kernel to panic if daemon exits */ \
- "console=ttyS0 " /* serial console */ \
- "udevtimeout=300 " /* good for very slow systems (RHBZ#480319) */ \
- "noapic " /* workaround for RHBZ#502058 - ok if not SMP */ \
- "acpi=off " /* we don't need ACPI, turn it off */ \
- "cgroup_disable=memory " /* saves us about 5 MB of RAM */ \
- "selinux=0 " /* SELinux is messed up if there's no policy */
-
- /* Linux kernel command line. */
- snprintf (append, sizeof append,
- LINUX_CMDLINE "guestfs=%s:%d%s%s%s",
- VMCHANNEL_ADDR, VMCHANNEL_PORT,
- g->verbose ? " guestfs_verbose=1" : "",
- g->append ? " " : "", g->append ? g->append : "");
-
- snprintf (memsize_str, sizeof memsize_str, "%d", g->memsize);
-
- add_cmdline (g, "-m");
- add_cmdline (g, memsize_str);
- add_cmdline (g, "-no-reboot"); /* Force exit instead of reboot on panic */
- 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");
-
-#if 0
- /* Doesn't work. See:
- * http://lists.gnu.org/archive/html/qemu-devel/2009-07/threads.html
- * Subject "guestfwd option doesn't allow supplementary ,server,nowait"
- */
- if (qemu_supports (g, "guestfwd")) {
- /* New-style -net user,guestfwd=... syntax for vmchannel. See:
- * http://git.savannah.gnu.org/cgit/qemu.git/commit/?id=c92ef6a22d3c71538fcc48fb61ad353f7ba03b62
- */
- snprintf (vmchannel, sizeof vmchannel,
- "user,vlan=0,net=10.0.2.0/8,guestfwd=tcp:%s:%d-unix:%s,server,nowait",
- VMCHANNEL_ADDR, VMCHANNEL_PORT, unixsock);
-
- add_cmdline (g, "-net");
- add_cmdline (g, vmchannel);
- } else {
-#endif
- /* Not guestfwd. HOPEFULLY this qemu uses the older -net channel
- * syntax, or if not then we'll get a quick failure.
- */
- snprintf (vmchannel, sizeof vmchannel,
- "channel,%d:unix:%s,server,nowait",
- VMCHANNEL_PORT, unixsock);
-
- add_cmdline (g, "-net");
- add_cmdline (g, vmchannel);
- add_cmdline (g, "-net");
- add_cmdline (g, "user,vlan=0,net=10.0.2.0/8");
-#if 0
- }
-#endif
- add_cmdline (g, "-net");
- add_cmdline (g, "nic,model=virtio,vlan=0");
-
- /* These options recommended by KVM developers to improve reliability. */
- if (qemu_supports (g, "-no-hpet"))
- add_cmdline (g, "-no-hpet");
-
- if (qemu_supports (g, "-rtc-td-hack"))
- add_cmdline (g, "-rtc-td-hack");
-
- /* Finish off the command line. */
- incr_cmdline_size (g);
- g->cmdline[g->cmdline_size-1] = NULL;
-
- if (g->verbose) {
- fprintf (stderr, "%s", g->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 (g->qemu, g->cmdline); /* Run qemu. */
- perror (g->qemu);
- _exit (1);
- }
-
- /* Parent (library). */
- g->pid = r;
-
- free (kernel);
- kernel = NULL;
- free (initrd);
- initrd = NULL;
-
- /* Fork the recovery process off which will kill qemu if the parent
- * process fails to do so (eg. if the parent segfaults).
- */
- r = fork ();
- if (r == 0) {
- pid_t qemu_pid = g->pid;
- pid_t parent_pid = getppid ();
-
- /* Writing to argv is hideously complicated and error prone. See:
- * http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/misc/ps_status.c?rev=1.33.2.1;content-type=text%2Fplain
- */
-
- /* Loop around waiting for one or both of the other processes to
- * disappear. It's fair to say this is very hairy. The PIDs that
- * we are looking at might be reused by another process. We are
- * effectively polling. Is the cure worse than the disease?
- */
- for (;;) {
- if (kill (qemu_pid, 0) == -1) /* qemu's gone away, we aren't needed */
- _exit (0);
- if (kill (parent_pid, 0) == -1) {
- /* Parent's gone away, qemu still around, so kill qemu. */
- kill (qemu_pid, 9);
- _exit (0);
- }
- sleep (2);
- }
- }
-
- /* Don't worry, if the fork failed, this will be -1. The recovery
- * process isn't essential.
- */
- g->recoverypid = 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;
- /* Always sleep at least once to give qemu a small chance to start up. */
- usleep (10000);
- while (tries > 0) {
- 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--;
- usleep (100000);
- }
-
- 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 =
- g->main_loop->add_handle (g->main_loop, g, g->fd[1],
- GUESTFS_HANDLE_READABLE,
- stdout_event, NULL);
- if (g->stdout_watch == -1) {
- error (g, _("could not watch qemu stdout"));
- goto cleanup3;
- }
-
- if (guestfs__switch_to_receiving (g) == -1)
- goto cleanup3;
-
- g->state = LAUNCHING;
- return 0;
-
- cleanup3:
- if (g->stdout_watch >= 0)
- g->main_loop->remove_handle (g->main_loop, g, g->stdout_watch);
- if (g->sock_watch >= 0)
- g->main_loop->remove_handle (g->main_loop, g, g->sock_watch);
-
- cleanup2:
- close (g->sock);
-
- cleanup1:
- close (wfd[1]);
- close (rfd[0]);
- kill (g->pid, 9);
- if (g->recoverypid > 0) kill (g->recoverypid, 9);
- waitpid (g->pid, NULL, 0);
- if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
- g->fd[0] = -1;
- g->fd[1] = -1;
- g->sock = -1;
- g->pid = 0;
- g->recoverypid = 0;
- g->start_t = 0;
- g->stdout_watch = -1;
- g->sock_watch = -1;
-
- cleanup0:
- free (kernel);
- free (initrd);
- return -1;
-}
-
-/* This function does the hard work of building the supermin appliance
- * on the fly. 'path' is the directory containing the control files.
- * 'kernel' and 'initrd' are where we will return the names of the
- * kernel and initrd (only initrd is built). The work is done by
- * an external script. We just tell it where to put the result.
- */
-static int
-build_supermin_appliance (guestfs_h *g, const char *path,
- char **kernel, char **initrd)
-{
- char cmd[4096];
- int r, len;
-
- len = strlen (g->tmpdir);
- *kernel = safe_malloc (g, len + 8);
- snprintf (*kernel, len+8, "%s/kernel", g->tmpdir);
- *initrd = safe_malloc (g, len + 8);
- snprintf (*initrd, len+8, "%s/initrd", g->tmpdir);
-
- snprintf (cmd, sizeof cmd,
- "PATH='%s':$PATH "
- "libguestfs-supermin-helper '%s' %s %s",
- path,
- path, *kernel, *initrd);
-
- r = system (cmd);
- if (r == -1 || WEXITSTATUS(r) != 0) {
- error (g, _("external command failed: %s"), cmd);
- free (*kernel);
- free (*initrd);
- *kernel = *initrd = NULL;
- return -1;
- }
-
- return 0;