-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathopen-qcow2.c
64 lines (54 loc) · 1.3 KB
/
open-qcow2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* This example shows how to use qemu-nbd
* to open a local qcow2 file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libnbd.h>
int
main (int argc, const char *argv[])
{
const char *filename;
struct nbd_handle *nbd;
char buf[512];
FILE *fp;
if (argc != 2) {
fprintf (stderr, "open-qcow2 file.qcow2\n");
exit (EXIT_FAILURE);
}
filename = argv[1];
/* Create the libnbd handle. */
nbd = nbd_create ();
if (nbd == NULL) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
/* Run qemu-nbd as a subprocess using
* systemd socket activation.
*/
char *args[] = {
"qemu-nbd", "-f", "qcow2",
(char *) filename,
NULL
};
if (nbd_connect_systemd_socket_activation (nbd,
args) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
/* Read the first sector and print it. */
if (nbd_pread (nbd, buf, sizeof buf, 0, 0) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
fp = popen ("hexdump -C", "w");
if (fp == NULL) {
perror ("popen: hexdump");
exit (EXIT_FAILURE);
}
fwrite (buf, sizeof buf, 1, fp);
pclose (fp);
/* Close the libnbd handle. */
nbd_close (nbd);
exit (EXIT_SUCCESS);
}