root / qemu-nbd.c @ 0e607a80
History | View | Annotate | Download (13 kB)
1 | cd831bd7 | ths | /*
|
---|---|---|---|
2 | 7a5ca864 | bellard | * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
|
3 | 7a5ca864 | bellard | *
|
4 | 7a5ca864 | bellard | * Network Block Device
|
5 | 7a5ca864 | bellard | *
|
6 | 7a5ca864 | bellard | * This program is free software; you can redistribute it and/or modify
|
7 | 7a5ca864 | bellard | * it under the terms of the GNU General Public License as published by
|
8 | 7a5ca864 | bellard | * the Free Software Foundation; under version 2 of the License.
|
9 | 7a5ca864 | bellard | *
|
10 | 7a5ca864 | bellard | * This program is distributed in the hope that it will be useful,
|
11 | 7a5ca864 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 | 7a5ca864 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 | 7a5ca864 | bellard | * GNU General Public License for more details.
|
14 | 7a5ca864 | bellard | *
|
15 | 7a5ca864 | bellard | * You should have received a copy of the GNU General Public License
|
16 | 8167ee88 | Blue Swirl | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
17 | 7a5ca864 | bellard | */
|
18 | 7a5ca864 | bellard | |
19 | 7a5ca864 | bellard | #include <qemu-common.h> |
20 | 7a5ca864 | bellard | #include "block_int.h" |
21 | 7a5ca864 | bellard | #include "nbd.h" |
22 | 7a5ca864 | bellard | |
23 | 7a5ca864 | bellard | #include <stdarg.h> |
24 | 7a5ca864 | bellard | #include <stdio.h> |
25 | 7a5ca864 | bellard | #include <getopt.h> |
26 | 7a5ca864 | bellard | #include <err.h> |
27 | cd831bd7 | ths | #include <sys/types.h> |
28 | 7a5ca864 | bellard | #include <sys/socket.h> |
29 | 7a5ca864 | bellard | #include <netinet/in.h> |
30 | 7a5ca864 | bellard | #include <netinet/tcp.h> |
31 | 7a5ca864 | bellard | #include <arpa/inet.h> |
32 | cd831bd7 | ths | #include <signal.h> |
33 | cd831bd7 | ths | |
34 | cd831bd7 | ths | #define SOCKET_PATH "/var/lock/qemu-nbd-%s" |
35 | 7a5ca864 | bellard | |
36 | 2f726488 | ths | #define NBD_BUFFER_SIZE (1024*1024) |
37 | 2f726488 | ths | |
38 | b1d8e52e | blueswir1 | static int verbose; |
39 | 7a5ca864 | bellard | |
40 | 7a5ca864 | bellard | static void usage(const char *name) |
41 | 7a5ca864 | bellard | { |
42 | 7a5ca864 | bellard | printf( |
43 | 7a5ca864 | bellard | "Usage: %s [OPTIONS] FILE\n"
|
44 | 7a5ca864 | bellard | "QEMU Disk Network Block Device Server\n"
|
45 | 7a5ca864 | bellard | "\n"
|
46 | 7a5ca864 | bellard | " -p, --port=PORT port to listen on (default `1024')\n"
|
47 | 7a5ca864 | bellard | " -o, --offset=OFFSET offset into the image\n"
|
48 | 7a5ca864 | bellard | " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
|
49 | cd831bd7 | ths | " -k, --socket=PATH path to the unix socket\n"
|
50 | cd831bd7 | ths | " (default '"SOCKET_PATH"')\n" |
51 | 7a5ca864 | bellard | " -r, --read-only export read-only\n"
|
52 | 7a5ca864 | bellard | " -P, --partition=NUM only expose partition NUM\n"
|
53 | 2f726488 | ths | " -s, --snapshot use snapshot file\n"
|
54 | 2f726488 | ths | " -n, --nocache disable host cache\n"
|
55 | cd831bd7 | ths | " -c, --connect=DEV connect FILE to the local NBD device DEV\n"
|
56 | cd831bd7 | ths | " -d, --disconnect disconnect the specified device\n"
|
57 | 3b05a8e9 | ths | " -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
|
58 | 75818250 | ths | " -t, --persistent don't exit on the last connection\n"
|
59 | 7a5ca864 | bellard | " -v, --verbose display extra debugging information\n"
|
60 | 7a5ca864 | bellard | " -h, --help display this help and exit\n"
|
61 | 7a5ca864 | bellard | " -V, --version output version information and exit\n"
|
62 | 7a5ca864 | bellard | "\n"
|
63 | 7a5ca864 | bellard | "Report bugs to <anthony@codemonkey.ws>\n"
|
64 | cd831bd7 | ths | , name, "DEVICE");
|
65 | 7a5ca864 | bellard | } |
66 | 7a5ca864 | bellard | |
67 | 7a5ca864 | bellard | static void version(const char *name) |
68 | 7a5ca864 | bellard | { |
69 | 7a5ca864 | bellard | printf( |
70 | 315bc7aa | ths | "%s version 0.0.1\n"
|
71 | 7a5ca864 | bellard | "Written by Anthony Liguori.\n"
|
72 | 7a5ca864 | bellard | "\n"
|
73 | 7a5ca864 | bellard | "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
|
74 | 7a5ca864 | bellard | "This is free software; see the source for copying conditions. There is NO\n"
|
75 | 7a5ca864 | bellard | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
|
76 | 315bc7aa | ths | , name); |
77 | 7a5ca864 | bellard | } |
78 | 7a5ca864 | bellard | |
79 | 7a5ca864 | bellard | struct partition_record
|
80 | 7a5ca864 | bellard | { |
81 | 7a5ca864 | bellard | uint8_t bootable; |
82 | 7a5ca864 | bellard | uint8_t start_head; |
83 | 7a5ca864 | bellard | uint32_t start_cylinder; |
84 | 7a5ca864 | bellard | uint8_t start_sector; |
85 | 7a5ca864 | bellard | uint8_t system; |
86 | 7a5ca864 | bellard | uint8_t end_head; |
87 | 7a5ca864 | bellard | uint8_t end_cylinder; |
88 | 7a5ca864 | bellard | uint8_t end_sector; |
89 | 7a5ca864 | bellard | uint32_t start_sector_abs; |
90 | 7a5ca864 | bellard | uint32_t nb_sectors_abs; |
91 | 7a5ca864 | bellard | }; |
92 | 7a5ca864 | bellard | |
93 | 7a5ca864 | bellard | static void read_partition(uint8_t *p, struct partition_record *r) |
94 | 7a5ca864 | bellard | { |
95 | 7a5ca864 | bellard | r->bootable = p[0];
|
96 | 7a5ca864 | bellard | r->start_head = p[1];
|
97 | 7a5ca864 | bellard | r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300); |
98 | 7a5ca864 | bellard | r->start_sector = p[2] & 0x3f; |
99 | 7a5ca864 | bellard | r->system = p[4];
|
100 | 7a5ca864 | bellard | r->end_head = p[5];
|
101 | 7a5ca864 | bellard | r->end_cylinder = p[7] | ((p[6] << 2) & 0x300); |
102 | 7a5ca864 | bellard | r->end_sector = p[6] & 0x3f; |
103 | 7a5ca864 | bellard | r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24; |
104 | 7a5ca864 | bellard | r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24; |
105 | 7a5ca864 | bellard | } |
106 | 7a5ca864 | bellard | |
107 | 7a5ca864 | bellard | static int find_partition(BlockDriverState *bs, int partition, |
108 | 7a5ca864 | bellard | off_t *offset, off_t *size) |
109 | 7a5ca864 | bellard | { |
110 | 7a5ca864 | bellard | struct partition_record mbr[4]; |
111 | 7a5ca864 | bellard | uint8_t data[512];
|
112 | 7a5ca864 | bellard | int i;
|
113 | 7a5ca864 | bellard | int ext_partnum = 4; |
114 | 7a5ca864 | bellard | |
115 | 7a5ca864 | bellard | if (bdrv_read(bs, 0, data, 1)) |
116 | 7a5ca864 | bellard | errx(EINVAL, "error while reading");
|
117 | 7a5ca864 | bellard | |
118 | 7a5ca864 | bellard | if (data[510] != 0x55 || data[511] != 0xaa) { |
119 | 7a5ca864 | bellard | errno = -EINVAL; |
120 | 7a5ca864 | bellard | return -1; |
121 | 7a5ca864 | bellard | } |
122 | 7a5ca864 | bellard | |
123 | 7a5ca864 | bellard | for (i = 0; i < 4; i++) { |
124 | 7a5ca864 | bellard | read_partition(&data[446 + 16 * i], &mbr[i]); |
125 | 7a5ca864 | bellard | |
126 | 7a5ca864 | bellard | if (!mbr[i].nb_sectors_abs)
|
127 | 7a5ca864 | bellard | continue;
|
128 | 7a5ca864 | bellard | |
129 | 7a5ca864 | bellard | if (mbr[i].system == 0xF || mbr[i].system == 0x5) { |
130 | 7a5ca864 | bellard | struct partition_record ext[4]; |
131 | 7a5ca864 | bellard | uint8_t data1[512];
|
132 | 7a5ca864 | bellard | int j;
|
133 | 7a5ca864 | bellard | |
134 | 7a5ca864 | bellard | if (bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) |
135 | 7a5ca864 | bellard | errx(EINVAL, "error while reading");
|
136 | 7a5ca864 | bellard | |
137 | 7a5ca864 | bellard | for (j = 0; j < 4; j++) { |
138 | 7a5ca864 | bellard | read_partition(&data1[446 + 16 * j], &ext[j]); |
139 | 7a5ca864 | bellard | if (!ext[j].nb_sectors_abs)
|
140 | 7a5ca864 | bellard | continue;
|
141 | 7a5ca864 | bellard | |
142 | 7a5ca864 | bellard | if ((ext_partnum + j + 1) == partition) { |
143 | 7a5ca864 | bellard | *offset = (uint64_t)ext[j].start_sector_abs << 9;
|
144 | 7a5ca864 | bellard | *size = (uint64_t)ext[j].nb_sectors_abs << 9;
|
145 | 7a5ca864 | bellard | return 0; |
146 | 7a5ca864 | bellard | } |
147 | 7a5ca864 | bellard | } |
148 | 7a5ca864 | bellard | ext_partnum += 4;
|
149 | 7a5ca864 | bellard | } else if ((i + 1) == partition) { |
150 | 7a5ca864 | bellard | *offset = (uint64_t)mbr[i].start_sector_abs << 9;
|
151 | 7a5ca864 | bellard | *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
|
152 | 7a5ca864 | bellard | return 0; |
153 | 7a5ca864 | bellard | } |
154 | 7a5ca864 | bellard | } |
155 | 7a5ca864 | bellard | |
156 | 7a5ca864 | bellard | errno = -ENOENT; |
157 | 7a5ca864 | bellard | return -1; |
158 | 7a5ca864 | bellard | } |
159 | 7a5ca864 | bellard | |
160 | cd831bd7 | ths | static void show_parts(const char *device) |
161 | cd831bd7 | ths | { |
162 | cd831bd7 | ths | if (fork() == 0) { |
163 | cd831bd7 | ths | int nbd;
|
164 | cd831bd7 | ths | |
165 | cd831bd7 | ths | /* linux just needs an open() to trigger
|
166 | cd831bd7 | ths | * the partition table update
|
167 | cd831bd7 | ths | * but remember to load the module with max_part != 0 :
|
168 | cd831bd7 | ths | * modprobe nbd max_part=63
|
169 | cd831bd7 | ths | */
|
170 | cd831bd7 | ths | nbd = open(device, O_RDWR); |
171 | cd831bd7 | ths | if (nbd != -1) |
172 | cd831bd7 | ths | close(nbd); |
173 | cd831bd7 | ths | exit(0);
|
174 | cd831bd7 | ths | } |
175 | cd831bd7 | ths | } |
176 | cd831bd7 | ths | |
177 | 7a5ca864 | bellard | int main(int argc, char **argv) |
178 | 7a5ca864 | bellard | { |
179 | 7a5ca864 | bellard | BlockDriverState *bs; |
180 | 7a5ca864 | bellard | off_t dev_offset = 0;
|
181 | 7a5ca864 | bellard | off_t offset = 0;
|
182 | 7a5ca864 | bellard | bool readonly = false; |
183 | cd831bd7 | ths | bool disconnect = false; |
184 | 7a5ca864 | bellard | const char *bindto = "0.0.0.0"; |
185 | 7a5ca864 | bellard | int port = 1024; |
186 | 7a5ca864 | bellard | struct sockaddr_in addr;
|
187 | 7a5ca864 | bellard | socklen_t addr_len = sizeof(addr);
|
188 | 7a5ca864 | bellard | off_t fd_size; |
189 | cd831bd7 | ths | char *device = NULL; |
190 | cd831bd7 | ths | char *socket = NULL; |
191 | cd831bd7 | ths | char sockpath[128]; |
192 | d6aa671f | aliguori | const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; |
193 | 7a5ca864 | bellard | struct option lopt[] = {
|
194 | 660f11be | Blue Swirl | { "help", 0, NULL, 'h' }, |
195 | 660f11be | Blue Swirl | { "version", 0, NULL, 'V' }, |
196 | 660f11be | Blue Swirl | { "bind", 1, NULL, 'b' }, |
197 | 660f11be | Blue Swirl | { "port", 1, NULL, 'p' }, |
198 | 660f11be | Blue Swirl | { "socket", 1, NULL, 'k' }, |
199 | 660f11be | Blue Swirl | { "offset", 1, NULL, 'o' }, |
200 | 660f11be | Blue Swirl | { "read-only", 0, NULL, 'r' }, |
201 | 660f11be | Blue Swirl | { "partition", 1, NULL, 'P' }, |
202 | 660f11be | Blue Swirl | { "connect", 1, NULL, 'c' }, |
203 | 660f11be | Blue Swirl | { "disconnect", 0, NULL, 'd' }, |
204 | 660f11be | Blue Swirl | { "snapshot", 0, NULL, 's' }, |
205 | 660f11be | Blue Swirl | { "nocache", 0, NULL, 'n' }, |
206 | 660f11be | Blue Swirl | { "shared", 1, NULL, 'e' }, |
207 | 660f11be | Blue Swirl | { "persistent", 0, NULL, 't' }, |
208 | 660f11be | Blue Swirl | { "verbose", 0, NULL, 'v' }, |
209 | 660f11be | Blue Swirl | { NULL, 0, NULL, 0 } |
210 | 7a5ca864 | bellard | }; |
211 | 7a5ca864 | bellard | int ch;
|
212 | 7a5ca864 | bellard | int opt_ind = 0; |
213 | 7a5ca864 | bellard | int li;
|
214 | 7a5ca864 | bellard | char *end;
|
215 | 2f726488 | ths | int flags = 0; |
216 | 7a5ca864 | bellard | int partition = -1; |
217 | cd831bd7 | ths | int ret;
|
218 | 3b05a8e9 | ths | int shared = 1; |
219 | 2f726488 | ths | uint8_t *data; |
220 | 3b05a8e9 | ths | fd_set fds; |
221 | 3b05a8e9 | ths | int *sharing_fds;
|
222 | 3b05a8e9 | ths | int fd;
|
223 | 3b05a8e9 | ths | int i;
|
224 | 3b05a8e9 | ths | int nb_fds = 0; |
225 | 3b05a8e9 | ths | int max_fd;
|
226 | 75818250 | ths | int persistent = 0; |
227 | 7a5ca864 | bellard | |
228 | 7a5ca864 | bellard | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
229 | 7a5ca864 | bellard | switch (ch) {
|
230 | 7a5ca864 | bellard | case 's': |
231 | 2f726488 | ths | flags |= BDRV_O_SNAPSHOT; |
232 | 2f726488 | ths | break;
|
233 | 2f726488 | ths | case 'n': |
234 | 9f7965c7 | aliguori | flags |= BDRV_O_NOCACHE; |
235 | 7a5ca864 | bellard | break;
|
236 | 7a5ca864 | bellard | case 'b': |
237 | 7a5ca864 | bellard | bindto = optarg; |
238 | 7a5ca864 | bellard | break;
|
239 | 7a5ca864 | bellard | case 'p': |
240 | 7a5ca864 | bellard | li = strtol(optarg, &end, 0);
|
241 | 7a5ca864 | bellard | if (*end) {
|
242 | 7a5ca864 | bellard | errx(EINVAL, "Invalid port `%s'", optarg);
|
243 | 7a5ca864 | bellard | } |
244 | 7a5ca864 | bellard | if (li < 1 || li > 65535) { |
245 | 7a5ca864 | bellard | errx(EINVAL, "Port out of range `%s'", optarg);
|
246 | 7a5ca864 | bellard | } |
247 | 7a5ca864 | bellard | port = (uint16_t)li; |
248 | 7a5ca864 | bellard | break;
|
249 | 7a5ca864 | bellard | case 'o': |
250 | 7a5ca864 | bellard | dev_offset = strtoll (optarg, &end, 0);
|
251 | 7a5ca864 | bellard | if (*end) {
|
252 | 7a5ca864 | bellard | errx(EINVAL, "Invalid offset `%s'", optarg);
|
253 | 7a5ca864 | bellard | } |
254 | 7a5ca864 | bellard | if (dev_offset < 0) { |
255 | 7a5ca864 | bellard | errx(EINVAL, "Offset must be positive `%s'", optarg);
|
256 | 7a5ca864 | bellard | } |
257 | 7a5ca864 | bellard | break;
|
258 | 7a5ca864 | bellard | case 'r': |
259 | 7a5ca864 | bellard | readonly = true;
|
260 | 7a5ca864 | bellard | break;
|
261 | 7a5ca864 | bellard | case 'P': |
262 | 7a5ca864 | bellard | partition = strtol(optarg, &end, 0);
|
263 | 7a5ca864 | bellard | if (*end)
|
264 | 7a5ca864 | bellard | errx(EINVAL, "Invalid partition `%s'", optarg);
|
265 | 7a5ca864 | bellard | if (partition < 1 || partition > 8) |
266 | 7a5ca864 | bellard | errx(EINVAL, "Invalid partition %d", partition);
|
267 | 7a5ca864 | bellard | break;
|
268 | cd831bd7 | ths | case 'k': |
269 | cd831bd7 | ths | socket = optarg; |
270 | cd831bd7 | ths | if (socket[0] != '/') |
271 | cd831bd7 | ths | errx(EINVAL, "socket path must be absolute\n");
|
272 | cd831bd7 | ths | break;
|
273 | cd831bd7 | ths | case 'd': |
274 | cd831bd7 | ths | disconnect = true;
|
275 | cd831bd7 | ths | break;
|
276 | cd831bd7 | ths | case 'c': |
277 | cd831bd7 | ths | device = optarg; |
278 | cd831bd7 | ths | break;
|
279 | 3b05a8e9 | ths | case 'e': |
280 | 3b05a8e9 | ths | shared = strtol(optarg, &end, 0);
|
281 | 3b05a8e9 | ths | if (*end) {
|
282 | 3b05a8e9 | ths | errx(EINVAL, "Invalid shared device number '%s'", optarg);
|
283 | 3b05a8e9 | ths | } |
284 | 3b05a8e9 | ths | if (shared < 1) { |
285 | 3b05a8e9 | ths | errx(EINVAL, "Shared device number must be greater than 0\n");
|
286 | 3b05a8e9 | ths | } |
287 | 3b05a8e9 | ths | break;
|
288 | 75818250 | ths | case 't': |
289 | 75818250 | ths | persistent = 1;
|
290 | 75818250 | ths | break;
|
291 | 7a5ca864 | bellard | case 'v': |
292 | 7a5ca864 | bellard | verbose = 1;
|
293 | 7a5ca864 | bellard | break;
|
294 | 7a5ca864 | bellard | case 'V': |
295 | 7a5ca864 | bellard | version(argv[0]);
|
296 | 7a5ca864 | bellard | exit(0);
|
297 | 7a5ca864 | bellard | break;
|
298 | 7a5ca864 | bellard | case 'h': |
299 | 7a5ca864 | bellard | usage(argv[0]);
|
300 | 7a5ca864 | bellard | exit(0);
|
301 | 7a5ca864 | bellard | break;
|
302 | 7a5ca864 | bellard | case '?': |
303 | 7a5ca864 | bellard | errx(EINVAL, "Try `%s --help' for more information.",
|
304 | 7a5ca864 | bellard | argv[0]);
|
305 | 7a5ca864 | bellard | } |
306 | 7a5ca864 | bellard | } |
307 | 7a5ca864 | bellard | |
308 | 7a5ca864 | bellard | if ((argc - optind) != 1) { |
309 | 7a5ca864 | bellard | errx(EINVAL, "Invalid number of argument.\n"
|
310 | 7a5ca864 | bellard | "Try `%s --help' for more information.",
|
311 | 7a5ca864 | bellard | argv[0]);
|
312 | 7a5ca864 | bellard | } |
313 | 7a5ca864 | bellard | |
314 | cd831bd7 | ths | if (disconnect) {
|
315 | cd831bd7 | ths | fd = open(argv[optind], O_RDWR); |
316 | cd831bd7 | ths | if (fd == -1) |
317 | cd831bd7 | ths | errx(errno, "Cannot open %s", argv[optind]);
|
318 | cd831bd7 | ths | |
319 | cd831bd7 | ths | nbd_disconnect(fd); |
320 | cd831bd7 | ths | |
321 | cd831bd7 | ths | close(fd); |
322 | cd831bd7 | ths | |
323 | cd831bd7 | ths | printf("%s disconnected\n", argv[optind]);
|
324 | cd831bd7 | ths | |
325 | cd831bd7 | ths | return 0; |
326 | cd831bd7 | ths | } |
327 | cd831bd7 | ths | |
328 | 7a5ca864 | bellard | bdrv_init(); |
329 | 7a5ca864 | bellard | |
330 | 7a5ca864 | bellard | bs = bdrv_new("hda");
|
331 | 7a5ca864 | bellard | if (bs == NULL) |
332 | 7a5ca864 | bellard | return 1; |
333 | 7a5ca864 | bellard | |
334 | 2f726488 | ths | if (bdrv_open(bs, argv[optind], flags) == -1) |
335 | 7a5ca864 | bellard | return 1; |
336 | 7a5ca864 | bellard | |
337 | 7a5ca864 | bellard | fd_size = bs->total_sectors * 512;
|
338 | 7a5ca864 | bellard | |
339 | 7a5ca864 | bellard | if (partition != -1 && |
340 | 7a5ca864 | bellard | find_partition(bs, partition, &dev_offset, &fd_size)) |
341 | 7a5ca864 | bellard | errx(errno, "Could not find partition %d", partition);
|
342 | 7a5ca864 | bellard | |
343 | cd831bd7 | ths | if (device) {
|
344 | 3b05a8e9 | ths | pid_t pid; |
345 | 3b05a8e9 | ths | int sock;
|
346 | 3b05a8e9 | ths | |
347 | f5de141b | Anthony Liguori | if (!verbose) {
|
348 | f5de141b | Anthony Liguori | /* detach client and server */
|
349 | f5de141b | Anthony Liguori | if (daemon(0, 0) == -1) { |
350 | f5de141b | Anthony Liguori | errx(errno, "Failed to daemonize");
|
351 | f5de141b | Anthony Liguori | } |
352 | f5de141b | Anthony Liguori | } |
353 | cd831bd7 | ths | |
354 | cd831bd7 | ths | if (socket == NULL) { |
355 | cd831bd7 | ths | sprintf(sockpath, SOCKET_PATH, basename(device)); |
356 | cd831bd7 | ths | socket = sockpath; |
357 | cd831bd7 | ths | } |
358 | cd831bd7 | ths | |
359 | cd831bd7 | ths | pid = fork(); |
360 | cd831bd7 | ths | if (pid < 0) |
361 | cd831bd7 | ths | return 1; |
362 | cd831bd7 | ths | if (pid != 0) { |
363 | cd831bd7 | ths | off_t size; |
364 | cd831bd7 | ths | size_t blocksize; |
365 | cd831bd7 | ths | |
366 | cd831bd7 | ths | ret = 0;
|
367 | cd831bd7 | ths | bdrv_close(bs); |
368 | cd831bd7 | ths | |
369 | cd831bd7 | ths | do {
|
370 | cd831bd7 | ths | sock = unix_socket_outgoing(socket); |
371 | cd831bd7 | ths | if (sock == -1) { |
372 | cd831bd7 | ths | if (errno != ENOENT && errno != ECONNREFUSED)
|
373 | cd831bd7 | ths | goto out;
|
374 | cd831bd7 | ths | sleep(1); /* wait children */ |
375 | cd831bd7 | ths | } |
376 | cd831bd7 | ths | } while (sock == -1); |
377 | cd831bd7 | ths | |
378 | cd831bd7 | ths | fd = open(device, O_RDWR); |
379 | cd831bd7 | ths | if (fd == -1) { |
380 | cd831bd7 | ths | ret = 1;
|
381 | cd831bd7 | ths | goto out;
|
382 | cd831bd7 | ths | } |
383 | cd831bd7 | ths | |
384 | cd831bd7 | ths | ret = nbd_receive_negotiate(sock, &size, &blocksize); |
385 | cd831bd7 | ths | if (ret == -1) { |
386 | cd831bd7 | ths | ret = 1;
|
387 | cd831bd7 | ths | goto out;
|
388 | cd831bd7 | ths | } |
389 | cd831bd7 | ths | |
390 | cd831bd7 | ths | ret = nbd_init(fd, sock, size, blocksize); |
391 | cd831bd7 | ths | if (ret == -1) { |
392 | cd831bd7 | ths | ret = 1;
|
393 | cd831bd7 | ths | goto out;
|
394 | cd831bd7 | ths | } |
395 | cd831bd7 | ths | |
396 | cd831bd7 | ths | printf("NBD device %s is now connected to file %s\n",
|
397 | cd831bd7 | ths | device, argv[optind]); |
398 | cd831bd7 | ths | |
399 | cd831bd7 | ths | /* update partition table */
|
400 | cd831bd7 | ths | |
401 | cd831bd7 | ths | show_parts(device); |
402 | cd831bd7 | ths | |
403 | cd831bd7 | ths | nbd_client(fd, sock); |
404 | cd831bd7 | ths | close(fd); |
405 | cd831bd7 | ths | out:
|
406 | cd831bd7 | ths | kill(pid, SIGTERM); |
407 | cd831bd7 | ths | unlink(socket); |
408 | cd831bd7 | ths | |
409 | cd831bd7 | ths | return ret;
|
410 | cd831bd7 | ths | } |
411 | cd831bd7 | ths | /* children */
|
412 | cd831bd7 | ths | } |
413 | cd831bd7 | ths | |
414 | 3b05a8e9 | ths | sharing_fds = qemu_malloc((shared + 1) * sizeof(int)); |
415 | 3b05a8e9 | ths | |
416 | cd831bd7 | ths | if (socket) {
|
417 | 3b05a8e9 | ths | sharing_fds[0] = unix_socket_incoming(socket);
|
418 | cd831bd7 | ths | } else {
|
419 | 3b05a8e9 | ths | sharing_fds[0] = tcp_socket_incoming(bindto, port);
|
420 | cd831bd7 | ths | } |
421 | cd831bd7 | ths | |
422 | 3b05a8e9 | ths | if (sharing_fds[0] == -1) |
423 | 7a5ca864 | bellard | return 1; |
424 | 3b05a8e9 | ths | max_fd = sharing_fds[0];
|
425 | 3b05a8e9 | ths | nb_fds++; |
426 | 7a5ca864 | bellard | |
427 | 3b05a8e9 | ths | data = qemu_memalign(512, NBD_BUFFER_SIZE);
|
428 | 3b05a8e9 | ths | if (data == NULL) |
429 | 3b05a8e9 | ths | errx(ENOMEM, "Cannot allocate data buffer");
|
430 | 7a5ca864 | bellard | |
431 | 3b05a8e9 | ths | do {
|
432 | 7a5ca864 | bellard | |
433 | 3b05a8e9 | ths | FD_ZERO(&fds); |
434 | 3b05a8e9 | ths | for (i = 0; i < nb_fds; i++) |
435 | 3b05a8e9 | ths | FD_SET(sharing_fds[i], &fds); |
436 | 3b05a8e9 | ths | |
437 | 3b05a8e9 | ths | ret = select(max_fd + 1, &fds, NULL, NULL, NULL); |
438 | 3b05a8e9 | ths | if (ret == -1) |
439 | 3b05a8e9 | ths | break;
|
440 | 3b05a8e9 | ths | |
441 | 3b05a8e9 | ths | if (FD_ISSET(sharing_fds[0], &fds)) |
442 | 3b05a8e9 | ths | ret--; |
443 | 3b05a8e9 | ths | for (i = 1; i < nb_fds && ret; i++) { |
444 | 3b05a8e9 | ths | if (FD_ISSET(sharing_fds[i], &fds)) {
|
445 | 3b05a8e9 | ths | if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
|
446 | 3b05a8e9 | ths | &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
|
447 | 3b05a8e9 | ths | close(sharing_fds[i]); |
448 | 3b05a8e9 | ths | nb_fds--; |
449 | 3b05a8e9 | ths | sharing_fds[i] = sharing_fds[nb_fds]; |
450 | 3b05a8e9 | ths | i--; |
451 | 3b05a8e9 | ths | } |
452 | 3b05a8e9 | ths | ret--; |
453 | 3b05a8e9 | ths | } |
454 | 3b05a8e9 | ths | } |
455 | 3b05a8e9 | ths | /* new connection ? */
|
456 | 3b05a8e9 | ths | if (FD_ISSET(sharing_fds[0], &fds)) { |
457 | 3b05a8e9 | ths | if (nb_fds < shared + 1) { |
458 | 3b05a8e9 | ths | sharing_fds[nb_fds] = accept(sharing_fds[0],
|
459 | 3b05a8e9 | ths | (struct sockaddr *)&addr,
|
460 | 3b05a8e9 | ths | &addr_len); |
461 | 3b05a8e9 | ths | if (sharing_fds[nb_fds] != -1 && |
462 | 27982661 | aliguori | nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) {
|
463 | 3b05a8e9 | ths | if (sharing_fds[nb_fds] > max_fd)
|
464 | 3b05a8e9 | ths | max_fd = sharing_fds[nb_fds]; |
465 | 3b05a8e9 | ths | nb_fds++; |
466 | 3b05a8e9 | ths | } |
467 | 3b05a8e9 | ths | } |
468 | 3b05a8e9 | ths | } |
469 | 75818250 | ths | } while (persistent || nb_fds > 1); |
470 | 2f726488 | ths | qemu_free(data); |
471 | 7a5ca864 | bellard | |
472 | 3b05a8e9 | ths | close(sharing_fds[0]);
|
473 | 7a5ca864 | bellard | bdrv_close(bs); |
474 | 3b05a8e9 | ths | qemu_free(sharing_fds); |
475 | cd831bd7 | ths | if (socket)
|
476 | cd831bd7 | ths | unlink(socket); |
477 | 7a5ca864 | bellard | |
478 | 7a5ca864 | bellard | return 0; |
479 | 7a5ca864 | bellard | } |