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.
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
19 #include <sys/types.h>
21 #include <netinet/tcp.h>
22 #include <netinet/in.h> /* sockaddr_in, htons, in_addr */
23 #include <netdb.h> /* hostent, gethostby*, getservby* */
29 #define MY_NAME "nbd_client"
31 #error I need GCC to work
34 #include <linux/ioctl.h>
38 * Open a connection to a given host on a given port.
39 * @param name The host to connect to
40 * @param port The port to connect to
41 * @return A connected socket to the given host on the given port
43 int opennet(char *name, int port)
46 struct sockaddr_in xaddrin;
47 int xaddrinlen = sizeof(xaddrin);
48 struct hostent *hostn;
50 hostn = gethostbyname(name);
52 err("Gethostname failed: %h\n");
54 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
55 err("Socket failed: %m");
57 xaddrin.sin_family = AF_INET;
58 xaddrin.sin_port = htons(port);
59 xaddrin.sin_addr.s_addr = *((int *) hostn->h_addr);
60 if ((connect(sock, (struct sockaddr *) &xaddrin, xaddrinlen) < 0))
69 * @todo cut this thing into little tiny manageable pieces, preferably with a
72 int main(int argc, char *argv[])
74 int port, sock, nbd, one = 1;
77 char buf[256] = "\0\0\0\0\0\0\0\0\0";
86 fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
87 fprintf(stderr, "Usage: nbd-client [bs=blocksize] host port nbd_device [-swap]\n");
88 fprintf(stderr, "Or : nbd-client -d nbd_device\n");
89 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
90 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
91 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
92 fprintf(stderr, "blocksizes other than 1024 without patches\n");
96 ++argv; --argc; /* skip programname */
98 if (strcmp(argv[0], "-d")==0) {
99 nbd = open(argv[1], O_RDWR);
101 err("Can not open NBD: %m");
102 printf("Disconnecting: que, ");
103 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
104 err("Ioctl failed: %m\n");
105 printf("disconnect, ");
106 #ifdef NBD_DISCONNECT
107 if (ioctl(nbd, NBD_DISCONNECT)<0)
108 err("Ioctl failed: %m\n");
111 fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
114 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
115 err("Ioctl failed: %m\n");
120 if (strncmp(argv[0], "bs=", 3)==0) {
121 blocksize=atoi(argv[0]+3);
122 ++argv; --argc; /* skip blocksize */
125 if (argc==0) goto errmsg;
127 ++argv; --argc; /* skip hostname */
129 if (argc==0) goto errmsg;
130 port = atoi(argv[0]);
131 ++argv; --argc; /* skip port */
133 if (argc==0) goto errmsg;
134 sock = opennet(hostname, port);
135 nbd = open(argv[0], O_RDWR);
137 err("Can not open NBD: %m");
138 ++argv; --argc; /* skip device */
140 if (argc>1) goto errmsg;
142 argv=NULL; argc=0; /* don't use it later suddenly */
144 printf("Negotiation: ");
145 if (read(sock, buf, 8) < 0)
148 err("Server closed connection");
149 if (strcmp(buf, INIT_PASSWD))
150 err("INIT_PASSWD bad");
152 if (read(sock, &magic, sizeof(magic)) < 0)
154 magic = ntohll(magic);
155 if (magic != cliserv_magic)
156 err("Not enough cliserv_magic");
159 if (read(sock, &size64, sizeof(size64)) < 0)
160 err("Failed/3: %m\n");
161 size64 = ntohll(size64);
163 #ifdef NBD_SET_SIZE_BLOCKS
164 if ((size64>>10) > (~0UL >> 1)) {
165 printf("size = %luMB", (unsigned long)(size64>>20));
166 err("Exported device is too big for me. Get 64-bit machine :-(\n");
168 printf("size = %luKB", (unsigned long)(size64>>10));
170 if (size64 > (~0UL >> 1)) {
171 printf("size = %luKB", (unsigned long)(size64>>10));
172 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
174 printf("size = %lu", (unsigned long)(size64));
177 if (read(sock, &buf, 128) < 0)
178 err("Failed/4: %m\n");
181 #ifdef NBD_SET_SIZE_BLOCKS
182 if (size64/blocksize > (~0UL >> 1))
183 err("Device too large.\n");
186 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
187 err("Ioctl/1.1a failed: %m\n");
188 size = (unsigned long)(size64/blocksize);
189 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
190 err("Ioctl/1.1b failed: %m\n");
191 fprintf(stderr, "bs=%d, sz=%lu\n", blocksize, size);
194 if (size64 > (~0UL >> 1)) {
195 err("Device too large.\n");
197 size = (unsigned long)size64;
198 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
199 err("Ioctl/1 failed: %m\n");
203 ioctl(nbd, NBD_CLEAR_SOCK);
204 if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
205 err("Ioctl/2 failed: %m\n");
209 err("You have to compile me on machine with swapping patch enabled in order to use it later.");
212 if (setsockopt(sock, SOL_SOCKET, SO_SWAPPING, &one, sizeof(int)) < 0)
213 err("Could not enable swapping: %m");
222 if (ioctl(nbd, NBD_DO_IT) < 0)
223 fprintf(stderr, "Kernel call returned: %m");
225 fprintf(stderr, "Kernel call returned.");
226 printf("Closing: que, ");
227 ioctl(nbd, NBD_CLEAR_QUE);
229 ioctl(nbd, NBD_CLEAR_SOCK);