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.
29 #include <libxml/xmlIO.h>
30 #include <libxml/xmlwriter.h>
38 /* Currently open libguestfs handle. */
44 int keys_from_stdin = 0;
46 const char *libvirt_uri = NULL;
49 static void output (char **roots);
50 static void output_roots (xmlTextWriterPtr xo, char **roots);
51 static void output_root (xmlTextWriterPtr xo, char *root);
52 static void output_mountpoints (xmlTextWriterPtr xo, char *root);
53 static void output_filesystems (xmlTextWriterPtr xo, char *root);
54 static void output_drive_mappings (xmlTextWriterPtr xo, char *root);
55 static void output_applications (xmlTextWriterPtr xo, char *root);
56 static void canonicalize (char *dev);
57 static void free_strings (char **argv);
58 static int count_strings (char *const*argv);
61 bad_cast (char const *s)
66 static void __attribute__((noreturn))
69 if (status != EXIT_SUCCESS)
70 fprintf (stderr, _("Try `%s --help' for more information.\n"),
74 _("%s: display information about a virtual machine\n"
75 "Copyright (C) 2010 Red Hat Inc.\n"
77 " %s [--options] -d domname file [file ...]\n"
78 " %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
80 " -a|--add image Add image\n"
81 " -c|--connect uri Specify libvirt URI for -d option\n"
82 " -d|--domain guest Add disks from libvirt guest\n"
83 " --echo-keys Don't turn off echo for passphrases\n"
84 " --format[=raw|..] Force disk format for -a option\n"
85 " --help Display brief help\n"
86 " --keys-from-stdin Read passphrases from stdin\n"
87 " -v|--verbose Verbose messages\n"
88 " -V|--version Display version and exit\n"
89 " -x Trace libguestfs API calls\n"
90 "For more information, see the manpage %s(1).\n"),
91 program_name, program_name, program_name,
98 main (int argc, char *argv[])
100 /* Set global program name that is not polluted with libtool artifacts. */
101 set_program_name (argv[0]);
103 setlocale (LC_ALL, "");
104 bindtextdomain (PACKAGE, LOCALEBASEDIR);
105 textdomain (PACKAGE);
107 enum { HELP_OPTION = CHAR_MAX + 1 };
109 static const char *options = "a:c:d:vVx";
110 static const struct option long_options[] = {
111 { "add", 1, 0, 'a' },
112 { "connect", 1, 0, 'c' },
113 { "domain", 1, 0, 'd' },
114 { "echo-keys", 0, 0, 0 },
115 { "format", 2, 0, 0 },
116 { "help", 0, 0, HELP_OPTION },
117 { "keys-from-stdin", 0, 0, 0 },
118 { "verbose", 0, 0, 'v' },
119 { "version", 0, 0, 'V' },
122 struct drv *drvs = NULL;
124 const char *format = NULL;
128 g = guestfs_create ();
130 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
134 argv[0] = bad_cast (program_name);
137 c = getopt_long (argc, argv, options, long_options, &option_index);
141 case 0: /* options which are long only */
142 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
144 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
146 } else if (STREQ (long_options[option_index].name, "format")) {
147 if (!optarg || STREQ (optarg, ""))
152 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
153 program_name, long_options[option_index].name, option_index);
171 usage (EXIT_SUCCESS);
186 usage (EXIT_SUCCESS);
189 usage (EXIT_FAILURE);
193 /* Old-style syntax? There were no -a or -d options in the old
194 * virt-inspector which is how we detect this.
197 while (optind < argc) {
198 if (strchr (argv[optind], '/') ||
199 access (argv[optind], F_OK) == 0) { /* simulate -a option */
200 drv = malloc (sizeof (struct drv));
206 drv->a.filename = argv[optind];
207 drv->a.format = NULL;
210 } else { /* simulate -d option */
211 drv = malloc (sizeof (struct drv));
217 drv->d.guest = argv[optind];
226 /* These are really constants, but they have to be variables for the
227 * options parsing code. Assert here that they have known-good
230 assert (read_only == 1);
231 assert (inspector == 1);
234 /* Must be no extra arguments on the command line. */
236 usage (EXIT_FAILURE);
238 /* User must have specified some drives. */
240 usage (EXIT_FAILURE);
242 /* Add drives, inspect and mount. Note that inspector is always true,
243 * and there is no -m option.
245 add_drives (drvs, 'a');
247 if (guestfs_launch (g) == -1)
250 /* Free up data structures, no longer needed after this point. */
253 /* NB. Can't call inspect_mount () here (ie. normal processing of
254 * the -i option) because it can only handle a single root. So we
255 * use low-level APIs.
257 inspect_do_decrypt ();
259 char **roots = guestfs_inspect_os (g);
261 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"),
268 free_strings (roots);
275 #define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
276 guestfs_error_handler_cb old_error_cb; \
277 void *old_error_data; \
278 old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
279 guestfs_set_error_handler (g, NULL, NULL); \
281 guestfs_set_error_handler (g, old_error_cb, old_error_data); \
284 #define XMLERROR(code,e) do { \
285 if ((e) == (code)) { \
286 fprintf (stderr, _("%s: XML write error at \"%s\": %m\n"), \
288 exit (EXIT_FAILURE); \
293 output (char **roots)
295 xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL);
298 _("%s: xmlOutputBufferCreateFd: failed to open stdout\n"),
303 xmlTextWriterPtr xo = xmlNewTextWriter (ob);
306 _("%s: xmlNewTextWriter: failed to create libxml2 writer\n"),
311 /* Pretty-print the output. */
312 XMLERROR (-1, xmlTextWriterSetIndent (xo, 1));
313 XMLERROR (-1, xmlTextWriterSetIndentString (xo, BAD_CAST " "));
315 XMLERROR (-1, xmlTextWriterStartDocument (xo, NULL, NULL, NULL));
316 output_roots (xo, roots);
317 XMLERROR (-1, xmlTextWriterEndDocument (xo));
319 /* 'ob' is freed by this too. */
320 xmlFreeTextWriter (xo);
324 output_roots (xmlTextWriterPtr xo, char **roots)
328 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystems"));
329 for (i = 0; roots[i] != NULL; ++i)
330 output_root (xo, roots[i]);
331 XMLERROR (-1, xmlTextWriterEndElement (xo));
335 output_root (xmlTextWriterPtr xo, char *root)
340 char canonical_root[strlen (root) + 1];
342 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystem"));
344 strcpy (canonical_root, root);
345 canonicalize (canonical_root);
347 xmlTextWriterWriteElement (xo, BAD_CAST "root", BAD_CAST canonical_root));
349 str = guestfs_inspect_get_type (g, root);
350 if (!str) exit (EXIT_FAILURE);
351 if (STRNEQ (str, "unknown"))
353 xmlTextWriterWriteElement (xo, BAD_CAST "name", BAD_CAST str));
356 str = guestfs_inspect_get_arch (g, root);
357 if (!str) exit (EXIT_FAILURE);
358 if (STRNEQ (str, "unknown"))
360 xmlTextWriterWriteElement (xo, BAD_CAST "arch", BAD_CAST str));
363 str = guestfs_inspect_get_distro (g, root);
364 if (!str) exit (EXIT_FAILURE);
365 if (STRNEQ (str, "unknown"))
367 xmlTextWriterWriteElement (xo, BAD_CAST "distro", BAD_CAST str));
370 str = guestfs_inspect_get_product_name (g, root);
371 if (!str) exit (EXIT_FAILURE);
372 if (STRNEQ (str, "unknown"))
374 xmlTextWriterWriteElement (xo, BAD_CAST "product_name", BAD_CAST str));
377 str = guestfs_inspect_get_product_variant (g, root);
378 if (!str) exit (EXIT_FAILURE);
379 if (STRNEQ (str, "unknown"))
381 xmlTextWriterWriteElement (xo, BAD_CAST "product_variant", BAD_CAST str));
384 i = guestfs_inspect_get_major_version (g, root);
385 snprintf (buf, sizeof buf, "%d", i);
387 xmlTextWriterWriteElement (xo, BAD_CAST "major_version", BAD_CAST buf));
388 i = guestfs_inspect_get_minor_version (g, root);
389 snprintf (buf, sizeof buf, "%d", i);
391 xmlTextWriterWriteElement (xo, BAD_CAST "minor_version", BAD_CAST buf));
393 str = guestfs_inspect_get_package_format (g, root);
394 if (!str) exit (EXIT_FAILURE);
395 if (STRNEQ (str, "unknown"))
397 xmlTextWriterWriteElement (xo, BAD_CAST "package_format", BAD_CAST str));
400 str = guestfs_inspect_get_package_management (g, root);
401 if (!str) exit (EXIT_FAILURE);
402 if (STRNEQ (str, "unknown"))
404 xmlTextWriterWriteElement (xo, BAD_CAST "package_management",
408 /* inspect-get-windows-systemroot will fail with non-windows guests,
409 * or if the systemroot could not be determined for a windows guest.
410 * Disable error output around this call.
412 DISABLE_GUESTFS_ERRORS_FOR (
413 str = guestfs_inspect_get_windows_systemroot (g, root);
416 xmlTextWriterWriteElement (xo, BAD_CAST "windows_systemroot",
420 DISABLE_GUESTFS_ERRORS_FOR (
421 str = guestfs_inspect_get_windows_current_control_set (g, root);
424 xmlTextWriterWriteElement (xo, BAD_CAST "windows_current_control_set",
429 str = guestfs_inspect_get_format (g, root);
430 if (!str) exit (EXIT_FAILURE);
431 if (STRNEQ (str, "unknown"))
433 xmlTextWriterWriteElement (xo, BAD_CAST "format",
437 r = guestfs_inspect_is_live (g, root);
440 xmlTextWriterStartElement (xo, BAD_CAST "live"));
441 XMLERROR (-1, xmlTextWriterEndElement (xo));
444 r = guestfs_inspect_is_netinst (g, root);
447 xmlTextWriterStartElement (xo, BAD_CAST "netinst"));
448 XMLERROR (-1, xmlTextWriterEndElement (xo));
451 r = guestfs_inspect_is_multipart (g, root);
454 xmlTextWriterStartElement (xo, BAD_CAST "multipart"));
455 XMLERROR (-1, xmlTextWriterEndElement (xo));
458 output_mountpoints (xo, root);
460 output_filesystems (xo, root);
462 output_drive_mappings (xo, root);
464 output_applications (xo, root);
466 XMLERROR (-1, xmlTextWriterEndElement (xo));
470 compare_keys (const void *p1, const void *p2)
472 const char *key1 = * (char * const *) p1;
473 const char *key2 = * (char * const *) p2;
475 return strcmp (key1, key2);
479 compare_keys_nocase (const void *p1, const void *p2)
481 const char *key1 = * (char * const *) p1;
482 const char *key2 = * (char * const *) p2;
484 return strcasecmp (key1, key2);
488 compare_keys_len (const void *p1, const void *p2)
490 const char *key1 = * (char * const *) p1;
491 const char *key2 = * (char * const *) p2;
494 c = strlen (key1) - strlen (key2);
498 return compare_keys (p1, p2);
502 output_mountpoints (xmlTextWriterPtr xo, char *root)
507 mountpoints = guestfs_inspect_get_mountpoints (g, root);
508 if (mountpoints == NULL)
511 /* Sort by key length, shortest key first, and then name, so the
514 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
517 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
519 for (i = 0; mountpoints[i] != NULL; i += 2) {
520 canonicalize (mountpoints[i+1]);
523 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
525 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
526 BAD_CAST mountpoints[i+1]));
528 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
529 XMLERROR (-1, xmlTextWriterEndElement (xo));
532 XMLERROR (-1, xmlTextWriterEndElement (xo));
534 free_strings (mountpoints);
538 output_filesystems (xmlTextWriterPtr xo, char *root)
544 filesystems = guestfs_inspect_get_filesystems (g, root);
545 if (filesystems == NULL)
548 /* Sort by name so the output is stable. */
549 qsort (filesystems, count_strings (filesystems), sizeof (char *),
552 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
554 for (i = 0; filesystems[i] != NULL; ++i) {
555 canonicalize (filesystems[i]);
557 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
559 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
560 BAD_CAST filesystems[i]));
562 DISABLE_GUESTFS_ERRORS_FOR (
563 str = guestfs_vfs_type (g, filesystems[i]);
566 xmlTextWriterWriteElement (xo, BAD_CAST "type",
571 DISABLE_GUESTFS_ERRORS_FOR (
572 str = guestfs_vfs_label (g, filesystems[i]);
575 xmlTextWriterWriteElement (xo, BAD_CAST "label",
580 DISABLE_GUESTFS_ERRORS_FOR (
581 str = guestfs_vfs_uuid (g, filesystems[i]);
584 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
589 XMLERROR (-1, xmlTextWriterEndElement (xo));
592 XMLERROR (-1, xmlTextWriterEndElement (xo));
594 free_strings (filesystems);
598 output_drive_mappings (xmlTextWriterPtr xo, char *root)
600 char **drive_mappings = NULL;
603 DISABLE_GUESTFS_ERRORS_FOR (
604 drive_mappings = guestfs_inspect_get_drive_mappings (g, root);
606 if (drive_mappings == NULL)
609 if (drive_mappings[0] == NULL) {
610 free_strings (drive_mappings);
615 qsort (drive_mappings,
616 count_strings (drive_mappings) / 2, 2 * sizeof (char *),
617 compare_keys_nocase);
619 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "drive_mappings"));
621 for (i = 0; drive_mappings[i] != NULL; i += 2) {
622 canonicalize (drive_mappings[i+1]);
625 xmlTextWriterStartElement (xo, BAD_CAST "drive_mapping"));
627 xmlTextWriterWriteAttribute (xo, BAD_CAST "name",
628 BAD_CAST drive_mappings[i]));
630 xmlTextWriterWriteString (xo, BAD_CAST drive_mappings[i+1]));
631 XMLERROR (-1, xmlTextWriterEndElement (xo));
634 XMLERROR (-1, xmlTextWriterEndElement (xo));
636 free_strings (drive_mappings);
640 output_applications (xmlTextWriterPtr xo, char *root)
642 struct guestfs_application_list *apps;
645 /* We need to mount everything up in order to read out the list of
648 inspect_mount_root (root);
650 /* This returns an empty list if we simply couldn't determine the
651 * applications, so if it returns NULL then it's a real error.
653 apps = guestfs_inspect_list_applications (g, root);
656 if (guestfs_umount_all (g) == -1)
659 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
661 for (i = 0; i < apps->len; ++i) {
662 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
664 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
666 xmlTextWriterWriteElement (xo, BAD_CAST "name",
667 BAD_CAST apps->val[i].app_name));
669 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
671 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
672 BAD_CAST apps->val[i].app_display_name));
674 if (apps->val[i].app_epoch != 0) {
677 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
680 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
683 if (apps->val[i].app_version && apps->val[i].app_version[0])
685 xmlTextWriterWriteElement (xo, BAD_CAST "version",
686 BAD_CAST apps->val[i].app_version));
687 if (apps->val[i].app_release && apps->val[i].app_release[0])
689 xmlTextWriterWriteElement (xo, BAD_CAST "release",
690 BAD_CAST apps->val[i].app_release));
691 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
693 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
694 BAD_CAST apps->val[i].app_install_path));
695 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
697 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
698 BAD_CAST apps->val[i].app_publisher));
699 if (apps->val[i].app_url && apps->val[i].app_url[0])
701 xmlTextWriterWriteElement (xo, BAD_CAST "url",
702 BAD_CAST apps->val[i].app_url));
703 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
705 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
706 BAD_CAST apps->val[i].app_source_package));
707 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
709 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
710 BAD_CAST apps->val[i].app_summary));
711 if (apps->val[i].app_description && apps->val[i].app_description[0])
713 xmlTextWriterWriteElement (xo, BAD_CAST "description",
714 BAD_CAST apps->val[i].app_description));
716 XMLERROR (-1, xmlTextWriterEndElement (xo));
719 XMLERROR (-1, xmlTextWriterEndElement (xo));
721 guestfs_free_application_list (apps);
724 /* "/dev/vda1" -> "/dev/sda1"
725 * See BLOCK DEVICE NAMING in guestfs(3).
728 canonicalize (char *dev)
730 if (STRPREFIX (dev, "/dev/") &&
731 (dev[5] == 'h' || dev[5] == 'v') &&
733 c_isalpha (dev[7]) &&
734 (c_isdigit (dev[8]) || dev[8] == '\0'))
739 free_strings (char **argv)
743 for (argc = 0; argv[argc] != NULL; ++argc)
749 count_strings (char *const *argv)
753 for (c = 0; argv[c]; ++c)