1nbd_pread(3) LIBNBD nbd_pread(3)
2
3
4
6 nbd_pread - read from the NBD server
7
9 #include <libnbd.h>
10
11 int nbd_pread (struct nbd_handle *h, void *buf,
12 size_t count, uint64_t offset,
13 uint32_t flags);
14
16 Issue a read command to the NBD server for the range starting at
17 "offset" and ending at "offset" + "count" - 1. NBD can only read all
18 or nothing using this call. The call returns when the data has been
19 read fully into "buf" or there is an error. See also
20 nbd_pread_structured(3), if finer visibility is required into the
21 server's replies, or if you want to use "LIBNBD_CMD_FLAG_DF".
22
23 The "flags" parameter must be 0 for now (it exists for future NBD
24 protocol extensions).
25
27 If the call is successful the function returns 0.
28
30 On error "-1" is returned.
31
32 Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
33 of the error.
34
36 The handle must be connected and finished handshaking with the server,
37 otherwise this call will return an error.
38
40 This function first appeared in libnbd 1.0.
41
42 If you need to test if this function is available at compile time check
43 if the following macro is defined:
44
45 #define LIBNBD_HAVE_NBD_PREAD 1
46
48 This example is also available as examples/fetch-first-sector.c in the
49 libnbd source code.
50
51 /* This example shows how to connect to an NBD server
52 * and fetch and print the first sector (usually the
53 * boot sector or partition table or filesystem
54 * superblock).
55 *
56 * You can test it with nbdkit like this:
57 *
58 * nbdkit -U - floppy . \
59 * --run './fetch-first-sector $unixsocket'
60 *
61 * The nbdkit floppy plugin creates an MBR disk so the
62 * first sector is the partition table.
63 */
64
65 #include <stdio.h>
66 #include <stdlib.h>
67
68 #include <libnbd.h>
69
70 int
71 main (int argc, char *argv[])
72 {
73 struct nbd_handle *nbd;
74 char buf[512];
75 FILE *pp;
76
77 if (argc != 2) {
78 fprintf (stderr, "%s socket\n", argv[0]);
79 exit (EXIT_FAILURE);
80 }
81
82 /* Create the libnbd handle. */
83 nbd = nbd_create ();
84 if (nbd == NULL) {
85 fprintf (stderr, "%s\n", nbd_get_error ());
86 exit (EXIT_FAILURE);
87 }
88
89 /* Connect to the NBD server over a
90 * Unix domain socket.
91 */
92 if (nbd_connect_unix (nbd, argv[1]) == -1) {
93 fprintf (stderr, "%s\n", nbd_get_error ());
94 exit (EXIT_FAILURE);
95 }
96
97 /* Read the first sector synchronously. */
98 if (nbd_pread (nbd, buf, sizeof buf, 0, 0) == -1) {
99 fprintf (stderr, "%s\n", nbd_get_error ());
100 exit (EXIT_FAILURE);
101 }
102
103 /* Close the libnbd handle. */
104 nbd_close (nbd);
105
106 /* Print the first sector. */
107 pp = popen ("hexdump -C", "w");
108 if (pp == NULL) {
109 perror ("popen: hexdump");
110 exit (EXIT_FAILURE);
111 }
112 fwrite (buf, sizeof buf, 1, pp);
113 pclose (pp);
114
115 exit (EXIT_SUCCESS);
116 }
117
119 nbd_aio_pread(3), nbd_pread_structured(3), nbd_create(3), libnbd(3).
120
122 Eric Blake
123
124 Richard W.M. Jones
125
127 Copyright (C) 2019 Red Hat Inc.
128
130 This library is free software; you can redistribute it and/or modify it
131 under the terms of the GNU Lesser General Public License as published
132 by the Free Software Foundation; either version 2 of the License, or
133 (at your option) any later version.
134
135 This library is distributed in the hope that it will be useful, but
136 WITHOUT ANY WARRANTY; without even the implied warranty of
137 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
138 Lesser General Public License for more details.
139
140 You should have received a copy of the GNU Lesser General Public
141 License along with this library; if not, write to the Free Software
142 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
143 02110-1301 USA
144
145
146
147libnbd-1.2.1 2019-11-14 nbd_pread(3)