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];
344 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystem"));
346 strcpy (canonical_root, root);
347 canonicalize (canonical_root);
349 xmlTextWriterWriteElement (xo, BAD_CAST "root", BAD_CAST canonical_root));
351 str = guestfs_inspect_get_type (g, root);
352 if (!str) exit (EXIT_FAILURE);
353 if (STRNEQ (str, "unknown"))
355 xmlTextWriterWriteElement (xo, BAD_CAST "name", BAD_CAST str));
358 str = guestfs_inspect_get_arch (g, root);
359 if (!str) exit (EXIT_FAILURE);
360 if (STRNEQ (str, "unknown"))
362 xmlTextWriterWriteElement (xo, BAD_CAST "arch", BAD_CAST str));
365 str = guestfs_inspect_get_distro (g, root);
366 if (!str) exit (EXIT_FAILURE);
367 if (STRNEQ (str, "unknown"))
369 xmlTextWriterWriteElement (xo, BAD_CAST "distro", BAD_CAST str));
372 str = guestfs_inspect_get_product_name (g, root);
373 if (!str) exit (EXIT_FAILURE);
374 if (STRNEQ (str, "unknown"))
376 xmlTextWriterWriteElement (xo, BAD_CAST "product_name", BAD_CAST str));
379 str = guestfs_inspect_get_product_variant (g, root);
380 if (!str) exit (EXIT_FAILURE);
381 if (STRNEQ (str, "unknown"))
383 xmlTextWriterWriteElement (xo, BAD_CAST "product_variant", BAD_CAST str));
386 i = guestfs_inspect_get_major_version (g, root);
387 snprintf (buf, sizeof buf, "%d", i);
389 xmlTextWriterWriteElement (xo, BAD_CAST "major_version", BAD_CAST buf));
390 i = guestfs_inspect_get_minor_version (g, root);
391 snprintf (buf, sizeof buf, "%d", i);
393 xmlTextWriterWriteElement (xo, BAD_CAST "minor_version", BAD_CAST buf));
395 str = guestfs_inspect_get_package_format (g, root);
396 if (!str) exit (EXIT_FAILURE);
397 if (STRNEQ (str, "unknown"))
399 xmlTextWriterWriteElement (xo, BAD_CAST "package_format", BAD_CAST str));
402 str = guestfs_inspect_get_package_management (g, root);
403 if (!str) exit (EXIT_FAILURE);
404 if (STRNEQ (str, "unknown"))
406 xmlTextWriterWriteElement (xo, BAD_CAST "package_management",
410 /* inspect-get-windows-systemroot will fail with non-windows guests,
411 * or if the systemroot could not be determined for a windows guest.
412 * Disable error output around this call.
414 DISABLE_GUESTFS_ERRORS_FOR (
415 str = guestfs_inspect_get_windows_systemroot (g, root);
418 xmlTextWriterWriteElement (xo, BAD_CAST "windows_systemroot",
422 DISABLE_GUESTFS_ERRORS_FOR (
423 str = guestfs_inspect_get_windows_current_control_set (g, root);
426 xmlTextWriterWriteElement (xo, BAD_CAST "windows_current_control_set",
431 str = guestfs_inspect_get_hostname (g, root);
432 if (!str) exit (EXIT_FAILURE);
433 if (STRNEQ (str, "unknown"))
435 xmlTextWriterWriteElement (xo, BAD_CAST "hostname",
439 str = guestfs_inspect_get_format (g, root);
440 if (!str) exit (EXIT_FAILURE);
441 if (STRNEQ (str, "unknown"))
443 xmlTextWriterWriteElement (xo, BAD_CAST "format",
447 r = guestfs_inspect_is_live (g, root);
450 xmlTextWriterStartElement (xo, BAD_CAST "live"));
451 XMLERROR (-1, xmlTextWriterEndElement (xo));
454 r = guestfs_inspect_is_netinst (g, root);
457 xmlTextWriterStartElement (xo, BAD_CAST "netinst"));
458 XMLERROR (-1, xmlTextWriterEndElement (xo));
461 r = guestfs_inspect_is_multipart (g, root);
464 xmlTextWriterStartElement (xo, BAD_CAST "multipart"));
465 XMLERROR (-1, xmlTextWriterEndElement (xo));
468 output_mountpoints (xo, root);
470 output_filesystems (xo, root);
472 output_drive_mappings (xo, root);
474 output_applications (xo, root);
476 XMLERROR (-1, xmlTextWriterEndElement (xo));
480 compare_keys (const void *p1, const void *p2)
482 const char *key1 = * (char * const *) p1;
483 const char *key2 = * (char * const *) p2;
485 return strcmp (key1, key2);
489 compare_keys_nocase (const void *p1, const void *p2)
491 const char *key1 = * (char * const *) p1;
492 const char *key2 = * (char * const *) p2;
494 return strcasecmp (key1, key2);
498 compare_keys_len (const void *p1, const void *p2)
500 const char *key1 = * (char * const *) p1;
501 const char *key2 = * (char * const *) p2;
504 c = strlen (key1) - strlen (key2);
508 return compare_keys (p1, p2);
512 output_mountpoints (xmlTextWriterPtr xo, char *root)
517 mountpoints = guestfs_inspect_get_mountpoints (g, root);
518 if (mountpoints == NULL)
521 /* Sort by key length, shortest key first, and then name, so the
524 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
527 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
529 for (i = 0; mountpoints[i] != NULL; i += 2) {
530 canonicalize (mountpoints[i+1]);
533 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
535 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
536 BAD_CAST mountpoints[i+1]));
538 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
539 XMLERROR (-1, xmlTextWriterEndElement (xo));
542 XMLERROR (-1, xmlTextWriterEndElement (xo));
544 free_strings (mountpoints);
548 output_filesystems (xmlTextWriterPtr xo, char *root)
554 filesystems = guestfs_inspect_get_filesystems (g, root);
555 if (filesystems == NULL)
558 /* Sort by name so the output is stable. */
559 qsort (filesystems, count_strings (filesystems), sizeof (char *),
562 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
564 for (i = 0; filesystems[i] != NULL; ++i) {
565 canonicalize (filesystems[i]);
567 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
569 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
570 BAD_CAST filesystems[i]));
572 DISABLE_GUESTFS_ERRORS_FOR (
573 str = guestfs_vfs_type (g, filesystems[i]);
576 xmlTextWriterWriteElement (xo, BAD_CAST "type",
581 DISABLE_GUESTFS_ERRORS_FOR (
582 str = guestfs_vfs_label (g, filesystems[i]);
585 xmlTextWriterWriteElement (xo, BAD_CAST "label",
590 DISABLE_GUESTFS_ERRORS_FOR (
591 str = guestfs_vfs_uuid (g, filesystems[i]);
594 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
599 XMLERROR (-1, xmlTextWriterEndElement (xo));
602 XMLERROR (-1, xmlTextWriterEndElement (xo));
604 free_strings (filesystems);
608 output_drive_mappings (xmlTextWriterPtr xo, char *root)
610 char **drive_mappings = NULL;
613 DISABLE_GUESTFS_ERRORS_FOR (
614 drive_mappings = guestfs_inspect_get_drive_mappings (g, root);
616 if (drive_mappings == NULL)
619 if (drive_mappings[0] == NULL) {
620 free_strings (drive_mappings);
625 qsort (drive_mappings,
626 count_strings (drive_mappings) / 2, 2 * sizeof (char *),
627 compare_keys_nocase);
629 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "drive_mappings"));
631 for (i = 0; drive_mappings[i] != NULL; i += 2) {
632 canonicalize (drive_mappings[i+1]);
635 xmlTextWriterStartElement (xo, BAD_CAST "drive_mapping"));
637 xmlTextWriterWriteAttribute (xo, BAD_CAST "name",
638 BAD_CAST drive_mappings[i]));
640 xmlTextWriterWriteString (xo, BAD_CAST drive_mappings[i+1]));
641 XMLERROR (-1, xmlTextWriterEndElement (xo));
644 XMLERROR (-1, xmlTextWriterEndElement (xo));
646 free_strings (drive_mappings);
650 output_applications (xmlTextWriterPtr xo, char *root)
652 struct guestfs_application_list *apps;
655 /* We need to mount everything up in order to read out the list of
658 inspect_mount_root (root);
660 /* This returns an empty list if we simply couldn't determine the
661 * applications, so if it returns NULL then it's a real error.
663 apps = guestfs_inspect_list_applications (g, root);
666 if (guestfs_umount_all (g) == -1)
669 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
671 for (i = 0; i < apps->len; ++i) {
672 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
674 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
676 xmlTextWriterWriteElement (xo, BAD_CAST "name",
677 BAD_CAST apps->val[i].app_name));
679 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
681 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
682 BAD_CAST apps->val[i].app_display_name));
684 if (apps->val[i].app_epoch != 0) {
687 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
690 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
693 if (apps->val[i].app_version && apps->val[i].app_version[0])
695 xmlTextWriterWriteElement (xo, BAD_CAST "version",
696 BAD_CAST apps->val[i].app_version));
697 if (apps->val[i].app_release && apps->val[i].app_release[0])
699 xmlTextWriterWriteElement (xo, BAD_CAST "release",
700 BAD_CAST apps->val[i].app_release));
701 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
703 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
704 BAD_CAST apps->val[i].app_install_path));
705 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
707 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
708 BAD_CAST apps->val[i].app_publisher));
709 if (apps->val[i].app_url && apps->val[i].app_url[0])
711 xmlTextWriterWriteElement (xo, BAD_CAST "url",
712 BAD_CAST apps->val[i].app_url));
713 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
715 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
716 BAD_CAST apps->val[i].app_source_package));
717 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
719 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
720 BAD_CAST apps->val[i].app_summary));
721 if (apps->val[i].app_description && apps->val[i].app_description[0])
723 xmlTextWriterWriteElement (xo, BAD_CAST "description",
724 BAD_CAST apps->val[i].app_description));
726 XMLERROR (-1, xmlTextWriterEndElement (xo));
729 XMLERROR (-1, xmlTextWriterEndElement (xo));
731 guestfs_free_application_list (apps);
734 /* "/dev/vda1" -> "/dev/sda1"
735 * See BLOCK DEVICE NAMING in guestfs(3).
738 canonicalize (char *dev)
740 if (STRPREFIX (dev, "/dev/") &&
741 (dev[5] == 'h' || dev[5] == 'v') &&
743 c_isalpha (dev[7]) &&
744 (c_isdigit (dev[8]) || dev[8] == '\0'))
749 free_strings (char **argv)
753 for (argc = 0; argv[argc] != NULL; ++argc)
759 count_strings (char *const *argv)
763 for (c = 0; argv[c]; ++c)