Revert "ci: skip "lib/test-fork-safe-execvpe.sh" on Alpine Linux"
[libnbd.git] / examples / open-qcow2.c
blob70ea9b371b21766378b0fdc3c5e1c49cd314ad07
1 /* This example shows how to use qemu-nbd
2 * to open a local qcow2 file.
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #include <libnbd.h>
11 int
12 main (int argc, const char *argv[])
14 const char *filename;
15 struct nbd_handle *nbd;
16 char buf[512];
17 FILE *fp;
19 if (argc != 2) {
20 fprintf (stderr, "open-qcow2 file.qcow2\n");
21 exit (EXIT_FAILURE);
23 filename = argv[1];
25 /* Create the libnbd handle. */
26 nbd = nbd_create ();
27 if (nbd == NULL) {
28 fprintf (stderr, "%s\n", nbd_get_error ());
29 exit (EXIT_FAILURE);
32 /* Run qemu-nbd as a subprocess using
33 * systemd socket activation.
35 char *args[] = {
36 "qemu-nbd", "-f", "qcow2",
37 (char *) filename,
38 NULL
40 if (nbd_connect_systemd_socket_activation (nbd,
41 args) == -1) {
42 fprintf (stderr, "%s\n", nbd_get_error ());
43 exit (EXIT_FAILURE);
46 /* Read the first sector and print it. */
47 if (nbd_pread (nbd, buf, sizeof buf, 0, 0) == -1) {
48 fprintf (stderr, "%s\n", nbd_get_error ());
49 exit (EXIT_FAILURE);
52 fp = popen ("hexdump -C", "w");
53 if (fp == NULL) {
54 perror ("popen: hexdump");
55 exit (EXIT_FAILURE);
57 fwrite (buf, sizeof buf, 1, fp);
58 pclose (fp);
60 /* Close the libnbd handle. */
61 nbd_close (nbd);
63 exit (EXIT_SUCCESS);