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.
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
23 #include <netinet/tcp.h>
24 #include <netinet/in.h> /* sockaddr_in, htons, in_addr */
25 #include <netdb.h> /* hostent, gethostby*, getservby* */
32 #error I need GCC to work
35 #include <linux/ioctl.h>
36 #define MY_NAME "nbd_client"
39 int opennet(char *name, int port) {
41 struct sockaddr_in xaddrin;
42 int xaddrinlen = sizeof(xaddrin);
43 struct hostent *hostn;
45 hostn = gethostbyname(name);
47 err("Gethostname failed: %h\n");
49 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
50 err("Socket failed: %m");
52 xaddrin.sin_family = AF_INET;
53 xaddrin.sin_port = htons(port);
54 xaddrin.sin_addr.s_addr = *((int *) hostn->h_addr);
55 if ((connect(sock, (struct sockaddr *) &xaddrin, xaddrinlen) < 0))
62 u64 negotiate(int sock, int blocksize) {
64 char buf[256] = "\0\0\0\0\0\0\0\0\0";
66 printf("Negotiation: ");
67 if (read(sock, buf, 8) < 0)
70 err("Server closed connection");
71 if (strcmp(buf, INIT_PASSWD))
72 err("INIT_PASSWD bad");
74 if (read(sock, &magic, sizeof(magic)) < 0)
76 magic = ntohll(magic);
77 if (magic != cliserv_magic)
78 err("Not enough cliserv_magic");
81 if (read(sock, &size64, sizeof(size64)) < 0)
82 err("Failed/3: %m\n");
83 size64 = ntohll(size64);
85 #ifdef NBD_SET_SIZE_BLOCKS
86 if ((size64>>10) > (~0UL >> 1)) {
87 printf("size = %luMB", (unsigned long)(size64>>20));
88 err("Exported device is too big for me. Get 64-bit machine :-(\n");
90 printf("size = %luKB", (unsigned long)(size64>>10));
92 if (size64 > (~0UL >> 1)) {
93 printf("size = %luKB", (unsigned long)(size64>>10));
94 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
96 printf("size = %lu", (unsigned long)(size64));
99 if (read(sock, &buf, 128) < 0)
100 err("Failed/4: %m\n");
106 void setsizes(int nbd, u64 size64, int blocksize) {
109 #ifdef NBD_SET_SIZE_BLOCKS
110 if (size64/blocksize > (~0UL >> 1))
111 err("Device too large.\n");
114 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
115 err("Ioctl/1.1a failed: %m\n");
116 size = (unsigned long)(size64/blocksize);
117 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
118 err("Ioctl/1.1b failed: %m\n");
119 fprintf(stderr, "bs=%d, sz=%lu\n", blocksize, size);
122 if (size64 > (~0UL >> 1)) {
123 err("Device too large.\n");
125 size = (unsigned long)size64;
126 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
127 err("Ioctl NBD_SET_SIZE failed: %m\n");
131 ioctl(nbd, NBD_CLEAR_SOCK);
134 void finish_sock(int sock, int nbd, int swap) {
135 if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
136 err("Ioctl NBD_SET_SOCK failed: %m\n");
140 err("You have to compile me on machine with swapping patch enabled in order to use it later.");
143 if (setsockopt(sock, SOL_SOCKET, SO_SWAPPING, &one, sizeof(int)) < 0)
144 err("Could not enable swapping: %m");
148 int main(int argc, char *argv[]) {
151 char *hostname, *nbddev;
160 fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
161 fprintf(stderr, "Usage: nbd-client [bs=blocksize] host port nbd_device [-swap] [-persist]\n");
162 fprintf(stderr, "Or : nbd-client -d nbd_device\n");
163 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
164 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
165 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
166 fprintf(stderr, "blocksizes other than 1024 without patches\n");
170 ++argv; --argc; /* skip programname */
172 if (strcmp(argv[0], "-d")==0) {
173 nbd = open(argv[1], O_RDWR);
175 err("Can not open NBD: %m");
176 printf("Disconnecting: que, ");
177 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
178 err("Ioctl failed: %m\n");
179 printf("disconnect, ");
180 #ifdef NBD_DISCONNECT
181 if (ioctl(nbd, NBD_DISCONNECT)<0)
182 err("Ioctl failed: %m\n");
185 fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
188 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
189 err("Ioctl failed: %m\n");
194 if (strncmp(argv[0], "bs=", 3)==0) {
195 blocksize=atoi(argv[0]+3);
196 ++argv; --argc; /* skip blocksize */
199 if (argc==0) goto errmsg;
201 ++argv; --argc; /* skip hostname */
203 if (argc==0) goto errmsg;
204 port = atoi(argv[0]);
205 ++argv; --argc; /* skip port */
207 if (argc==0) goto errmsg;
208 sock = opennet(hostname, port);
210 nbd = open(nbddev, O_RDWR);
212 err("Can not open NBD: %m");
213 ++argv; --argc; /* skip device */
215 if (argc>2) goto errmsg;
217 if(strncmp(argv[0], "-swap", 5)==0) {
223 if(strncmp(argv[0], "-persist", 8)==0) {
228 argv=NULL; argc=0; /* don't use it later suddenly */
230 size64 = negotiate(sock, blocksize);
231 setsizes(nbd, size64, blocksize);
232 finish_sock(sock, nbd, swap);
243 if (ioctl(nbd, NBD_DO_IT) < 0) {
244 fprintf(stderr, "Kernel call returned: %m");
246 /* The user probably did 'nbd-client -d' on us.
251 fprintf(stderr, " Reconnecting\n");
252 close(sock); close(nbd);
253 sock = opennet(hostname, port);
254 nbd = open(nbddev, O_RDWR);
255 if(size64!=negotiate(sock,blocksize)) {
256 err("Size of the device changed. Bye");
258 setsizes(nbd, size64, blocksize);
259 finish_sock(sock,nbd,swap);
263 /* We're on 2.4. It's not clearly defined what exactly
264 * happened at this point. Probably best to quit, now
266 fprintf(stderr, "Kernel call returned.");
270 printf("Closing: que, ");
271 ioctl(nbd, NBD_CLEAR_QUE);
273 ioctl(nbd, NBD_CLEAR_SOCK);