1nbd_get_size(3)                     LIBNBD                     nbd_get_size(3)
2
3
4

NAME

6       nbd_get_size - return the export size
7

SYNOPSIS

9        #include <libnbd.h>
10
11        int64_t nbd_get_size (struct nbd_handle *h);
12

DESCRIPTION

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

RETURN VALUE

20       This call returns a 64 bit signed integer ≥ 0.
21

ERRORS

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
28       The following parameters must not be NULL: "h".  For more information
29       see "Non-NULL parameters" in libnbd(3).
30

HANDLE STATE

32       The handle must be negotiating, or connected with the server, or shut
33       down, otherwise this call will return an error.
34

VERSION

36       This function first appeared in libnbd 1.0.
37
38       If you need to test if this function is available at compile time check
39       if the following macro is defined:
40
41        #define LIBNBD_HAVE_NBD_GET_SIZE 1
42

EXAMPLE

44       This example is also available as examples/get-size.c in the libnbd
45       source code.
46
47        /* This example shows how to connect to an NBD
48         * server and read the size of the disk.
49         *
50         * You can test it with nbdkit like this:
51         *
52         * nbdkit -U - memory 1M \
53         *   --run './get-size $unixsocket'
54         */
55
56        #include <stdio.h>
57        #include <stdlib.h>
58        #include <stdint.h>
59        #include <inttypes.h>
60
61        #include <libnbd.h>
62
63        int
64        main (int argc, char *argv[])
65        {
66          struct nbd_handle *nbd;
67          int64_t size;
68
69          if (argc != 2) {
70            fprintf (stderr, "%s socket\n", argv[0]);
71            exit (EXIT_FAILURE);
72          }
73
74          /* Create the libnbd handle. */
75          nbd = nbd_create ();
76          if (nbd == NULL) {
77            fprintf (stderr, "%s\n", nbd_get_error ());
78            exit (EXIT_FAILURE);
79          }
80
81          /* Connect to the NBD server over a
82           * Unix domain socket.
83           */
84          if (nbd_connect_unix (nbd, argv[1]) == -1) {
85            fprintf (stderr, "%s\n", nbd_get_error ());
86            exit (EXIT_FAILURE);
87          }
88
89          /* Read the size in bytes and print it. */
90          size = nbd_get_size (nbd);
91          if (size == -1) {
92            fprintf (stderr, "%s\n", nbd_get_error ());
93            exit (EXIT_FAILURE);
94          }
95          printf ("%s: size = %" PRIi64 " bytes\n",
96                  argv[1], size);
97
98          /* Close the libnbd handle. */
99          nbd_close (nbd);
100
101          exit (EXIT_SUCCESS);
102        }
103

SEE ALSO

105       nbd_create(3), nbd_opt_info(3), "Size of the export" in libnbd(3),
106       libnbd(3).
107

AUTHORS

109       Eric Blake
110
111       Richard W.M. Jones
112
114       Copyright (C) 2019-2021 Red Hat Inc.
115

LICENSE

117       This library is free software; you can redistribute it and/or modify it
118       under the terms of the GNU Lesser General Public License as published
119       by the Free Software Foundation; either version 2 of the License, or
120       (at your option) any later version.
121
122       This library is distributed in the hope that it will be useful, but
123       WITHOUT ANY WARRANTY; without even the implied warranty of
124       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
125       Lesser General Public License for more details.
126
127       You should have received a copy of the GNU Lesser General Public
128       License along with this library; if not, write to the Free Software
129       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
130       02110-1301 USA
131
132
133
134libnbd-1.14.2                     2023-01-03                   nbd_get_size(3)
Impressum