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