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 /* Add the disk image to libguestfs. */
65 if (guestfs_add_drive_opts (g, "disk.img",
66 GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", /* raw format */
67 GUESTFS_ADD_DRIVE_OPTS_READONLY, 0, /* for write */
68 -1) /* this marks end of optional arguments */
69 == -1)
70 exit (EXIT_FAILURE);
71
72 /* Run the libguestfs back-end. */
73 if (guestfs_launch (g) == -1)
74 exit (EXIT_FAILURE);
75
76 /* Get the list of devices. Because we only added one drive
77 * above, we expect that this list should contain a single
78 * element.
79 */
80 char **devices = guestfs_list_devices (g);
81 if (devices == NULL)
82 exit (EXIT_FAILURE);
83 if (devices[0] == NULL || devices[1] != NULL) {
84 fprintf (stderr, "error: expected a single device from list-devices\n");
85 exit (EXIT_FAILURE);
86 }
87
88 /* Partition the disk as one single MBR partition. */
89 if (guestfs_part_disk (g, devices[0], "mbr") == -1)
90 exit (EXIT_FAILURE);
91
92 /* Get the list of partitions. We expect a single element, which
93 * is the partition we have just created.
94 */
95 char **partitions = guestfs_list_partitions (g);
96 if (partitions == NULL)
97 exit (EXIT_FAILURE);
98 if (partitions[0] == NULL || partitions[1] != NULL) {
99 fprintf (stderr, "error: expected a single partition from list-partitions\n");
100 exit (EXIT_FAILURE);
101 }
102
103 /* Create a filesystem on the partition. */
104 if (guestfs_mkfs (g, "ext4", partitions[0]) == -1)
105 exit (EXIT_FAILURE);
106
107 /* Now mount the filesystem so that we can add files. */
108 if (guestfs_mount (g, partitions[0], "/") == -1)
109 exit (EXIT_FAILURE);
110
111 /* Create some files and directories. */
112 if (guestfs_touch (g, "/empty") == -1)
113 exit (EXIT_FAILURE);
114 const char *message = "Hello, world\n";
115 if (guestfs_write (g, "/hello", message, strlen (message)) == -1)
116 exit (EXIT_FAILURE);
117 if (guestfs_mkdir (g, "/foo") == -1)
118 exit (EXIT_FAILURE);
119
120 /* This one uploads the local file /etc/resolv.conf into
121 * the disk image.
122 */
123 if (guestfs_upload (g, "/etc/resolv.conf", "/foo/resolv.conf") == -1)
124 exit (EXIT_FAILURE);
125
126 /* Because we wrote to the disk and we want to detect write
127 * errors, call guestfs_shutdown. You don't need to do this:
128 * guestfs_close will do it implicitly.
129 */
130 if (guestfs_shutdown (g) == -1)
131 exit (EXIT_FAILURE);
132
133 guestfs_close (g);
134
135 /* Free up the lists. */
136 for (i = 0; devices[i] != NULL; ++i)
137 free (devices[i]);
138 free (devices);
139 for (i = 0; partitions[i] != NULL; ++i)
140 free (partitions[i]);
141 free (partitions);
142
143 exit (EXIT_SUCCESS);
144 }
145
147 /* Inspect a disk image and display operating systems it may contain. */
148
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-erlang(3), guestfs-java(3), guestfs-lua(3),
286 guestfs-ocaml(3), guestfs-perl(3), guestfs-python(3),
287 guestfs-recipes(1), guestfs-ruby(3), http://libguestfs.org/.
288
290 Richard W.M. Jones ("rjones at redhat dot com")
291
293 Copyright (C) 2010-2012 Red Hat Inc.
294
296 This manual page contains examples which we hope you will use in your
297 programs. The examples may be freely copied, modified and distributed
298 for any purpose without any restrictions.
299
301 To get a list of bugs against libguestfs, use this link:
302 https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools
303
304 To report a new bug against libguestfs, use this link:
305 https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools
306
307 When reporting a bug, please supply:
308
309 · The version of libguestfs.
310
311 · Where you got libguestfs (eg. which Linux distro, compiled from
312 source, etc)
313
314 · Describe the bug accurately and give a way to reproduce it.
315
316 · Run libguestfs-test-tool(1) and paste the complete, unedited output
317 into the bug report.
318
319
320
321libguestfs-1.20.11 2013-08-27 guestfs-examples(3)