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
10 /* I added new option '-d' to send the disconnect request */
13 #include <sys/ioctl.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
17 #include <netinet/tcp.h>
18 #include <netinet/in.h> /* sockaddr_in, htons, in_addr */
19 #include <netdb.h> /* hostent, gethostby*, getservby* */
25 #define MY_NAME "nbd_client"
27 #error I need GCC to work
30 #include <linux/ioctl.h>
33 int opennet(char *name, int port)
36 struct sockaddr_in xaddrin;
37 int xaddrinlen = sizeof(xaddrin);
38 struct hostent *hostn;
40 hostn = gethostbyname(name);
42 err("Gethostname failed: %h\n");
44 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
45 err("Socket failed: %m");
47 xaddrin.sin_family = AF_INET;
48 xaddrin.sin_port = htons(port);
49 xaddrin.sin_addr.s_addr = *((int *) hostn->h_addr);
50 if ((connect(sock, (struct sockaddr *) &xaddrin, xaddrinlen) < 0))
57 int main(int argc, char *argv[])
59 int port, sock, nbd, one = 1;
62 char buf[256] = "\0\0\0\0\0\0\0\0\0";
63 int swap = (argc > 4);
69 fprintf(stderr, "Usage: host port nbd_device -swap\n");
70 fprintf(stderr, "or -d nbd_device \n");
74 if (strcmp(argv[1],"-d")==0) {
75 nbd = open(argv[2], O_RDWR);
77 err("Can not open NBD: %m");
78 printf("Disconnecting: que, ");
79 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
80 err("Ioctl failed: %m\n");
81 printf("disconnect, ");
83 if (ioctl(nbd, NBD_DISCONNECT)<0)
84 err("Ioctl failed: %m\n");
87 die("Can't disconnect: I was not compiled with disconnect support!\n" );
89 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
90 err("Ioctl failed: %m\n");
95 if (argc<4) goto errmsg;
97 sock = opennet(argv[1], port);
98 nbd = open(argv[3], O_RDWR);
100 err("Can not open NBD: %m");
102 printf("Negotiation: ");
103 if (read(sock, buf, 8) < 0)
105 if (strcmp(buf, INIT_PASSWD))
106 err("INIT_PASSWD bad");
108 if (read(sock, &magic, sizeof(magic)) < 0)
110 magic = ntohll(magic);
111 if (magic != cliserv_magic)
112 err("Not enough cliserv_magic");
115 if (read(sock, &size64, sizeof(size64)) < 0)
116 err("Failed/3: %m\n");
117 size64 = ntohll(size64);
118 if (size64 > (~0UL >> 1)) {
119 #ifdef NBD_SET_SIZE_BLOCKS
120 if ((size64 >> 10) > (~0UL >> 1)) {
121 printf("size = %luMB", (unsigned long)(size64>>20));
122 err("Exported device is too big for me. Get 64-bit machine :-(\n");
124 printf("size = %luKB", (unsigned long)(size64>>10));
126 printf("size = %luKB", (unsigned long)(size64>>10));
127 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
130 printf("size = %lu", (unsigned long)(size64));
132 if (read(sock, &buf, 128) < 0)
133 err("Failed/4: %m\n");
136 if (size64 > (~0UL >> 1)) {
137 #ifdef NBD_SET_SIZE_BLOCKS
138 if ((size64 >> 10) > (~0UL >> 1))
140 * If you really need NBDs larger than 2TB on 32-bit
141 * machines you can use blocksizes larger than 1kB
144 err("Device too large.\n");
148 if (ioctl(nbd, NBD_SET_BLKSIZE, 1UL << 10) < 0)
149 err("Ioctl/1.1a failed: %m\n");
150 size = (unsigned long)(size64 >> 10);
151 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
152 err("Ioctl/1.1b failed: %m\n");
155 err("Device too large.\n");
158 size = (unsigned long)size64;
159 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
160 err("Ioctl/1 failed: %m\n");
162 ioctl(nbd, NBD_CLEAR_SOCK);
163 if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
164 err("Ioctl/2 failed: %m\n");
168 err("You have to compile me on machine with swapping patch enabled in order to use it later.");
171 if (setsockopt(sock, SOL_SOCKET, SO_SWAPPING, &one, sizeof(int)) < 0)
172 err("Could not enable swapping: %m");
181 if (ioctl(nbd, NBD_DO_IT) < 0)
182 fprintf(stderr, "Kernel call returned: %m");
184 fprintf(stderr, "Kernel call returned.");
185 printf("Closing: que, ");
186 ioctl(nbd, NBD_CLEAR_QUE);
188 ioctl(nbd, NBD_CLEAR_SOCK);