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., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <libxml/xmlIO.h>
32 #include <libxml/xmlwriter.h>
40 /* Currently open libguestfs handle. */
46 int keys_from_stdin = 0;
48 const char *libvirt_uri = NULL;
51 static void output (char **roots);
52 static void output_roots (xmlTextWriterPtr xo, char **roots);
53 static void output_root (xmlTextWriterPtr xo, char *root);
54 static void output_mountpoints (xmlTextWriterPtr xo, char *root);
55 static void output_filesystems (xmlTextWriterPtr xo, char *root);
56 static void output_drive_mappings (xmlTextWriterPtr xo, char *root);
57 static void output_applications (xmlTextWriterPtr xo, char *root);
58 static void canonicalize (char *dev);
59 static void free_strings (char **argv);
60 static int count_strings (char *const*argv);
63 bad_cast (char const *s)
68 static void __attribute__((noreturn))
71 if (status != EXIT_SUCCESS)
72 fprintf (stderr, _("Try `%s --help' for more information.\n"),
76 _("%s: display information about a virtual machine\n"
77 "Copyright (C) 2010 Red Hat Inc.\n"
79 " %s [--options] -d domname file [file ...]\n"
80 " %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
82 " -a|--add image Add image\n"
83 " -c|--connect uri Specify libvirt URI for -d option\n"
84 " -d|--domain guest Add disks from libvirt guest\n"
85 " --echo-keys Don't turn off echo for passphrases\n"
86 " --format[=raw|..] Force disk format for -a option\n"
87 " --help Display brief help\n"
88 " --keys-from-stdin Read passphrases from stdin\n"
89 " -v|--verbose Verbose messages\n"
90 " -V|--version Display version and exit\n"
91 " -x Trace libguestfs API calls\n"
92 "For more information, see the manpage %s(1).\n"),
93 program_name, program_name, program_name,
100 main (int argc, char *argv[])
102 /* Set global program name that is not polluted with libtool artifacts. */
103 set_program_name (argv[0]);
105 setlocale (LC_ALL, "");
106 bindtextdomain (PACKAGE, LOCALEBASEDIR);
107 textdomain (PACKAGE);
109 enum { HELP_OPTION = CHAR_MAX + 1 };
111 static const char *options = "a:c:d:vVx";
112 static const struct option long_options[] = {
113 { "add", 1, 0, 'a' },
114 { "connect", 1, 0, 'c' },
115 { "domain", 1, 0, 'd' },
116 { "echo-keys", 0, 0, 0 },
117 { "format", 2, 0, 0 },
118 { "help", 0, 0, HELP_OPTION },
119 { "keys-from-stdin", 0, 0, 0 },
120 { "verbose", 0, 0, 'v' },
121 { "version", 0, 0, 'V' },
124 struct drv *drvs = NULL;
126 const char *format = NULL;
130 g = guestfs_create ();
132 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
136 argv[0] = bad_cast (program_name);
139 c = getopt_long (argc, argv, options, long_options, &option_index);
143 case 0: /* options which are long only */
144 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
146 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
148 } else if (STREQ (long_options[option_index].name, "format")) {
149 if (!optarg || STREQ (optarg, ""))
154 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
155 program_name, long_options[option_index].name, option_index);
173 usage (EXIT_SUCCESS);
188 usage (EXIT_SUCCESS);
191 usage (EXIT_FAILURE);
195 /* Old-style syntax? There were no -a or -d options in the old
196 * virt-inspector which is how we detect this.
199 while (optind < argc) {
200 if (strchr (argv[optind], '/') ||
201 access (argv[optind], F_OK) == 0) { /* simulate -a option */
202 drv = malloc (sizeof (struct drv));
208 drv->a.filename = argv[optind];
209 drv->a.format = NULL;
212 } else { /* simulate -d option */
213 drv = malloc (sizeof (struct drv));
219 drv->d.guest = argv[optind];
228 /* These are really constants, but they have to be variables for the
229 * options parsing code. Assert here that they have known-good
232 assert (read_only == 1);
233 assert (inspector == 1);
236 /* Must be no extra arguments on the command line. */
238 usage (EXIT_FAILURE);
240 /* User must have specified some drives. */
242 usage (EXIT_FAILURE);
244 /* Add drives, inspect and mount. Note that inspector is always true,
245 * and there is no -m option.
247 add_drives (drvs, 'a');
249 if (guestfs_launch (g) == -1)
252 /* Free up data structures, no longer needed after this point. */
255 /* NB. Can't call inspect_mount () here (ie. normal processing of
256 * the -i option) because it can only handle a single root. So we
257 * use low-level APIs.
259 inspect_do_decrypt ();
261 char **roots = guestfs_inspect_os (g);
263 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"),
270 free_strings (roots);
277 #define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
278 guestfs_error_handler_cb old_error_cb; \
279 void *old_error_data; \
280 old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
281 guestfs_set_error_handler (g, NULL, NULL); \
283 guestfs_set_error_handler (g, old_error_cb, old_error_data); \
286 #define XMLERROR(code,e) do { \
287 if ((e) == (code)) { \
288 fprintf (stderr, _("%s: XML write error at \"%s\": %m\n"), \
290 exit (EXIT_FAILURE); \
295 output (char **roots)
297 xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL);
300 _("%s: xmlOutputBufferCreateFd: failed to open stdout\n"),
305 xmlTextWriterPtr xo = xmlNewTextWriter (ob);
308 _("%s: xmlNewTextWriter: failed to create libxml2 writer\n"),
313 /* Pretty-print the output. */
314 XMLERROR (-1, xmlTextWriterSetIndent (xo, 1));
315 XMLERROR (-1, xmlTextWriterSetIndentString (xo, BAD_CAST " "));
317 XMLERROR (-1, xmlTextWriterStartDocument (xo, NULL, NULL, NULL));
318 output_roots (xo, roots);
319 XMLERROR (-1, xmlTextWriterEndDocument (xo));
321 /* 'ob' is freed by this too. */
322 xmlFreeTextWriter (xo);
326 output_roots (xmlTextWriterPtr xo, char **roots)
330 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystems"));
331 for (i = 0; roots[i] != NULL; ++i)
332 output_root (xo, roots[i]);
333 XMLERROR (-1, xmlTextWriterEndElement (xo));
337 output_root (xmlTextWriterPtr xo, char *root)
342 char canonical_root[strlen (root) + 1];
345 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystem"));
347 strcpy (canonical_root, root);
348 canonicalize (canonical_root);
350 xmlTextWriterWriteElement (xo, BAD_CAST "root", BAD_CAST canonical_root));
352 str = guestfs_inspect_get_type (g, root);
353 if (!str) exit (EXIT_FAILURE);
354 if (STRNEQ (str, "unknown"))
356 xmlTextWriterWriteElement (xo, BAD_CAST "name", BAD_CAST str));
359 str = guestfs_inspect_get_arch (g, root);
360 if (!str) exit (EXIT_FAILURE);
361 if (STRNEQ (str, "unknown"))
363 xmlTextWriterWriteElement (xo, BAD_CAST "arch", BAD_CAST str));
366 str = guestfs_inspect_get_distro (g, root);
367 if (!str) exit (EXIT_FAILURE);
368 if (STRNEQ (str, "unknown"))
370 xmlTextWriterWriteElement (xo, BAD_CAST "distro", BAD_CAST str));
373 str = guestfs_inspect_get_product_name (g, root);
374 if (!str) exit (EXIT_FAILURE);
375 if (STRNEQ (str, "unknown"))
377 xmlTextWriterWriteElement (xo, BAD_CAST "product_name", BAD_CAST str));
380 str = guestfs_inspect_get_product_variant (g, root);
381 if (!str) exit (EXIT_FAILURE);
382 if (STRNEQ (str, "unknown"))
384 xmlTextWriterWriteElement (xo, BAD_CAST "product_variant", BAD_CAST str));
387 i = guestfs_inspect_get_major_version (g, root);
388 snprintf (buf, sizeof buf, "%d", i);
390 xmlTextWriterWriteElement (xo, BAD_CAST "major_version", BAD_CAST buf));
391 i = guestfs_inspect_get_minor_version (g, root);
392 snprintf (buf, sizeof buf, "%d", i);
394 xmlTextWriterWriteElement (xo, BAD_CAST "minor_version", BAD_CAST buf));
396 str = guestfs_inspect_get_package_format (g, root);
397 if (!str) exit (EXIT_FAILURE);
398 if (STRNEQ (str, "unknown"))
400 xmlTextWriterWriteElement (xo, BAD_CAST "package_format", BAD_CAST str));
403 str = guestfs_inspect_get_package_management (g, root);
404 if (!str) exit (EXIT_FAILURE);
405 if (STRNEQ (str, "unknown"))
407 xmlTextWriterWriteElement (xo, BAD_CAST "package_management",
411 /* inspect-get-windows-systemroot will fail with non-windows guests,
412 * or if the systemroot could not be determined for a windows guest.
413 * Disable error output around this call.
415 DISABLE_GUESTFS_ERRORS_FOR (
416 str = guestfs_inspect_get_windows_systemroot (g, root);
419 xmlTextWriterWriteElement (xo, BAD_CAST "windows_systemroot",
423 DISABLE_GUESTFS_ERRORS_FOR (
424 str = guestfs_inspect_get_windows_current_control_set (g, root);
427 xmlTextWriterWriteElement (xo, BAD_CAST "windows_current_control_set",
432 str = guestfs_inspect_get_hostname (g, root);
433 if (!str) exit (EXIT_FAILURE);
434 if (STRNEQ (str, "unknown"))
436 xmlTextWriterWriteElement (xo, BAD_CAST "hostname",
440 str = guestfs_inspect_get_format (g, root);
441 if (!str) exit (EXIT_FAILURE);
442 if (STRNEQ (str, "unknown"))
444 xmlTextWriterWriteElement (xo, BAD_CAST "format",
448 r = guestfs_inspect_is_live (g, root);
451 xmlTextWriterStartElement (xo, BAD_CAST "live"));
452 XMLERROR (-1, xmlTextWriterEndElement (xo));
455 r = guestfs_inspect_is_netinst (g, root);
458 xmlTextWriterStartElement (xo, BAD_CAST "netinst"));
459 XMLERROR (-1, xmlTextWriterEndElement (xo));
462 r = guestfs_inspect_is_multipart (g, root);
465 xmlTextWriterStartElement (xo, BAD_CAST "multipart"));
466 XMLERROR (-1, xmlTextWriterEndElement (xo));
469 output_mountpoints (xo, root);
471 output_filesystems (xo, root);
473 output_drive_mappings (xo, root);
475 /* We need to mount everything up in order to read out the list of
476 * applications and the icon, ie. everything below this point.
478 inspect_mount_root (root);
480 output_applications (xo, root);
482 /* Don't return favicon. XXX Should we? */
483 str = guestfs_inspect_get_icon (g, root, &size,
484 GUESTFS_INSPECT_GET_ICON_FAVICON, 0,
486 if (!str) exit (EXIT_FAILURE);
488 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "icon"));
489 XMLERROR (-1, xmlTextWriterWriteBase64 (xo, str, 0, size));
490 XMLERROR (-1, xmlTextWriterEndElement (xo));
492 /* Note we must free (str) even if size == 0, because that indicates
497 /* Unmount (see inspect_mount_root above). */
498 if (guestfs_umount_all (g) == -1)
501 XMLERROR (-1, xmlTextWriterEndElement (xo));
505 compare_keys (const void *p1, const void *p2)
507 const char *key1 = * (char * const *) p1;
508 const char *key2 = * (char * const *) p2;
510 return strcmp (key1, key2);
514 compare_keys_nocase (const void *p1, const void *p2)
516 const char *key1 = * (char * const *) p1;
517 const char *key2 = * (char * const *) p2;
519 return strcasecmp (key1, key2);
523 compare_keys_len (const void *p1, const void *p2)
525 const char *key1 = * (char * const *) p1;
526 const char *key2 = * (char * const *) p2;
529 c = strlen (key1) - strlen (key2);
533 return compare_keys (p1, p2);
537 output_mountpoints (xmlTextWriterPtr xo, char *root)
542 mountpoints = guestfs_inspect_get_mountpoints (g, root);
543 if (mountpoints == NULL)
546 /* Sort by key length, shortest key first, and then name, so the
549 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
552 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
554 for (i = 0; mountpoints[i] != NULL; i += 2) {
555 canonicalize (mountpoints[i+1]);
558 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
560 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
561 BAD_CAST mountpoints[i+1]));
563 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
564 XMLERROR (-1, xmlTextWriterEndElement (xo));
567 XMLERROR (-1, xmlTextWriterEndElement (xo));
569 free_strings (mountpoints);
573 output_filesystems (xmlTextWriterPtr xo, char *root)
579 filesystems = guestfs_inspect_get_filesystems (g, root);
580 if (filesystems == NULL)
583 /* Sort by name so the output is stable. */
584 qsort (filesystems, count_strings (filesystems), sizeof (char *),
587 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
589 for (i = 0; filesystems[i] != NULL; ++i) {
590 canonicalize (filesystems[i]);
592 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
594 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
595 BAD_CAST filesystems[i]));
597 DISABLE_GUESTFS_ERRORS_FOR (
598 str = guestfs_vfs_type (g, filesystems[i]);
601 xmlTextWriterWriteElement (xo, BAD_CAST "type",
606 DISABLE_GUESTFS_ERRORS_FOR (
607 str = guestfs_vfs_label (g, filesystems[i]);
610 xmlTextWriterWriteElement (xo, BAD_CAST "label",
615 DISABLE_GUESTFS_ERRORS_FOR (
616 str = guestfs_vfs_uuid (g, filesystems[i]);
619 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
624 XMLERROR (-1, xmlTextWriterEndElement (xo));
627 XMLERROR (-1, xmlTextWriterEndElement (xo));
629 free_strings (filesystems);
633 output_drive_mappings (xmlTextWriterPtr xo, char *root)
635 char **drive_mappings = NULL;
638 DISABLE_GUESTFS_ERRORS_FOR (
639 drive_mappings = guestfs_inspect_get_drive_mappings (g, root);
641 if (drive_mappings == NULL)
644 if (drive_mappings[0] == NULL) {
645 free_strings (drive_mappings);
650 qsort (drive_mappings,
651 count_strings (drive_mappings) / 2, 2 * sizeof (char *),
652 compare_keys_nocase);
654 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "drive_mappings"));
656 for (i = 0; drive_mappings[i] != NULL; i += 2) {
657 canonicalize (drive_mappings[i+1]);
660 xmlTextWriterStartElement (xo, BAD_CAST "drive_mapping"));
662 xmlTextWriterWriteAttribute (xo, BAD_CAST "name",
663 BAD_CAST drive_mappings[i]));
665 xmlTextWriterWriteString (xo, BAD_CAST drive_mappings[i+1]));
666 XMLERROR (-1, xmlTextWriterEndElement (xo));
669 XMLERROR (-1, xmlTextWriterEndElement (xo));
671 free_strings (drive_mappings);
675 output_applications (xmlTextWriterPtr xo, char *root)
677 struct guestfs_application_list *apps;
680 /* This returns an empty list if we simply couldn't determine the
681 * applications, so if it returns NULL then it's a real error.
683 apps = guestfs_inspect_list_applications (g, root);
687 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
689 for (i = 0; i < apps->len; ++i) {
690 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
692 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
694 xmlTextWriterWriteElement (xo, BAD_CAST "name",
695 BAD_CAST apps->val[i].app_name));
697 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
699 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
700 BAD_CAST apps->val[i].app_display_name));
702 if (apps->val[i].app_epoch != 0) {
705 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
708 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
711 if (apps->val[i].app_version && apps->val[i].app_version[0])
713 xmlTextWriterWriteElement (xo, BAD_CAST "version",
714 BAD_CAST apps->val[i].app_version));
715 if (apps->val[i].app_release && apps->val[i].app_release[0])
717 xmlTextWriterWriteElement (xo, BAD_CAST "release",
718 BAD_CAST apps->val[i].app_release));
719 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
721 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
722 BAD_CAST apps->val[i].app_install_path));
723 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
725 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
726 BAD_CAST apps->val[i].app_publisher));
727 if (apps->val[i].app_url && apps->val[i].app_url[0])
729 xmlTextWriterWriteElement (xo, BAD_CAST "url",
730 BAD_CAST apps->val[i].app_url));
731 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
733 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
734 BAD_CAST apps->val[i].app_source_package));
735 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
737 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
738 BAD_CAST apps->val[i].app_summary));
739 if (apps->val[i].app_description && apps->val[i].app_description[0])
741 xmlTextWriterWriteElement (xo, BAD_CAST "description",
742 BAD_CAST apps->val[i].app_description));
744 XMLERROR (-1, xmlTextWriterEndElement (xo));
747 XMLERROR (-1, xmlTextWriterEndElement (xo));
749 guestfs_free_application_list (apps);
752 /* "/dev/vda1" -> "/dev/sda1"
753 * See BLOCK DEVICE NAMING in guestfs(3).
756 canonicalize (char *dev)
758 if (STRPREFIX (dev, "/dev/") &&
759 (dev[5] == 'h' || dev[5] == 'v') &&
761 c_isalpha (dev[7]) &&
762 (c_isdigit (dev[8]) || dev[8] == '\0'))
767 free_strings (char **argv)
771 for (argc = 0; argv[argc] != NULL; ++argc)
777 count_strings (char *const *argv)
781 for (c = 0; argv[c]; ++c)