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. */
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 i = guestfs_inspect_get_major_version (g, root);
377 snprintf (buf, sizeof buf, "%d", i);
379 xmlTextWriterWriteElement (xo, BAD_CAST "major_version", BAD_CAST buf));
380 i = guestfs_inspect_get_minor_version (g, root);
381 snprintf (buf, sizeof buf, "%d", i);
383 xmlTextWriterWriteElement (xo, BAD_CAST "minor_version", BAD_CAST buf));
385 str = guestfs_inspect_get_package_format (g, root);
386 if (!str) exit (EXIT_FAILURE);
387 if (STRNEQ (str, "unknown"))
389 xmlTextWriterWriteElement (xo, BAD_CAST "package_format", BAD_CAST str));
392 str = guestfs_inspect_get_package_management (g, root);
393 if (!str) exit (EXIT_FAILURE);
394 if (STRNEQ (str, "unknown"))
396 xmlTextWriterWriteElement (xo, BAD_CAST "package_management",
400 /* inspect-get-windows-systemroot will fail with non-windows guests,
401 * or if the systemroot could not be determined for a windows guest.
402 * Disable error output around this call.
404 DISABLE_GUESTFS_ERRORS_FOR (
405 str = guestfs_inspect_get_windows_systemroot (g, root);
408 xmlTextWriterWriteElement (xo, BAD_CAST "windows_systemroot",
413 str = guestfs_inspect_get_format (g, root);
414 if (!str) exit (EXIT_FAILURE);
415 if (STRNEQ (str, "unknown"))
417 xmlTextWriterWriteElement (xo, BAD_CAST "format",
421 r = guestfs_inspect_is_live (g, root);
424 xmlTextWriterStartElement (xo, BAD_CAST "live"));
425 XMLERROR (-1, xmlTextWriterEndElement (xo));
428 r = guestfs_inspect_is_netinst (g, root);
431 xmlTextWriterStartElement (xo, BAD_CAST "netinst"));
432 XMLERROR (-1, xmlTextWriterEndElement (xo));
435 r = guestfs_inspect_is_multipart (g, root);
438 xmlTextWriterStartElement (xo, BAD_CAST "multipart"));
439 XMLERROR (-1, xmlTextWriterEndElement (xo));
442 output_mountpoints (xo, root);
444 output_filesystems (xo, root);
446 output_applications (xo, root);
448 XMLERROR (-1, xmlTextWriterEndElement (xo));
452 compare_keys (const void *p1, const void *p2)
454 const char *key1 = * (char * const *) p1;
455 const char *key2 = * (char * const *) p2;
457 return strcmp (key1, key2);
461 compare_keys_len (const void *p1, const void *p2)
463 const char *key1 = * (char * const *) p1;
464 const char *key2 = * (char * const *) p2;
467 c = strlen (key1) - strlen (key2);
471 return compare_keys (p1, p2);
475 output_mountpoints (xmlTextWriterPtr xo, char *root)
480 mountpoints = guestfs_inspect_get_mountpoints (g, root);
481 if (mountpoints == NULL)
484 /* Sort by key length, shortest key first, and then name, so the
487 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
490 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "mountpoints"));
492 for (i = 0; mountpoints[i] != NULL; i += 2) {
493 canonicalize (mountpoints[i+1]);
496 xmlTextWriterStartElement (xo, BAD_CAST "mountpoint"));
498 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
499 BAD_CAST mountpoints[i+1]));
501 xmlTextWriterWriteString (xo, BAD_CAST mountpoints[i]));
502 XMLERROR (-1, xmlTextWriterEndElement (xo));
505 XMLERROR (-1, xmlTextWriterEndElement (xo));
507 free_strings (mountpoints);
511 output_filesystems (xmlTextWriterPtr xo, char *root)
517 filesystems = guestfs_inspect_get_filesystems (g, root);
518 if (filesystems == NULL)
521 /* Sort by name so the output is stable. */
522 qsort (filesystems, count_strings (filesystems), sizeof (char *),
525 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystems"));
527 for (i = 0; filesystems[i] != NULL; ++i) {
528 canonicalize (filesystems[i]);
530 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "filesystem"));
532 xmlTextWriterWriteAttribute (xo, BAD_CAST "dev",
533 BAD_CAST filesystems[i]));
535 DISABLE_GUESTFS_ERRORS_FOR (
536 str = guestfs_vfs_type (g, filesystems[i]);
539 xmlTextWriterWriteElement (xo, BAD_CAST "type",
544 DISABLE_GUESTFS_ERRORS_FOR (
545 str = guestfs_vfs_label (g, filesystems[i]);
548 xmlTextWriterWriteElement (xo, BAD_CAST "label",
553 DISABLE_GUESTFS_ERRORS_FOR (
554 str = guestfs_vfs_uuid (g, filesystems[i]);
557 xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
562 XMLERROR (-1, xmlTextWriterEndElement (xo));
565 XMLERROR (-1, xmlTextWriterEndElement (xo));
567 free_strings (filesystems);
571 output_applications (xmlTextWriterPtr xo, char *root)
573 struct guestfs_application_list *apps;
576 /* We need to mount everything up in order to read out the list of
579 inspect_mount_root (root);
581 /* This returns an empty list if we simply couldn't determine the
582 * applications, so if it returns NULL then it's a real error.
584 apps = guestfs_inspect_list_applications (g, root);
587 if (guestfs_umount_all (g) == -1)
590 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "applications"));
592 for (i = 0; i < apps->len; ++i) {
593 XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "application"));
595 assert (apps->val[i].app_name && apps->val[i].app_name[0]);
597 xmlTextWriterWriteElement (xo, BAD_CAST "name",
598 BAD_CAST apps->val[i].app_name));
600 if (apps->val[i].app_display_name && apps->val[i].app_display_name[0])
602 xmlTextWriterWriteElement (xo, BAD_CAST "display_name",
603 BAD_CAST apps->val[i].app_display_name));
605 if (apps->val[i].app_epoch != 0) {
608 snprintf (buf, sizeof buf, "%d", apps->val[i].app_epoch);
611 xmlTextWriterWriteElement (xo, BAD_CAST "epoch", BAD_CAST buf));
614 if (apps->val[i].app_version && apps->val[i].app_version[0])
616 xmlTextWriterWriteElement (xo, BAD_CAST "version",
617 BAD_CAST apps->val[i].app_version));
618 if (apps->val[i].app_release && apps->val[i].app_release[0])
620 xmlTextWriterWriteElement (xo, BAD_CAST "release",
621 BAD_CAST apps->val[i].app_release));
622 if (apps->val[i].app_install_path && apps->val[i].app_install_path[0])
624 xmlTextWriterWriteElement (xo, BAD_CAST "install_path",
625 BAD_CAST apps->val[i].app_install_path));
626 if (apps->val[i].app_publisher && apps->val[i].app_publisher[0])
628 xmlTextWriterWriteElement (xo, BAD_CAST "publisher",
629 BAD_CAST apps->val[i].app_publisher));
630 if (apps->val[i].app_url && apps->val[i].app_url[0])
632 xmlTextWriterWriteElement (xo, BAD_CAST "url",
633 BAD_CAST apps->val[i].app_url));
634 if (apps->val[i].app_source_package && apps->val[i].app_source_package[0])
636 xmlTextWriterWriteElement (xo, BAD_CAST "source_package",
637 BAD_CAST apps->val[i].app_source_package));
638 if (apps->val[i].app_summary && apps->val[i].app_summary[0])
640 xmlTextWriterWriteElement (xo, BAD_CAST "summary",
641 BAD_CAST apps->val[i].app_summary));
642 if (apps->val[i].app_description && apps->val[i].app_description[0])
644 xmlTextWriterWriteElement (xo, BAD_CAST "description",
645 BAD_CAST apps->val[i].app_description));
647 XMLERROR (-1, xmlTextWriterEndElement (xo));
650 XMLERROR (-1, xmlTextWriterEndElement (xo));
652 guestfs_free_application_list (apps);
655 /* "/dev/vda1" -> "/dev/sda1"
656 * See BLOCK DEVICE NAMING in guestfs(3).
659 canonicalize (char *dev)
661 if (STRPREFIX (dev, "/dev/") &&
662 (dev[5] == 'h' || dev[5] == 'v') &&
664 c_isalpha (dev[7]) &&
665 (c_isdigit (dev[8]) || dev[8] == '\0'))
670 free_strings (char **argv)
674 for (argc = 0; argv[argc] != NULL; ++argc)
680 count_strings (char *const *argv)
684 for (c = 0; argv[c]; ++c)