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 str = guestfs_inspect_get_hostname (g, root);
412 if (!str) exit (EXIT_FAILURE);
413 if (STRNEQ (str, "unknown"))
415 xmlTextWriterWriteElement (xo, BAD_CAST "hostname",
419 output_mountpoints (xo, root);
421 output_filesystems (xo, root);
423 output_applications (xo, root);
425 XMLERROR (-1, xmlTextWriterEndElement (xo));
429 compare_keys (const void *p1, const void *p2)
431 const char *key1 = * (char * const *) p1;
432 const char *key2 = * (char * const *) p2;
434 return strcmp (key1, key2);
438 compare_keys_len (const void *p1, const void *p2)
440 const char *key1 = * (char * const *) p1;
441 const char *key2 = * (char * const *) p2;
444 c = strlen (key1) - strlen (key2);
448 return compare_keys (p1, p2);
452 output_mountpoints (xmlTextWriterPtr xo, char *root)
457 mountpoints = guestfs_inspect_get_mountpoints (g, root);
458 if (mountpoints == NULL)
461 /* Sort by key length, shortest key first, and then name, so the
464 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
467 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
469 for (i = 0; mountpoints[i] != NULL; i += 2) {
470 canonicalize (mountpoints[i+1]);
473 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
475 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
476 BAD_CAST mountpoints[i+1]));
478 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
479 XMLERROR (-1, xmlTextWriterEndElement (xo));
482 XMLERROR (-1, xmlTextWriterEndElement (xo));
484 free_strings (mountpoints);
488 output_filesystems (xmlTextWriterPtr xo, char *root)
494 filesystems = guestfs_inspect_get_filesystems (g, root);
495 if (filesystems == NULL)
498 /* Sort by name so the output is stable. */
499 qsort (filesystems, count_strings (filesystems), sizeof (char *),
502 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
504 for (i = 0; filesystems[i] != NULL; ++i) {
505 canonicalize (filesystems[i]);
507 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
509 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
510 BAD_CAST filesystems[i]));
512 DISABLE_GUESTFS_ERRORS_FOR (
513 str = guestfs_vfs_type (g, filesystems[i]);
516 xmlTextWriterWriteElement (xo, BAD_CAST "type",
521 DISABLE_GUESTFS_ERRORS_FOR (
522 str = guestfs_vfs_label (g, filesystems[i]);
525 xmlTextWriterWriteElement (xo, BAD_CAST "label",
530 DISABLE_GUESTFS_ERRORS_FOR (
531 str = guestfs_vfs_uuid (g, filesystems[i]);
534 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
539 XMLERROR (-1, xmlTextWriterEndElement (xo));
542 XMLERROR (-1, xmlTextWriterEndElement (xo));
544 free_strings (filesystems);
548 output_applications (xmlTextWriterPtr xo, char *root)
550 struct guestfs_application_list *apps;
553 /* We need to mount everything up in order to read out the list of
556 inspect_mount_root (root);
558 /* This returns an empty list if we simply couldn't determine the
559 * applications, so if it returns NULL then it's a real error.
561 apps = guestfs_inspect_list_applications (g, root);
564 if (guestfs_umount_all (g) == -1)
567 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
569 for (i = 0; i < apps->len; ++i) {
570 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
572 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
574 xmlTextWriterWriteElement (xo, BAD_CAST "name",
575 BAD_CAST apps->val[i].app_name));
577 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
579 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
580 BAD_CAST apps->val[i].app_display_name));
582 if (apps->val[i].app_epoch != 0) {
585 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
588 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
591 if (apps->val[i].app_version && apps->val[i].app_version[0])
593 xmlTextWriterWriteElement (xo, BAD_CAST "version",
594 BAD_CAST apps->val[i].app_version));
595 if (apps->val[i].app_release && apps->val[i].app_release[0])
597 xmlTextWriterWriteElement (xo, BAD_CAST "release",
598 BAD_CAST apps->val[i].app_release));
599 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
601 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
602 BAD_CAST apps->val[i].app_install_path));
603 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
605 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
606 BAD_CAST apps->val[i].app_publisher));
607 if (apps->val[i].app_url && apps->val[i].app_url[0])
609 xmlTextWriterWriteElement (xo, BAD_CAST "url",
610 BAD_CAST apps->val[i].app_url));
611 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
613 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
614 BAD_CAST apps->val[i].app_source_package));
615 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
617 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
618 BAD_CAST apps->val[i].app_summary));
619 if (apps->val[i].app_description && apps->val[i].app_description[0])
621 xmlTextWriterWriteElement (xo, BAD_CAST "description",
622 BAD_CAST apps->val[i].app_description));
624 XMLERROR (-1, xmlTextWriterEndElement (xo));
627 XMLERROR (-1, xmlTextWriterEndElement (xo));
629 guestfs_free_application_list (apps);
632 /* "/dev/vda1" -> "/dev/sda1"
633 * See BLOCK DEVICE NAMING in guestfs(3).
636 canonicalize (char *dev)
638 if (STRPREFIX (dev, "/dev/") &&
639 (dev[5] == 'h' || dev[5] == 'v') &&
641 c_isalpha (dev[7]) &&
642 (c_isdigit (dev[8]) || dev[8] == '\0'))
647 free_strings (char **argv)
651 for (argc = 0; argv[argc] != NULL; ++argc)
657 count_strings (char *const *argv)
661 for (c = 0; argv[c]; ++c)