1 /* nbd-client.c - network block device client
2 *
3 * Copyright 2010 Rob Landley <rob@landley.net>
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 /* TODO:
25 usage: nbd-client [-Sp] [-t SECS] [-N name] HOST PORT DEVICE
26
27 -t timeout in seconds
28 -S sdp
29 -p persist
30 -d DEVICE
31 -c DEVICE
32 */
33
34 #define FOR_nbd_client
35 #include "toys.h"
36 #include <linux/nbd.h>
37
GLOBALS(long b;int nbd;)38 GLOBALS(
39 long b;
40
41 int nbd;
42 )
43
44 static void sig_cleanup(int catch)
45 {
46 // Flush on the way out
47 ioctl(TT.nbd, NBD_CLEAR_QUE);
48 ioctl(TT.nbd, NBD_CLEAR_SOCK);
49 _exit(catch ? 128+catch : 0);
50 }
51
nbd_client_main(void)52 void nbd_client_main(void)
53 {
54 int sock = -1, flags, temp;
55 unsigned long timeout = 0;
56 char *host=toys.optargs[0], *port=toys.optargs[1], *device=toys.optargs[2];
57 unsigned long long devsize;
58
59 // Daemonize in a nommu-friendly way, but retain stderr
60 if (toys.stacktop && !FLAG(n)) {
61 dup2(2, 222);
62 xvdaemon();
63 }
64 dup2(222, 2);
65 close(222);
66
67 TT.nbd = xopen(device, O_RDWR);
68 xsignal(SIGINT, sig_cleanup);
69 xsignal(SIGTERM, sig_cleanup);
70
71 for (;;) {
72 // Find and connect to server
73 sock = xconnectany(xgetaddrinfo(host, port, AF_UNSPEC, SOCK_STREAM, 0, 0));
74 temp = 1;
75 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &temp, sizeof(int));
76
77 // Read login data
78 xreadall(sock, toybuf, 152);
79 if (smemcmp(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16))
80 error_exit("bad login %s:%s", host, port);
81 devsize = SWAP_BE64(*(unsigned long long *)(toybuf+16));
82 flags = SWAP_BE32(*(int *)(toybuf+24));
83
84 // Use 4k block size
85 ioctl(TT.nbd, NBD_SET_BLKSIZE, TT.b);
86 ioctl(TT.nbd, NBD_SET_SIZE_BLOCKS, devsize/TT.b); // rounds down
87 ioctl(TT.nbd, NBD_CLEAR_SOCK);
88
89 // Locally respect read only exports
90 flags = (flags>>1)&1;
91 xioctl(TT.nbd, BLKROSET, &flags);
92
93 if (timeout && ioctl(TT.nbd, NBD_SET_TIMEOUT, timeout)<0) break;
94 if (ioctl(TT.nbd, NBD_SET_SOCK, sock) < 0) break;
95
96 if (FLAG(s)) mlockall(MCL_CURRENT|MCL_FUTURE);
97
98 // Open the device to force reread of the partition table.
99 if (!CFG_TOYBOX_FORK || !xfork()) {
100 char *s = strrchr(device, '/');
101 int i;
102
103 // Give device up to 10 seconds to come up
104 sprintf(toybuf, "/sys/block/%.32s/pid", s ? s+1 : device);
105 for (i=0; i<100; i++) {
106 if (access(toybuf, F_OK)) break;
107 msleep(100);
108 }
109 close(open(device, O_RDONLY));
110 if (CFG_TOYBOX_FORK) _exit(0);
111 }
112
113 // Process NBD requests until further notice.
114
115 if (ioctl(TT.nbd, NBD_DO_IT)>=0 || errno==EBADR) break;
116 close(sock);
117 ioctl(TT.nbd, NBD_CLEAR_QUE);
118 }
119
120 // Flush queue and exit.
121 if (CFG_TOYBOX_FREE) close(TT.nbd);
122 }
123