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.
29 #include <libxml/xmlIO.h>
30 #include <libxml/xmlwriter.h>
38 /* 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);
231 /* Must be no extra arguments on the command line. */
233 usage (EXIT_FAILURE);
235 /* User must have specified some drives. */
237 usage (EXIT_FAILURE);
239 /* Add drives, inspect and mount. Note that inspector is always true,
240 * and there is no -m option.
242 add_drives (drvs, 'a');
244 if (guestfs_launch (g) == -1)
247 /* Free up data structures, no longer needed after this point. */
250 /* NB. Can't call inspect_mount () here (ie. normal processing of
251 * the -i option) because it can only handle a single root. So we
252 * use low-level APIs.
254 inspect_do_decrypt ();
256 char **roots = guestfs_inspect_os (g);
258 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"),
265 free_strings (roots);
272 #define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
273 guestfs_error_handler_cb old_error_cb; \
274 void *old_error_data; \
275 old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
276 guestfs_set_error_handler (g, NULL, NULL); \
278 guestfs_set_error_handler (g, old_error_cb, old_error_data); \
281 #define XMLERROR(code,e) do { \
282 if ((e) == (code)) { \
283 fprintf (stderr, _("%s: XML write error at \"%s\": %m\n"), \
285 exit (EXIT_FAILURE); \
290 output (char **roots)
292 xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL);
295 _("%s: xmlOutputBufferCreateFd: failed to open stdout\n"),
300 xmlTextWriterPtr xo = xmlNewTextWriter (ob);
303 _("%s: xmlNewTextWriter: failed to create libxml2 writer\n"),
308 /* Pretty-print the output. */
309 XMLERROR (-1, xmlTextWriterSetIndent (xo, 1));
310 XMLERROR (-1, xmlTextWriterSetIndentString (xo, BAD_CAST " "));
312 XMLERROR (-1, xmlTextWriterStartDocument (xo, NULL, NULL, NULL));
313 output_roots (xo, roots);
314 XMLERROR (-1, xmlTextWriterEndDocument (xo));
316 /* 'ob' is freed by this too. */
317 xmlFreeTextWriter (xo);
321 output_roots (xmlTextWriterPtr xo, char **roots)
325 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystems"));
326 for (i = 0; roots[i] != NULL; ++i)
327 output_root (xo, roots[i]);
328 XMLERROR (-1, xmlTextWriterEndElement (xo));
332 output_root (xmlTextWriterPtr xo, char *root)
337 char canonical_root[strlen (root) + 1];
339 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "operatingsystem"));
341 strcpy (canonical_root, root);
342 canonicalize (canonical_root);
344 xmlTextWriterWriteElement (xo, BAD_CAST "root", BAD_CAST canonical_root));
346 str = guestfs_inspect_get_type (g, root);
347 if (!str) exit (EXIT_FAILURE);
348 if (STRNEQ (str, "unknown"))
350 xmlTextWriterWriteElement (xo, BAD_CAST "name", BAD_CAST str));
353 str = guestfs_inspect_get_arch (g, root);
354 if (!str) exit (EXIT_FAILURE);
355 if (STRNEQ (str, "unknown"))
357 xmlTextWriterWriteElement (xo, BAD_CAST "arch", BAD_CAST str));
360 str = guestfs_inspect_get_distro (g, root);
361 if (!str) exit (EXIT_FAILURE);
362 if (STRNEQ (str, "unknown"))
364 xmlTextWriterWriteElement (xo, BAD_CAST "distro", BAD_CAST str));
367 str = guestfs_inspect_get_product_name (g, root);
368 if (!str) exit (EXIT_FAILURE);
369 if (STRNEQ (str, "unknown"))
371 xmlTextWriterWriteElement (xo, BAD_CAST "product_name", BAD_CAST str));
374 i = guestfs_inspect_get_major_version (g, root);
375 snprintf (buf, sizeof buf, "%d", i);
377 xmlTextWriterWriteElement (xo, BAD_CAST "major_version", BAD_CAST buf));
378 i = guestfs_inspect_get_minor_version (g, root);
379 snprintf (buf, sizeof buf, "%d", i);
381 xmlTextWriterWriteElement (xo, BAD_CAST "minor_version", BAD_CAST buf));
383 str = guestfs_inspect_get_package_format (g, root);
384 if (!str) exit (EXIT_FAILURE);
385 if (STRNEQ (str, "unknown"))
387 xmlTextWriterWriteElement (xo, BAD_CAST "package_format", BAD_CAST str));
390 str = guestfs_inspect_get_package_management (g, root);
391 if (!str) exit (EXIT_FAILURE);
392 if (STRNEQ (str, "unknown"))
394 xmlTextWriterWriteElement (xo, BAD_CAST "package_management",
398 /* inspect-get-windows-systemroot will fail with non-windows guests,
399 * or if the systemroot could not be determined for a windows guest.
400 * Disable error output around this call.
402 DISABLE_GUESTFS_ERRORS_FOR (
403 str = guestfs_inspect_get_windows_systemroot (g, root);
406 xmlTextWriterWriteElement (xo, BAD_CAST "windows_systemroot",
411 output_mountpoints (xo, root);
413 output_filesystems (xo, root);
415 output_applications (xo, root);
417 XMLERROR (-1, xmlTextWriterEndElement (xo));
421 compare_keys (const void *p1, const void *p2)
423 const char *key1 = * (char * const *) p1;
424 const char *key2 = * (char * const *) p2;
426 return strcmp (key1, key2);
430 compare_keys_len (const void *p1, const void *p2)
432 const char *key1 = * (char * const *) p1;
433 const char *key2 = * (char * const *) p2;
436 c = strlen (key1) - strlen (key2);
440 return compare_keys (p1, p2);
444 output_mountpoints (xmlTextWriterPtr xo, char *root)
449 mountpoints = guestfs_inspect_get_mountpoints (g, root);
450 if (mountpoints == NULL)
453 /* Sort by key length, shortest key first, and then name, so the
456 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
459 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
461 for (i = 0; mountpoints[i] != NULL; i += 2) {
462 canonicalize (mountpoints[i+1]);
465 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
467 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
468 BAD_CAST mountpoints[i+1]));
470 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
471 XMLERROR (-1, xmlTextWriterEndElement (xo));
474 XMLERROR (-1, xmlTextWriterEndElement (xo));
476 free_strings (mountpoints);
480 output_filesystems (xmlTextWriterPtr xo, char *root)
486 filesystems = guestfs_inspect_get_filesystems (g, root);
487 if (filesystems == NULL)
490 /* Sort by name so the output is stable. */
491 qsort (filesystems, count_strings (filesystems), sizeof (char *),
494 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
496 for (i = 0; filesystems[i] != NULL; ++i) {
497 canonicalize (filesystems[i]);
499 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
501 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
502 BAD_CAST filesystems[i]));
504 DISABLE_GUESTFS_ERRORS_FOR (
505 str = guestfs_vfs_type (g, filesystems[i]);
508 xmlTextWriterWriteElement (xo, BAD_CAST "type",
513 DISABLE_GUESTFS_ERRORS_FOR (
514 str = guestfs_vfs_label (g, filesystems[i]);
517 xmlTextWriterWriteElement (xo, BAD_CAST "label",
522 DISABLE_GUESTFS_ERRORS_FOR (
523 str = guestfs_vfs_uuid (g, filesystems[i]);
526 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
531 XMLERROR (-1, xmlTextWriterEndElement (xo));
534 XMLERROR (-1, xmlTextWriterEndElement (xo));
536 free_strings (filesystems);
540 output_applications (xmlTextWriterPtr xo, char *root)
542 struct guestfs_application_list *apps;
545 /* We need to mount everything up in order to read out the list of
548 inspect_mount_root (root);
550 /* This returns an empty list if we simply couldn't determine the
551 * applications, so if it returns NULL then it's a real error.
553 apps = guestfs_inspect_list_applications (g, root);
556 if (guestfs_umount_all (g) == -1)
559 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
561 for (i = 0; i < apps->len; ++i) {
562 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
564 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
566 xmlTextWriterWriteElement (xo, BAD_CAST "name",
567 BAD_CAST apps->val[i].app_name));
569 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
571 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
572 BAD_CAST apps->val[i].app_display_name));
574 if (apps->val[i].app_epoch != 0) {
577 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
580 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
583 if (apps->val[i].app_version && apps->val[i].app_version[0])
585 xmlTextWriterWriteElement (xo, BAD_CAST "version",
586 BAD_CAST apps->val[i].app_version));
587 if (apps->val[i].app_release && apps->val[i].app_release[0])
589 xmlTextWriterWriteElement (xo, BAD_CAST "release",
590 BAD_CAST apps->val[i].app_release));
591 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
593 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
594 BAD_CAST apps->val[i].app_install_path));
595 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
597 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
598 BAD_CAST apps->val[i].app_publisher));
599 if (apps->val[i].app_url && apps->val[i].app_url[0])
601 xmlTextWriterWriteElement (xo, BAD_CAST "url",
602 BAD_CAST apps->val[i].app_url));
603 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
605 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
606 BAD_CAST apps->val[i].app_source_package));
607 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
609 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
610 BAD_CAST apps->val[i].app_summary));
611 if (apps->val[i].app_description && apps->val[i].app_description[0])
613 xmlTextWriterWriteElement (xo, BAD_CAST "description",
614 BAD_CAST apps->val[i].app_description));
616 XMLERROR (-1, xmlTextWriterEndElement (xo));
619 XMLERROR (-1, xmlTextWriterEndElement (xo));
621 guestfs_free_application_list (apps);
624 /* "/dev/vda1" -> "/dev/sda1"
625 * See BLOCK DEVICE NAMING in guestfs(3).
628 canonicalize (char *dev)
630 if (STRPREFIX (dev, "/dev/") &&
631 (dev[5] == 'h' || dev[5] == 'v') &&
633 c_isalpha (dev[7]) &&
634 (c_isdigit (dev[8]) || dev[8] == '\0'))
639 free_strings (char **argv)
643 for (argc = 0; argv[argc] != NULL; ++argc)
649 count_strings (char *const *argv)
653 for (c = 0; argv[c]; ++c)