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 ...]
21 /* Note that if any API call fails, we can just exit. The
22 * standard error handler will have printed the error message
25 #define CALL(call,errcode) \
26 if ((call) == (errcode)) exit (1);
28 static void display_partition (guestfs_h *g, const char *dev);
29 static void display_partitions (guestfs_h *g, const char *dev);
30 static void display_ext234 (guestfs_h *g, const char *dev, const char *fstype);
33 main (int argc, char *argv[])
38 if (argc < 2 || access (argv[1], F_OK) != 0) {
39 fprintf (stderr, "Usage: to-xml guest.img [guest.img ...]\n");
43 if (!(g = guestfs_create ())) {
44 fprintf (stderr, "Cannot create libguestfs handle.\n");
48 for (i = 1; i < argc; ++i)
49 CALL (guestfs_add_drive (g, argv[i]), -1);
51 CALL (guestfs_launch (g), -1);
52 CALL (guestfs_wait_ready (g), -1);
54 printf ("<guestfs-system>\n");
56 /* list-devices should return the devices that we just attached?
57 * Better to find out what the kernel thinks are devices anyway ...
60 CALL (devices = guestfs_list_devices (g), NULL);
61 printf ("<devices>\n");
62 for (i = 0; devices[i] != NULL; ++i) {
64 CALL (size = guestfs_blockdev_getsize64 (g, devices[i]), -1);
65 printf ("<device dev=\"%s\" size=\"%" PRIi64 "\">\n", devices[i], size);
66 display_partitions (g, devices[i]);
68 printf ("</device>\n");
71 printf ("</devices>\n");
73 /* Now do the same for VGs and LVs. Note that a VG may span
74 * multiple PVs / block devices, in arbitrary ways, which is
75 * why VGs are in a separate top-level XML class.
79 printf ("<volgroups>\n");
80 CALL (vgs = guestfs_vgs (g), NULL);
81 CALL (lvs = guestfs_lvs (g), NULL);
82 for (i = 0; vgs[i] != NULL; ++i) {
83 printf ("<volgroup name=\"%s\">\n", vgs[i]);
85 /* Just the LVs in this VG. */
86 int len = strlen (vgs[i]);
88 for (j = 0; lvs[j] != NULL; ++j) {
89 if (strncmp (lvs[j], "/dev/", 5) == 0 &&
90 strncmp (&lvs[j][5], vgs[i], len) == 0 &&
91 lvs[j][len+5] == '/') {
93 CALL (size = guestfs_blockdev_getsize64 (g, lvs[j]), -1);
94 printf ("<logvol name=\"%s\" size=\"%" PRIi64 "\">\n", lvs[j], size);
95 display_partition (g, lvs[j]);
96 printf ("</logvol>\n");
102 printf ("</volgroup>\n");
106 printf ("</volgroups>\n");
109 printf ("</guestfs-system>\n");
114 /* Display a partition or LV. */
116 display_partition (guestfs_h *g, const char *dev)
120 CALL (what = guestfs_file (g, dev), NULL);
122 if (strcmp (what, "x86 boot sector") == 0)
123 /* This is what 'file' program shows for Windows/NTFS partitions. */
124 printf ("<windows/>\n");
125 else if (strstr (what, "boot sector") != NULL)
126 display_partitions (g, dev);
127 else if (strncmp (what, "LVM2", 4) == 0)
128 printf ("<physvol/>\n");
129 else if (strstr (what, "ext2 filesystem data") != NULL)
130 display_ext234 (g, dev, "ext2");
131 else if (strstr (what, "ext3 filesystem data") != NULL)
132 display_ext234 (g, dev, "ext3");
133 else if (strstr (what, "ext4 filesystem data") != NULL)
134 display_ext234 (g, dev, "ext4");
135 else if (strstr (what, "Linux/i386 swap file") != NULL)
136 printf ("<linux-swap/>\n");
138 printf ("<unknown/>\n");
143 /* Display an MBR-formatted boot sector. */
145 display_partitions (guestfs_h *g, const char *dev)
147 /* We can't look into a boot sector which is an LV or partition.
148 * That's a limitation of sorts of the Linux kernel. (Actually,
149 * we could do this if we add the kpartx program to libguestfs).
151 if (strncmp (dev, "/dev/sd", 7) != 0 || isdigit (dev[strlen(dev)-1])) {
152 printf ("<vm-image dev=\"%s\"/>\n", dev);
158 CALL (parts = guestfs_list_partitions (g), NULL);
159 printf ("<partitions>\n");
162 for (i = 0; parts[i] != NULL; ++i) {
163 /* Only display partition if it's in the device. */
164 if (strncmp (parts[i], dev, len) == 0) {
166 CALL (size = guestfs_blockdev_getsize64 (g, parts[i]), -1);
167 printf ("<partition dev=\"%s\" size=\"%" PRIi64 "\">\n", parts[i], size);
168 display_partition (g, parts[i]);
169 printf ("</partition>\n");
175 printf ("</partitions>\n");
178 /* Display some details on the ext2/3/4 filesystem on dev. */
180 display_ext234 (guestfs_h *g, const char *dev, const char *fstype)
185 printf ("<fs type=\"%s\">\n", fstype);
186 CALL (sbfields = guestfs_tune2fs_l (g, dev), NULL);
188 for (i = 0; sbfields[i] != NULL; i += 2) {
189 /* Just pick out a few important fields to display. There
190 * is much more that could be displayed here.
192 if (strcmp (sbfields[i], "Filesystem UUID") == 0)
193 printf ("<uuid>%s</uuid>\n", sbfields[i+1]);
194 else if (strcmp (sbfields[i], "Block size") == 0)
195 printf ("<blocksize>%s</blocksize>\n", sbfields[i+1]);
198 free (sbfields[i+1]);