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 ...]
23 /* Note that if any API call fails, we can just exit. The
24 * standard error handler will have printed the error message
27 #define CALL(call,errcode) \
28 if ((call) == (errcode)) exit (1);
30 static void display_partition (guestfs_h *g, const char *dev);
31 static void display_partitions (guestfs_h *g, const char *dev);
32 static void display_ext234 (guestfs_h *g, const char *dev, const char *fstype);
35 main (int argc, char *argv[])
40 if (argc < 2 || access (argv[1], F_OK) != 0) {
41 fprintf (stderr, "Usage: to-xml guest.img [guest.img ...]\n");
45 if (!(g = guestfs_create ())) {
46 fprintf (stderr, "Cannot create libguestfs handle.\n");
50 for (i = 1; i < argc; ++i)
51 CALL (guestfs_add_drive (g, argv[i]), -1);
53 CALL (guestfs_launch (g), -1);
55 printf ("<guestfs-system>\n");
57 /* list-devices should return the devices that we just attached?
58 * Better to find out what the kernel thinks are devices anyway ...
61 CALL (devices = guestfs_list_devices (g), NULL);
62 printf ("<devices>\n");
63 for (i = 0; devices[i] != NULL; ++i) {
65 CALL (size = guestfs_blockdev_getsize64 (g, devices[i]), -1);
66 printf ("<device dev=\"%s\" size=\"%" PRIi64 "\">\n", devices[i], size);
67 display_partitions (g, devices[i]);
69 printf ("</device>\n");
72 printf ("</devices>\n");
74 /* Now do the same for VGs and LVs. Note that a VG may span
75 * multiple PVs / block devices, in arbitrary ways, which is
76 * why VGs are in a separate top-level XML class.
80 printf ("<volgroups>\n");
81 CALL (vgs = guestfs_vgs (g), NULL);
82 CALL (lvs = guestfs_lvs (g), NULL);
83 for (i = 0; vgs[i] != NULL; ++i) {
84 printf ("<volgroup name=\"%s\">\n", vgs[i]);
86 /* Just the LVs in this VG. */
87 int len = strlen (vgs[i]);
89 for (j = 0; lvs[j] != NULL; ++j) {
90 if (strncmp (lvs[j], "/dev/", 5) == 0 &&
91 strncmp (&lvs[j][5], vgs[i], len) == 0 &&
92 lvs[j][len+5] == '/') {
94 CALL (size = guestfs_blockdev_getsize64 (g, lvs[j]), -1);
95 printf ("<logvol name=\"%s\" size=\"%" PRIi64 "\">\n", lvs[j], size);
96 display_partition (g, lvs[j]);
97 printf ("</logvol>\n");
103 printf ("</volgroup>\n");
107 printf ("</volgroups>\n");
110 printf ("</guestfs-system>\n");
115 /* Display a partition or LV. */
117 display_partition (guestfs_h *g, const char *dev)
121 CALL (what = guestfs_file (g, dev), NULL);
123 if (strcmp (what, "x86 boot sector") == 0)
124 /* This is what 'file' program shows for Windows/NTFS partitions. */
125 printf ("<windows/>\n");
126 else if (strstr (what, "boot sector") != NULL)
127 display_partitions (g, dev);
128 else if (strncmp (what, "LVM2", 4) == 0)
129 printf ("<physvol/>\n");
130 else if (strstr (what, "ext2 filesystem data") != NULL)
131 display_ext234 (g, dev, "ext2");
132 else if (strstr (what, "ext3 filesystem data") != NULL)
133 display_ext234 (g, dev, "ext3");
134 else if (strstr (what, "ext4 filesystem data") != NULL)
135 display_ext234 (g, dev, "ext4");
136 else if (strstr (what, "Linux/i386 swap file") != NULL)
137 printf ("<linux-swap/>\n");
139 printf ("<unknown/>\n");
144 /* Display an MBR-formatted boot sector. */
146 display_partitions (guestfs_h *g, const char *dev)
148 /* We can't look into a boot sector which is an LV or partition.
149 * That's a limitation of sorts of the Linux kernel. (Actually,
150 * we could do this if we add the kpartx program to libguestfs).
152 if (strncmp (dev, "/dev/sd", 7) != 0 || c_isdigit (dev[strlen(dev)-1])) {
153 printf ("<vm-image dev=\"%s\"/>\n", dev);
159 CALL (parts = guestfs_list_partitions (g), NULL);
160 printf ("<partitions>\n");
163 for (i = 0; parts[i] != NULL; ++i) {
164 /* Only display partition if it's in the device. */
165 if (strncmp (parts[i], dev, len) == 0) {
167 CALL (size = guestfs_blockdev_getsize64 (g, parts[i]), -1);
168 printf ("<partition dev=\"%s\" size=\"%" PRIi64 "\">\n", parts[i], size);
169 display_partition (g, parts[i]);
170 printf ("</partition>\n");
176 printf ("</partitions>\n");
179 /* Display some details on the ext2/3/4 filesystem on dev. */
181 display_ext234 (guestfs_h *g, const char *dev, const char *fstype)
186 printf ("<fs type=\"%s\">\n", fstype);
187 CALL (sbfields = guestfs_tune2fs_l (g, dev), NULL);
189 for (i = 0; sbfields[i] != NULL; i += 2) {
190 /* Just pick out a few important fields to display. There
191 * is much more that could be displayed here.
193 if (strcmp (sbfields[i], "Filesystem UUID") == 0)
194 printf ("<uuid>%s</uuid>\n", sbfields[i+1]);
195 else if (strcmp (sbfields[i], "Block size") == 0)
196 printf ("<blocksize>%s</blocksize>\n", sbfields[i+1]);
199 free (sbfields[i+1]);