1 /* nbd-client.c - network block device client
2 *
3 * Copyright 2010 Rob Landley <[email protected]>
4 *
5 * Not in SUSv4.
6
7 // This little dance is because a NEWTOY with - in the name tries to do
8 // things like prototype "nbd-client_main" which isn't a valid symbol. So
9 // we hide the underscore name and OLDTOY the name we want.
10 USE_NBD_CLIENT(NEWTOY(nbd_client, "<3>3b#<1>4294967295=4096ns", 0))
11 USE_NBD_CLIENT(OLDTOY(nbd-client, nbd_client, TOYFLAG_USR|TOYFLAG_BIN))
12
13 config NBD_CLIENT
14 bool "nbd-client"
15 default y
16 help
17 usage: nbd-client [-ns] [-b BLKSZ] HOST PORT DEVICE
18
19 -b Block size (default 4096)
20 -n Do not daemonize
21 -s nbd swap support (lock server into memory)
22 */
23
24 #define FOR_nbd_client
25 #include "toys.h"
26 #include <linux/nbd.h>
27
GLOBALS(long b;int nbd;)28 GLOBALS(
29 long b;
30
31 int nbd;
32 )
33
34 static void sig_cleanup(int catch)
35 {
36 // Flush on the way out
37 ioctl(TT.nbd, NBD_CLEAR_QUE);
38 ioctl(TT.nbd, NBD_CLEAR_SOCK);
39 _exit(catch ? 128+catch : 0);
40 }
41
nbd_client_main(void)42 void nbd_client_main(void)
43 {
44 int sock = -1, flags, temp;
45 unsigned long timeout = 0;
46 char *host=toys.optargs[0], *port=toys.optargs[1], *device=toys.optargs[2];
47 unsigned long long devsize;
48
49 // Daemonize in a nommu-friendly way, but retain stderr
50 if (toys.stacktop && !FLAG(n)) {
51 dup2(2, 222);
52 xvdaemon();
53 }
54 dup2(222, 2);
55 close(222);
56
57 TT.nbd = xopen(device, O_RDWR);
58 xsignal(SIGINT, sig_cleanup);
59 xsignal(SIGTERM, sig_cleanup);
60
61 for (;;) {
62 // Find and connect to server
63 sock = xconnectany(xgetaddrinfo(host, port, AF_UNSPEC, SOCK_STREAM, 0, 0));
64 temp = 1;
65 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &temp, sizeof(int));
66
67 // Read login data
68 xreadall(sock, toybuf, 152);
69 if (smemcmp(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16))
70 error_exit("bad login %s:%s", host, port);
71 devsize = SWAP_BE64(*(unsigned long long *)(toybuf+16));
72 flags = SWAP_BE32(*(int *)(toybuf+24));
73
74 // Use 4k block size
75 ioctl(TT.nbd, NBD_SET_BLKSIZE, TT.b);
76 ioctl(TT.nbd, NBD_SET_SIZE_BLOCKS, devsize/TT.b); // rounds down
77 ioctl(TT.nbd, NBD_CLEAR_SOCK);
78
79 // Locally respect read only exports
80 flags = (flags>>1)&1;
81 xioctl(TT.nbd, BLKROSET, &flags);
82
83 if (timeout && ioctl(TT.nbd, NBD_SET_TIMEOUT, timeout)<0) break;
84 if (ioctl(TT.nbd, NBD_SET_SOCK, sock) < 0) break;
85
86 if (FLAG(s)) mlockall(MCL_CURRENT|MCL_FUTURE);
87
88 // Open the device to force reread of the partition table.
89 if (!CFG_TOYBOX_FORK || !xfork()) {
90 char *s = strrchr(device, '/');
91 int i;
92
93 // Give device up to 10 seconds to come up
94 sprintf(toybuf, "/sys/block/%.32s/pid", s ? s+1 : device);
95 for (i=0; i<100; i++) {
96 if (access(toybuf, F_OK)) break;
97 msleep(100);
98 }
99 close(open(device, O_RDONLY));
100 if (CFG_TOYBOX_FORK) _exit(0);
101 }
102
103 // Process NBD requests until further notice.
104
105 if (ioctl(TT.nbd, NBD_DO_IT)>=0 || errno==EBADR) break;
106 close(sock);
107 ioctl(TT.nbd, NBD_CLEAR_QUE);
108 }
109
110 // Flush queue and exit.
111 if (CFG_TOYBOX_FREE) close(TT.nbd);
112 }
113