always include <config.h>
[libguestfs.git] / examples / to-xml.c
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.
5  *
6  * Usage:
7  *   to-xml guest.img [guest.img ...]
8  */
9
10 #include <config.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdint.h>
15 #include <inttypes.h>
16 #include <unistd.h>
17 #include <ctype.h>
18
19 #include <guestfs.h>
20
21 /* Note that if any API call fails, we can just exit.  The
22  * standard error handler will have printed the error message
23  * to stderr already.
24  */
25 #define CALL(call,errcode)                      \
26   if ((call) == (errcode)) exit (1);
27
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);
31
32 int
33 main (int argc, char *argv[])
34 {
35   guestfs_h *g;
36   int i;
37
38   if (argc < 2 || access (argv[1], F_OK) != 0) {
39     fprintf (stderr, "Usage: to-xml guest.img [guest.img ...]\n");
40     exit (1);
41   }
42
43   if (!(g = guestfs_create ())) {
44     fprintf (stderr, "Cannot create libguestfs handle.\n");
45     exit (1);
46   }
47
48   for (i = 1; i < argc; ++i)
49     CALL (guestfs_add_drive (g, argv[i]), -1);
50
51   CALL (guestfs_launch (g), -1);
52   CALL (guestfs_wait_ready (g), -1);
53
54   printf ("<guestfs-system>\n");
55
56   /* list-devices should return the devices that we just attached?
57    * Better to find out what the kernel thinks are devices anyway ...
58    */
59   char **devices;
60   CALL (devices = guestfs_list_devices (g), NULL);
61   printf ("<devices>\n");
62   for (i = 0; devices[i] != NULL; ++i) {
63     int64_t size;
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]);
67     free (devices[i]);
68     printf ("</device>\n");
69   }
70   free (devices);
71   printf ("</devices>\n");
72
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.
76    */
77   char **vgs;
78   char **lvs;
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]);
84
85     /* Just the LVs in this VG. */
86     int len = strlen (vgs[i]);
87     int j;
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] == '/') {
92         int64_t size;
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");
97         free (lvs[j]);
98       }
99     }
100
101     free (vgs[i]);
102     printf ("</volgroup>\n");
103   }
104   free (vgs);
105   free (lvs);
106   printf ("</volgroups>\n");
107
108   guestfs_close (g);
109   printf ("</guestfs-system>\n");
110
111   return 0;
112 }
113
114 /* Display a partition or LV. */
115 static void
116 display_partition (guestfs_h *g, const char *dev)
117 {
118   char *what;
119
120   CALL (what = guestfs_file (g, dev), NULL);
121
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");
137   else
138     printf ("<unknown/>\n");
139
140   free (what);
141 }
142
143 /* Display an MBR-formatted boot sector. */
144 static void
145 display_partitions (guestfs_h *g, const char *dev)
146 {
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).
150    */
151   if (strncmp (dev, "/dev/sd", 7) != 0 || isdigit (dev[strlen(dev)-1])) {
152     printf ("<vm-image dev=\"%s\"/>\n", dev);
153     return;
154   }
155
156   char **parts;
157   int i, len;
158   CALL (parts = guestfs_list_partitions (g), NULL);
159   printf ("<partitions>\n");
160
161   len = strlen (dev);
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) {
165       int64_t size;
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");
170     }
171
172     free (parts[i]);
173   }
174   free (parts);
175   printf ("</partitions>\n");
176 }
177
178 /* Display some details on the ext2/3/4 filesystem on dev. */
179 static void
180 display_ext234 (guestfs_h *g, const char *dev, const char *fstype)
181 {
182   char **sbfields;
183   int i;
184
185   printf ("<fs type=\"%s\">\n", fstype);
186   CALL (sbfields = guestfs_tune2fs_l (g, dev), NULL);
187
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.
191      */
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]);
196
197     free (sbfields[i]);
198     free (sbfields[i+1]);
199   }
200   free (sbfields);
201
202   printf ("</fs>\n");
203 }