7 compare_keys_len (const void *p1, const void *p2)
9 const char *key1 = * (char * const *) p1;
10 const char *key2 = * (char * const *) p2;
11 return strlen (key1) - strlen (key2);
15 count_strings (char *const *argv)
19 for (c = 0; argv[c]; ++c)
25 main (int argc, char *argv[])
29 char **roots, *root, *str, **mountpoints, **lines;
33 fprintf (stderr, "usage: inspect_vm disk.img\n");
38 g = guestfs_create ();
40 perror ("failed to create libguestfs handle");
44 /* Attach the disk image read-only to libguestfs. */
45 if (guestfs_add_drive_opts (g, disk,
46 /* GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", */
47 GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
48 -1) /* this marks end of optional arguments */
52 /* Run the libguestfs back-end. */
53 if (guestfs_launch (g) == -1)
56 /* Ask libguestfs to inspect for operating systems. */
57 roots = guestfs_inspect_os (g);
60 if (roots[0] == NULL) {
61 fprintf (stderr, "inspect_vm: no operating systems found\n");
65 for (j = 0; roots[j] != NULL; ++j) {
68 printf ("Root device: %s\n", root);
70 /* Print basic information about the operating system. */
71 str = guestfs_inspect_get_product_name (g, root);
73 printf (" Product name: %s\n", str);
76 printf (" Version: %d.%d\n",
77 guestfs_inspect_get_major_version (g, root),
78 guestfs_inspect_get_minor_version (g, root));
80 str = guestfs_inspect_get_type (g, root);
82 printf (" Type: %s\n", str);
84 str = guestfs_inspect_get_distro (g, root);
86 printf (" Distro: %s\n", str);
89 /* Mount up the disks, like guestfish -i.
91 * Sort keys by length, shortest first, so that we end up
92 * mounting the filesystems in the correct order.
94 mountpoints = guestfs_inspect_get_mountpoints (g, root);
95 if (mountpoints == NULL)
98 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
100 for (i = 0; mountpoints[i] != NULL; i += 2) {
101 /* Ignore failures from this call, since bogus entries can
102 * appear in the guest's /etc/fstab.
104 guestfs_mount_ro (g, mountpoints[i+1], mountpoints[i]);
105 free (mountpoints[i]);
106 free (mountpoints[i+1]);
110 /* If /etc/issue.net file exists, print up to 3 lines. */
111 if (guestfs_is_file (g, "/etc/issue.net") > 0) {
112 printf ("--- /etc/issue.net ---\n");
113 lines = guestfs_head_n (g, 3, "/etc/issue.net");
116 for (i = 0; lines[i] != NULL; ++i) {
117 printf ("%s\n", lines[i]);
123 /* Unmount everything. */
124 if (guestfs_umount_all (g) == -1)