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 Note that libnbd currently enforces a maximum read buffer of 64MiB,
24 even if the server would permit a larger buffer in a single
25 transaction; attempts to exceed this will result in an "ERANGE" error.
26 The server may enforce a smaller limit, which can be learned with
27 nbd_get_block_size(3).
28
29 The "flags" parameter must be 0 for now (it exists for future NBD
30 protocol extensions).
31
32 Note that if this command fails, and nbd_get_pread_initialize(3)
33 returns true, then libnbd sanitized "buf", but it is unspecified
34 whether the contents of "buf" will read as zero or as partial results
35 from the server. If nbd_get_pread_initialize(3) returns false, then
36 libnbd did not sanitize "buf", and the contents are undefined on
37 failure.
38
39 By default, libnbd will reject attempts to use this function with
40 parameters that are likely to result in server failure, such as
41 requesting an unknown command flag. The nbd_set_strict_mode(3)
42 function can be used to alter which scenarios should await a server
43 reply rather than failing fast.
44
46 If the call is successful the function returns 0.
47
49 On error "-1" is returned.
50
51 Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
52 of the error.
53
55 The handle must be connected with the server, otherwise this call will
56 return an error.
57
59 This function first appeared in libnbd 1.0.
60
61 If you need to test if this function is available at compile time check
62 if the following macro is defined:
63
64 #define LIBNBD_HAVE_NBD_PREAD 1
65
67 This example is also available as examples/fetch-first-sector.c in the
68 libnbd source code.
69
70 /* This example shows how to connect to an NBD server
71 * and fetch and print the first sector (usually the
72 * boot sector or partition table or filesystem
73 * superblock).
74 *
75 * You can test it with nbdkit like this:
76 *
77 * nbdkit -U - floppy . \
78 * --run './fetch-first-sector $unixsocket'
79 *
80 * The nbdkit floppy plugin creates an MBR disk so the
81 * first sector is the partition table.
82 */
83
84 #include <stdio.h>
85 #include <stdlib.h>
86
87 #include <libnbd.h>
88
89 int
90 main (int argc, char *argv[])
91 {
92 struct nbd_handle *nbd;
93 char buf[512];
94 FILE *pp;
95
96 if (argc != 2) {
97 fprintf (stderr, "%s socket\n", argv[0]);
98 exit (EXIT_FAILURE);
99 }
100
101 /* Create the libnbd handle. */
102 nbd = nbd_create ();
103 if (nbd == NULL) {
104 fprintf (stderr, "%s\n", nbd_get_error ());
105 exit (EXIT_FAILURE);
106 }
107
108 /* Connect to the NBD server over a
109 * Unix domain socket.
110 */
111 if (nbd_connect_unix (nbd, argv[1]) == -1) {
112 fprintf (stderr, "%s\n", nbd_get_error ());
113 exit (EXIT_FAILURE);
114 }
115
116 /* Read the first sector synchronously. */
117 if (nbd_pread (nbd, buf, sizeof buf, 0, 0) == -1) {
118 fprintf (stderr, "%s\n", nbd_get_error ());
119 exit (EXIT_FAILURE);
120 }
121
122 /* Close the libnbd handle. */
123 nbd_close (nbd);
124
125 /* Print the first sector. */
126 pp = popen ("hexdump -C", "w");
127 if (pp == NULL) {
128 perror ("popen: hexdump");
129 exit (EXIT_FAILURE);
130 }
131 fwrite (buf, sizeof buf, 1, pp);
132 pclose (pp);
133
134 exit (EXIT_SUCCESS);
135 }
136
138 nbd_aio_pread(3), nbd_create(3), nbd_get_block_size(3),
139 nbd_get_pread_initialize(3), nbd_pread_structured(3),
140 nbd_set_pread_initialize(3), nbd_set_strict_mode(3), libnbd(3).
141
143 Eric Blake
144
145 Richard W.M. Jones
146
148 Copyright (C) 2019-2021 Red Hat Inc.
149
151 This library is free software; you can redistribute it and/or modify it
152 under the terms of the GNU Lesser General Public License as published
153 by the Free Software Foundation; either version 2 of the License, or
154 (at your option) any later version.
155
156 This library is distributed in the hope that it will be useful, but
157 WITHOUT ANY WARRANTY; without even the implied warranty of
158 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
159 Lesser General Public License for more details.
160
161 You should have received a copy of the GNU Lesser General Public
162 License along with this library; if not, write to the Free Software
163 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
164 02110-1301 USA
165
166
167
168libnbd-1.12.5 2022-07-10 nbd_pread(3)