2 * Open connection for network block device
4 * Copyright 1997,1998 Pavel Machek, distribute under GPL
5 * <pavel@atrey.karlin.mff.cuni.cz>
6 * Copyright (c) 2002 - 2011 Wouter Verhelst <w@uter.be>
8 * Version 1.0 - 64bit issues should be fixed, now
9 * Version 1.1 - added bs (blocksize) option (Alexey Guzeev, aga@permonline.ru)
10 * Version 1.2 - I added new option '-d' to send the disconnect request
11 * Version 2.0 - Version synchronised with server
12 * Version 2.1 - Check for disconnection before INIT_PASSWD is received
13 * to make errormsg a bit more helpful in case the server can't
14 * open the exported file.
15 * 16/03/2010 - Add IPv6 support.
16 * Kitt Tientanopajai <kitt@kitty.in.th>
17 * Neutron Soutmun <neo.neutron@gmail.com>
18 * Suriya Soutmun <darksolar@gmail.com>
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
28 #include <netinet/tcp.h>
29 #include <netinet/in.h>
35 #include <sys/mount.h>
41 #include <linux/ioctl.h>
42 #define MY_NAME "nbd_client"
49 int check_conn(char* devname, int do_print) {
55 if(!strncmp(devname, "/dev/", 5)) {
58 if((p=strchr(devname, 'p'))) {
59 /* We can't do checks on partitions. */
62 snprintf(buf, 256, "/sys/block/%s/pid", devname);
63 if((fd=open(buf, O_RDONLY))<0) {
70 len=read(fd, buf, 256);
72 if(do_print) printf("%s\n", buf);
76 int opennet(char *name, char* portstr, int sdp) {
78 struct addrinfo hints;
79 struct addrinfo *ai = NULL;
80 struct addrinfo *rp = NULL;
83 memset(&hints,'\0',sizeof(hints));
84 hints.ai_family = AF_UNSPEC;
85 hints.ai_socktype = SOCK_STREAM;
86 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
87 hints.ai_protocol = IPPROTO_TCP;
89 e = getaddrinfo(name, portstr, &hints, &ai);
92 fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(e));
99 if (ai->ai_family == AF_INET)
100 ai->ai_family = AF_INET_SDP;
101 else (ai->ai_family == AF_INET6)
102 ai->ai_family = AF_INET6_SDP;
104 err("Can't do SDP: I was not compiled with SDP support!");
108 for(rp = ai; rp != NULL; rp = rp->ai_next) {
109 sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
112 continue; /* error */
114 if(connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
119 err("Socket failed: %m");
127 void negotiate(int sock, u64 *rsize64, u32 *flags, char* name) {
130 char buf[256] = "\0\0\0\0\0\0\0\0\0";
132 printf("Negotiation: ");
133 if (read(sock, buf, 8) < 0)
136 err("Server closed connection");
137 if (strcmp(buf, INIT_PASSWD))
138 err("INIT_PASSWD bad");
140 if (read(sock, &magic, sizeof(magic)) < 0)
142 magic = ntohll(magic);
146 uint32_t reserved = 0;
148 if (magic != opts_magic)
149 err("Not enough opts_magic");
151 if(read(sock, &tmp, sizeof(uint16_t)) < 0) {
152 err("Failed reading flags: %m");
154 *flags = ((u32)ntohs(tmp));
156 /* reserved for future use*/
157 if (write(sock, &reserved, sizeof(reserved)) < 0)
158 err("Failed/2.1: %m");
160 /* Write the export name that we're after */
161 magic = ntohll(opts_magic);
162 if (write(sock, &magic, sizeof(magic)) < 0)
163 err("Failed/2.2: %m");
164 opt = ntohl(NBD_OPT_EXPORT_NAME);
165 if (write(sock, &opt, sizeof(opt)) < 0)
166 err("Failed/2.3: %m");
167 namesize = (u32)strlen(name);
168 namesize = ntohl(namesize);
169 if (write(sock, &namesize, sizeof(namesize)) < 0)
170 err("Failed/2.4: %m");
171 if (write(sock, name, strlen(name)) < 0)
172 err("Failed/2.4: %m");
174 if (magic != cliserv_magic)
175 err("Not enough cliserv_magic");
179 if (read(sock, &size64, sizeof(size64)) < 0)
180 err("Failed/3: %m\n");
181 size64 = ntohll(size64);
183 #ifdef NBD_SET_SIZE_BLOCKS
184 if ((size64>>12) > (uint64_t)~0UL) {
185 printf("size = %luMB", (unsigned long)(size64>>20));
186 err("Exported device is too big for me. Get 64-bit machine :-(\n");
188 printf("size = %luMB", (unsigned long)(size64>>20));
190 if (size64 > (~0UL >> 1)) {
191 printf("size = %luKB", (unsigned long)(size64>>10));
192 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
194 printf("size = %lu", (unsigned long)(size64));
198 if (read(sock, flags, sizeof(*flags)) < 0)
199 err("Failed/4: %m\n");
200 *flags = ntohl(*flags);
202 if(read(sock, &tmp, sizeof(tmp)) < 0)
203 err("Failed/4: %m\n");
204 *flags |= (uint32_t)ntohs(tmp);
207 if (read(sock, &buf, 124) < 0)
208 err("Failed/5: %m\n");
214 void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
216 int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
218 #ifdef NBD_SET_SIZE_BLOCKS
219 if (size64>>12 > (uint64_t)~0UL)
220 err("Device too large.\n");
222 if (ioctl(nbd, NBD_SET_BLKSIZE, 4096UL) < 0)
223 err("Ioctl/1.1a failed: %m\n");
224 size = (unsigned long)(size64>>12);
225 if (ioctl(nbd, NBD_SET_SIZE_BLOCKS, size) < 0)
226 err("Ioctl/1.1b failed: %m\n");
227 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
228 err("Ioctl/1.1c failed: %m\n");
229 fprintf(stderr, "bs=%d, sz=%llu bytes\n", blocksize, 4096ULL*size);
232 if (size64 > (~0UL >> 1)) {
233 err("Device too large.\n");
235 size = (unsigned long)size64;
236 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
237 err("Ioctl NBD_SET_SIZE failed: %m\n");
241 ioctl(nbd, NBD_CLEAR_SOCK);
243 /* ignore error as kernel may not support */
244 ioctl(nbd, NBD_SET_FLAGS, (unsigned long) flags);
246 if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
247 err("Unable to set read-only attribute for device");
250 void set_timeout(int nbd, int timeout) {
252 #ifdef NBD_SET_TIMEOUT
253 if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long)timeout) < 0)
254 err("Ioctl NBD_SET_TIMEOUT failed: %m\n");
255 fprintf(stderr, "timeout=%d\n", timeout);
257 err("Ioctl NBD_SET_TIMEOUT cannot be called when compiled on a system that does not support it\n");
262 void finish_sock(int sock, int nbd, int swap) {
263 if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
264 err("Ioctl NBD_SET_SOCK failed: %m\n");
267 mlockall(MCL_CURRENT | MCL_FUTURE);
270 void usage(char* errmsg, ...) {
274 va_start(ap, errmsg);
275 snprintf(tmp, 256, "ERROR: %s\n\n", errmsg);
276 vfprintf(stderr, errmsg, ap);
279 fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
281 fprintf(stderr, "Usage: nbd-client host port nbd_device [-block-size|-b block size] [-timeout|-t timeout] [-swap|-s] [-sdp|-S] [-persist|-p] [-nofork|-n]\n");
282 fprintf(stderr, "Or : nbd-client -name|-N name host [port] nbd_device [-block-size|-b block size] [-timeout|-t timeout] [-swap|-s] [-sdp|-S] [-persist|-p] [-nofork|-n]\n");
283 fprintf(stderr, "Or : nbd-client -d nbd_device\n");
284 fprintf(stderr, "Or : nbd-client -c nbd_device\n");
285 fprintf(stderr, "Or : nbd-client -h|--help\n");
286 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
287 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
288 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
289 fprintf(stderr, "blocksizes other than 1024 without patches\n");
290 fprintf(stderr, "Default value for port with -N is 10809. Note that port must always be numeric\n");
293 void disconnect(char* device) {
294 int nbd = open(device, O_RDWR);
297 err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
298 printf("Disconnecting: que, ");
299 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
300 err("Ioctl failed: %m\n");
301 printf("disconnect, ");
302 #ifdef NBD_DISCONNECT
303 if (ioctl(nbd, NBD_DISCONNECT)<0)
304 err("Ioctl failed: %m\n");
307 fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
310 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
311 err("Ioctl failed: %m\n");
315 int main(int argc, char *argv[]) {
325 int G_GNUC_UNUSED nofork=0; // if -dNOFORK
331 struct option long_options[] = {
332 { "block-size", required_argument, NULL, 'b' },
333 { "check", required_argument, NULL, 'c' },
334 { "disconnect", required_argument, NULL, 'd' },
335 { "help", no_argument, NULL, 'h' },
336 { "name", required_argument, NULL, 'N' },
337 { "nofork", no_argument, NULL, 'n' },
338 { "persist", no_argument, NULL, 'p' },
339 { "sdp", no_argument, NULL, 'S' },
340 { "swap", no_argument, NULL, 's' },
341 { "timeout", required_argument, NULL, 't' },
347 while((c=getopt_long_only(argc, argv, "-b:c:d:hnN:pSst:", long_options, NULL))>=0) {
350 // non-option argument
351 if(strchr(optarg, '=')) {
352 // old-style 'bs=' or 'timeout='
354 fprintf(stderr, "WARNING: old-style command-line argument encountered. This is deprecated.\n");
355 if(!strncmp(optarg, "bs=", 3)) {
359 if(!strncmp(optarg, "timeout=", 8)) {
363 usage("unknown option %s encountered", optarg);
366 switch(nonspecial++) {
373 if(!strtol(optarg, NULL, 0)) {
374 // not parseable as a number, assume it's the device and we have a name
386 usage("too many non-option arguments specified");
392 blocksize=(int)strtol(optarg, NULL, 0);
395 return check_conn(optarg, 1);
408 port = NBD_DEFAULT_PORT;
422 timeout=strtol(optarg, NULL, 0);
425 fprintf(stderr, "E: option eaten by 42 mice\n");
430 if((!port && !name) || !hostname || !nbddev) {
431 usage("not enough information specified");
435 nbd = open(nbddev, O_RDWR);
437 err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
439 sock = opennet(hostname, port, sdp);
441 negotiate(sock, &size64, &flags, name);
442 setsizes(nbd, size64, blocksize, flags);
443 set_timeout(nbd, timeout);
444 finish_sock(sock, nbd, swap);
451 err("Cannot detach from terminal");
457 /* Due to a race, the kernel NBD driver cannot
458 * call for a reread of the partition table
459 * in the handling of the NBD_DO_IT ioctl().
460 * Therefore, this is done in the first open()
461 * of the device. We therefore make sure that
462 * the device is opened at least once after the
463 * connection was made. This has to be done in a
464 * separate process, since the NBD_DO_IT ioctl()
465 * does not return until the NBD device has
468 while(check_conn(nbddev, 0)) {
471 open(nbddev, O_RDONLY);
476 if (ioctl(nbd, NBD_DO_IT) < 0) {
477 fprintf(stderr, "Kernel call returned: %m");
479 /* The user probably did 'nbd-client -d' on us.
487 fprintf(stderr, " Reconnecting\n");
488 close(sock); close(nbd);
489 sock = opennet(hostname, port, sdp);
490 nbd = open(nbddev, O_RDWR);
491 negotiate(sock, &new_size, &new_flags, name);
492 if (size64 != new_size) {
493 err("Size of the device changed. Bye");
495 setsizes(nbd, size64, blocksize,
498 set_timeout(nbd, timeout);
499 finish_sock(sock,nbd,swap);
503 /* We're on 2.4. It's not clearly defined what exactly
504 * happened at this point. Probably best to quit, now
506 fprintf(stderr, "Kernel call returned.");
510 printf("Closing: que, ");
511 ioctl(nbd, NBD_CLEAR_QUE);
513 ioctl(nbd, NBD_CLEAR_SOCK);