Network Block Device  @PACKAGE_VERSION@
nbd-client.c
Go to the documentation of this file.
1 /*
2  * Open connection for network block device
3  *
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>
7  *
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>
19  */
20 
21 #include "config.h"
22 #include "lfs.h"
23 
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <netinet/tcp.h>
29 #include <netinet/in.h>
30 #include <netdb.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <syslog.h>
34 #include <stdlib.h>
35 #include <sys/mount.h>
36 #include <sys/mman.h>
37 #include <errno.h>
38 #include <getopt.h>
39 #include <stdarg.h>
40 
41 #include <linux/ioctl.h>
42 #define MY_NAME "nbd_client"
43 #include "cliserv.h"
44 
45 #ifdef WITH_SDP
46 #include <sdp_inet.h>
47 #endif
48 
49 #define NBDC_DO_LIST 1
50 
51 int check_conn(char* devname, int do_print) {
52  char buf[256];
53  char* p;
54  int fd;
55  int len;
56 
57  if( (p=strrchr(devname, '/')) ) {
58  devname=p+1;
59  }
60  if((p=strchr(devname, 'p'))) {
61  /* We can't do checks on partitions. */
62  *p='\0';
63  }
64  snprintf(buf, 256, "/sys/block/%s/pid", devname);
65  if((fd=open(buf, O_RDONLY))<0) {
66  if(errno==ENOENT) {
67  return 1;
68  } else {
69  return 2;
70  }
71  }
72  len=read(fd, buf, 256);
73  buf[len-1]='\0';
74  if(do_print) printf("%s\n", buf);
75  close(fd);
76  return 0;
77 }
78 
79 int opennet(char *name, char* portstr, int sdp) {
80  int sock;
81  struct addrinfo hints;
82  struct addrinfo *ai = NULL;
83  struct addrinfo *rp = NULL;
84  int e;
85 
86  memset(&hints,'\0',sizeof(hints));
87  hints.ai_family = AF_UNSPEC;
88  hints.ai_socktype = SOCK_STREAM;
89  hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
90  hints.ai_protocol = IPPROTO_TCP;
91 
92  e = getaddrinfo(name, portstr, &hints, &ai);
93 
94  if(e != 0) {
95  fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(e));
96  freeaddrinfo(ai);
97  exit(EXIT_FAILURE);
98  }
99 
100  if(sdp) {
101 #ifdef WITH_SDP
102  if (ai->ai_family == AF_INET)
103  ai->ai_family = AF_INET_SDP;
104  else (ai->ai_family == AF_INET6)
105  ai->ai_family = AF_INET6_SDP;
106 #else
107  err("Can't do SDP: I was not compiled with SDP support!");
108 #endif
109  }
110 
111  for(rp = ai; rp != NULL; rp = rp->ai_next) {
112  sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
113 
114  if(sock == -1)
115  continue; /* error */
116 
117  if(connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
118  break; /* success */
119  }
120 
121  if (rp == NULL)
122  err("Socket failed: %m");
123 
124  setmysockopt(sock);
125 
126  freeaddrinfo(ai);
127  return sock;
128 }
129 
130 void ask_list(int sock) {
131  uint32_t opt;
132  uint32_t opt_server;
133  uint32_t len;
134  uint32_t reptype;
135  uint64_t magic;
136  char buf[1024];
137 
138  magic = ntohll(opts_magic);
139  if (write(sock, &magic, sizeof(magic)) < 0)
140  err("Failed/2.2: %m");
141 
142  /* Ask for the list */
143  opt = htonl(NBD_OPT_LIST);
144  if(write(sock, &opt, sizeof(opt)) < 0) {
145  err("writing list option failed: %m");
146  }
147  /* Send the length (zero) */
148  len = htonl(0);
149  if(write(sock, &len, sizeof(len)) < 0) {
150  err("writing length failed: %m");
151  }
152  /* newline, move away from the "Negotiation:" line */
153  printf("\n");
154  do {
155  memset(buf, 0, 1024);
156  if(read(sock, &magic, sizeof(magic)) < 0) {
157  err("Reading magic from server: %m");
158  }
159  if(read(sock, &opt_server, sizeof(opt_server)) < 0) {
160  err("Reading option: %m");
161  }
162  if(read(sock, &reptype, sizeof(reptype)) <0) {
163  err("Reading reply from server: %m");
164  }
165  if(read(sock, &len, sizeof(len)) < 0) {
166  err("Reading length from server: %m");
167  }
168  magic=ntohll(magic);
169  len=ntohl(len);
170  reptype=ntohl(reptype);
171  if(magic != rep_magic) {
172  err("Not enough magic from server");
173  }
174  if(reptype & NBD_REP_FLAG_ERROR) {
175  switch(reptype) {
176  case NBD_REP_ERR_POLICY:
177  fprintf(stderr, "\nE: listing not allowed by server.\n");
178  break;
179  default:
180  fprintf(stderr, "\nE: unexpected error from server.\n");
181  break;
182  }
183  if(len) {
184  if(read(sock, buf, len) < 0) {
185  fprintf(stderr, "\nE: could not read error message from server\n");
186  }
187  fprintf(stderr, "Server said: %s\n", buf);
188  }
189  exit(EXIT_FAILURE);
190  } else {
191  if(len) {
192  if(reptype != NBD_REP_SERVER) {
193  err("Server sent us a reply we don't understand!");
194  }
195  if(read(sock, &len, sizeof(len)) < 0) {
196  fprintf(stderr, "\nE: could not read export name length from server\n");
197  exit(EXIT_FAILURE);
198  }
199  len=ntohl(len);
200  if(read(sock, buf, len) < 0) {
201  fprintf(stderr, "\nE: could not read export name from server\n");
202  exit(EXIT_FAILURE);
203  }
204  printf("%s\n", buf);
205  }
206  }
207  } while(reptype != NBD_REP_ACK);
208  opt=htonl(NBD_OPT_ABORT);
209  len=htonl(0);
210  magic=htonll(opts_magic);
211  if (write(sock, &magic, sizeof(magic)) < 0)
212  err("Failed/2.2: %m");
213  if (write(sock, &opt, sizeof(opt)) < 0)
214  err("Failed writing abort");
215  if (write(sock, &len, sizeof(len)) < 0)
216  err("Failed writing length");
217 }
218 
219 void negotiate(int sock, u64 *rsize64, u32 *flags, char* name, uint32_t needed_flags, uint32_t client_flags, uint32_t do_opts) {
220  u64 magic, size64;
221  uint16_t tmp;
222  char buf[256] = "\0\0\0\0\0\0\0\0\0";
223 
224  printf("Negotiation: ");
225  if (read(sock, buf, 8) < 0)
226  err("Failed/1: %m");
227  if (strlen(buf)==0)
228  err("Server closed connection");
229  if (strcmp(buf, INIT_PASSWD))
230  err("INIT_PASSWD bad");
231  printf(".");
232  if (read(sock, &magic, sizeof(magic)) < 0)
233  err("Failed/2: %m");
234  magic = ntohll(magic);
235  if(name) {
236  uint32_t opt;
237  uint32_t namesize;
238 
239  if (magic != opts_magic)
240  err("Not enough opts_magic");
241  printf(".");
242  if(read(sock, &tmp, sizeof(uint16_t)) < 0) {
243  err("Failed reading flags: %m");
244  }
245  *flags = ((u32)ntohs(tmp));
246  if((needed_flags & *flags) != needed_flags) {
247  /* There's currently really only one reason why this
248  * check could possibly fail, but we may need to change
249  * this error message in the future... */
250  fprintf(stderr, "\nE: Server does not support listing exports\n");
251  exit(EXIT_FAILURE);
252  }
253 
254  client_flags = htonl(client_flags);
255  if (write(sock, &client_flags, sizeof(client_flags)) < 0)
256  err("Failed/2.1: %m");
257 
258  if(do_opts & NBDC_DO_LIST) {
259  ask_list(sock);
260  exit(EXIT_SUCCESS);
261  }
262 
263  /* Write the export name that we're after */
264  magic = htonll(opts_magic);
265  if (write(sock, &magic, sizeof(magic)) < 0)
266  err("Failed/2.2: %m");
267 
268  opt = ntohl(NBD_OPT_EXPORT_NAME);
269  if (write(sock, &opt, sizeof(opt)) < 0)
270  err("Failed/2.3: %m");
271  namesize = (u32)strlen(name);
272  namesize = ntohl(namesize);
273  if (write(sock, &namesize, sizeof(namesize)) < 0)
274  err("Failed/2.4: %m");
275  if (write(sock, name, strlen(name)) < 0)
276  err("Failed/2.4: %m");
277  } else {
278  if (magic != cliserv_magic)
279  err("Not enough cliserv_magic");
280  printf(".");
281  }
282 
283  if (read(sock, &size64, sizeof(size64)) < 0)
284  err("Failed/3: %m\n");
285  size64 = ntohll(size64);
286 
287 #ifdef NBD_SET_SIZE_BLOCKS
288  if ((size64>>12) > (uint64_t)~0UL) {
289  printf("size = %luMB", (unsigned long)(size64>>20));
290  err("Exported device is too big for me. Get 64-bit machine :-(\n");
291  } else
292  printf("size = %luMB", (unsigned long)(size64>>20));
293 #else
294  if (size64 > (~0UL >> 1)) {
295  printf("size = %luKB", (unsigned long)(size64>>10));
296  err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
297  } else
298  printf("size = %lu", (unsigned long)(size64));
299 #endif
300 
301  if(!name) {
302  if (read(sock, flags, sizeof(*flags)) < 0)
303  err("Failed/4: %m\n");
304  *flags = ntohl(*flags);
305  } else {
306  if(read(sock, &tmp, sizeof(tmp)) < 0)
307  err("Failed/4: %m\n");
308  *flags |= (uint32_t)ntohs(tmp);
309  }
310 
311  if (read(sock, &buf, 124) < 0)
312  err("Failed/5: %m\n");
313  printf("\n");
314 
315  *rsize64 = size64;
316 }
317 
318 void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
319  unsigned long size;
320  int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
321 
322 #ifdef NBD_SET_SIZE_BLOCKS
323  if (size64>>12 > (uint64_t)~0UL)
324  err("Device too large.\n");
325  else {
326  if (ioctl(nbd, NBD_SET_BLKSIZE, 4096UL) < 0)
327  err("Ioctl/1.1a failed: %m\n");
328  size = (unsigned long)(size64>>12);
329  if (ioctl(nbd, NBD_SET_SIZE_BLOCKS, size) < 0)
330  err("Ioctl/1.1b failed: %m\n");
331  if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
332  err("Ioctl/1.1c failed: %m\n");
333  fprintf(stderr, "bs=%d, sz=%llu bytes\n", blocksize, 4096ULL*size);
334  }
335 #else
336  if (size64 > (~0UL >> 1)) {
337  err("Device too large.\n");
338  } else {
339  size = (unsigned long)size64;
340  if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
341  err("Ioctl NBD_SET_SIZE failed: %m\n");
342  }
343 #endif
344 
345  ioctl(nbd, NBD_CLEAR_SOCK);
346 
347  /* ignore error as kernel may not support */
348  ioctl(nbd, NBD_SET_FLAGS, (unsigned long) flags);
349 
350  if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
351  err("Unable to set read-only attribute for device");
352 }
353 
354 void set_timeout(int nbd, int timeout) {
355  if (timeout) {
356 #ifdef NBD_SET_TIMEOUT
357  if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long)timeout) < 0)
358  err("Ioctl NBD_SET_TIMEOUT failed: %m\n");
359  fprintf(stderr, "timeout=%d\n", timeout);
360 #else
361  err("Ioctl NBD_SET_TIMEOUT cannot be called when compiled on a system that does not support it\n");
362 #endif
363  }
364 }
365 
366 void finish_sock(int sock, int nbd, int swap) {
367  if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
368  err("Ioctl NBD_SET_SOCK failed: %m\n");
369 
370  if (swap)
371  mlockall(MCL_CURRENT | MCL_FUTURE);
372 }
373 
374 void usage(char* errmsg, ...) {
375  if(errmsg) {
376  char tmp[256];
377  va_list ap;
378  va_start(ap, errmsg);
379  snprintf(tmp, 256, "ERROR: %s\n\n", errmsg);
380  vfprintf(stderr, tmp, ap);
381  va_end(ap);
382  } else {
383  fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
384  }
385  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");
386  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");
387  fprintf(stderr, "Or : nbd-client -d nbd_device\n");
388  fprintf(stderr, "Or : nbd-client -c nbd_device\n");
389  fprintf(stderr, "Or : nbd-client -h|--help\n");
390  fprintf(stderr, "Or : nbd-client -l|--list host\n");
391  fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
392  fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
393  fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
394  fprintf(stderr, "blocksizes other than 1024 without patches\n");
395  fprintf(stderr, "Default value for port with -N is 10809. Note that port must always be numeric\n");
396 }
397 
398 void disconnect(char* device) {
399  int nbd = open(device, O_RDWR);
400 
401  if (nbd < 0)
402  err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
403  printf("Disconnecting: que, ");
404  if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
405  err("Ioctl failed: %m\n");
406  printf("disconnect, ");
407 #ifdef NBD_DISCONNECT
408  if (ioctl(nbd, NBD_DISCONNECT)<0)
409  err("Ioctl failed: %m\n");
410  printf("sock, ");
411 #else
412  fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
413  exit(1);
414 #endif
415  if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
416  err("Ioctl failed: %m\n");
417  printf("done\n");
418 }
419 
420 int main(int argc, char *argv[]) {
421  char* port=NULL;
422  int sock, nbd;
423  int blocksize=1024;
424  char *hostname=NULL;
425  char *nbddev=NULL;
426  int swap=0;
427  int cont=0;
428  int timeout=0;
429  int sdp=0;
430  int G_GNUC_UNUSED nofork=0; // if -dNOFORK
431  u64 size64;
432  u32 flags;
433  int c;
434  int nonspecial=0;
435  char* name=NULL;
436  uint32_t needed_flags;
437  uint32_t cflags;
438  uint32_t opts;
439  struct option long_options[] = {
440  { "block-size", required_argument, NULL, 'b' },
441  { "check", required_argument, NULL, 'c' },
442  { "disconnect", required_argument, NULL, 'd' },
443  { "help", no_argument, NULL, 'h' },
444  { "list", no_argument, NULL, 'l' },
445  { "name", required_argument, NULL, 'N' },
446  { "nofork", no_argument, NULL, 'n' },
447  { "persist", no_argument, NULL, 'p' },
448  { "sdp", no_argument, NULL, 'S' },
449  { "swap", no_argument, NULL, 's' },
450  { "timeout", required_argument, NULL, 't' },
451  { 0, 0, 0, 0 },
452  };
453 
454  logging();
455 
456  while((c=getopt_long_only(argc, argv, "-b:c:d:hlnN:pSst:", long_options, NULL))>=0) {
457  switch(c) {
458  case 1:
459  // non-option argument
460  if(strchr(optarg, '=')) {
461  // old-style 'bs=' or 'timeout='
462  // argument
463  fprintf(stderr, "WARNING: old-style command-line argument encountered. This is deprecated.\n");
464  if(!strncmp(optarg, "bs=", 3)) {
465  optarg+=3;
466  goto blocksize;
467  }
468  if(!strncmp(optarg, "timeout=", 8)) {
469  optarg+=8;
470  goto timeout;
471  }
472  usage("unknown option %s encountered", optarg);
473  exit(EXIT_FAILURE);
474  }
475  switch(nonspecial++) {
476  case 0:
477  // host
478  hostname=optarg;
479  break;
480  case 1:
481  // port
482  if(!strtol(optarg, NULL, 0)) {
483  // not parseable as a number, assume it's the device and we have a name
484  nbddev = optarg;
485  nonspecial++;
486  } else {
487  port = optarg;
488  }
489  break;
490  case 2:
491  // device
492  nbddev = optarg;
493  break;
494  default:
495  usage("too many non-option arguments specified");
496  exit(EXIT_FAILURE);
497  }
498  break;
499  case 'b':
500  blocksize:
501  blocksize=(int)strtol(optarg, NULL, 0);
502  break;
503  case 'c':
504  return check_conn(optarg, 1);
505  case 'd':
506  disconnect(optarg);
507  exit(EXIT_SUCCESS);
508  case 'h':
509  usage(NULL);
510  exit(EXIT_SUCCESS);
511  case 'l':
512  needed_flags |= NBD_FLAG_FIXED_NEWSTYLE;
513  cflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
514  opts |= NBDC_DO_LIST;
515  name="";
516  nbddev="";
517  port = NBD_DEFAULT_PORT;
518  break;
519  case 'n':
520  nofork=1;
521  break;
522  case 'N':
523  name=optarg;
524  if(!port) {
525  port = NBD_DEFAULT_PORT;
526  }
527  break;
528  case 'p':
529  cont=1;
530  break;
531  case 's':
532  swap=1;
533  break;
534  case 'S':
535  sdp=1;
536  break;
537  case 't':
538  timeout:
539  timeout=strtol(optarg, NULL, 0);
540  break;
541  default:
542  fprintf(stderr, "E: option eaten by 42 mice\n");
543  exit(EXIT_FAILURE);
544  }
545  }
546 
547  if((!port && !name) || !hostname || !nbddev) {
548  usage("not enough information specified");
549  exit(EXIT_FAILURE);
550  }
551 
552  sock = opennet(hostname, port, sdp);
553 
554  negotiate(sock, &size64, &flags, name, needed_flags, cflags, opts);
555 
556  nbd = open(nbddev, O_RDWR);
557  if (nbd < 0)
558  err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
559 
560  setsizes(nbd, size64, blocksize, flags);
561  set_timeout(nbd, timeout);
562  finish_sock(sock, nbd, swap);
563 
564  /* Go daemon */
565 
566 #ifndef NOFORK
567  if(!nofork) {
568  if (daemon(0,0) < 0)
569  err("Cannot detach from terminal");
570  }
571 #endif
572  do {
573 #ifndef NOFORK
574  if (!fork()) {
575  /* Due to a race, the kernel NBD driver cannot
576  * call for a reread of the partition table
577  * in the handling of the NBD_DO_IT ioctl().
578  * Therefore, this is done in the first open()
579  * of the device. We therefore make sure that
580  * the device is opened at least once after the
581  * connection was made. This has to be done in a
582  * separate process, since the NBD_DO_IT ioctl()
583  * does not return until the NBD device has
584  * disconnected.
585  */
586  while(check_conn(nbddev, 0)) {
587  sleep(1);
588  }
589  open(nbddev, O_RDONLY);
590  exit(0);
591  }
592 #endif
593 
594  if (ioctl(nbd, NBD_DO_IT) < 0) {
595  int error = errno;
596  fprintf(stderr, "nbd,%d: Kernel call returned: %d", getpid(), error);
597  if(error==EBADR) {
598  /* The user probably did 'nbd-client -d' on us.
599  * quit */
600  cont=0;
601  } else {
602  if(cont) {
603  u64 new_size;
604  u32 new_flags;
605 
606  fprintf(stderr, " Reconnecting\n");
607  close(sock); close(nbd);
608  sock = opennet(hostname, port, sdp);
609  nbd = open(nbddev, O_RDWR);
610  negotiate(sock, &new_size, &new_flags, name, needed_flags, cflags, opts);
611  if (size64 != new_size) {
612  err("Size of the device changed. Bye");
613  }
614  setsizes(nbd, size64, blocksize,
615  new_flags);
616 
617  set_timeout(nbd, timeout);
618  finish_sock(sock,nbd,swap);
619  }
620  }
621  } else {
622  /* We're on 2.4. It's not clearly defined what exactly
623  * happened at this point. Probably best to quit, now
624  */
625  fprintf(stderr, "Kernel call returned.");
626  cont=0;
627  }
628  } while(cont);
629  printf("Closing: que, ");
630  ioctl(nbd, NBD_CLEAR_QUE);
631  printf("sock, ");
632  ioctl(nbd, NBD_CLEAR_SOCK);
633  printf("done\n");
634  return 0;
635 }