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",
420 str = guestfs_inspect_get_format (g, root);
421 if (!str) exit (EXIT_FAILURE);
422 if (STRNEQ (str, "unknown"))
424 xmlTextWriterWriteElement (xo, BAD_CAST "format",
428 r = guestfs_inspect_is_live (g, root);
431 xmlTextWriterStartElement (xo, BAD_CAST "live"));
432 XMLERROR (-1, xmlTextWriterEndElement (xo));
435 r = guestfs_inspect_is_netinst (g, root);
438 xmlTextWriterStartElement (xo, BAD_CAST "netinst"));
439 XMLERROR (-1, xmlTextWriterEndElement (xo));
442 r = guestfs_inspect_is_multipart (g, root);
445 xmlTextWriterStartElement (xo, BAD_CAST "multipart"));
446 XMLERROR (-1, xmlTextWriterEndElement (xo));
449 output_mountpoints (xo, root);
451 output_filesystems (xo, root);
453 output_applications (xo, root);
455 XMLERROR (-1, xmlTextWriterEndElement (xo));
459 compare_keys (const void *p1, const void *p2)
461 const char *key1 = * (char * const *) p1;
462 const char *key2 = * (char * const *) p2;
464 return strcmp (key1, key2);
468 compare_keys_len (const void *p1, const void *p2)
470 const char *key1 = * (char * const *) p1;
471 const char *key2 = * (char * const *) p2;
474 c = strlen (key1) - strlen (key2);
478 return compare_keys (p1, p2);
482 output_mountpoints (xmlTextWriterPtr xo, char *root)
487 mountpoints = guestfs_inspect_get_mountpoints (g, root);
488 if (mountpoints == NULL)
491 /* Sort by key length, shortest key first, and then name, so the
494 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
497 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
499 for (i = 0; mountpoints[i] != NULL; i += 2) {
500 canonicalize (mountpoints[i+1]);
503 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
505 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
506 BAD_CAST mountpoints[i+1]));
508 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
509 XMLERROR (-1, xmlTextWriterEndElement (xo));
512 XMLERROR (-1, xmlTextWriterEndElement (xo));
514 free_strings (mountpoints);
518 output_filesystems (xmlTextWriterPtr xo, char *root)
524 filesystems = guestfs_inspect_get_filesystems (g, root);
525 if (filesystems == NULL)
528 /* Sort by name so the output is stable. */
529 qsort (filesystems, count_strings (filesystems), sizeof (char *),
532 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
534 for (i = 0; filesystems[i] != NULL; ++i) {
535 canonicalize (filesystems[i]);
537 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
539 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
540 BAD_CAST filesystems[i]));
542 DISABLE_GUESTFS_ERRORS_FOR (
543 str = guestfs_vfs_type (g, filesystems[i]);
546 xmlTextWriterWriteElement (xo, BAD_CAST "type",
551 DISABLE_GUESTFS_ERRORS_FOR (
552 str = guestfs_vfs_label (g, filesystems[i]);
555 xmlTextWriterWriteElement (xo, BAD_CAST "label",
560 DISABLE_GUESTFS_ERRORS_FOR (
561 str = guestfs_vfs_uuid (g, filesystems[i]);
564 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
569 XMLERROR (-1, xmlTextWriterEndElement (xo));
572 XMLERROR (-1, xmlTextWriterEndElement (xo));
574 free_strings (filesystems);
578 output_applications (xmlTextWriterPtr xo, char *root)
580 struct guestfs_application_list *apps;
583 /* We need to mount everything up in order to read out the list of
586 inspect_mount_root (root);
588 /* This returns an empty list if we simply couldn't determine the
589 * applications, so if it returns NULL then it's a real error.
591 apps = guestfs_inspect_list_applications (g, root);
594 if (guestfs_umount_all (g) == -1)
597 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
599 for (i = 0; i < apps->len; ++i) {
600 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
602 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
604 xmlTextWriterWriteElement (xo, BAD_CAST "name",
605 BAD_CAST apps->val[i].app_name));
607 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
609 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
610 BAD_CAST apps->val[i].app_display_name));
612 if (apps->val[i].app_epoch != 0) {
615 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
618 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
621 if (apps->val[i].app_version && apps->val[i].app_version[0])
623 xmlTextWriterWriteElement (xo, BAD_CAST "version",
624 BAD_CAST apps->val[i].app_version));
625 if (apps->val[i].app_release && apps->val[i].app_release[0])
627 xmlTextWriterWriteElement (xo, BAD_CAST "release",
628 BAD_CAST apps->val[i].app_release));
629 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
631 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
632 BAD_CAST apps->val[i].app_install_path));
633 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
635 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
636 BAD_CAST apps->val[i].app_publisher));
637 if (apps->val[i].app_url && apps->val[i].app_url[0])
639 xmlTextWriterWriteElement (xo, BAD_CAST "url",
640 BAD_CAST apps->val[i].app_url));
641 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
643 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
644 BAD_CAST apps->val[i].app_source_package));
645 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
647 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
648 BAD_CAST apps->val[i].app_summary));
649 if (apps->val[i].app_description && apps->val[i].app_description[0])
651 xmlTextWriterWriteElement (xo, BAD_CAST "description",
652 BAD_CAST apps->val[i].app_description));
654 XMLERROR (-1, xmlTextWriterEndElement (xo));
657 XMLERROR (-1, xmlTextWriterEndElement (xo));
659 guestfs_free_application_list (apps);
662 /* "/dev/vda1" -> "/dev/sda1"
663 * See BLOCK DEVICE NAMING in guestfs(3).
666 canonicalize (char *dev)
668 if (STRPREFIX (dev, "/dev/") &&
669 (dev[5] == 'h' || dev[5] == 'v') &&
671 c_isalpha (dev[7]) &&
672 (c_isdigit (dev[8]) || dev[8] == '\0'))
677 free_strings (char **argv)
681 for (argc = 0; argv[argc] != NULL; ++argc)
687 count_strings (char *const *argv)
691 for (c = 0; argv[c]; ++c)