2 * Copyright (C) 2010-2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <libxml/xmlIO.h>
32 #include <libxml/xmlwriter.h>
33 #include <libxml/xpath.h>
34 #include <libxml/parser.h>
35 #include <libxml/tree.h>
36 #include <libxml/xmlsave.h>
44 /* Currently open libguestfs handle. */
50 int keys_from_stdin = 0;
52 const char *libvirt_uri = NULL;
54 static const char *xpath = NULL;
56 static void output (char **roots);
57 static void output_roots (xmlTextWriterPtr xo, char **roots);
58 static void output_root (xmlTextWriterPtr xo, char *root);
59 static void output_mountpoints (xmlTextWriterPtr xo, char *root);
60 static void output_filesystems (xmlTextWriterPtr xo, char *root);
61 static void output_drive_mappings (xmlTextWriterPtr xo, char *root);
62 static void output_applications (xmlTextWriterPtr xo, char *root);
63 static void canonicalize (char *dev);
64 static void free_strings (char **argv);
65 static int count_strings (char *const*argv);
66 static void do_xpath (const char *query);
69 bad_cast (char const *s)
74 static void __attribute__((noreturn))
77 if (status != EXIT_SUCCESS)
78 fprintf (stderr, _("Try `%s --help' for more information.\n"),
82 _("%s: display information about a virtual machine\n"
83 "Copyright (C) 2010 Red Hat Inc.\n"
85 " %s [--options] -d domname file [file ...]\n"
86 " %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
88 " -a|--add image Add image\n"
89 " -c|--connect uri Specify libvirt URI for -d option\n"
90 " -d|--domain guest Add disks from libvirt guest\n"
91 " --echo-keys Don't turn off echo for passphrases\n"
92 " --format[=raw|..] Force disk format for -a option\n"
93 " --help Display brief help\n"
94 " --keys-from-stdin Read passphrases from stdin\n"
95 " -v|--verbose Verbose messages\n"
96 " -V|--version Display version and exit\n"
97 " -x Trace libguestfs API calls\n"
98 " --xpath query Perform an XPath query\n"
99 "For more information, see the manpage %s(1).\n"),
100 program_name, program_name, program_name,
107 main (int argc, char *argv[])
109 /* Set global program name that is not polluted with libtool artifacts. */
110 set_program_name (argv[0]);
112 setlocale (LC_ALL, "");
113 bindtextdomain (PACKAGE, LOCALEBASEDIR);
114 textdomain (PACKAGE);
116 enum { HELP_OPTION = CHAR_MAX + 1 };
118 static const char *options = "a:c:d:vVx";
119 static const struct option long_options[] = {
120 { "add", 1, 0, 'a' },
121 { "connect", 1, 0, 'c' },
122 { "domain", 1, 0, 'd' },
123 { "echo-keys", 0, 0, 0 },
124 { "format", 2, 0, 0 },
125 { "help", 0, 0, HELP_OPTION },
126 { "keys-from-stdin", 0, 0, 0 },
127 { "verbose", 0, 0, 'v' },
128 { "version", 0, 0, 'V' },
129 { "xpath", 1, 0, 0 },
132 struct drv *drvs = NULL;
134 const char *format = NULL;
138 g = guestfs_create ();
140 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
144 argv[0] = bad_cast (program_name);
147 c = getopt_long (argc, argv, options, long_options, &option_index);
151 case 0: /* options which are long only */
152 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
154 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
156 } else if (STREQ (long_options[option_index].name, "format")) {
157 if (!optarg || STREQ (optarg, ""))
161 } else if (STREQ (long_options[option_index].name, "xpath")) {
164 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
165 program_name, long_options[option_index].name, option_index);
183 usage (EXIT_SUCCESS);
198 usage (EXIT_SUCCESS);
201 usage (EXIT_FAILURE);
205 /* Old-style syntax? There were no -a or -d options in the old
206 * virt-inspector which is how we detect this.
209 while (optind < argc) {
210 if (strchr (argv[optind], '/') ||
211 access (argv[optind], F_OK) == 0) { /* simulate -a option */
212 drv = malloc (sizeof (struct drv));
218 drv->a.filename = argv[optind];
219 drv->a.format = NULL;
222 } else { /* simulate -d option */
223 drv = malloc (sizeof (struct drv));
229 drv->d.guest = argv[optind];
238 /* These are really constants, but they have to be variables for the
239 * options parsing code. Assert here that they have known-good
242 assert (read_only == 1);
243 assert (inspector == 1);
246 /* Must be no extra arguments on the command line. */
248 usage (EXIT_FAILURE);
250 /* XPath is modal: no drives should be specified. There must be
251 * one extra parameter on the command line.
255 fprintf (stderr, _("%s: cannot use --xpath together with other options.\n"),
265 /* User must have specified some drives. */
267 usage (EXIT_FAILURE);
269 /* Add drives, inspect and mount. Note that inspector is always true,
270 * and there is no -m option.
272 add_drives (drvs, 'a');
274 if (guestfs_launch (g) == -1)
277 /* Free up data structures, no longer needed after this point. */
280 /* NB. Can't call inspect_mount () here (ie. normal processing of
281 * the -i option) because it can only handle a single root. So we
282 * use low-level APIs.
284 inspect_do_decrypt ();
286 char **roots = guestfs_inspect_os (g);
288 fprintf (stderr, _("%s: no operating system could be detected inside this disk image.\n\nThis may be because the file is not a disk image, or is not a virtual machine\nimage, or because the OS type is not understood by libguestfs.\n\nNOTE for Red Hat Enterprise Linux 6 users: for Windows guest support you must\ninstall the separate libguestfs-winsupport package.\n\nIf you feel this is an error, please file a bug report including as much\ninformation about the disk image as possible.\n"),
295 free_strings (roots);
302 #define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
303 guestfs_error_handler_cb old_error_cb; \
304 void *old_error_data; \
305 old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
306 guestfs_set_error_handler (g, NULL, NULL); \
308 guestfs_set_error_handler (g, old_error_cb, old_error_data); \
311 #define XMLERROR(code,e) do { \
312 if ((e) == (code)) { \
313 fprintf (stderr, _("%s: XML write error at \"%s\": %m\n"), \
315 exit (EXIT_FAILURE); \
320 output (char **roots)
322 xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL);
325 _("%s: xmlOutputBufferCreateFd: failed to open stdout\n"),
330 xmlTextWriterPtr xo = xmlNewTextWriter (ob);
333 _("%s: xmlNewTextWriter: failed to create libxml2 writer\n"),
338 /* Pretty-print the output. */
339 XMLERROR (-1, xmlTextWriterSetIndent (xo, 1));
340 XMLERROR (-1, xmlTextWriterSetIndentString (xo, BAD_CAST " "));
342 XMLERROR (-1, xmlTextWriterStartDocument (xo, NULL, NULL, NULL));
343 output_roots (xo, roots);
344 XMLERROR (-1, xmlTextWriterEndDocument (xo));
346 /* 'ob' is freed by this too. */
347 xmlFreeTextWriter (xo);
351 output_roots (xmlTextWriterPtr xo, char **roots)
355 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystems"));
356 for (i = 0; roots[i] != NULL; ++i)
357 output_root (xo, roots[i]);
358 XMLERROR (-1, xmlTextWriterEndElement (xo));
362 output_root (xmlTextWriterPtr xo, char *root)
367 char canonical_root[strlen (root) + 1];
370 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystem"));
372 strcpy (canonical_root, root);
373 canonicalize (canonical_root);
375 xmlTextWriterWriteElement (xo, BAD_CAST "root", BAD_CAST canonical_root));
377 str = guestfs_inspect_get_type (g, root);
378 if (!str) exit (EXIT_FAILURE);
379 if (STRNEQ (str, "unknown"))
381 xmlTextWriterWriteElement (xo, BAD_CAST "name", BAD_CAST str));
384 str = guestfs_inspect_get_arch (g, root);
385 if (!str) exit (EXIT_FAILURE);
386 if (STRNEQ (str, "unknown"))
388 xmlTextWriterWriteElement (xo, BAD_CAST "arch", BAD_CAST str));
391 str = guestfs_inspect_get_distro (g, root);
392 if (!str) exit (EXIT_FAILURE);
393 if (STRNEQ (str, "unknown"))
395 xmlTextWriterWriteElement (xo, BAD_CAST "distro", BAD_CAST str));
398 str = guestfs_inspect_get_product_name (g, root);
399 if (!str) exit (EXIT_FAILURE);
400 if (STRNEQ (str, "unknown"))
402 xmlTextWriterWriteElement (xo, BAD_CAST "product_name", BAD_CAST str));
405 str = guestfs_inspect_get_product_variant (g, root);
406 if (!str) exit (EXIT_FAILURE);
407 if (STRNEQ (str, "unknown"))
409 xmlTextWriterWriteElement (xo, BAD_CAST "product_variant", BAD_CAST str));
412 i = guestfs_inspect_get_major_version (g, root);
413 snprintf (buf, sizeof buf, "%d", i);
415 xmlTextWriterWriteElement (xo, BAD_CAST "major_version", BAD_CAST buf));
416 i = guestfs_inspect_get_minor_version (g, root);
417 snprintf (buf, sizeof buf, "%d", i);
419 xmlTextWriterWriteElement (xo, BAD_CAST "minor_version", BAD_CAST buf));
421 str = guestfs_inspect_get_package_format (g, root);
422 if (!str) exit (EXIT_FAILURE);
423 if (STRNEQ (str, "unknown"))
425 xmlTextWriterWriteElement (xo, BAD_CAST "package_format", BAD_CAST str));
428 str = guestfs_inspect_get_package_management (g, root);
429 if (!str) exit (EXIT_FAILURE);
430 if (STRNEQ (str, "unknown"))
432 xmlTextWriterWriteElement (xo, BAD_CAST "package_management",
436 /* inspect-get-windows-systemroot will fail with non-windows guests,
437 * or if the systemroot could not be determined for a windows guest.
438 * Disable error output around this call.
440 DISABLE_GUESTFS_ERRORS_FOR (
441 str = guestfs_inspect_get_windows_systemroot (g, root);
444 xmlTextWriterWriteElement (xo, BAD_CAST "windows_systemroot",
448 DISABLE_GUESTFS_ERRORS_FOR (
449 str = guestfs_inspect_get_windows_current_control_set (g, root);
452 xmlTextWriterWriteElement (xo, BAD_CAST "windows_current_control_set",
457 str = guestfs_inspect_get_hostname (g, root);
458 if (!str) exit (EXIT_FAILURE);
459 if (STRNEQ (str, "unknown"))
461 xmlTextWriterWriteElement (xo, BAD_CAST "hostname",
465 str = guestfs_inspect_get_format (g, root);
466 if (!str) exit (EXIT_FAILURE);
467 if (STRNEQ (str, "unknown"))
469 xmlTextWriterWriteElement (xo, BAD_CAST "format",
473 r = guestfs_inspect_is_live (g, root);
476 xmlTextWriterStartElement (xo, BAD_CAST "live"));
477 XMLERROR (-1, xmlTextWriterEndElement (xo));
480 r = guestfs_inspect_is_netinst (g, root);
483 xmlTextWriterStartElement (xo, BAD_CAST "netinst"));
484 XMLERROR (-1, xmlTextWriterEndElement (xo));
487 r = guestfs_inspect_is_multipart (g, root);
490 xmlTextWriterStartElement (xo, BAD_CAST "multipart"));
491 XMLERROR (-1, xmlTextWriterEndElement (xo));
494 output_mountpoints (xo, root);
496 output_filesystems (xo, root);
498 output_drive_mappings (xo, root);
500 /* We need to mount everything up in order to read out the list of
501 * applications and the icon, ie. everything below this point.
503 inspect_mount_root (root);
505 output_applications (xo, root);
507 /* Don't return favicon. XXX Should we? */
508 str = guestfs_inspect_get_icon (g, root, &size,
509 GUESTFS_INSPECT_GET_ICON_FAVICON, 0,
511 if (!str) exit (EXIT_FAILURE);
513 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "icon"));
514 XMLERROR (-1, xmlTextWriterWriteBase64 (xo, str, 0, size));
515 XMLERROR (-1, xmlTextWriterEndElement (xo));
517 /* Note we must free (str) even if size == 0, because that indicates
522 /* Unmount (see inspect_mount_root above). */
523 if (guestfs_umount_all (g) == -1)
526 XMLERROR (-1, xmlTextWriterEndElement (xo));
530 compare_keys (const void *p1, const void *p2)
532 const char *key1 = * (char * const *) p1;
533 const char *key2 = * (char * const *) p2;
535 return strcmp (key1, key2);
539 compare_keys_nocase (const void *p1, const void *p2)
541 const char *key1 = * (char * const *) p1;
542 const char *key2 = * (char * const *) p2;
544 return strcasecmp (key1, key2);
548 compare_keys_len (const void *p1, const void *p2)
550 const char *key1 = * (char * const *) p1;
551 const char *key2 = * (char * const *) p2;
554 c = strlen (key1) - strlen (key2);
558 return compare_keys (p1, p2);
562 output_mountpoints (xmlTextWriterPtr xo, char *root)
567 mountpoints = guestfs_inspect_get_mountpoints (g, root);
568 if (mountpoints == NULL)
571 /* Sort by key length, shortest key first, and then name, so the
574 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
577 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
579 for (i = 0; mountpoints[i] != NULL; i += 2) {
580 canonicalize (mountpoints[i+1]);
583 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
585 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
586 BAD_CAST mountpoints[i+1]));
588 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
589 XMLERROR (-1, xmlTextWriterEndElement (xo));
592 XMLERROR (-1, xmlTextWriterEndElement (xo));
594 free_strings (mountpoints);
598 output_filesystems (xmlTextWriterPtr xo, char *root)
604 filesystems = guestfs_inspect_get_filesystems (g, root);
605 if (filesystems == NULL)
608 /* Sort by name so the output is stable. */
609 qsort (filesystems, count_strings (filesystems), sizeof (char *),
612 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
614 for (i = 0; filesystems[i] != NULL; ++i) {
615 canonicalize (filesystems[i]);
617 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
619 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
620 BAD_CAST filesystems[i]));
622 DISABLE_GUESTFS_ERRORS_FOR (
623 str = guestfs_vfs_type (g, filesystems[i]);
626 xmlTextWriterWriteElement (xo, BAD_CAST "type",
631 DISABLE_GUESTFS_ERRORS_FOR (
632 str = guestfs_vfs_label (g, filesystems[i]);
635 xmlTextWriterWriteElement (xo, BAD_CAST "label",
640 DISABLE_GUESTFS_ERRORS_FOR (
641 str = guestfs_vfs_uuid (g, filesystems[i]);
644 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
649 XMLERROR (-1, xmlTextWriterEndElement (xo));
652 XMLERROR (-1, xmlTextWriterEndElement (xo));
654 free_strings (filesystems);
658 output_drive_mappings (xmlTextWriterPtr xo, char *root)
660 char **drive_mappings = NULL;
663 DISABLE_GUESTFS_ERRORS_FOR (
664 drive_mappings = guestfs_inspect_get_drive_mappings (g, root);
666 if (drive_mappings == NULL)
669 if (drive_mappings[0] == NULL) {
670 free_strings (drive_mappings);
675 qsort (drive_mappings,
676 count_strings (drive_mappings) / 2, 2 * sizeof (char *),
677 compare_keys_nocase);
679 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "drive_mappings"));
681 for (i = 0; drive_mappings[i] != NULL; i += 2) {
682 canonicalize (drive_mappings[i+1]);
685 xmlTextWriterStartElement (xo, BAD_CAST "drive_mapping"));
687 xmlTextWriterWriteAttribute (xo, BAD_CAST "name",
688 BAD_CAST drive_mappings[i]));
690 xmlTextWriterWriteString (xo, BAD_CAST drive_mappings[i+1]));
691 XMLERROR (-1, xmlTextWriterEndElement (xo));
694 XMLERROR (-1, xmlTextWriterEndElement (xo));
696 free_strings (drive_mappings);
700 output_applications (xmlTextWriterPtr xo, char *root)
702 struct guestfs_application_list *apps;
705 /* This returns an empty list if we simply couldn't determine the
706 * applications, so if it returns NULL then it's a real error.
708 apps = guestfs_inspect_list_applications (g, root);
712 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
714 for (i = 0; i < apps->len; ++i) {
715 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
717 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
719 xmlTextWriterWriteElement (xo, BAD_CAST "name",
720 BAD_CAST apps->val[i].app_name));
722 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
724 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
725 BAD_CAST apps->val[i].app_display_name));
727 if (apps->val[i].app_epoch != 0) {
730 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
733 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
736 if (apps->val[i].app_version && apps->val[i].app_version[0])
738 xmlTextWriterWriteElement (xo, BAD_CAST "version",
739 BAD_CAST apps->val[i].app_version));
740 if (apps->val[i].app_release && apps->val[i].app_release[0])
742 xmlTextWriterWriteElement (xo, BAD_CAST "release",
743 BAD_CAST apps->val[i].app_release));
744 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
746 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
747 BAD_CAST apps->val[i].app_install_path));
748 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
750 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
751 BAD_CAST apps->val[i].app_publisher));
752 if (apps->val[i].app_url && apps->val[i].app_url[0])
754 xmlTextWriterWriteElement (xo, BAD_CAST "url",
755 BAD_CAST apps->val[i].app_url));
756 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
758 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
759 BAD_CAST apps->val[i].app_source_package));
760 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
762 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
763 BAD_CAST apps->val[i].app_summary));
764 if (apps->val[i].app_description && apps->val[i].app_description[0])
766 xmlTextWriterWriteElement (xo, BAD_CAST "description",
767 BAD_CAST apps->val[i].app_description));
769 XMLERROR (-1, xmlTextWriterEndElement (xo));
772 XMLERROR (-1, xmlTextWriterEndElement (xo));
774 guestfs_free_application_list (apps);
777 /* "/dev/vda1" -> "/dev/sda1"
778 * See BLOCK DEVICE NAMING in guestfs(3).
781 canonicalize (char *dev)
783 if (STRPREFIX (dev, "/dev/") &&
784 (dev[5] == 'h' || dev[5] == 'v') &&
786 c_isalpha (dev[7]) &&
787 (c_isdigit (dev[8]) || dev[8] == '\0'))
792 free_strings (char **argv)
796 for (argc = 0; argv[argc] != NULL; ++argc)
802 count_strings (char *const *argv)
806 for (c = 0; argv[c]; ++c)
811 /* Run an XPath query on XML on stdin, print results to stdout. */
813 do_xpath (const char *query)
816 xmlXPathContextPtr xpathCtx;
817 xmlXPathObjectPtr xpathObj;
821 xmlSaveCtxtPtr saveCtx;
825 doc = xmlReadFd (STDIN_FILENO, NULL, "utf8", 0);
827 fprintf (stderr, _("%s: unable to parse XML from stdin\n"), program_name);
831 xpathCtx = xmlXPathNewContext (doc);
832 if (xpathCtx == NULL) {
833 fprintf (stderr, _("%s: unable to create new XPath context\n"),
838 xpathObj = xmlXPathEvalExpression (BAD_CAST query, xpathCtx);
839 if (xpathObj == NULL) {
840 fprintf (stderr, _("%s: unable to evaluate XPath expression\n"),
845 switch (xpathObj->type) {
847 nodes = xpathObj->nodesetval;
849 saveCtx = xmlSaveToFd (STDOUT_FILENO, NULL, XML_SAVE_NO_DECL);
850 if (saveCtx == NULL) {
851 fprintf (stderr, _("%s: xmlSaveToFd failed\n"), program_name);
855 for (i = 0; i < (size_t) nodes->nodeNr; ++i) {
856 wrdoc = xmlNewDoc (BAD_CAST "1.0");
858 fprintf (stderr, _("%s: xmlNewDoc failed\n"), program_name);
861 wrnode = xmlCopyNode (nodes->nodeTab[i], 1);
862 if (wrnode == NULL) {
863 fprintf (stderr, _("%s: xmlCopyNode failed\n"), program_name);
867 xmlDocSetRootElement (wrdoc, wrnode);
869 if (xmlSaveDoc (saveCtx, wrdoc) == -1) {
870 fprintf (stderr, _("%s: xmlSaveDoc failed\n"), program_name);
877 xmlSaveClose (saveCtx);
882 r = (char *) xpathObj->stringval;
885 if (i > 0 && r[i-1] != '\n')
889 case XPATH_UNDEFINED: /* grrrrr ... switch-enum is a useless warning */
894 case XPATH_LOCATIONSET:
896 case XPATH_XSLT_TREE:
898 r = (char *) xmlXPathCastToString (xpathObj);
903 xmlXPathFreeObject (xpathObj);
904 xmlXPathFreeContext (xpathCtx);