1guestfs-examples(3) Virtualization Support guestfs-examples(3)
2
3
4
6 guestfs-examples - Examples of using libguestfs from C
7
9 #include <guestfs.h>
10
11 guestfs_h *g = guestfs_create ();
12 guestfs_add_drive_ro (g, "disk.img");
13 guestfs_launch (g);
14
15 cc prog.c -o prog -lguestfs
16 or:
17 cc prog.c -o prog `pkg-config libguestfs --cflags --libs`
18
20 This manual page contains examples of calling libguestfs from the C
21 programming language. If you are not familiar with using libguestfs,
22 you also need to read guestfs(3).
23
25 /* Example showing how to create a disk image. */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <guestfs.h>
33
34 int
35 main (int argc, char *argv[])
36 {
37 guestfs_h *g;
38 size_t i;
39
40 g = guestfs_create ();
41 if (g == NULL) {
42 perror ("failed to create libguestfs handle");
43 exit (EXIT_FAILURE);
44 }
45
46 /* Create a raw-format sparse disk image, 512 MB in size. */
47 int fd = open ("disk.img", O_CREAT|O_WRONLY|O_TRUNC|O_NOCTTY, 0666);
48 if (fd == -1) {
49 perror ("disk.img");
50 exit (EXIT_FAILURE);
51 }
52 if (ftruncate (fd, 512 * 1024 * 1024) == -1) {
53 perror ("disk.img: truncate");
54 exit (EXIT_FAILURE);
55 }
56 if (close (fd) == -1) {
57 perror ("disk.img: close");
58 exit (EXIT_FAILURE);
59 }
60
61 /* Set the trace flag so that we can see each libguestfs call. */
62 guestfs_set_trace (g, 1);
63
64 /* Set the autosync flag so that the disk will be synchronized
65 * automatically when the libguestfs handle is closed.
66 */
67 guestfs_set_autosync (g, 1);
68
69 /* Add the disk image to libguestfs. */
70 if (guestfs_add_drive_opts (g, "disk.img",
71 GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", /* raw format */
72 GUESTFS_ADD_DRIVE_OPTS_READONLY, 0, /* for write */
73 -1) /* this marks end of optional arguments */
74 == -1)
75 exit (EXIT_FAILURE);
76
77 /* Run the libguestfs back-end. */
78 if (guestfs_launch (g) == -1)
79 exit (EXIT_FAILURE);
80
81 /* Get the list of devices. Because we only added one drive
82 * above, we expect that this list should contain a single
83 * element.
84 */
85 char **devices = guestfs_list_devices (g);
86 if (devices == NULL)
87 exit (EXIT_FAILURE);
88 if (devices[0] == NULL || devices[1] != NULL) {
89 fprintf (stderr, "error: expected a single device from list-devices\n");
90 exit (EXIT_FAILURE);
91 }
92
93 /* Partition the disk as one single MBR partition. */
94 if (guestfs_part_disk (g, devices[0], "mbr") == -1)
95 exit (EXIT_FAILURE);
96
97 /* Get the list of partitions. We expect a single element, which
98 * is the partition we have just created.
99 */
100 char **partitions = guestfs_list_partitions (g);
101 if (partitions == NULL)
102 exit (EXIT_FAILURE);
103 if (partitions[0] == NULL || partitions[1] != NULL) {
104 fprintf (stderr, "error: expected a single partition from list-partitions\n");
105 exit (EXIT_FAILURE);
106 }
107
108 /* Create a filesystem on the partition. */
109 if (guestfs_mkfs (g, "ext4", partitions[0]) == -1)
110 exit (EXIT_FAILURE);
111
112 /* Now mount the filesystem so that we can add files. */
113 if (guestfs_mount_options (g, "", partitions[0], "/") == -1)
114 exit (EXIT_FAILURE);
115
116 /* Create some files and directories. */
117 if (guestfs_touch (g, "/empty") == -1)
118 exit (EXIT_FAILURE);
119 const char *message = "Hello, world\n";
120 if (guestfs_write (g, "/hello", message, strlen (message)) == -1)
121 exit (EXIT_FAILURE);
122 if (guestfs_mkdir (g, "/foo") == -1)
123 exit (EXIT_FAILURE);
124
125 /* This one uploads the local file /etc/resolv.conf into
126 * the disk image.
127 */
128 if (guestfs_upload (g, "/etc/resolv.conf", "/foo/resolv.conf") == -1)
129 exit (EXIT_FAILURE);
130
131 /* Because 'autosync' was set (above) we can just close the handle
132 * and the disk contents will be synchronized. You can also do
133 * this manually by calling guestfs_umount_all and guestfs_sync.
134 */
135 guestfs_close (g);
136
137 /* Free up the lists. */
138 for (i = 0; devices[i] != NULL; ++i)
139 free (devices[i]);
140 free (devices);
141 for (i = 0; partitions[i] != NULL; ++i)
142 free (partitions[i]);
143 free (partitions);
144
145 exit (EXIT_SUCCESS);
146 }
147
149 #include <stdio.h>
150 #include <stdlib.h>
151 #include <string.h>
152 #include <guestfs.h>
153
154 static int
155 compare_keys_len (const void *p1, const void *p2)
156 {
157 const char *key1 = * (char * const *) p1;
158 const char *key2 = * (char * const *) p2;
159 return strlen (key1) - strlen (key2);
160 }
161
162 static size_t
163 count_strings (char *const *argv)
164 {
165 size_t c;
166
167 for (c = 0; argv[c]; ++c)
168 ;
169 return c;
170 }
171
172 int
173 main (int argc, char *argv[])
174 {
175 guestfs_h *g;
176 const char *disk;
177 char **roots, *root, *str, **mountpoints, **lines;
178 size_t i, j;
179
180 if (argc != 2) {
181 fprintf (stderr, "usage: inspect_vm disk.img\n");
182 exit (EXIT_FAILURE);
183 }
184 disk = argv[1];
185
186 g = guestfs_create ();
187 if (g == NULL) {
188 perror ("failed to create libguestfs handle");
189 exit (EXIT_FAILURE);
190 }
191
192 /* Attach the disk image read-only to libguestfs. */
193 if (guestfs_add_drive_opts (g, disk,
194 /* GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", */
195 GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
196 -1) /* this marks end of optional arguments */
197 == -1)
198 exit (EXIT_FAILURE);
199
200 /* Run the libguestfs back-end. */
201 if (guestfs_launch (g) == -1)
202 exit (EXIT_FAILURE);
203
204 /* Ask libguestfs to inspect for operating systems. */
205 roots = guestfs_inspect_os (g);
206 if (roots == NULL)
207 exit (EXIT_FAILURE);
208 if (roots[0] == NULL) {
209 fprintf (stderr, "inspect_vm: no operating systems found\n");
210 exit (EXIT_FAILURE);
211 }
212
213 for (j = 0; roots[j] != NULL; ++j) {
214 root = roots[j];
215
216 printf ("Root device: %s\n", root);
217
218 /* Print basic information about the operating system. */
219 str = guestfs_inspect_get_product_name (g, root);
220 if (str)
221 printf (" Product name: %s\n", str);
222 free (str);
223
224 printf (" Version: %d.%d\n",
225 guestfs_inspect_get_major_version (g, root),
226 guestfs_inspect_get_minor_version (g, root));
227
228 str = guestfs_inspect_get_type (g, root);
229 if (str)
230 printf (" Type: %s\n", str);
231 free (str);
232 str = guestfs_inspect_get_distro (g, root);
233 if (str)
234 printf (" Distro: %s\n", str);
235 free (str);
236
237 /* Mount up the disks, like guestfish -i.
238 *
239 * Sort keys by length, shortest first, so that we end up
240 * mounting the filesystems in the correct order.
241 */
242 mountpoints = guestfs_inspect_get_mountpoints (g, root);
243 if (mountpoints == NULL)
244 exit (EXIT_FAILURE);
245
246 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
247 compare_keys_len);
248 for (i = 0; mountpoints[i] != NULL; i += 2) {
249 /* Ignore failures from this call, since bogus entries can
250 * appear in the guest's /etc/fstab.
251 */
252 guestfs_mount_ro (g, mountpoints[i+1], mountpoints[i]);
253 free (mountpoints[i]);
254 free (mountpoints[i+1]);
255 }
256 free (mountpoints);
257
258 /* If /etc/issue.net file exists, print up to 3 lines. */
259 if (guestfs_is_file (g, "/etc/issue.net") > 0) {
260 printf ("--- /etc/issue.net ---\n");
261 lines = guestfs_head_n (g, 3, "/etc/issue.net");
262 if (lines == NULL)
263 exit (EXIT_FAILURE);
264 for (i = 0; lines[i] != NULL; ++i) {
265 printf ("%s\n", lines[i]);
266 free (lines[i]);
267 }
268 free (lines);
269 }
270
271 /* Unmount everything. */
272 if (guestfs_umount_all (g) == -1)
273 exit (EXIT_FAILURE);
274
275 free (root);
276 }
277 free (roots);
278
279 guestfs_close (g);
280
281 exit (EXIT_SUCCESS);
282 }
283
285 guestfs(3), guestfs-ocaml(3), guestfs-python(3), guestfs-ruby(3),
286 <http://libguestfs.org/>.
287
289 Richard W.M. Jones ("rjones at redhat dot com")
290
292 Copyright (C) 2010 Red Hat Inc. <http://libguestfs.org/>
293
294 The examples in this manual page may be freely copied, modified and
295 distributed without any restrictions.
296
297 This library is free software; you can redistribute it and/or modify it
298 under the terms of the GNU Lesser General Public License as published
299 by the Free Software Foundation; either version 2 of the License, or
300 (at your option) any later version.
301
302 This library is distributed in the hope that it will be useful, but
303 WITHOUT ANY WARRANTY; without even the implied warranty of
304 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
305 Lesser General Public License for more details.
306
307 You should have received a copy of the GNU Lesser General Public
308 License along with this library; if not, write to the Free Software
309 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
310 02110-1301 USA
311
312
313
314libguestfs-1.8.15 2011-11-10 guestfs-examples(3)