2 * Copyright (C) 2009-2010 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #define _BSD_SOURCE /* for mkdtemp, usleep */
34 #include <sys/select.h>
39 #include <rpc/types.h>
46 #ifdef HAVE_SYS_TYPES_H
47 #include <sys/types.h>
50 #ifdef HAVE_SYS_WAIT_H
54 #ifdef HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
62 #include <arpa/inet.h>
63 #include <netinet/in.h>
66 #include "glthread/lock.h"
69 #include "guestfs-internal.h"
70 #include "guestfs-internal-actions.h"
71 #include "guestfs_protocol.h"
73 static int launch_appliance (guestfs_h *g);
74 static int connect_unix_socket (guestfs_h *g, const char *sock);
75 static int qemu_supports (guestfs_h *g, const char *option);
77 /* Add a string to the current command line. */
79 incr_cmdline_size (guestfs_h *g)
81 if (g->cmdline == NULL) {
82 /* g->cmdline[0] is reserved for argv[0], set in guestfs_launch. */
84 g->cmdline = safe_malloc (g, sizeof (char *));
89 g->cmdline = safe_realloc (g, g->cmdline, sizeof (char *) * g->cmdline_size);
93 add_cmdline (guestfs_h *g, const char *str)
95 if (g->state != CONFIG) {
97 _("command line cannot be altered after qemu subprocess launched"));
101 incr_cmdline_size (g);
102 g->cmdline[g->cmdline_size-1] = safe_strdup (g, str);
107 guestfs___checkpoint_cmdline (guestfs_h *g)
109 return g->cmdline_size;
113 guestfs___rollback_cmdline (guestfs_h *g, int pos)
117 assert (g->cmdline_size >= pos);
119 for (i = g->cmdline_size - 1; i >= pos; --i)
120 free (g->cmdline[i]);
122 g->cmdline_size = pos;
125 /* Internal command to return the command line. */
127 guestfs__debug_cmdline (guestfs_h *g)
132 if (g->cmdline == NULL) {
133 r = safe_malloc (g, sizeof (char *) * 1);
138 r = safe_malloc (g, sizeof (char *) * (g->cmdline_size + 1));
139 r[0] = safe_strdup (g, g->qemu); /* g->cmdline[0] is always NULL */
141 for (i = 1; i < g->cmdline_size; ++i)
142 r[i] = safe_strdup (g, g->cmdline[i]);
144 r[g->cmdline_size] = NULL;
146 return r; /* caller frees */
150 guestfs__config (guestfs_h *g,
151 const char *qemu_param, const char *qemu_value)
153 if (qemu_param[0] != '-') {
154 error (g, _("guestfs_config: parameter must begin with '-' character"));
158 /* A bit fascist, but the user will probably break the extra
159 * parameters that we add if they try to set any of these.
161 if (STREQ (qemu_param, "-kernel") ||
162 STREQ (qemu_param, "-initrd") ||
163 STREQ (qemu_param, "-nographic") ||
164 STREQ (qemu_param, "-serial") ||
165 STREQ (qemu_param, "-full-screen") ||
166 STREQ (qemu_param, "-std-vga") ||
167 STREQ (qemu_param, "-vnc")) {
168 error (g, _("guestfs_config: parameter '%s' isn't allowed"), qemu_param);
172 if (add_cmdline (g, qemu_param) != 0) return -1;
174 if (qemu_value != NULL) {
175 if (add_cmdline (g, qemu_value) != 0) return -1;
181 /* cache=off improves reliability in the event of a host crash.
183 * However this option causes qemu to try to open the file with
184 * O_DIRECT. This fails on some filesystem types (notably tmpfs).
185 * So we check if we can open the file with or without O_DIRECT,
186 * and use cache=off (or not) accordingly.
189 test_cache_off (guestfs_h *g, const char *filename)
191 int fd = open (filename, O_RDONLY|O_DIRECT);
197 fd = open (filename, O_RDONLY);
203 perrorf (g, "%s", filename);
207 /* Check string parameter matches ^[-_[:alnum:]]+$ (in C locale). */
209 valid_format_iface (const char *str)
211 size_t len = strlen (str);
219 if (c != '-' && c != '_' && !c_isalnum (c))
226 guestfs__add_drive_opts (guestfs_h *g, const char *filename,
227 const struct guestfs_add_drive_opts_argv *optargs)
233 if (strchr (filename, ',') != NULL) {
234 error (g, _("filename cannot contain ',' (comma) character"));
238 readonly = optargs->bitmask & GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK
239 ? optargs->readonly : 0;
240 format = optargs->bitmask & GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK
241 ? optargs->format : NULL;
242 iface = optargs->bitmask & GUESTFS_ADD_DRIVE_OPTS_IFACE_BITMASK
243 ? optargs->iface : DRIVE_IF;
245 if (format && !valid_format_iface (format)) {
246 error (g, _("%s parameter is empty or contains disallowed characters"),
250 if (!valid_format_iface (iface)) {
251 error (g, _("%s parameter is empty or contains disallowed characters"),
256 /* For writable files, see if we can use cache=off. This also
257 * checks for the existence of the file. For readonly we have
258 * to do the check explicitly.
260 int use_cache_off = readonly ? 0 : test_cache_off (g, filename);
261 if (use_cache_off == -1)
265 if (access (filename, F_OK) == -1) {
266 perrorf (g, "%s", filename);
271 /* Construct the final -drive parameter. */
272 size_t len = 64 + strlen (filename) + strlen (iface);
273 if (format) len += strlen (format);
276 snprintf (buf, len, "file=%s%s%s%s%s,if=%s",
278 readonly ? ",snapshot=on" : "",
279 use_cache_off ? ",cache=off" : "",
280 format ? ",format=" : "",
281 format ? format : "",
284 return guestfs__config (g, "-drive", buf);
288 guestfs__add_drive (guestfs_h *g, const char *filename)
290 struct guestfs_add_drive_opts_argv optargs = {
294 return guestfs__add_drive_opts (g, filename, &optargs);
298 guestfs__add_drive_ro (guestfs_h *g, const char *filename)
300 struct guestfs_add_drive_opts_argv optargs = {
301 .bitmask = GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK,
305 return guestfs__add_drive_opts (g, filename, &optargs);
309 guestfs__add_drive_with_if (guestfs_h *g, const char *filename,
312 struct guestfs_add_drive_opts_argv optargs = {
313 .bitmask = GUESTFS_ADD_DRIVE_OPTS_IFACE_BITMASK,
317 return guestfs__add_drive_opts (g, filename, &optargs);
321 guestfs__add_drive_ro_with_if (guestfs_h *g, const char *filename,
324 struct guestfs_add_drive_opts_argv optargs = {
325 .bitmask = GUESTFS_ADD_DRIVE_OPTS_IFACE_BITMASK
326 | GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK,
331 return guestfs__add_drive_opts (g, filename, &optargs);
335 guestfs__add_cdrom (guestfs_h *g, const char *filename)
337 if (strchr (filename, ',') != NULL) {
338 error (g, _("filename cannot contain ',' (comma) character"));
342 if (access (filename, F_OK) == -1) {
343 perrorf (g, "%s", filename);
347 return guestfs__config (g, "-cdrom", filename);
350 static int is_openable (guestfs_h *g, const char *path, int flags);
353 guestfs__launch (guestfs_h *g)
356 if (g->state != CONFIG) {
357 error (g, _("the libguestfs handle has already been launched"));
361 /* Make the temporary directory. */
363 TMP_TEMPLATE_ON_STACK (dir_template);
364 g->tmpdir = safe_strdup (g, dir_template);
365 if (mkdtemp (g->tmpdir) == NULL) {
366 perrorf (g, _("%s: cannot create temporary directory"), dir_template);
371 /* Allow anyone to read the temporary directory. The socket in this
372 * directory won't be readable but anyone can see it exists if they
373 * want. (RHBZ#610880).
375 if (chmod (g->tmpdir, 0755) == -1)
376 fprintf (stderr, "chmod: %s: %m (ignored)\n", g->tmpdir);
378 /* Launch the appliance or attach to an existing daemon. */
379 switch (g->attach_method) {
380 case ATTACH_METHOD_APPLIANCE:
381 return launch_appliance (g);
383 case ATTACH_METHOD_UNIX:
384 return connect_unix_socket (g, g->attach_method_arg);
392 launch_appliance (guestfs_h *g)
397 struct sockaddr_un addr;
399 /* At present you must add drives before starting the appliance. In
400 * future when we enable hotplugging you won't need to do this.
403 error (g, _("you must call guestfs_add_drive before guestfs_launch"));
407 /* Start the clock ... */
408 gettimeofday (&g->launch_t, NULL);
410 /* Locate and/or build the appliance. */
411 char *kernel = NULL, *initrd = NULL, *appliance = NULL;
412 if (guestfs___build_appliance (g, &kernel, &initrd, &appliance) == -1)
416 guestfs___print_timestamped_message (g, "begin testing qemu features");
418 /* Get qemu help text and version. */
419 if (qemu_supports (g, NULL) == -1)
422 /* Using virtio-serial, we need to create a local Unix domain socket
423 * for qemu to connect to.
425 snprintf (unixsock, sizeof unixsock, "%s/sock", g->tmpdir);
428 g->sock = socket (AF_UNIX, SOCK_STREAM, 0);
430 perrorf (g, "socket");
434 if (fcntl (g->sock, F_SETFL, O_NONBLOCK) == -1) {
435 perrorf (g, "fcntl");
439 addr.sun_family = AF_UNIX;
440 strncpy (addr.sun_path, unixsock, UNIX_PATH_MAX);
441 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
443 if (bind (g->sock, &addr, sizeof addr) == -1) {
448 if (listen (g->sock, 1) == -1) {
449 perrorf (g, "listen");
454 if (pipe (wfd) == -1 || pipe (rfd) == -1) {
461 guestfs___print_timestamped_message (g, "finished testing qemu features");
475 if (r == 0) { /* Child (qemu). */
478 /* Set up the full command line. Do this in the subprocess so we
479 * don't need to worry about cleaning up.
481 g->cmdline[0] = g->qemu;
483 if (qemu_supports (g, "-nodefconfig"))
484 add_cmdline (g, "-nodefconfig");
486 /* qemu sometimes needs this option to enable hardware
487 * virtualization, but some versions of 'qemu-kvm' will use KVM
488 * regardless (even where this option appears in the help text).
489 * It is rumoured that there are versions of qemu where supplying
490 * this option when hardware virtualization is not available will
491 * cause qemu to fail, so we we have to check at least that
492 * /dev/kvm is openable. That's not reliable, since /dev/kvm
493 * might be openable by qemu but not by us (think: SELinux) in
494 * which case the user would not get hardware virtualization,
495 * although at least shouldn't fail. A giant clusterfuck with the
496 * qemu command line, again.
498 if (qemu_supports (g, "-enable-kvm") &&
499 is_openable (g, "/dev/kvm", O_RDWR))
500 add_cmdline (g, "-enable-kvm");
502 /* Newer versions of qemu (from around 2009/12) changed the
503 * behaviour of monitors so that an implicit '-monitor stdio' is
504 * assumed if we are in -nographic mode and there is no other
505 * -monitor option. Only a single stdio device is allowed, so
506 * this broke the '-serial stdio' option. There is a new flag
507 * called -nodefaults which gets rid of all this default crud, so
508 * let's use that to avoid this and any future surprises.
510 if (qemu_supports (g, "-nodefaults"))
511 add_cmdline (g, "-nodefaults");
513 add_cmdline (g, "-nographic");
515 snprintf (buf, sizeof buf, "%d", g->memsize);
516 add_cmdline (g, "-m");
517 add_cmdline (g, buf);
519 /* Force exit instead of reboot on panic */
520 add_cmdline (g, "-no-reboot");
522 /* These options recommended by KVM developers to improve reliability. */
523 if (qemu_supports (g, "-no-hpet"))
524 add_cmdline (g, "-no-hpet");
526 if (qemu_supports (g, "-rtc-td-hack"))
527 add_cmdline (g, "-rtc-td-hack");
529 /* Create the virtio serial bus. */
530 add_cmdline (g, "-device");
531 add_cmdline (g, "virtio-serial");
534 /* Use virtio-console (a variant form of virtio-serial) for the
535 * guest's serial console.
537 add_cmdline (g, "-chardev");
538 add_cmdline (g, "stdio,id=console");
539 add_cmdline (g, "-device");
540 add_cmdline (g, "virtconsole,chardev=console,name=org.libguestfs.console.0");
542 /* When the above works ... until then: */
543 add_cmdline (g, "-serial");
544 add_cmdline (g, "stdio");
547 /* Set up virtio-serial for the communications channel. */
548 add_cmdline (g, "-chardev");
549 snprintf (buf, sizeof buf, "socket,path=%s,id=channel0", unixsock);
550 add_cmdline (g, buf);
551 add_cmdline (g, "-device");
552 add_cmdline (g, "virtserialport,chardev=channel0,name=org.libguestfs.channel.0");
554 /* Enable user networking. */
555 if (g->enable_network) {
556 add_cmdline (g, "-netdev");
557 add_cmdline (g, "user,id=usernet,net=169.254.0.0/16");
558 add_cmdline (g, "-device");
559 add_cmdline (g, NET_IF ",netdev=usernet");
562 #define LINUX_CMDLINE \
563 "panic=1 " /* force kernel to panic if daemon exits */ \
564 "console=ttyS0 " /* serial console */ \
565 "udevtimeout=300 " /* good for very slow systems (RHBZ#480319) */ \
566 "noapic " /* workaround for RHBZ#502058 - ok if not SMP */ \
567 "acpi=off " /* we don't need ACPI, turn it off */ \
568 "printk.time=1 " /* display timestamp before kernel messages */ \
569 "cgroup_disable=memory " /* saves us about 5 MB of RAM */
571 /* Linux kernel command line. */
572 snprintf (buf, sizeof buf,
574 "%s " /* (selinux) */
575 "%s " /* (verbose) */
576 "TERM=%s " /* (TERM environment variable) */
578 g->selinux ? "selinux=1 enforcing=0" : "selinux=0",
579 g->verbose ? "guestfs_verbose=1" : "",
580 getenv ("TERM") ? : "linux",
581 g->append ? g->append : "");
583 add_cmdline (g, "-kernel");
584 add_cmdline (g, kernel);
585 add_cmdline (g, "-initrd");
586 add_cmdline (g, initrd);
587 add_cmdline (g, "-append");
588 add_cmdline (g, buf);
590 /* Add the ext2 appliance drive (last of all). */
592 const char *cachemode = "";
593 if (qemu_supports (g, "cache=")) {
594 if (qemu_supports (g, "unsafe"))
595 cachemode = ",cache=unsafe";
596 else if (qemu_supports (g, "writeback"))
597 cachemode = ",cache=writeback";
600 char buf2[PATH_MAX + 64];
601 add_cmdline (g, "-drive");
602 snprintf (buf2, sizeof buf2, "file=%s,snapshot=on,if=" DRIVE_IF "%s",
603 appliance, cachemode);
604 add_cmdline (g, buf2);
607 /* Finish off the command line. */
608 incr_cmdline_size (g);
609 g->cmdline[g->cmdline_size-1] = NULL;
612 guestfs___print_timestamped_argv (g, (const char **)g->cmdline);
615 /* Set up stdin, stdout. */
621 if (dup (wfd[0]) == -1) {
623 perror ("dup failed");
624 _exit (EXIT_FAILURE);
626 if (dup (rfd[1]) == -1)
634 /* Set up a new process group, so we can signal this process
635 * and all subprocesses (eg. if qemu is really a shell script).
640 setenv ("LC_ALL", "C", 1);
642 execv (g->qemu, g->cmdline); /* Run qemu. */
644 _exit (EXIT_FAILURE);
647 /* Parent (library). */
657 /* Fork the recovery process off which will kill qemu if the parent
658 * process fails to do so (eg. if the parent segfaults).
661 if (g->recovery_proc) {
664 pid_t qemu_pid = g->pid;
665 pid_t parent_pid = getppid ();
667 /* Writing to argv is hideously complicated and error prone. See:
668 * http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/misc/ps_status.c?rev=1.33.2.1;content-type=text%2Fplain
671 /* Loop around waiting for one or both of the other processes to
672 * disappear. It's fair to say this is very hairy. The PIDs that
673 * we are looking at might be reused by another process. We are
674 * effectively polling. Is the cure worse than the disease?
677 if (kill (qemu_pid, 0) == -1) /* qemu's gone away, we aren't needed */
678 _exit (EXIT_SUCCESS);
679 if (kill (parent_pid, 0) == -1) {
680 /* Parent's gone away, qemu still around, so kill qemu. */
682 _exit (EXIT_SUCCESS);
688 /* Don't worry, if the fork failed, this will be -1. The recovery
689 * process isn't essential.
695 /* Close the other ends of the pipe. */
699 if (fcntl (wfd[1], F_SETFL, O_NONBLOCK) == -1 ||
700 fcntl (rfd[0], F_SETFL, O_NONBLOCK) == -1) {
701 perrorf (g, "fcntl");
705 g->fd[0] = wfd[1]; /* stdin of child */
706 g->fd[1] = rfd[0]; /* stdout of child */
708 g->fd[0] = open ("/dev/null", O_RDWR);
709 if (g->fd[0] == -1) {
710 perrorf (g, "open /dev/null");
713 g->fd[1] = dup (g->fd[0]);
714 if (g->fd[1] == -1) {
721 g->state = LAUNCHING;
723 /* Wait for qemu to start and to connect back to us via
724 * virtio-serial and send the GUESTFS_LAUNCH_FLAG message.
726 r = guestfs___accept_from_daemon (g);
730 close (g->sock); /* Close the listening socket. */
731 g->sock = r; /* This is the accepted data socket. */
733 if (fcntl (g->sock, F_SETFL, O_NONBLOCK) == -1) {
734 perrorf (g, "fcntl");
740 r = guestfs___recv_from_daemon (g, &size, &buf);
743 if (r == -1) return -1;
745 if (size != GUESTFS_LAUNCH_FLAG) {
746 error (g, _("guestfs_launch failed, see earlier error messages"));
751 guestfs___print_timestamped_message (g, "appliance is up");
753 /* This is possible in some really strange situations, such as
754 * guestfsd starts up OK but then qemu immediately exits. Check for
755 * it because the caller is probably expecting to be able to send
756 * commands after this function returns.
758 if (g->state != READY) {
759 error (g, _("qemu launched and contacted daemon, but state != READY"));
770 if (g->pid > 0) kill (g->pid, 9);
771 if (g->recoverypid > 0) kill (g->recoverypid, 9);
772 waitpid (g->pid, NULL, 0);
773 if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
778 memset (&g->launch_t, 0, sizeof g->launch_t);
792 /* Alternate attach method: instead of launching the appliance,
793 * connect to an existing unix socket.
796 connect_unix_socket (guestfs_h *g, const char *sockpath)
799 struct sockaddr_un addr;
801 /* Start the clock ... */
802 gettimeofday (&g->launch_t, NULL);
804 /* Set these to nothing so we don't try to kill random processes or
805 * read from random file descriptors.
813 guestfs___print_timestamped_message (g, "connecting to %s", sockpath);
815 g->sock = socket (AF_UNIX, SOCK_STREAM, 0);
817 perrorf (g, "socket");
821 addr.sun_family = AF_UNIX;
822 strncpy (addr.sun_path, sockpath, UNIX_PATH_MAX);
823 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
825 g->state = LAUNCHING;
827 if (connect (g->sock, &addr, sizeof addr) == -1) {
832 if (fcntl (g->sock, F_SETFL, O_NONBLOCK) == -1) {
833 perrorf (g, "fcntl");
839 r = guestfs___recv_from_daemon (g, &size, &buf);
842 if (r == -1) return -1;
844 if (size != GUESTFS_LAUNCH_FLAG) {
845 error (g, _("guestfs_launch failed, unexpected initial message from guestfsd"));
850 guestfs___print_timestamped_message (g, "connected");
852 if (g->state != READY) {
853 error (g, _("contacted guestfsd, but state != READY"));
864 /* Return the location of the tmpdir (eg. "/tmp") and allow users
865 * to override it at runtime using $TMPDIR.
866 * http://www.pathname.com/fhs/pub/fhs-2.3.html#TMPTEMPORARYFILES
869 guestfs_tmpdir (void)
879 const char *t = getenv ("TMPDIR");
885 /* Return the location of the persistent tmpdir (eg. "/var/tmp") and
886 * allow users to override it at runtime using $TMPDIR.
887 * http://www.pathname.com/fhs/pub/fhs-2.3.html#VARTMPTEMPORARYFILESPRESERVEDBETWEE
890 guestfs___persistent_tmpdir (void)
896 const char *t = getenv ("TMPDIR");
902 /* Compute Y - X and return the result in milliseconds.
903 * Approximately the same as this code:
904 * http://www.mpp.mpg.de/~huber/util/timevaldiff.c
907 timeval_diff (const struct timeval *x, const struct timeval *y)
911 msec = (y->tv_sec - x->tv_sec) * 1000;
912 msec += (y->tv_usec - x->tv_usec) / 1000;
917 guestfs___print_timestamped_argv (guestfs_h *g, const char * argv[])
923 gettimeofday (&tv, NULL);
924 fprintf (stderr, "[%05" PRIi64 "ms] ", timeval_diff (&g->launch_t, &tv));
927 if (argv[i][0] == '-') /* -option starts a new line */
928 fprintf (stderr, " \\\n ");
930 if (i > 0) fputc (' ', stderr);
932 /* Does it need shell quoting? This only deals with simple cases. */
933 needs_quote = strcspn (argv[i], " ") != strlen (argv[i]);
935 if (needs_quote) fputc ('\'', stderr);
936 fprintf (stderr, "%s", argv[i]);
937 if (needs_quote) fputc ('\'', stderr);
941 fputc ('\n', stderr);
945 guestfs___print_timestamped_message (guestfs_h *g, const char *fs, ...)
953 err = vasprintf (&msg, fs, args);
958 gettimeofday (&tv, NULL);
960 fprintf (stderr, "[%05" PRIi64 "ms] %s\n",
961 timeval_diff (&g->launch_t, &tv), msg);
966 static int read_all (guestfs_h *g, FILE *fp, char **ret);
968 /* Test qemu binary (or wrapper) runs, and do 'qemu -help' and
969 * 'qemu -version' so we know what options this qemu supports and
973 test_qemu (guestfs_h *g)
978 snprintf (cmd, sizeof cmd, "LC_ALL=C '%s' -nographic -help", g->qemu);
980 fp = popen (cmd, "r");
981 /* qemu -help should always work (qemu -version OTOH wasn't
982 * supported by qemu 0.9). If this command doesn't work then it
983 * probably indicates that the qemu binary is missing.
986 /* XXX This error is never printed, even if the qemu binary
987 * doesn't exist. Why?
990 perrorf (g, _("%s: command failed: If qemu is located on a non-standard path, try setting the LIBGUESTFS_QEMU environment variable."), cmd);
994 if (read_all (g, fp, &g->qemu_help) == -1)
997 if (pclose (fp) == -1)
1000 snprintf (cmd, sizeof cmd, "LC_ALL=C '%s' -nographic -version 2>/dev/null",
1003 fp = popen (cmd, "r");
1005 /* Intentionally ignore errors. */
1006 read_all (g, fp, &g->qemu_version);
1014 read_all (guestfs_h *g, FILE *fp, char **ret)
1021 *ret = safe_realloc (g, *ret, n + 1);
1026 *ret = safe_realloc (g, *ret, n + BUFSIZ);
1028 r = fread (p, 1, BUFSIZ, fp);
1030 perrorf (g, "read");
1037 /* Test if option is supported by qemu command line (just by grepping
1040 * The first time this is used, it has to run the external qemu
1041 * binary. If that fails, it returns -1.
1043 * To just do the first-time run of the qemu binary, call this with
1044 * option == NULL, in which case it will return -1 if there was an
1048 qemu_supports (guestfs_h *g, const char *option)
1050 if (!g->qemu_help) {
1051 if (test_qemu (g) == -1)
1058 return strstr (g->qemu_help, option) != NULL;
1061 /* Check if a file can be opened. */
1063 is_openable (guestfs_h *g, const char *path, int flags)
1065 int fd = open (path, flags);
1075 /* You had to call this function after launch in versions <= 1.0.70,
1076 * but it is now a no-op.
1079 guestfs__wait_ready (guestfs_h *g)
1081 if (g->state != READY) {
1082 error (g, _("qemu has not been launched yet"));
1090 guestfs__kill_subprocess (guestfs_h *g)
1092 if (g->state == CONFIG) {
1093 error (g, _("no subprocess to kill"));
1098 fprintf (stderr, "sending SIGTERM to process %d\n", g->pid);
1100 if (g->pid > 0) kill (g->pid, SIGTERM);
1101 if (g->recoverypid > 0) kill (g->recoverypid, 9);
1106 /* Access current state. */
1108 guestfs__is_config (guestfs_h *g)
1110 return g->state == CONFIG;
1114 guestfs__is_launching (guestfs_h *g)
1116 return g->state == LAUNCHING;
1120 guestfs__is_ready (guestfs_h *g)
1122 return g->state == READY;
1126 guestfs__is_busy (guestfs_h *g)
1128 return g->state == BUSY;
1132 guestfs__get_state (guestfs_h *g)