Convert all TABs-as-indentation to spaces.
[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 #if HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdint.h>
17 #include <inttypes.h>
18 #include <unistd.h>
19 #include <ctype.h>
20
21 #include <guestfs.h>
22
23 /* Note that if any API call fails, we can just exit.  The
24  * standard error handler will have printed the error message
25  * to stderr already.
26  */
27 #define CALL(call,errcode)                      \
28   if ((call) == (errcode)) exit (1);
29
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);
33
34 int
35 main (int argc, char *argv[])
36 {
37   guestfs_h *g;
38   int i;
39
40   if (argc < 2 || access (argv[1], F_OK) != 0) {
41     fprintf (stderr, "Usage: to-xml guest.img [guest.img ...]\n");
42     exit (1);
43   }
44
45   if (!(g = guestfs_create ())) {
46     fprintf (stderr, "Cannot create libguestfs handle.\n");
47     exit (1);
48   }
49
50   for (i = 1; i < argc; ++i)
51     CALL (guestfs_add_drive (g, argv[i]), -1);
52
53   CALL (guestfs_launch (g), -1);
54   CALL (guestfs_wait_ready (g), -1);
55
56   printf ("<guestfs-system>\n");
57
58   /* list-devices should return the devices that we just attached?
59    * Better to find out what the kernel thinks are devices anyway ...
60    */
61   char **devices;
62   CALL (devices = guestfs_list_devices (g), NULL);
63   printf ("<devices>\n");
64   for (i = 0; devices[i] != NULL; ++i) {
65     int64_t size;
66     CALL (size = guestfs_blockdev_getsize64 (g, devices[i]), -1);
67     printf ("<device dev=\"%s\" size=\"%" PRIi64 "\">\n", devices[i], size);
68     display_partitions (g, devices[i]);
69     free (devices[i]);
70     printf ("</device>\n");
71   }
72   free (devices);
73   printf ("</devices>\n");
74
75   /* Now do the same for VGs and LVs.  Note that a VG may span
76    * multiple PVs / block devices, in arbitrary ways, which is
77    * why VGs are in a separate top-level XML class.
78    */
79   char **vgs;
80   char **lvs;
81   printf ("<volgroups>\n");
82   CALL (vgs = guestfs_vgs (g), NULL);
83   CALL (lvs = guestfs_lvs (g), NULL);
84   for (i = 0; vgs[i] != NULL; ++i) {
85     printf ("<volgroup name=\"%s\">\n", vgs[i]);
86
87     /* Just the LVs in this VG. */
88     int len = strlen (vgs[i]);
89     int j;
90     for (j = 0; lvs[j] != NULL; ++j) {
91       if (strncmp (lvs[j], "/dev/", 5) == 0 &&
92           strncmp (&lvs[j][5], vgs[i], len) == 0 &&
93           lvs[j][len+5] == '/') {
94         int64_t size;
95         CALL (size = guestfs_blockdev_getsize64 (g, lvs[j]), -1);
96         printf ("<logvol name=\"%s\" size=\"%" PRIi64 "\">\n", lvs[j], size);
97         display_partition (g, lvs[j]);
98         printf ("</logvol>\n");
99         free (lvs[j]);
100       }
101     }
102
103     free (vgs[i]);
104     printf ("</volgroup>\n");
105   }
106   free (vgs);
107   free (lvs);
108   printf ("</volgroups>\n");
109
110   guestfs_close (g);
111   printf ("</guestfs-system>\n");
112
113   return 0;
114 }
115
116 /* Display a partition or LV. */
117 static void
118 display_partition (guestfs_h *g, const char *dev)
119 {
120   char *what;
121
122   CALL (what = guestfs_file (g, dev), NULL);
123
124   if (strcmp (what, "x86 boot sector") == 0)
125     /* This is what 'file' program shows for Windows/NTFS partitions. */
126     printf ("<windows/>\n");
127   else if (strstr (what, "boot sector") != NULL)
128     display_partitions (g, dev);
129   else if (strncmp (what, "LVM2", 4) == 0)
130     printf ("<physvol/>\n");
131   else if (strstr (what, "ext2 filesystem data") != NULL)
132     display_ext234 (g, dev, "ext2");
133   else if (strstr (what, "ext3 filesystem data") != NULL)
134     display_ext234 (g, dev, "ext3");
135   else if (strstr (what, "ext4 filesystem data") != NULL)
136     display_ext234 (g, dev, "ext4");
137   else if (strstr (what, "Linux/i386 swap file") != NULL)
138     printf ("<linux-swap/>\n");
139   else
140     printf ("<unknown/>\n");
141
142   free (what);
143 }
144
145 /* Display an MBR-formatted boot sector. */
146 static void
147 display_partitions (guestfs_h *g, const char *dev)
148 {
149   /* We can't look into a boot sector which is an LV or partition.
150    * That's a limitation of sorts of the Linux kernel.  (Actually,
151    * we could do this if we add the kpartx program to libguestfs).
152    */
153   if (strncmp (dev, "/dev/sd", 7) != 0 || isdigit (dev[strlen(dev)-1])) {
154     printf ("<vm-image dev=\"%s\"/>\n", dev);
155     return;
156   }
157
158   char **parts;
159   int i, len;
160   CALL (parts = guestfs_list_partitions (g), NULL);
161   printf ("<partitions>\n");
162
163   len = strlen (dev);
164   for (i = 0; parts[i] != NULL; ++i) {
165     /* Only display partition if it's in the device. */
166     if (strncmp (parts[i], dev, len) == 0) {
167       int64_t size;
168       CALL (size = guestfs_blockdev_getsize64 (g, parts[i]), -1);
169       printf ("<partition dev=\"%s\" size=\"%" PRIi64 "\">\n", parts[i], size);
170       display_partition (g, parts[i]);
171       printf ("</partition>\n");
172     }
173
174     free (parts[i]);
175   }
176   free (parts);
177   printf ("</partitions>\n");
178 }
179
180 /* Display some details on the ext2/3/4 filesystem on dev. */
181 static void
182 display_ext234 (guestfs_h *g, const char *dev, const char *fstype)
183 {
184   char **sbfields;
185   int i;
186
187   printf ("<fs type=\"%s\">\n", fstype);
188   CALL (sbfields = guestfs_tune2fs_l (g, dev), NULL);
189
190   for (i = 0; sbfields[i] != NULL; i += 2) {
191     /* Just pick out a few important fields to display.  There
192      * is much more that could be displayed here.
193      */
194     if (strcmp (sbfields[i], "Filesystem UUID") == 0)
195       printf ("<uuid>%s</uuid>\n", sbfields[i+1]);
196     else if (strcmp (sbfields[i], "Block size") == 0)
197       printf ("<blocksize>%s</blocksize>\n", sbfields[i+1]);
198
199     free (sbfields[i]);
200     free (sbfields[i+1]);
201   }
202   free (sbfields);
203
204   printf ("</fs>\n");
205 }