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