+ /* First search g->path for the supermin appliance, and try to
+ * synthesize a kernel and initrd from that. If it fails, we
+ * try the path search again looking for a backup ordinary
+ * appliance.
+ */
+ pelem = path = safe_strdup (g, g->path);
+ do {
+ pend = strchrnul (pelem, ':');
+ pmore = *pend == ':';
+ *pend = '\0';
+ len = pend - pelem;
+
+ /* Empty element of "." means cwd. */
+ if (len == 0 || (len == 1 && *pelem == '.')) {
+ if (g->verbose)
+ fprintf (stderr,
+ "looking for supermin appliance in current directory\n");
+ if (dir_contains_files (".",
+ supermin_name, supermin_hostfiles_name,
+ "kmod.whitelist", NULL)) {
+ if (build_supermin_appliance (g, ".", &kernel, &initrd) == -1)
+ return -1;
+ break;
+ }
+ }
+ /* Look at <path>/supermin* etc. */
+ else {
+ if (g->verbose)
+ fprintf (stderr, "looking for supermin appliance in %s\n", pelem);
+
+ if (dir_contains_files (pelem,
+ supermin_name, supermin_hostfiles_name,
+ "kmod.whitelist", NULL)) {
+ if (build_supermin_appliance (g, pelem, &kernel, &initrd) == -1)
+ return -1;
+ break;
+ }
+ }
+
+ pelem = pend + 1;
+ } while (pmore);
+
+ free (path);
+
+ if (kernel == NULL || initrd == NULL) {
+ /* Search g->path for the kernel and initrd. */
+ pelem = path = safe_strdup (g, g->path);
+ do {
+ pend = strchrnul (pelem, ':');
+ pmore = *pend == ':';
+ *pend = '\0';
+ len = pend - pelem;
+
+ /* Empty element or "." means cwd. */
+ if (len == 0 || (len == 1 && *pelem == '.')) {
+ if (g->verbose)
+ fprintf (stderr,
+ "looking for appliance in current directory\n");
+ if (dir_contains_files (".", kernel_name, initrd_name, NULL)) {
+ kernel = safe_strdup (g, kernel_name);
+ initrd = safe_strdup (g, initrd_name);
+ break;
+ }
+ }
+ /* Look at <path>/kernel etc. */
+ else {
+ if (g->verbose)
+ fprintf (stderr, "looking for appliance in %s\n", pelem);
+
+ if (dir_contains_files (pelem, kernel_name, initrd_name, NULL)) {
+ kernel = safe_malloc (g, len + strlen (kernel_name) + 2);
+ initrd = safe_malloc (g, len + strlen (initrd_name) + 2);
+ sprintf (kernel, "%s/%s", pelem, kernel_name);
+ sprintf (initrd, "%s/%s", pelem, initrd_name);
+ break;
+ }
+ }
+
+ pelem = pend + 1;
+ } while (pmore);
+
+ free (path);
+ }
+
+ if (kernel == NULL || initrd == NULL) {
+ error (g, _("cannot find %s or %s on LIBGUESTFS_PATH (current path = %s)"),
+ kernel_name, initrd_name, g->path);
+ goto cleanup0;
+ }
+
+ if (g->verbose)
+ print_timestamped_message (g, "begin testing qemu features");
+
+ /* Get qemu help text and version. */
+ if (qemu_supports (g, NULL) == -1)
+ goto cleanup0;
+
+ /* Choose which vmchannel implementation to use. */
+ if (CAN_CHECK_PEER_EUID && qemu_supports (g, "-net user")) {
+ /* The "null vmchannel" implementation. Requires SLIRP (user mode
+ * networking in qemu) but no other vmchannel support. The daemon
+ * will connect back to a random port number on localhost.
+ */
+ struct sockaddr_in addr;
+ socklen_t addrlen = sizeof addr;
+
+ g->sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (g->sock == -1) {
+ perrorf (g, "socket");
+ goto cleanup0;
+ }
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons (0);
+ addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
+ if (bind (g->sock, (struct sockaddr *) &addr, addrlen) == -1) {
+ perrorf (g, "bind");
+ goto cleanup0;
+ }
+
+ if (listen (g->sock, 256) == -1) {
+ perrorf (g, "listen");
+ goto cleanup0;
+ }
+
+ if (getsockname (g->sock, (struct sockaddr *) &addr, &addrlen) == -1) {
+ perrorf (g, "getsockname");
+ goto cleanup0;
+ }
+
+ if (fcntl (g->sock, F_SETFL, O_NONBLOCK) == -1) {
+ perrorf (g, "fcntl");
+ goto cleanup0;
+ }
+
+ null_vmchannel_sock = ntohs (addr.sin_port);
+ if (g->verbose)
+ fprintf (stderr, "null_vmchannel_sock = %d\n", null_vmchannel_sock);
+ } else {
+ /* Using some vmchannel impl. We need to create a local Unix
+ * domain socket for qemu to use.
+ */
+ snprintf (unixsock, sizeof unixsock, "%s/sock", g->tmpdir);
+ unlink (unixsock);
+ null_vmchannel_sock = 0;
+ }
+
+ if (!g->direct) {
+ if (pipe (wfd) == -1 || pipe (rfd) == -1) {
+ perrorf (g, "pipe");
+ goto cleanup0;
+ }
+ }
+
+ if (g->verbose)
+ print_timestamped_message (g, "finished testing qemu features");