2 * Open connection for network block device
4 * Copyright 1997,1998 Pavel Machek, distribute under GPL
5 * <pavel@atrey.karlin.mff.cuni.cz>
7 * Version 1.0 - 64bit issues should be fixed, now
8 * Version 1.1 - added bs (blocksize) option (Alexey Guzeev, aga@permonline.ru)
9 * Version 1.2 - I added new option '-d' to send the disconnect request
10 * Version 2.0 - Version synchronised with server
11 * Version 2.1 - Check for disconnection before INIT_PASSWD is received
12 * to make errormsg a bit more helpful in case the server can't
13 * open the exported file.
14 * 16/03/2010 - Add IPv6 support.
15 * Kitt Tientanopajai <kitt@kitty.in.th>
16 * Neutron Soutmun <neo.neutron@gmail.com>
17 * Suriya Soutmun <darksolar@gmail.com>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
27 #include <netinet/tcp.h>
28 #include <netinet/in.h>
34 #include <sys/mount.h>
40 #error I need GCC to work
43 #include <linux/ioctl.h>
44 #define MY_NAME "nbd_client"
47 int check_conn(char* devname, int do_print) {
53 if(!strncmp(devname, "/dev/", 5)) {
56 if((p=strchr(devname, 'p'))) {
57 /* We can't do checks on partitions. */
60 snprintf(buf, 256, "/sys/block/%s/pid", devname);
61 if((fd=open(buf, O_RDONLY))<0) {
68 len=read(fd, buf, 256);
70 if(do_print) printf("%s\n", buf);
74 int opennet(char *name, int port, int sdp) {
77 struct addrinfo hints;
78 struct addrinfo *ai = NULL;
79 struct addrinfo *rp = NULL;
82 snprintf(portstr, sizeof(portstr), "%d", port);
84 memset(&hints,'\0',sizeof(hints));
85 hints.ai_family = AF_UNSPEC;
86 hints.ai_socktype = SOCK_STREAM;
87 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
88 hints.ai_protocol = IPPROTO_TCP;
90 e = getaddrinfo(name, portstr, &hints, &ai);
93 fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(e));
100 if (ai->ai_family == AF_INET)
101 ai->ai_family = AF_INET_SDP;
102 else (ai->ai_family == AF_INET6)
103 ai->ai_family = AF_INET6_SDP;
105 err("Can't do SDP: I was not compiled with SDP support!");
109 for(rp = ai; rp != NULL; rp = rp->ai_next) {
110 sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
113 continue; /* error */
115 if(connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
120 err("Socket failed: %m");
128 void negotiate(int sock, u64 *rsize64, u32 *flags) {
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);
143 if (magic != cliserv_magic)
144 err("Not enough cliserv_magic");
147 if (read(sock, &size64, sizeof(size64)) < 0)
148 err("Failed/3: %m\n");
149 size64 = ntohll(size64);
151 #ifdef NBD_SET_SIZE_BLOCKS
152 if ((size64>>10) > (~0UL >> 1)) {
153 printf("size = %luMB", (unsigned long)(size64>>20));
154 err("Exported device is too big for me. Get 64-bit machine :-(\n");
156 printf("size = %luKB", (unsigned long)(size64>>10));
158 if (size64 > (~0UL >> 1)) {
159 printf("size = %luKB", (unsigned long)(size64>>10));
160 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
162 printf("size = %lu", (unsigned long)(size64));
165 if (read(sock, flags, sizeof(*flags)) < 0)
166 err("Failed/4: %m\n");
167 *flags = ntohl(*flags);
169 if (read(sock, &buf, 124) < 0)
170 err("Failed/5: %m\n");
176 void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
178 int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
180 #ifdef NBD_SET_SIZE_BLOCKS
181 if (size64/blocksize > (~0UL >> 1))
182 err("Device too large.\n");
185 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
186 err("Ioctl/1.1a failed: %m\n");
187 size = (unsigned long)(size64/blocksize);
188 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
189 err("Ioctl/1.1b failed: %m\n");
190 fprintf(stderr, "bs=%d, sz=%lu\n", blocksize, size);
193 if (size64 > (~0UL >> 1)) {
194 err("Device too large.\n");
196 size = (unsigned long)size64;
197 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
198 err("Ioctl NBD_SET_SIZE failed: %m\n");
202 ioctl(nbd, NBD_CLEAR_SOCK);
204 if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
205 err("Unable to set read-only attribute for device");
208 void set_timeout(int nbd, int timeout) {
210 #ifdef NBD_SET_TIMEOUT
211 if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long)timeout) < 0)
212 err("Ioctl NBD_SET_TIMEOUT failed: %m\n");
213 fprintf(stderr, "timeout=%d\n", timeout);
215 err("Ioctl NBD_SET_TIMEOUT cannot be called when compiled on a system that does not support it\n");
220 void finish_sock(int sock, int nbd, int swap) {
221 if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
222 err("Ioctl NBD_SET_SOCK failed: %m\n");
225 mlockall(MCL_CURRENT | MCL_FUTURE);
229 fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
230 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");
231 fprintf(stderr, "Or : nbd-client -d nbd_device\n");
232 fprintf(stderr, "Or : nbd-client -c nbd_device\n");
233 fprintf(stderr, "Or : nbd-client -h|--help\n");
234 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
235 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
236 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
237 fprintf(stderr, "blocksizes other than 1024 without patches\n");
240 void disconnect(char* device) {
241 int nbd = open(device, O_RDWR);
244 err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
245 printf("Disconnecting: que, ");
246 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
247 err("Ioctl failed: %m\n");
248 printf("disconnect, ");
249 #ifdef NBD_DISCONNECT
250 if (ioctl(nbd, NBD_DISCONNECT)<0)
251 err("Ioctl failed: %m\n");
254 fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
257 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
258 err("Ioctl failed: %m\n");
262 int main(int argc, char *argv[]) {
277 struct option long_options[] = {
278 { "block-size", required_argument, NULL, 'b' },
279 { "check", required_argument, NULL, 'c' },
280 { "disconnect", required_argument, NULL, 'd' },
281 { "help", no_argument, NULL, 'h' },
282 { "nofork", no_argument, NULL, 'n' },
283 { "persist", no_argument, NULL, 'p' },
284 { "sdp", no_argument, NULL, 'S' },
285 { "swap", no_argument, NULL, 's' },
286 { "timeout", required_argument, NULL, 't' },
292 while((c=getopt_long_only(argc, argv, "-b:c:d:hnpSst:", long_options, NULL))>=0) {
295 // non-option argument
296 if(strchr(optarg, '=')) {
297 // old-style 'bs=' or 'timeout='
299 fprintf(stderr, "WARNING: old-style command-line argument encountered. This is deprecated.\n");
300 if(!strncmp(optarg, "bs=", 3)) {
304 if(!strncmp(optarg, "timeout=", 8)) {
308 fprintf(stderr, "ERROR: unknown option %s encountered\n", optarg);
311 switch(nonspecial++) {
318 port = strtol(optarg, NULL, 0);
325 fprintf(stderr, "ERROR: too many non-option arguments specified\n");
331 blocksize=(int)strtol(optarg, NULL, 0);
334 return check_conn(optarg, 1);
355 timeout=strtol(optarg, NULL, 0);
358 fprintf(stderr, "E: option eaten by 42 mice\n");
363 if(!port || !hostname || !nbddev) {
368 nbd = open(nbddev, O_RDWR);
370 err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
371 ++argv; --argc; /* skip device */
373 sock = opennet(hostname, port, sdp);
375 negotiate(sock, &size64, &flags);
376 setsizes(nbd, size64, blocksize, flags);
377 set_timeout(nbd, timeout);
378 finish_sock(sock, nbd, swap);
383 if(!nofork) daemon(0,0);
388 /* Due to a race, the kernel NBD driver cannot
389 * call for a reread of the partition table
390 * in the handling of the NBD_DO_IT ioctl().
391 * Therefore, this is done in the first open()
392 * of the device. We therefore make sure that
393 * the device is opened at least once after the
394 * connection was made. This has to be done in a
395 * separate process, since the NBD_DO_IT ioctl()
396 * does not return until the NBD device has
399 while(check_conn(nbddev, 0)) {
402 open(nbddev, O_RDONLY);
407 if (ioctl(nbd, NBD_DO_IT) < 0) {
408 fprintf(stderr, "Kernel call returned: %m");
410 /* The user probably did 'nbd-client -d' on us.
418 fprintf(stderr, " Reconnecting\n");
419 close(sock); close(nbd);
420 sock = opennet(hostname, port, sdp);
421 nbd = open(nbddev, O_RDWR);
422 negotiate(sock, &new_size, &new_flags);
423 if (size64 != new_size) {
424 err("Size of the device changed. Bye");
426 setsizes(nbd, size64, blocksize,
429 set_timeout(nbd, timeout);
430 finish_sock(sock,nbd,swap);
434 /* We're on 2.4. It's not clearly defined what exactly
435 * happened at this point. Probably best to quit, now
437 fprintf(stderr, "Kernel call returned.");
441 printf("Closing: que, ");
442 ioctl(nbd, NBD_CLEAR_QUE);
444 ioctl(nbd, NBD_CLEAR_SOCK);