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 connected and finished handshaking with the server,
30 or shut 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), "Size of the export" in libnbd(3), libnbd(3).
103
105 Eric Blake
106
107 Richard W.M. Jones
108
110 Copyright (C) 2019 Red Hat Inc.
111
113 This library is free software; you can redistribute it and/or modify it
114 under the terms of the GNU Lesser General Public License as published
115 by the Free Software Foundation; either version 2 of the License, or
116 (at your option) any later version.
117
118 This library is distributed in the hope that it will be useful, but
119 WITHOUT ANY WARRANTY; without even the implied warranty of
120 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
121 Lesser General Public License for more details.
122
123 You should have received a copy of the GNU Lesser General Public
124 License along with this library; if not, write to the Free Software
125 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
126 02110-1301 USA
127
128
129
130libnbd-1.3.7 2020-04-23 nbd_get_size(3)