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_applications (xmlTextWriterPtr xo, char *root);
55 static void canonicalize (char *dev);
56 static void free_strings (char **argv);
57 static int count_strings (char *const*argv);
60 bad_cast (char const *s)
65 static void __attribute__((noreturn))
68 if (status != EXIT_SUCCESS)
69 fprintf (stderr, _("Try `%s --help' for more information.\n"),
73 _("%s: display information about a virtual machine\n"
74 "Copyright (C) 2010 Red Hat Inc.\n"
76 " %s [--options] -d domname file [file ...]\n"
77 " %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
79 " -a|--add image Add image\n"
80 " -c|--connect uri Specify libvirt URI for -d option\n"
81 " -d|--domain guest Add disks from libvirt guest\n"
82 " --echo-keys Don't turn off echo for passphrases\n"
83 " --format[=raw|..] Force disk format for -a option\n"
84 " --help Display brief help\n"
85 " --keys-from-stdin Read passphrases from stdin\n"
86 " -v|--verbose Verbose messages\n"
87 " -V|--version Display version and exit\n"
88 " -x Trace libguestfs API calls\n"
89 "For more information, see the manpage %s(1).\n"),
90 program_name, program_name, program_name,
97 main (int argc, char *argv[])
99 /* Set global program name that is not polluted with libtool artifacts. */
100 set_program_name (argv[0]);
102 setlocale (LC_ALL, "");
103 bindtextdomain (PACKAGE, LOCALEBASEDIR);
104 textdomain (PACKAGE);
106 enum { HELP_OPTION = CHAR_MAX + 1 };
108 static const char *options = "a:c:d:vVx";
109 static const struct option long_options[] = {
110 { "add", 1, 0, 'a' },
111 { "connect", 1, 0, 'c' },
112 { "domain", 1, 0, 'd' },
113 { "echo-keys", 0, 0, 0 },
114 { "format", 2, 0, 0 },
115 { "help", 0, 0, HELP_OPTION },
116 { "keys-from-stdin", 0, 0, 0 },
117 { "verbose", 0, 0, 'v' },
118 { "version", 0, 0, 'V' },
121 struct drv *drvs = NULL;
123 const char *format = NULL;
127 g = guestfs_create ();
129 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
133 argv[0] = bad_cast (program_name);
136 c = getopt_long (argc, argv, options, long_options, &option_index);
140 case 0: /* options which are long only */
141 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
143 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
145 } else if (STREQ (long_options[option_index].name, "format")) {
146 if (!optarg || STREQ (optarg, ""))
151 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
152 program_name, long_options[option_index].name, option_index);
170 usage (EXIT_SUCCESS);
185 usage (EXIT_SUCCESS);
188 usage (EXIT_FAILURE);
192 /* Old-style syntax? There were no -a or -d options in the old
193 * virt-inspector which is how we detect this.
196 while (optind < argc) {
197 if (strchr (argv[optind], '/') ||
198 access (argv[optind], F_OK) == 0) { /* simulate -a option */
199 drv = malloc (sizeof (struct drv));
205 drv->a.filename = argv[optind];
206 drv->a.format = NULL;
209 } else { /* simulate -d option */
210 drv = malloc (sizeof (struct drv));
216 drv->d.guest = argv[optind];
225 /* These are really constants, but they have to be variables for the
226 * options parsing code. Assert here that they have known-good
229 assert (read_only == 1);
230 assert (inspector == 1);
233 /* Must be no extra arguments on the command line. */
235 usage (EXIT_FAILURE);
237 /* User must have specified some drives. */
239 usage (EXIT_FAILURE);
241 /* Add drives, inspect and mount. Note that inspector is always true,
242 * and there is no -m option.
244 add_drives (drvs, 'a');
246 if (guestfs_launch (g) == -1)
249 /* Free up data structures, no longer needed after this point. */
252 /* NB. Can't call inspect_mount () here (ie. normal processing of
253 * the -i option) because it can only handle a single root. So we
254 * use low-level APIs.
256 inspect_do_decrypt ();
258 char **roots = guestfs_inspect_os (g);
260 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"),
267 free_strings (roots);
274 #define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
275 guestfs_error_handler_cb old_error_cb; \
276 void *old_error_data; \
277 old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
278 guestfs_set_error_handler (g, NULL, NULL); \
280 guestfs_set_error_handler (g, old_error_cb, old_error_data); \
283 #define XMLERROR(code,e) do { \
284 if ((e) == (code)) { \
285 fprintf (stderr, _("%s: XML write error at \"%s\": %m\n"), \
287 exit (EXIT_FAILURE); \
292 output (char **roots)
294 xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL);
297 _("%s: xmlOutputBufferCreateFd: failed to open stdout\n"),
302 xmlTextWriterPtr xo = xmlNewTextWriter (ob);
305 _("%s: xmlNewTextWriter: failed to create libxml2 writer\n"),
310 /* Pretty-print the output. */
311 XMLERROR (-1, xmlTextWriterSetIndent (xo, 1));
312 XMLERROR (-1, xmlTextWriterSetIndentString (xo, BAD_CAST " "));
314 XMLERROR (-1, xmlTextWriterStartDocument (xo, NULL, NULL, NULL));
315 output_roots (xo, roots);
316 XMLERROR (-1, xmlTextWriterEndDocument (xo));
318 /* 'ob' is freed by this too. */
319 xmlFreeTextWriter (xo);
323 output_roots (xmlTextWriterPtr xo, char **roots)
327 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystems"));
328 for (i = 0; roots[i] != NULL; ++i)
329 output_root (xo, roots[i]);
330 XMLERROR (-1, xmlTextWriterEndElement (xo));
334 output_root (xmlTextWriterPtr xo, char *root)
339 char canonical_root[strlen (root) + 1];
341 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystem"));
343 strcpy (canonical_root, root);
344 canonicalize (canonical_root);
346 xmlTextWriterWriteElement (xo, BAD_CAST "root", BAD_CAST canonical_root));
348 str = guestfs_inspect_get_type (g, root);
349 if (!str) exit (EXIT_FAILURE);
350 if (STRNEQ (str, "unknown"))
352 xmlTextWriterWriteElement (xo, BAD_CAST "name", BAD_CAST str));
355 str = guestfs_inspect_get_arch (g, root);
356 if (!str) exit (EXIT_FAILURE);
357 if (STRNEQ (str, "unknown"))
359 xmlTextWriterWriteElement (xo, BAD_CAST "arch", BAD_CAST str));
362 str = guestfs_inspect_get_distro (g, root);
363 if (!str) exit (EXIT_FAILURE);
364 if (STRNEQ (str, "unknown"))
366 xmlTextWriterWriteElement (xo, BAD_CAST "distro", BAD_CAST str));
369 str = guestfs_inspect_get_product_name (g, root);
370 if (!str) exit (EXIT_FAILURE);
371 if (STRNEQ (str, "unknown"))
373 xmlTextWriterWriteElement (xo, BAD_CAST "product_name", BAD_CAST str));
376 str = guestfs_inspect_get_product_variant (g, root);
377 if (!str) exit (EXIT_FAILURE);
378 if (STRNEQ (str, "unknown"))
380 xmlTextWriterWriteElement (xo, BAD_CAST "product_variant", BAD_CAST str));
383 i = guestfs_inspect_get_major_version (g, root);
384 snprintf (buf, sizeof buf, "%d", i);
386 xmlTextWriterWriteElement (xo, BAD_CAST "major_version", BAD_CAST buf));
387 i = guestfs_inspect_get_minor_version (g, root);
388 snprintf (buf, sizeof buf, "%d", i);
390 xmlTextWriterWriteElement (xo, BAD_CAST "minor_version", BAD_CAST buf));
392 str = guestfs_inspect_get_package_format (g, root);
393 if (!str) exit (EXIT_FAILURE);
394 if (STRNEQ (str, "unknown"))
396 xmlTextWriterWriteElement (xo, BAD_CAST "package_format", BAD_CAST str));
399 str = guestfs_inspect_get_package_management (g, root);
400 if (!str) exit (EXIT_FAILURE);
401 if (STRNEQ (str, "unknown"))
403 xmlTextWriterWriteElement (xo, BAD_CAST "package_management",
407 /* inspect-get-windows-systemroot will fail with non-windows guests,
408 * or if the systemroot could not be determined for a windows guest.
409 * Disable error output around this call.
411 DISABLE_GUESTFS_ERRORS_FOR (
412 str = guestfs_inspect_get_windows_systemroot (g, root);
415 xmlTextWriterWriteElement (xo, BAD_CAST "windows_systemroot",
419 DISABLE_GUESTFS_ERRORS_FOR (
420 str = guestfs_inspect_get_windows_current_control_set (g, root);
423 xmlTextWriterWriteElement (xo, BAD_CAST "windows_current_control_set",
428 str = guestfs_inspect_get_format (g, root);
429 if (!str) exit (EXIT_FAILURE);
430 if (STRNEQ (str, "unknown"))
432 xmlTextWriterWriteElement (xo, BAD_CAST "format",
436 r = guestfs_inspect_is_live (g, root);
439 xmlTextWriterStartElement (xo, BAD_CAST "live"));
440 XMLERROR (-1, xmlTextWriterEndElement (xo));
443 r = guestfs_inspect_is_netinst (g, root);
446 xmlTextWriterStartElement (xo, BAD_CAST "netinst"));
447 XMLERROR (-1, xmlTextWriterEndElement (xo));
450 r = guestfs_inspect_is_multipart (g, root);
453 xmlTextWriterStartElement (xo, BAD_CAST "multipart"));
454 XMLERROR (-1, xmlTextWriterEndElement (xo));
457 output_mountpoints (xo, root);
459 output_filesystems (xo, root);
461 output_applications (xo, root);
463 XMLERROR (-1, xmlTextWriterEndElement (xo));
467 compare_keys (const void *p1, const void *p2)
469 const char *key1 = * (char * const *) p1;
470 const char *key2 = * (char * const *) p2;
472 return strcmp (key1, key2);
476 compare_keys_len (const void *p1, const void *p2)
478 const char *key1 = * (char * const *) p1;
479 const char *key2 = * (char * const *) p2;
482 c = strlen (key1) - strlen (key2);
486 return compare_keys (p1, p2);
490 output_mountpoints (xmlTextWriterPtr xo, char *root)
495 mountpoints = guestfs_inspect_get_mountpoints (g, root);
496 if (mountpoints == NULL)
499 /* Sort by key length, shortest key first, and then name, so the
502 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
505 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
507 for (i = 0; mountpoints[i] != NULL; i += 2) {
508 canonicalize (mountpoints[i+1]);
511 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
513 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
514 BAD_CAST mountpoints[i+1]));
516 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
517 XMLERROR (-1, xmlTextWriterEndElement (xo));
520 XMLERROR (-1, xmlTextWriterEndElement (xo));
522 free_strings (mountpoints);
526 output_filesystems (xmlTextWriterPtr xo, char *root)
532 filesystems = guestfs_inspect_get_filesystems (g, root);
533 if (filesystems == NULL)
536 /* Sort by name so the output is stable. */
537 qsort (filesystems, count_strings (filesystems), sizeof (char *),
540 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
542 for (i = 0; filesystems[i] != NULL; ++i) {
543 canonicalize (filesystems[i]);
545 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
547 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
548 BAD_CAST filesystems[i]));
550 DISABLE_GUESTFS_ERRORS_FOR (
551 str = guestfs_vfs_type (g, filesystems[i]);
554 xmlTextWriterWriteElement (xo, BAD_CAST "type",
559 DISABLE_GUESTFS_ERRORS_FOR (
560 str = guestfs_vfs_label (g, filesystems[i]);
563 xmlTextWriterWriteElement (xo, BAD_CAST "label",
568 DISABLE_GUESTFS_ERRORS_FOR (
569 str = guestfs_vfs_uuid (g, filesystems[i]);
572 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
577 XMLERROR (-1, xmlTextWriterEndElement (xo));
580 XMLERROR (-1, xmlTextWriterEndElement (xo));
582 free_strings (filesystems);
586 output_applications (xmlTextWriterPtr xo, char *root)
588 struct guestfs_application_list *apps;
591 /* We need to mount everything up in order to read out the list of
594 inspect_mount_root (root);
596 /* This returns an empty list if we simply couldn't determine the
597 * applications, so if it returns NULL then it's a real error.
599 apps = guestfs_inspect_list_applications (g, root);
602 if (guestfs_umount_all (g) == -1)
605 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
607 for (i = 0; i < apps->len; ++i) {
608 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
610 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
612 xmlTextWriterWriteElement (xo, BAD_CAST "name",
613 BAD_CAST apps->val[i].app_name));
615 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
617 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
618 BAD_CAST apps->val[i].app_display_name));
620 if (apps->val[i].app_epoch != 0) {
623 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
626 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
629 if (apps->val[i].app_version && apps->val[i].app_version[0])
631 xmlTextWriterWriteElement (xo, BAD_CAST "version",
632 BAD_CAST apps->val[i].app_version));
633 if (apps->val[i].app_release && apps->val[i].app_release[0])
635 xmlTextWriterWriteElement (xo, BAD_CAST "release",
636 BAD_CAST apps->val[i].app_release));
637 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
639 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
640 BAD_CAST apps->val[i].app_install_path));
641 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
643 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
644 BAD_CAST apps->val[i].app_publisher));
645 if (apps->val[i].app_url && apps->val[i].app_url[0])
647 xmlTextWriterWriteElement (xo, BAD_CAST "url",
648 BAD_CAST apps->val[i].app_url));
649 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
651 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
652 BAD_CAST apps->val[i].app_source_package));
653 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
655 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
656 BAD_CAST apps->val[i].app_summary));
657 if (apps->val[i].app_description && apps->val[i].app_description[0])
659 xmlTextWriterWriteElement (xo, BAD_CAST "description",
660 BAD_CAST apps->val[i].app_description));
662 XMLERROR (-1, xmlTextWriterEndElement (xo));
665 XMLERROR (-1, xmlTextWriterEndElement (xo));
667 guestfs_free_application_list (apps);
670 /* "/dev/vda1" -> "/dev/sda1"
671 * See BLOCK DEVICE NAMING in guestfs(3).
674 canonicalize (char *dev)
676 if (STRPREFIX (dev, "/dev/") &&
677 (dev[5] == 'h' || dev[5] == 'v') &&
679 c_isalpha (dev[7]) &&
680 (c_isdigit (dev[8]) || dev[8] == '\0'))
685 free_strings (char **argv)
689 for (argc = 0; argv[argc] != NULL; ++argc)
695 count_strings (char *const *argv)
699 for (c = 0; argv[c]; ++c)