1nbd_get_size(3) LIBNBD nbd_get_size(3)
2
3
4
6 nbd_get_size - return the export size
7
9 #include <libnbd.h>
10
11 int64_t nbd_get_size (
12 struct nbd_handle *h
13 );
14
16 Returns the size in bytes of the NBD export.
17
18 Note that this call fails with "EOVERFLOW" for an unlikely server that
19 advertises a size which cannot fit in a 64-bit signed integer.
20
21 This call does not block, because it returns data that is saved in the
22 handle from the NBD protocol handshake.
23
25 This call returns a 64 bit signed integer ≥ 0.
26
28 On error -1 is returned.
29
30 Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
31 of the error.
32
33 The following parameters must not be NULL: "h". For more information
34 see "Non-NULL parameters" in libnbd(3).
35
37 The handle must be negotiating, or connected with the server, or shut
38 down, otherwise this call will return an error.
39
41 This function first appeared in libnbd 1.0.
42
43 If you need to test if this function is available at compile time check
44 if the following macro is defined:
45
46 #define LIBNBD_HAVE_NBD_GET_SIZE 1
47
49 This example is also available as examples/get-size.c in the libnbd
50 source code.
51
52 /* This example shows how to connect to an NBD
53 * server and read the size of the disk.
54 *
55 * You can test it with nbdkit like this:
56 *
57 * nbdkit -U - memory 1M \
58 * --run './get-size $unixsocket'
59 */
60
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <stdint.h>
64 #include <inttypes.h>
65
66 #include <libnbd.h>
67
68 int
69 main (int argc, char *argv[])
70 {
71 struct nbd_handle *nbd;
72 int64_t size;
73
74 if (argc != 2) {
75 fprintf (stderr, "%s socket\n", argv[0]);
76 exit (EXIT_FAILURE);
77 }
78
79 /* Create the libnbd handle. */
80 nbd = nbd_create ();
81 if (nbd == NULL) {
82 fprintf (stderr, "%s\n", nbd_get_error ());
83 exit (EXIT_FAILURE);
84 }
85
86 /* Connect to the NBD server over a
87 * Unix domain socket.
88 */
89 if (nbd_connect_unix (nbd, argv[1]) == -1) {
90 fprintf (stderr, "%s\n", nbd_get_error ());
91 exit (EXIT_FAILURE);
92 }
93
94 /* Read the size in bytes and print it. */
95 size = nbd_get_size (nbd);
96 if (size == -1) {
97 fprintf (stderr, "%s\n", nbd_get_error ());
98 exit (EXIT_FAILURE);
99 }
100 printf ("%s: size = %" PRIi64 " bytes\n",
101 argv[1], size);
102
103 /* Close the libnbd handle. */
104 nbd_close (nbd);
105
106 exit (EXIT_SUCCESS);
107 }
108
110 nbd_create(3), nbd_opt_info(3), "Size of the export" in libnbd(3),
111 libnbd(3).
112
114 Eric Blake
115
116 Richard W.M. Jones
117
119 Copyright Red Hat
120
122 This library is free software; you can redistribute it and/or modify it
123 under the terms of the GNU Lesser General Public License as published
124 by the Free Software Foundation; either version 2 of the License, or
125 (at your option) any later version.
126
127 This library is distributed in the hope that it will be useful, but
128 WITHOUT ANY WARRANTY; without even the implied warranty of
129 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
130 Lesser General Public License for more details.
131
132 You should have received a copy of the GNU Lesser General Public
133 License along with this library; if not, write to the Free Software
134 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
135 02110-1301 USA
136
137
138
139libnbd-1.16.5 2023-09-26 nbd_get_size(3)