1 /* This inspects a block device and produces an XML representation of
2 * the partitions, LVM, filesystems that we find there. This could be
3 * useful as example code of how to do this sort of probing, or to
4 * feed the XML to other programs.
7 * to-xml guest.img [guest.img ...]
20 /* Note that if any API call fails, we can just exit. The
21 * standard error handler will have printed the error message
24 #define CALL(call,errcode) \
25 if ((call) == (errcode)) exit (1);
27 static void display_partition (guestfs_h *g, const char *dev);
28 static void display_partitions (guestfs_h *g, const char *dev);
29 static void display_ext234 (guestfs_h *g, const char *dev, const char *fstype);
32 main (int argc, char *argv[])
37 if (argc < 2 || access (argv[1], F_OK) != 0) {
38 fprintf (stderr, "Usage: to-xml guest.img [guest.img ...]\n");
42 if (!(g = guestfs_create ())) {
43 fprintf (stderr, "Cannot create libguestfs handle.\n");
47 for (i = 1; i < argc; ++i)
48 CALL (guestfs_add_drive (g, argv[i]), -1);
50 CALL (guestfs_launch (g), -1);
51 CALL (guestfs_wait_ready (g), -1);
53 printf ("<guestfs-system>\n");
55 /* list-devices should return the devices that we just attached?
56 * Better to find out what the kernel thinks are devices anyway ...
59 CALL (devices = guestfs_list_devices (g), NULL);
60 printf ("<devices>\n");
61 for (i = 0; devices[i] != NULL; ++i) {
63 CALL (size = guestfs_blockdev_getsize64 (g, devices[i]), -1);
64 printf ("<device dev=\"%s\" size=\"%" PRIi64 "\">\n", devices[i], size);
65 display_partitions (g, devices[i]);
67 printf ("</device>\n");
70 printf ("</devices>\n");
72 /* Now do the same for VGs and LVs. Note that a VG may span
73 * multiple PVs / block devices, in arbitrary ways, which is
74 * why VGs are in a separate top-level XML class.
78 printf ("<volgroups>\n");
79 CALL (vgs = guestfs_vgs (g), NULL);
80 CALL (lvs = guestfs_lvs (g), NULL);
81 for (i = 0; vgs[i] != NULL; ++i) {
82 printf ("<volgroup name=\"%s\">\n", vgs[i]);
84 /* Just the LVs in this VG. */
85 int len = strlen (vgs[i]);
87 for (j = 0; lvs[j] != NULL; ++j) {
88 if (strncmp (lvs[j], "/dev/", 5) == 0 &&
89 strncmp (&lvs[j][5], vgs[i], len) == 0 &&
90 lvs[j][len+5] == '/') {
92 CALL (size = guestfs_blockdev_getsize64 (g, lvs[j]), -1);
93 printf ("<logvol name=\"%s\" size=\"%" PRIi64 "\">\n", lvs[j], size);
94 display_partition (g, lvs[j]);
95 printf ("</logvol>\n");
101 printf ("</volgroup>\n");
105 printf ("</volgroups>\n");
108 printf ("</guestfs-system>\n");
113 /* Display a partition or LV. */
115 display_partition (guestfs_h *g, const char *dev)
119 CALL (what = guestfs_file (g, dev), NULL);
121 if (strcmp (what, "x86 boot sector") == 0)
122 /* This is what 'file' program shows for Windows/NTFS partitions. */
123 printf ("<windows/>\n");
124 else if (strstr (what, "boot sector") != NULL)
125 display_partitions (g, dev);
126 else if (strncmp (what, "LVM2", 4) == 0)
127 printf ("<physvol/>\n");
128 else if (strstr (what, "ext2 filesystem data") != NULL)
129 display_ext234 (g, dev, "ext2");
130 else if (strstr (what, "ext3 filesystem data") != NULL)
131 display_ext234 (g, dev, "ext3");
132 else if (strstr (what, "ext4 filesystem data") != NULL)
133 display_ext234 (g, dev, "ext4");
134 else if (strstr (what, "Linux/i386 swap file") != NULL)
135 printf ("<linux-swap/>\n");
137 printf ("<unknown/>\n");
142 /* Display an MBR-formatted boot sector. */
144 display_partitions (guestfs_h *g, const char *dev)
146 /* We can't look into a boot sector which is an LV or partition.
147 * That's a limitation of sorts of the Linux kernel. (Actually,
148 * we could do this if we add the kpartx program to libguestfs).
150 if (strncmp (dev, "/dev/sd", 7) != 0 || isdigit (dev[strlen(dev)-1])) {
151 printf ("<vm-image dev=\"%s\"/>\n", dev);
157 CALL (parts = guestfs_list_partitions (g), NULL);
158 printf ("<partitions>\n");
161 for (i = 0; parts[i] != NULL; ++i) {
162 /* Only display partition if it's in the device. */
163 if (strncmp (parts[i], dev, len) == 0) {
165 CALL (size = guestfs_blockdev_getsize64 (g, parts[i]), -1);
166 printf ("<partition dev=\"%s\" size=\"%" PRIi64 "\">\n", parts[i], size);
167 display_partition (g, parts[i]);
168 printf ("</partition>\n");
174 printf ("</partitions>\n");
177 /* Display some details on the ext2/3/4 filesystem on dev. */
179 display_ext234 (guestfs_h *g, const char *dev, const char *fstype)
184 printf ("<fs type=\"%s\">\n", fstype);
185 CALL (sbfields = guestfs_tune2fs_l (g, dev), NULL);
187 for (i = 0; sbfields[i] != NULL; i += 2) {
188 /* Just pick out a few important fields to display. There
189 * is much more that could be displayed here.
191 if (strcmp (sbfields[i], "Filesystem UUID") == 0)
192 printf ("<uuid>%s</uuid>\n", sbfields[i+1]);
193 else if (strcmp (sbfields[i], "Block size") == 0)
194 printf ("<blocksize>%s</blocksize>\n", sbfields[i+1]);
197 free (sbfields[i+1]);