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>3ns", 0))
11 USE_NBD_CLIENT(OLDTOY(nbd-client, nbd_client, TOYFLAG_USR|TOYFLAG_BIN))
12
13 config NBD_CLIENT
14 bool "nbd-client"
15 depends on TOYBOX_FORK
16 default y
17 help
18 usage: nbd-client [-ns] HOST PORT DEVICE
19
20 -n Do not fork into background
21 -s nbd swap support (lock server into memory)
22 */
23
24 /* TODO:
25 usage: nbd-client [-sSpn] [-b BLKSZ] [-t SECS] [-N name] HOST PORT DEVICE
26
27 -b block size
28 -t timeout in seconds
29 -S sdp
30 -p persist
31 -n nofork
32 -d DEVICE
33 -c DEVICE
34 */
35
36 #define FOR_nbd_client
37 #include "toys.h"
38 #include <linux/nbd.h>
39
nbd_client_main(void)40 void 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 // Repeat until spanked
48
49 nbd = xopen(device, O_RDWR);
50 for (;;) {
51 int temp;
52
53 // Find and connect to server
54
55 sock = xconnect(host, port, AF_UNSPEC, SOCK_STREAM, 0, 0);
56 temp = 1;
57 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &temp, sizeof(int));
58
59 // Read login data
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 // Set 4k block size. Everything uses that these days.
68 ioctl(nbd, NBD_SET_BLKSIZE, 4096);
69 ioctl(nbd, NBD_SET_SIZE_BLOCKS, devsize/4096);
70 ioctl(nbd, NBD_CLEAR_SOCK);
71
72 // If the sucker was exported read only, respect that locally.
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 // Open the device to force reread of the partition table.
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 // Is it up yet? (Give it 10 seconds.)
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 // Daemonize here.
101
102 if (daemon(0,0)) perror_exit("daemonize");
103
104 // Process NBD requests until further notice.
105
106 if (ioctl(nbd, NBD_DO_IT)>=0 || errno==EBADR) break;
107 close(sock);
108 }
109
110 // Flush queue and exit.
111 ioctl(nbd, NBD_CLEAR_QUE);
112 ioctl(nbd, NBD_CLEAR_SOCK);
113 if (CFG_TOYBOX_FREE) close(nbd);
114 }
115