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
14 #include <sys/ioctl.h>
15 #include <sys/socket.h>
16 #include <sys/types.h>
18 #include <netinet/tcp.h>
19 #include <netinet/in.h> /* sockaddr_in, htons, in_addr */
20 #include <netdb.h> /* hostent, gethostby*, getservby* */
26 #define MY_NAME "nbd_client"
28 #error I need GCC to work
31 #include <linux/ioctl.h>
34 int opennet(char *name, int port)
37 struct sockaddr_in xaddrin;
38 int xaddrinlen = sizeof(xaddrin);
39 struct hostent *hostn;
41 hostn = gethostbyname(name);
43 err("Gethostname failed: %h\n");
45 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
46 err("Socket failed: %m");
48 xaddrin.sin_family = AF_INET;
49 xaddrin.sin_port = htons(port);
50 xaddrin.sin_addr.s_addr = *((int *) hostn->h_addr);
51 if ((connect(sock, (struct sockaddr *) &xaddrin, xaddrinlen) < 0))
58 int main(int argc, char *argv[])
60 int port, sock, nbd, one = 1;
63 char buf[256] = "\0\0\0\0\0\0\0\0\0";
72 fprintf(stderr, "Usage: nbd-client [bs=blocksize] host port nbd_device [-swap]\n");
73 fprintf(stderr, "Or : nbd-client -d nbd_device\n");
74 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
75 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
76 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
77 fprintf(stderr, "blocksizes other than 1024 without patches\n");
81 ++argv; --argc; /* skip programname */
83 if (strcmp(argv[0], "-d")==0) {
84 nbd = open(argv[1], O_RDWR);
86 err("Can not open NBD: %m");
87 printf("Disconnecting: que, ");
88 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
89 err("Ioctl failed: %m\n");
90 printf("disconnect, ");
92 if (ioctl(nbd, NBD_DISCONNECT)<0)
93 err("Ioctl failed: %m\n");
96 fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
99 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
100 err("Ioctl failed: %m\n");
105 if (strncmp(argv[0], "bs=", 3)==0) {
106 blocksize=atoi(argv[0]+3);
107 ++argv; --argc; /* skip blocksize */
110 if (argc==0) goto errmsg;
112 ++argv; --argc; /* skip hostname */
114 if (argc==0) goto errmsg;
115 port = atoi(argv[0]);
116 ++argv; --argc; /* skip port */
118 if (argc==0) goto errmsg;
119 sock = opennet(hostname, port);
120 nbd = open(argv[0], O_RDWR);
122 err("Can not open NBD: %m");
123 ++argv; --argc; /* skip device */
125 if (argc>1) goto errmsg;
127 argv=NULL; argc=0; /* don't use it later suddenly */
129 printf("Negotiation: ");
130 if (read(sock, buf, 8) < 0)
132 if (strcmp(buf, INIT_PASSWD))
133 err("INIT_PASSWD bad");
135 if (read(sock, &magic, sizeof(magic)) < 0)
137 magic = ntohll(magic);
138 if (magic != cliserv_magic)
139 err("Not enough cliserv_magic");
142 if (read(sock, &size64, sizeof(size64)) < 0)
143 err("Failed/3: %m\n");
144 size64 = ntohll(size64);
146 #ifdef NBD_SET_SIZE_BLOCKS
147 if ((size64>>10) > (~0UL >> 1)) {
148 printf("size = %luMB", (unsigned long)(size64>>20));
149 err("Exported device is too big for me. Get 64-bit machine :-(\n");
151 printf("size = %luKB", (unsigned long)(size64>>10));
153 if (size64 > (~0UL >> 1)) {
154 printf("size = %luKB", (unsigned long)(size64>>10));
155 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
157 printf("size = %lu", (unsigned long)(size64));
160 if (read(sock, &buf, 128) < 0)
161 err("Failed/4: %m\n");
164 #ifdef NBD_SET_SIZE_BLOCKS
165 if (size64/blocksize > (~0UL >> 1))
166 err("Device too large.\n");
169 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
170 err("Ioctl/1.1a failed: %m\n");
171 size = (unsigned long)(size64/blocksize);
172 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
173 err("Ioctl/1.1b failed: %m\n");
174 fprintf(stderr, "bs=%d, sz=%lu\n", blocksize, size);
177 if (size64 > (~0UL >> 1)) {
178 err("Device too large.\n");
180 size = (unsigned long)size64;
181 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
182 err("Ioctl/1 failed: %m\n");
186 ioctl(nbd, NBD_CLEAR_SOCK);
187 if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
188 err("Ioctl/2 failed: %m\n");
192 err("You have to compile me on machine with swapping patch enabled in order to use it later.");
195 if (setsockopt(sock, SOL_SOCKET, SO_SWAPPING, &one, sizeof(int)) < 0)
196 err("Could not enable swapping: %m");
205 if (ioctl(nbd, NBD_DO_IT) < 0)
206 fprintf(stderr, "Kernel call returned: %m");
208 fprintf(stderr, "Kernel call returned.");
209 printf("Closing: que, ");
210 ioctl(nbd, NBD_CLEAR_QUE);
212 ioctl(nbd, NBD_CLEAR_SOCK);