2 * Copyright (C) 2010 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.
28 #include <libxml/xmlIO.h>
29 #include <libxml/xmlwriter.h>
37 /* Currently open libguestfs handle. */
43 int keys_from_stdin = 0;
45 const char *libvirt_uri = NULL;
48 static void output (char **roots);
49 static void output_roots (xmlTextWriterPtr xo, char **roots);
50 static void output_root (xmlTextWriterPtr xo, char *root);
51 static void output_mountpoints (xmlTextWriterPtr xo, char *root);
52 static void output_filesystems (xmlTextWriterPtr xo, char *root);
53 static void output_applications (xmlTextWriterPtr xo, char *root);
54 static void canonicalize (char *dev);
55 static void free_strings (char **argv);
56 static int count_strings (char *const*argv);
59 bad_cast (char const *s)
64 static void __attribute__((noreturn))
67 if (status != EXIT_SUCCESS)
68 fprintf (stderr, _("Try `%s --help' for more information.\n"),
72 _("%s: display information about a virtual machine\n"
73 "Copyright (C) 2010 Red Hat Inc.\n"
75 " %s [--options] -d domname file [file ...]\n"
76 " %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
78 " -a|--add image Add image\n"
79 " -c|--connect uri Specify libvirt URI for -d option\n"
80 " -d|--domain guest Add disks from libvirt guest\n"
81 " --echo-keys Don't turn off echo for passphrases\n"
82 " --format[=raw|..] Force disk format for -a option\n"
83 " --help Display brief help\n"
84 " --keys-from-stdin Read passphrases from stdin\n"
85 " -v|--verbose Verbose messages\n"
86 " -V|--version Display version and exit\n"
87 " -x Trace libguestfs API calls\n"
88 "For more information, see the manpage %s(1).\n"),
89 program_name, program_name, program_name,
96 main (int argc, char *argv[])
98 /* Set global program name that is not polluted with libtool artifacts. */
99 set_program_name (argv[0]);
101 setlocale (LC_ALL, "");
102 bindtextdomain (PACKAGE, LOCALEBASEDIR);
103 textdomain (PACKAGE);
105 enum { HELP_OPTION = CHAR_MAX + 1 };
107 static const char *options = "a:c:d:vVx";
108 static const struct option long_options[] = {
109 { "add", 1, 0, 'a' },
110 { "connect", 1, 0, 'c' },
111 { "domain", 1, 0, 'd' },
112 { "echo-keys", 0, 0, 0 },
113 { "format", 2, 0, 0 },
114 { "help", 0, 0, HELP_OPTION },
115 { "keys-from-stdin", 0, 0, 0 },
116 { "verbose", 0, 0, 'v' },
117 { "version", 0, 0, 'V' },
120 struct drv *drvs = NULL;
122 const char *format = NULL;
126 g = guestfs_create ();
128 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
132 argv[0] = bad_cast (program_name);
135 c = getopt_long (argc, argv, options, long_options, &option_index);
139 case 0: /* options which are long only */
140 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
142 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
144 } else if (STREQ (long_options[option_index].name, "format")) {
145 if (!optarg || STREQ (optarg, ""))
150 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
151 program_name, long_options[option_index].name, option_index);
169 usage (EXIT_SUCCESS);
184 usage (EXIT_SUCCESS);
187 usage (EXIT_FAILURE);
191 /* Old-style syntax? There were no -a or -d options in the old
192 * virt-inspector which is how we detect this.
195 while (optind < argc) {
196 if (strchr (argv[optind], '/') ||
197 access (argv[optind], F_OK) == 0) { /* simulate -a option */
198 drv = malloc (sizeof (struct drv));
204 drv->a.filename = argv[optind];
205 drv->a.format = NULL;
208 } else { /* simulate -d option */
209 drv = malloc (sizeof (struct drv));
215 drv->d.guest = argv[optind];
224 /* These are really constants, but they have to be variables for the
225 * options parsing code. Assert here that they have known-good
228 assert (read_only == 1);
229 assert (inspector == 1);
232 /* Must be no extra arguments on the command line. */
234 usage (EXIT_FAILURE);
236 /* User must have specified some drives. */
238 usage (EXIT_FAILURE);
240 /* Add drives, inspect and mount. Note that inspector is always true,
241 * and there is no -m option.
243 add_drives (drvs, 'a');
245 if (guestfs_launch (g) == -1)
248 /* Free up data structures, no longer needed after this point. */
251 /* NB. Can't call inspect_mount () here (ie. normal processing of
252 * the -i option) because it can only handle a single root. So we
253 * use low-level APIs.
255 inspect_do_decrypt ();
257 char **roots = guestfs_inspect_os (g);
259 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"),
266 free_strings (roots);
273 #define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
274 guestfs_error_handler_cb old_error_cb; \
275 void *old_error_data; \
276 old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
277 guestfs_set_error_handler (g, NULL, NULL); \
279 guestfs_set_error_handler (g, old_error_cb, old_error_data); \
282 #define XMLERROR(code,e) do { \
283 if ((e) == (code)) { \
284 fprintf (stderr, _("%s: XML write error at \"%s\": %m\n"), \
286 exit (EXIT_FAILURE); \
291 output (char **roots)
293 xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL);
296 _("%s: xmlOutputBufferCreateFd: failed to open stdout\n"),
301 xmlTextWriterPtr xo = xmlNewTextWriter (ob);
304 _("%s: xmlNewTextWriter: failed to create libxml2 writer\n"),
309 /* Pretty-print the output. */
310 XMLERROR (-1, xmlTextWriterSetIndent (xo, 1));
311 XMLERROR (-1, xmlTextWriterSetIndentString (xo, BAD_CAST " "));
313 XMLERROR (-1, xmlTextWriterStartDocument (xo, NULL, NULL, NULL));
314 output_roots (xo, roots);
315 XMLERROR (-1, xmlTextWriterEndDocument (xo));
317 /* 'ob' is freed by this too. */
318 xmlFreeTextWriter (xo);
322 output_roots (xmlTextWriterPtr xo, char **roots)
326 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystems"));
327 for (i = 0; roots[i] != NULL; ++i)
328 output_root (xo, roots[i]);
329 XMLERROR (-1, xmlTextWriterEndElement (xo));
333 output_root (xmlTextWriterPtr xo, char *root)
338 char canonical_root[strlen (root) + 1];
340 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystem"));
342 strcpy (canonical_root, root);
343 canonicalize (canonical_root);
345 xmlTextWriterWriteElement (xo, BAD_CAST "root", BAD_CAST canonical_root));
347 str = guestfs_inspect_get_type (g, root);
348 if (!str) exit (EXIT_FAILURE);
349 if (STRNEQ (str, "unknown"))
351 xmlTextWriterWriteElement (xo, BAD_CAST "name", BAD_CAST str));
354 str = guestfs_inspect_get_arch (g, root);
355 if (!str) exit (EXIT_FAILURE);
356 if (STRNEQ (str, "unknown"))
358 xmlTextWriterWriteElement (xo, BAD_CAST "arch", BAD_CAST str));
361 str = guestfs_inspect_get_distro (g, root);
362 if (!str) exit (EXIT_FAILURE);
363 if (STRNEQ (str, "unknown"))
365 xmlTextWriterWriteElement (xo, BAD_CAST "distro", BAD_CAST str));
368 str = guestfs_inspect_get_product_name (g, root);
369 if (!str) exit (EXIT_FAILURE);
370 if (STRNEQ (str, "unknown"))
372 xmlTextWriterWriteElement (xo, BAD_CAST "product_name", BAD_CAST str));
375 i = guestfs_inspect_get_major_version (g, root);
376 snprintf (buf, sizeof buf, "%d", i);
378 xmlTextWriterWriteElement (xo, BAD_CAST "major_version", BAD_CAST buf));
379 i = guestfs_inspect_get_minor_version (g, root);
380 snprintf (buf, sizeof buf, "%d", i);
382 xmlTextWriterWriteElement (xo, BAD_CAST "minor_version", BAD_CAST buf));
384 str = guestfs_inspect_get_package_format (g, root);
385 if (!str) exit (EXIT_FAILURE);
386 if (STRNEQ (str, "unknown"))
388 xmlTextWriterWriteElement (xo, BAD_CAST "package_format", BAD_CAST str));
391 str = guestfs_inspect_get_package_management (g, root);
392 if (!str) exit (EXIT_FAILURE);
393 if (STRNEQ (str, "unknown"))
395 xmlTextWriterWriteElement (xo, BAD_CAST "package_management",
399 /* inspect-get-windows-systemroot will fail with non-windows guests,
400 * or if the systemroot could not be determined for a windows guest.
401 * Disable error output around this call.
403 DISABLE_GUESTFS_ERRORS_FOR (
404 str = guestfs_inspect_get_windows_systemroot (g, root);
407 xmlTextWriterWriteElement (xo, BAD_CAST "windows_systemroot",
412 str = guestfs_inspect_get_format (g, root);
413 if (!str) exit (EXIT_FAILURE);
414 if (STRNEQ (str, "unknown"))
416 xmlTextWriterWriteElement (xo, BAD_CAST "format",
420 r = guestfs_inspect_is_live (g, root);
423 xmlTextWriterStartElement (xo, BAD_CAST "live"));
424 XMLERROR (-1, xmlTextWriterEndElement (xo));
427 r = guestfs_inspect_is_netinst (g, root);
430 xmlTextWriterStartElement (xo, BAD_CAST "netinst"));
431 XMLERROR (-1, xmlTextWriterEndElement (xo));
434 r = guestfs_inspect_is_multipart (g, root);
437 xmlTextWriterStartElement (xo, BAD_CAST "multipart"));
438 XMLERROR (-1, xmlTextWriterEndElement (xo));
441 output_mountpoints (xo, root);
443 output_filesystems (xo, root);
445 output_applications (xo, root);
447 XMLERROR (-1, xmlTextWriterEndElement (xo));
451 compare_keys (const void *p1, const void *p2)
453 const char *key1 = * (char * const *) p1;
454 const char *key2 = * (char * const *) p2;
456 return strcmp (key1, key2);
460 compare_keys_len (const void *p1, const void *p2)
462 const char *key1 = * (char * const *) p1;
463 const char *key2 = * (char * const *) p2;
466 c = strlen (key1) - strlen (key2);
470 return compare_keys (p1, p2);
474 output_mountpoints (xmlTextWriterPtr xo, char *root)
479 mountpoints = guestfs_inspect_get_mountpoints (g, root);
480 if (mountpoints == NULL)
483 /* Sort by key length, shortest key first, and then name, so the
486 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
489 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
491 for (i = 0; mountpoints[i] != NULL; i += 2) {
492 canonicalize (mountpoints[i+1]);
495 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
497 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
498 BAD_CAST mountpoints[i+1]));
500 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
501 XMLERROR (-1, xmlTextWriterEndElement (xo));
504 XMLERROR (-1, xmlTextWriterEndElement (xo));
506 free_strings (mountpoints);
510 output_filesystems (xmlTextWriterPtr xo, char *root)
516 filesystems = guestfs_inspect_get_filesystems (g, root);
517 if (filesystems == NULL)
520 /* Sort by name so the output is stable. */
521 qsort (filesystems, count_strings (filesystems), sizeof (char *),
524 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
526 for (i = 0; filesystems[i] != NULL; ++i) {
527 canonicalize (filesystems[i]);
529 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
531 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
532 BAD_CAST filesystems[i]));
534 DISABLE_GUESTFS_ERRORS_FOR (
535 str = guestfs_vfs_type (g, filesystems[i]);
538 xmlTextWriterWriteElement (xo, BAD_CAST "type",
543 DISABLE_GUESTFS_ERRORS_FOR (
544 str = guestfs_vfs_label (g, filesystems[i]);
547 xmlTextWriterWriteElement (xo, BAD_CAST "label",
552 DISABLE_GUESTFS_ERRORS_FOR (
553 str = guestfs_vfs_uuid (g, filesystems[i]);
556 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
561 XMLERROR (-1, xmlTextWriterEndElement (xo));
564 XMLERROR (-1, xmlTextWriterEndElement (xo));
566 free_strings (filesystems);
570 output_applications (xmlTextWriterPtr xo, char *root)
572 struct guestfs_application_list *apps;
575 /* We need to mount everything up in order to read out the list of
578 inspect_mount_root (root);
580 /* This returns an empty list if we simply couldn't determine the
581 * applications, so if it returns NULL then it's a real error.
583 apps = guestfs_inspect_list_applications (g, root);
586 if (guestfs_umount_all (g) == -1)
589 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
591 for (i = 0; i < apps->len; ++i) {
592 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
594 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
596 xmlTextWriterWriteElement (xo, BAD_CAST "name",
597 BAD_CAST apps->val[i].app_name));
599 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
601 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
602 BAD_CAST apps->val[i].app_display_name));
604 if (apps->val[i].app_epoch != 0) {
607 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
610 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
613 if (apps->val[i].app_version && apps->val[i].app_version[0])
615 xmlTextWriterWriteElement (xo, BAD_CAST "version",
616 BAD_CAST apps->val[i].app_version));
617 if (apps->val[i].app_release && apps->val[i].app_release[0])
619 xmlTextWriterWriteElement (xo, BAD_CAST "release",
620 BAD_CAST apps->val[i].app_release));
621 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
623 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
624 BAD_CAST apps->val[i].app_install_path));
625 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
627 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
628 BAD_CAST apps->val[i].app_publisher));
629 if (apps->val[i].app_url && apps->val[i].app_url[0])
631 xmlTextWriterWriteElement (xo, BAD_CAST "url",
632 BAD_CAST apps->val[i].app_url));
633 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
635 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
636 BAD_CAST apps->val[i].app_source_package));
637 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
639 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
640 BAD_CAST apps->val[i].app_summary));
641 if (apps->val[i].app_description && apps->val[i].app_description[0])
643 xmlTextWriterWriteElement (xo, BAD_CAST "description",
644 BAD_CAST apps->val[i].app_description));
646 XMLERROR (-1, xmlTextWriterEndElement (xo));
649 XMLERROR (-1, xmlTextWriterEndElement (xo));
651 guestfs_free_application_list (apps);
654 /* "/dev/vda1" -> "/dev/sda1"
655 * See BLOCK DEVICE NAMING in guestfs(3).
658 canonicalize (char *dev)
660 if (STRPREFIX (dev, "/dev/") &&
661 (dev[5] == 'h' || dev[5] == 'v') &&
663 c_isalpha (dev[7]) &&
664 (c_isdigit (dev[8]) || dev[8] == '\0'))
669 free_strings (char **argv)
673 for (argc = 0; argv[argc] != NULL; ++argc)
679 count_strings (char *const *argv)
683 for (c = 0; argv[c]; ++c)