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. */
42 int keys_from_stdin = 0;
44 const char *libvirt_uri = NULL;
47 static void output (char **roots);
48 static void output_roots (xmlTextWriterPtr xo, char **roots);
49 static void output_root (xmlTextWriterPtr xo, char *root);
50 static void output_mountpoints (xmlTextWriterPtr xo, char *root);
51 static void output_filesystems (xmlTextWriterPtr xo, char *root);
52 static void output_applications (xmlTextWriterPtr xo, char *root);
53 static void canonicalize (char *dev);
54 static void free_strings (char **argv);
55 static int count_strings (char *const*argv);
58 bad_cast (char const *s)
63 static void __attribute__((noreturn))
66 if (status != EXIT_SUCCESS)
67 fprintf (stderr, _("Try `%s --help' for more information.\n"),
71 _("%s: display information about a virtual machine\n"
72 "Copyright (C) 2010 Red Hat Inc.\n"
74 " %s [--options] -d domname file [file ...]\n"
75 " %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
77 " -a|--add image Add image\n"
78 " -c|--connect uri Specify libvirt URI for -d option\n"
79 " -d|--domain guest Add disks from libvirt guest\n"
80 " --echo-keys Don't turn off echo for passphrases\n"
81 " --format[=raw|..] Force disk format for -a option\n"
82 " --help Display brief help\n"
83 " --keys-from-stdin Read passphrases from stdin\n"
84 " -v|--verbose Verbose messages\n"
85 " -V|--version Display version and exit\n"
86 " -x Trace libguestfs API calls\n"
87 "For more information, see the manpage %s(1).\n"),
88 program_name, program_name, program_name,
95 main (int argc, char *argv[])
97 /* Set global program name that is not polluted with libtool artifacts. */
98 set_program_name (argv[0]);
100 setlocale (LC_ALL, "");
101 bindtextdomain (PACKAGE, LOCALEBASEDIR);
102 textdomain (PACKAGE);
104 enum { HELP_OPTION = CHAR_MAX + 1 };
106 static const char *options = "a:c:d:vVx";
107 static const struct option long_options[] = {
108 { "add", 1, 0, 'a' },
109 { "connect", 1, 0, 'c' },
110 { "domain", 1, 0, 'd' },
111 { "echo-keys", 0, 0, 0 },
112 { "format", 2, 0, 0 },
113 { "help", 0, 0, HELP_OPTION },
114 { "keys-from-stdin", 0, 0, 0 },
115 { "verbose", 0, 0, 'v' },
116 { "version", 0, 0, 'V' },
119 struct drv *drvs = NULL;
121 char *p, *file = NULL;
122 const char *format = NULL;
125 int next_prepared_drive = 1;
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);
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 output_mountpoints (xo, root);
414 output_filesystems (xo, root);
416 output_applications (xo, root);
418 XMLERROR (-1, xmlTextWriterEndElement (xo));
422 compare_keys (const void *p1, const void *p2)
424 const char *key1 = * (char * const *) p1;
425 const char *key2 = * (char * const *) p2;
427 return strcmp (key1, key2);
431 compare_keys_len (const void *p1, const void *p2)
433 const char *key1 = * (char * const *) p1;
434 const char *key2 = * (char * const *) p2;
437 c = strlen (key1) - strlen (key2);
441 return compare_keys (p1, p2);
445 output_mountpoints (xmlTextWriterPtr xo, char *root)
450 mountpoints = guestfs_inspect_get_mountpoints (g, root);
451 if (mountpoints == NULL)
454 /* Sort by key length, shortest key first, and then name, so the
457 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
460 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
462 for (i = 0; mountpoints[i] != NULL; i += 2) {
463 canonicalize (mountpoints[i+1]);
466 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
468 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
469 BAD_CAST mountpoints[i+1]));
471 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
472 XMLERROR (-1, xmlTextWriterEndElement (xo));
475 XMLERROR (-1, xmlTextWriterEndElement (xo));
477 free_strings (mountpoints);
481 output_filesystems (xmlTextWriterPtr xo, char *root)
487 filesystems = guestfs_inspect_get_filesystems (g, root);
488 if (filesystems == NULL)
491 /* Sort by name so the output is stable. */
492 qsort (filesystems, count_strings (filesystems), sizeof (char *),
495 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
497 for (i = 0; filesystems[i] != NULL; ++i) {
498 canonicalize (filesystems[i]);
500 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
502 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
503 BAD_CAST filesystems[i]));
505 DISABLE_GUESTFS_ERRORS_FOR (
506 str = guestfs_vfs_type (g, filesystems[i]);
509 xmlTextWriterWriteElement (xo, BAD_CAST "type",
514 DISABLE_GUESTFS_ERRORS_FOR (
515 str = guestfs_vfs_label (g, filesystems[i]);
518 xmlTextWriterWriteElement (xo, BAD_CAST "label",
523 DISABLE_GUESTFS_ERRORS_FOR (
524 str = guestfs_vfs_uuid (g, filesystems[i]);
527 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
532 XMLERROR (-1, xmlTextWriterEndElement (xo));
535 XMLERROR (-1, xmlTextWriterEndElement (xo));
537 free_strings (filesystems);
541 output_applications (xmlTextWriterPtr xo, char *root)
543 struct guestfs_application_list *apps;
546 /* We need to mount everything up in order to read out the list of
549 inspect_mount_root (root);
551 /* This returns an empty list if we simply couldn't determine the
552 * applications, so if it returns NULL then it's a real error.
554 apps = guestfs_inspect_list_applications (g, root);
557 if (guestfs_umount_all (g) == -1)
560 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
562 for (i = 0; i < apps->len; ++i) {
563 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
565 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
567 xmlTextWriterWriteElement (xo, BAD_CAST "name",
568 BAD_CAST apps->val[i].app_name));
570 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
572 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
573 BAD_CAST apps->val[i].app_display_name));
575 if (apps->val[i].app_epoch != 0) {
578 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
581 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
584 if (apps->val[i].app_version && apps->val[i].app_version[0])
586 xmlTextWriterWriteElement (xo, BAD_CAST "version",
587 BAD_CAST apps->val[i].app_version));
588 if (apps->val[i].app_release && apps->val[i].app_release[0])
590 xmlTextWriterWriteElement (xo, BAD_CAST "release",
591 BAD_CAST apps->val[i].app_release));
592 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
594 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
595 BAD_CAST apps->val[i].app_install_path));
596 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
598 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
599 BAD_CAST apps->val[i].app_publisher));
600 if (apps->val[i].app_url && apps->val[i].app_url[0])
602 xmlTextWriterWriteElement (xo, BAD_CAST "url",
603 BAD_CAST apps->val[i].app_url));
604 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
606 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
607 BAD_CAST apps->val[i].app_source_package));
608 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
610 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
611 BAD_CAST apps->val[i].app_summary));
612 if (apps->val[i].app_description && apps->val[i].app_description[0])
614 xmlTextWriterWriteElement (xo, BAD_CAST "description",
615 BAD_CAST apps->val[i].app_description));
617 XMLERROR (-1, xmlTextWriterEndElement (xo));
620 XMLERROR (-1, xmlTextWriterEndElement (xo));
622 guestfs_free_application_list (apps);
625 /* "/dev/vda1" -> "/dev/sda1"
626 * See BLOCK DEVICE NAMING in guestfs(3).
629 canonicalize (char *dev)
631 if (STRPREFIX (dev, "/dev/") &&
632 (dev[5] == 'h' || dev[5] == 'v') &&
634 c_isalpha (dev[7]) &&
635 (c_isdigit (dev[8]) || dev[8] == '\0'))
640 free_strings (char **argv)
644 for (argc = 0; argv[argc] != NULL; ++argc)
650 count_strings (char *const *argv)
654 for (c = 0; argv[c]; ++c)