• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   default y
16   help
17     usage: nbd-client [-ns] HOST PORT DEVICE
18 
19     -n	Do not fork into background
20     -s	nbd swap support (lock server into memory)
21 */
22 
23 /*  TODO:
24     usage: nbd-client [-sSpn] [-b BLKSZ] [-t SECS] [-N name] HOST PORT DEVICE
25 
26     -b	block size
27     -t	timeout in seconds
28     -S	sdp
29     -p	persist
30     -n	nofork
31     -d	DEVICE
32     -c	DEVICE
33 */
34 
35 #define FOR_nbd_client
36 #include "toys.h"
37 #include <linux/nbd.h>
38 
nbd_client_main(void)39 void nbd_client_main(void)
40 {
41   int sock = -1, nbd, flags;
42   unsigned long timeout = 0;
43   struct addrinfo *addr, *p;
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     struct addrinfo hints;
53 
54     // Find and connect to server
55 
56     memset(&hints, 0, sizeof(hints));
57     hints.ai_family = PF_UNSPEC;
58     hints.ai_socktype = SOCK_STREAM;
59     if (getaddrinfo(host, port, &hints, &addr)) addr = 0;
60     for (p = addr; p; p = p->ai_next) {
61       sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
62       if (-1 != connect(sock, p->ai_addr, p->ai_addrlen)) break;
63       close(sock);
64     }
65     freeaddrinfo(addr);
66 
67     if (!p) perror_exit("%s:%s", host, port);
68 
69     temp = 1;
70     setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &temp, sizeof(int));
71 
72     // Read login data
73 
74     xreadall(sock, toybuf, 152);
75     if (memcmp(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16))
76       error_exit("bad login %s:%s", host, port);
77     devsize = SWAP_BE64(*(uint64_t *)(toybuf+16));
78     flags = SWAP_BE32(*(int *)(toybuf+24));
79 
80     // Set 4k block size.  Everything uses that these days.
81     ioctl(nbd, NBD_SET_BLKSIZE, 4096);
82     ioctl(nbd, NBD_SET_SIZE_BLOCKS, devsize/4096);
83     ioctl(nbd, NBD_CLEAR_SOCK);
84 
85     // If the sucker was exported read only, respect that locally.
86     temp = (flags & 2) ? 1 : 0;
87     xioctl(nbd, BLKROSET, &temp);
88 
89     if (timeout && ioctl(nbd, NBD_SET_TIMEOUT, timeout)<0) break;
90     if (ioctl(nbd, NBD_SET_SOCK, sock) < 0) break;
91 
92     if (toys.optflags & FLAG_s) mlockall(MCL_CURRENT|MCL_FUTURE);
93 
94     // Open the device to force reread of the partition table.
95     if ((toys.optflags & FLAG_n) || !xfork()) {
96       char *s = strrchr(device, '/');
97       int i;
98 
99       sprintf(toybuf, "/sys/block/%.32s/pid", s ? s+1 : device);
100       // Is it up yet? (Give it 10 seconds.)
101       for (i=0; i<100; i++) {
102         temp = open(toybuf, O_RDONLY);
103         if (temp == -1) msleep(100);
104         else {
105           close(temp);
106           break;
107         }
108       }
109       close(open(device, O_RDONLY));
110       if (!(toys.optflags & FLAG_n)) exit(0);
111     }
112 
113     // Daemonize here.
114 
115     if (daemon(0,0)) perror_exit("daemonize");
116 
117     // Process NBD requests until further notice.
118 
119     if (ioctl(nbd, NBD_DO_IT)>=0 || errno==EBADR) break;
120     close(sock);
121   }
122 
123   // Flush queue and exit.
124   ioctl(nbd, NBD_CLEAR_QUE);
125   ioctl(nbd, NBD_CLEAR_SOCK);
126   if (CFG_TOYBOX_FREE) close(nbd);
127 }
128