1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#define FOR_nbd_client
37#include "toys.h"
38#include <linux/nbd.h>
39
40void nbd_client_main(void)
41{
42 int sock = -1, nbd, flags;
43 unsigned long timeout = 0;
44 char *host=toys.optargs[0], *port=toys.optargs[1], *device=toys.optargs[2];
45 uint64_t devsize;
46
47
48
49 nbd = xopen(device, O_RDWR);
50 for (;;) {
51 int temp;
52
53
54
55 sock = xconnect(xgetaddrinfo(host, port, AF_UNSPEC, SOCK_STREAM, 0, 0));
56 temp = 1;
57 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &temp, sizeof(int));
58
59
60
61 xreadall(sock, toybuf, 152);
62 if (memcmp(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16))
63 error_exit("bad login %s:%s", host, port);
64 devsize = SWAP_BE64(*(uint64_t *)(toybuf+16));
65 flags = SWAP_BE32(*(int *)(toybuf+24));
66
67
68 ioctl(nbd, NBD_SET_BLKSIZE, 4096);
69 ioctl(nbd, NBD_SET_SIZE_BLOCKS, devsize/4096);
70 ioctl(nbd, NBD_CLEAR_SOCK);
71
72
73 temp = (flags & 2) ? 1 : 0;
74 xioctl(nbd, BLKROSET, &temp);
75
76 if (timeout && ioctl(nbd, NBD_SET_TIMEOUT, timeout)<0) break;
77 if (ioctl(nbd, NBD_SET_SOCK, sock) < 0) break;
78
79 if (toys.optflags & FLAG_s) mlockall(MCL_CURRENT|MCL_FUTURE);
80
81
82 if ((toys.optflags & FLAG_n) || !xfork()) {
83 char *s = strrchr(device, '/');
84 int i;
85
86 sprintf(toybuf, "/sys/block/%.32s/pid", s ? s+1 : device);
87
88 for (i=0; i<100; i++) {
89 temp = open(toybuf, O_RDONLY);
90 if (temp == -1) msleep(100);
91 else {
92 close(temp);
93 break;
94 }
95 }
96 close(open(device, O_RDONLY));
97 if (!(toys.optflags & FLAG_n)) exit(0);
98 }
99
100
101
102 if (daemon(0,0)) perror_exit("daemonize");
103
104
105
106 if (ioctl(nbd, NBD_DO_IT)>=0 || errno==EBADR) break;
107 close(sock);
108 }
109
110
111 ioctl(nbd, NBD_CLEAR_QUE);
112 ioctl(nbd, NBD_CLEAR_SOCK);
113 if (CFG_TOYBOX_FREE) close(nbd);
114}
115