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 const char *format = NULL;
125 g = guestfs_create ();
127 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
131 argv[0] = bad_cast (program_name);
134 c = getopt_long (argc, argv, options, long_options, &option_index);
138 case 0: /* options which are long only */
139 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
141 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
143 } else if (STREQ (long_options[option_index].name, "format")) {
144 if (!optarg || STREQ (optarg, ""))
149 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
150 program_name, long_options[option_index].name, option_index);
168 usage (EXIT_SUCCESS);
183 usage (EXIT_SUCCESS);
186 usage (EXIT_FAILURE);
190 /* Old-style syntax? There were no -a or -d options in the old
191 * virt-inspector which is how we detect this.
194 while (optind < argc) {
195 if (strchr (argv[optind], '/') ||
196 access (argv[optind], F_OK) == 0) { /* simulate -a option */
197 drv = malloc (sizeof (struct drv));
203 drv->a.filename = argv[optind];
204 drv->a.format = NULL;
207 } else { /* simulate -d option */
208 drv = malloc (sizeof (struct drv));
214 drv->d.guest = argv[optind];
223 /* These are really constants, but they have to be variables for the
224 * options parsing code. Assert here that they have known-good
227 assert (read_only == 1);
228 assert (inspector == 1);
230 /* Must be no extra arguments on the command line. */
232 usage (EXIT_FAILURE);
234 /* User must have specified some drives. */
236 usage (EXIT_FAILURE);
238 /* Add drives, inspect and mount. Note that inspector is always true,
239 * and there is no -m option.
241 add_drives (drvs, 'a');
243 if (guestfs_launch (g) == -1)
246 /* Free up data structures, no longer needed after this point. */
249 /* NB. Can't call inspect_mount () here (ie. normal processing of
250 * the -i option) because it can only handle a single root. So we
251 * use low-level APIs.
253 inspect_do_decrypt ();
255 char **roots = guestfs_inspect_os (g);
257 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"),
264 free_strings (roots);
271 #define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
272 guestfs_error_handler_cb old_error_cb; \
273 void *old_error_data; \
274 old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
275 guestfs_set_error_handler (g, NULL, NULL); \
277 guestfs_set_error_handler (g, old_error_cb, old_error_data); \
280 #define XMLERROR(code,e) do { \
281 if ((e) == (code)) { \
282 fprintf (stderr, _("%s: XML write error at \"%s\": %m\n"), \
284 exit (EXIT_FAILURE); \
289 output (char **roots)
291 xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL);
294 _("%s: xmlOutputBufferCreateFd: failed to open stdout\n"),
299 xmlTextWriterPtr xo = xmlNewTextWriter (ob);
302 _("%s: xmlNewTextWriter: failed to create libxml2 writer\n"),
307 /* Pretty-print the output. */
308 XMLERROR (-1, xmlTextWriterSetIndent (xo, 1));
309 XMLERROR (-1, xmlTextWriterSetIndentString (xo, BAD_CAST " "));
311 XMLERROR (-1, xmlTextWriterStartDocument (xo, NULL, NULL, NULL));
312 output_roots (xo, roots);
313 XMLERROR (-1, xmlTextWriterEndDocument (xo));
315 /* 'ob' is freed by this too. */
316 xmlFreeTextWriter (xo);
320 output_roots (xmlTextWriterPtr xo, char **roots)
324 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystems"));
325 for (i = 0; roots[i] != NULL; ++i)
326 output_root (xo, roots[i]);
327 XMLERROR (-1, xmlTextWriterEndElement (xo));
331 output_root (xmlTextWriterPtr xo, char *root)
336 char canonical_root[strlen (root) + 1];
338 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystem"));
340 strcpy (canonical_root, root);
341 canonicalize (canonical_root);
343 xmlTextWriterWriteElement (xo, BAD_CAST "root", BAD_CAST canonical_root));
345 str = guestfs_inspect_get_type (g, root);
346 if (!str) exit (EXIT_FAILURE);
347 if (STRNEQ (str, "unknown"))
349 xmlTextWriterWriteElement (xo, BAD_CAST "name", BAD_CAST str));
352 str = guestfs_inspect_get_arch (g, root);
353 if (!str) exit (EXIT_FAILURE);
354 if (STRNEQ (str, "unknown"))
356 xmlTextWriterWriteElement (xo, BAD_CAST "arch", BAD_CAST str));
359 str = guestfs_inspect_get_distro (g, root);
360 if (!str) exit (EXIT_FAILURE);
361 if (STRNEQ (str, "unknown"))
363 xmlTextWriterWriteElement (xo, BAD_CAST "distro", BAD_CAST str));
366 str = guestfs_inspect_get_product_name (g, root);
367 if (!str) exit (EXIT_FAILURE);
368 if (STRNEQ (str, "unknown"))
370 xmlTextWriterWriteElement (xo, BAD_CAST "product_name", BAD_CAST str));
373 i = guestfs_inspect_get_major_version (g, root);
374 snprintf (buf, sizeof buf, "%d", i);
376 xmlTextWriterWriteElement (xo, BAD_CAST "major_version", BAD_CAST buf));
377 i = guestfs_inspect_get_minor_version (g, root);
378 snprintf (buf, sizeof buf, "%d", i);
380 xmlTextWriterWriteElement (xo, BAD_CAST "minor_version", BAD_CAST buf));
382 str = guestfs_inspect_get_package_format (g, root);
383 if (!str) exit (EXIT_FAILURE);
384 if (STRNEQ (str, "unknown"))
386 xmlTextWriterWriteElement (xo, BAD_CAST "package_format", BAD_CAST str));
389 str = guestfs_inspect_get_package_management (g, root);
390 if (!str) exit (EXIT_FAILURE);
391 if (STRNEQ (str, "unknown"))
393 xmlTextWriterWriteElement (xo, BAD_CAST "package_management",
397 /* inspect-get-windows-systemroot will fail with non-windows guests,
398 * or if the systemroot could not be determined for a windows guest.
399 * Disable error output around this call.
401 DISABLE_GUESTFS_ERRORS_FOR (
402 str = guestfs_inspect_get_windows_systemroot (g, root);
405 xmlTextWriterWriteElement (xo, BAD_CAST "windows_systemroot",
410 output_mountpoints (xo, root);
412 output_filesystems (xo, root);
414 output_applications (xo, root);
416 XMLERROR (-1, xmlTextWriterEndElement (xo));
420 compare_keys (const void *p1, const void *p2)
422 const char *key1 = * (char * const *) p1;
423 const char *key2 = * (char * const *) p2;
425 return strcmp (key1, key2);
429 compare_keys_len (const void *p1, const void *p2)
431 const char *key1 = * (char * const *) p1;
432 const char *key2 = * (char * const *) p2;
435 c = strlen (key1) - strlen (key2);
439 return compare_keys (p1, p2);
443 output_mountpoints (xmlTextWriterPtr xo, char *root)
448 mountpoints = guestfs_inspect_get_mountpoints (g, root);
449 if (mountpoints == NULL)
452 /* Sort by key length, shortest key first, and then name, so the
455 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
458 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
460 for (i = 0; mountpoints[i] != NULL; i += 2) {
461 canonicalize (mountpoints[i+1]);
464 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
466 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
467 BAD_CAST mountpoints[i+1]));
469 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
470 XMLERROR (-1, xmlTextWriterEndElement (xo));
473 XMLERROR (-1, xmlTextWriterEndElement (xo));
475 free_strings (mountpoints);
479 output_filesystems (xmlTextWriterPtr xo, char *root)
485 filesystems = guestfs_inspect_get_filesystems (g, root);
486 if (filesystems == NULL)
489 /* Sort by name so the output is stable. */
490 qsort (filesystems, count_strings (filesystems), sizeof (char *),
493 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
495 for (i = 0; filesystems[i] != NULL; ++i) {
496 canonicalize (filesystems[i]);
498 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
500 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
501 BAD_CAST filesystems[i]));
503 DISABLE_GUESTFS_ERRORS_FOR (
504 str = guestfs_vfs_type (g, filesystems[i]);
507 xmlTextWriterWriteElement (xo, BAD_CAST "type",
512 DISABLE_GUESTFS_ERRORS_FOR (
513 str = guestfs_vfs_label (g, filesystems[i]);
516 xmlTextWriterWriteElement (xo, BAD_CAST "label",
521 DISABLE_GUESTFS_ERRORS_FOR (
522 str = guestfs_vfs_uuid (g, filesystems[i]);
525 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
530 XMLERROR (-1, xmlTextWriterEndElement (xo));
533 XMLERROR (-1, xmlTextWriterEndElement (xo));
535 free_strings (filesystems);
539 output_applications (xmlTextWriterPtr xo, char *root)
541 struct guestfs_application_list *apps;
544 /* We need to mount everything up in order to read out the list of
547 inspect_mount_root (root);
549 /* This returns an empty list if we simply couldn't determine the
550 * applications, so if it returns NULL then it's a real error.
552 apps = guestfs_inspect_list_applications (g, root);
555 if (guestfs_umount_all (g) == -1)
558 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
560 for (i = 0; i < apps->len; ++i) {
561 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
563 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
565 xmlTextWriterWriteElement (xo, BAD_CAST "name",
566 BAD_CAST apps->val[i].app_name));
568 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
570 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
571 BAD_CAST apps->val[i].app_display_name));
573 if (apps->val[i].app_epoch != 0) {
576 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
579 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
582 if (apps->val[i].app_version && apps->val[i].app_version[0])
584 xmlTextWriterWriteElement (xo, BAD_CAST "version",
585 BAD_CAST apps->val[i].app_version));
586 if (apps->val[i].app_release && apps->val[i].app_release[0])
588 xmlTextWriterWriteElement (xo, BAD_CAST "release",
589 BAD_CAST apps->val[i].app_release));
590 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
592 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
593 BAD_CAST apps->val[i].app_install_path));
594 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
596 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
597 BAD_CAST apps->val[i].app_publisher));
598 if (apps->val[i].app_url && apps->val[i].app_url[0])
600 xmlTextWriterWriteElement (xo, BAD_CAST "url",
601 BAD_CAST apps->val[i].app_url));
602 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
604 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
605 BAD_CAST apps->val[i].app_source_package));
606 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
608 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
609 BAD_CAST apps->val[i].app_summary));
610 if (apps->val[i].app_description && apps->val[i].app_description[0])
612 xmlTextWriterWriteElement (xo, BAD_CAST "description",
613 BAD_CAST apps->val[i].app_description));
615 XMLERROR (-1, xmlTextWriterEndElement (xo));
618 XMLERROR (-1, xmlTextWriterEndElement (xo));
620 guestfs_free_application_list (apps);
623 /* "/dev/vda1" -> "/dev/sda1"
624 * See BLOCK DEVICE NAMING in guestfs(3).
627 canonicalize (char *dev)
629 if (STRPREFIX (dev, "/dev/") &&
630 (dev[5] == 'h' || dev[5] == 'v') &&
632 c_isalpha (dev[7]) &&
633 (c_isdigit (dev[8]) || dev[8] == '\0'))
638 free_strings (char **argv)
642 for (argc = 0; argv[argc] != NULL; ++argc)
648 count_strings (char *const *argv)
652 for (c = 0; argv[c]; ++c)