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