root / qemu-nbd.c @ b6353bea
History | View | Annotate | Download (13.2 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 | 2bff4b6f | Blue Swirl | #include <libgen.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 | b1d8e52e | blueswir1 | static 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 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "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 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "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 | d6aa671f | aliguori | const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; |
194 | 7a5ca864 | bellard | struct option lopt[] = {
|
195 | 660f11be | Blue Swirl | { "help", 0, NULL, 'h' }, |
196 | 660f11be | Blue Swirl | { "version", 0, NULL, 'V' }, |
197 | 660f11be | Blue Swirl | { "bind", 1, NULL, 'b' }, |
198 | 660f11be | Blue Swirl | { "port", 1, NULL, 'p' }, |
199 | 660f11be | Blue Swirl | { "socket", 1, NULL, 'k' }, |
200 | 660f11be | Blue Swirl | { "offset", 1, NULL, 'o' }, |
201 | 660f11be | Blue Swirl | { "read-only", 0, NULL, 'r' }, |
202 | 660f11be | Blue Swirl | { "partition", 1, NULL, 'P' }, |
203 | 660f11be | Blue Swirl | { "connect", 1, NULL, 'c' }, |
204 | 660f11be | Blue Swirl | { "disconnect", 0, NULL, 'd' }, |
205 | 660f11be | Blue Swirl | { "snapshot", 0, NULL, 's' }, |
206 | 660f11be | Blue Swirl | { "nocache", 0, NULL, 'n' }, |
207 | 660f11be | Blue Swirl | { "shared", 1, NULL, 'e' }, |
208 | 660f11be | Blue Swirl | { "persistent", 0, NULL, 't' }, |
209 | 660f11be | Blue Swirl | { "verbose", 0, NULL, 'v' }, |
210 | 660f11be | Blue Swirl | { NULL, 0, NULL, 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 | f5edb014 | Naphtali Sprei | int flags = BDRV_O_RDWR;
|
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 | 9f7965c7 | aliguori | flags |= BDRV_O_NOCACHE; |
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 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
|
244 | 7a5ca864 | bellard | } |
245 | 7a5ca864 | bellard | if (li < 1 || li > 65535) { |
246 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "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 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
|
254 | 7a5ca864 | bellard | } |
255 | 7a5ca864 | bellard | if (dev_offset < 0) { |
256 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
|
257 | 7a5ca864 | bellard | } |
258 | 7a5ca864 | bellard | break;
|
259 | 7a5ca864 | bellard | case 'r': |
260 | 7a5ca864 | bellard | readonly = true;
|
261 | 07108b29 | Naphtali Sprei | flags &= ~BDRV_O_RDWR; |
262 | 7a5ca864 | bellard | break;
|
263 | 7a5ca864 | bellard | case 'P': |
264 | 7a5ca864 | bellard | partition = strtol(optarg, &end, 0);
|
265 | 7a5ca864 | bellard | if (*end)
|
266 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
|
267 | 7a5ca864 | bellard | if (partition < 1 || partition > 8) |
268 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid partition %d", partition);
|
269 | 7a5ca864 | bellard | break;
|
270 | cd831bd7 | ths | case 'k': |
271 | cd831bd7 | ths | socket = optarg; |
272 | cd831bd7 | ths | if (socket[0] != '/') |
273 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "socket path must be absolute\n");
|
274 | cd831bd7 | ths | break;
|
275 | cd831bd7 | ths | case 'd': |
276 | cd831bd7 | ths | disconnect = true;
|
277 | cd831bd7 | ths | break;
|
278 | cd831bd7 | ths | case 'c': |
279 | cd831bd7 | ths | device = optarg; |
280 | cd831bd7 | ths | break;
|
281 | 3b05a8e9 | ths | case 'e': |
282 | 3b05a8e9 | ths | shared = strtol(optarg, &end, 0);
|
283 | 3b05a8e9 | ths | if (*end) {
|
284 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
|
285 | 3b05a8e9 | ths | } |
286 | 3b05a8e9 | ths | if (shared < 1) { |
287 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
|
288 | 3b05a8e9 | ths | } |
289 | 3b05a8e9 | ths | break;
|
290 | 75818250 | ths | case 't': |
291 | 75818250 | ths | persistent = 1;
|
292 | 75818250 | ths | break;
|
293 | 7a5ca864 | bellard | case 'v': |
294 | 7a5ca864 | bellard | verbose = 1;
|
295 | 7a5ca864 | bellard | break;
|
296 | 7a5ca864 | bellard | case 'V': |
297 | 7a5ca864 | bellard | version(argv[0]);
|
298 | 7a5ca864 | bellard | exit(0);
|
299 | 7a5ca864 | bellard | break;
|
300 | 7a5ca864 | bellard | case 'h': |
301 | 7a5ca864 | bellard | usage(argv[0]);
|
302 | 7a5ca864 | bellard | exit(0);
|
303 | 7a5ca864 | bellard | break;
|
304 | 7a5ca864 | bellard | case '?': |
305 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Try `%s --help' for more information.",
|
306 | 7a5ca864 | bellard | argv[0]);
|
307 | 7a5ca864 | bellard | } |
308 | 7a5ca864 | bellard | } |
309 | 7a5ca864 | bellard | |
310 | 7a5ca864 | bellard | if ((argc - optind) != 1) { |
311 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid number of argument.\n"
|
312 | 7a5ca864 | bellard | "Try `%s --help' for more information.",
|
313 | 7a5ca864 | bellard | argv[0]);
|
314 | 7a5ca864 | bellard | } |
315 | 7a5ca864 | bellard | |
316 | cd831bd7 | ths | if (disconnect) {
|
317 | cd831bd7 | ths | fd = open(argv[optind], O_RDWR); |
318 | cd831bd7 | ths | if (fd == -1) |
319 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Cannot open %s", argv[optind]);
|
320 | cd831bd7 | ths | |
321 | cd831bd7 | ths | nbd_disconnect(fd); |
322 | cd831bd7 | ths | |
323 | cd831bd7 | ths | close(fd); |
324 | cd831bd7 | ths | |
325 | cd831bd7 | ths | printf("%s disconnected\n", argv[optind]);
|
326 | cd831bd7 | ths | |
327 | cd831bd7 | ths | return 0; |
328 | cd831bd7 | ths | } |
329 | cd831bd7 | ths | |
330 | 7a5ca864 | bellard | bdrv_init(); |
331 | 7a5ca864 | bellard | |
332 | 7a5ca864 | bellard | bs = bdrv_new("hda");
|
333 | 7a5ca864 | bellard | if (bs == NULL) |
334 | 7a5ca864 | bellard | return 1; |
335 | 7a5ca864 | bellard | |
336 | a16c174c | Ryota Ozaki | if (bdrv_open(bs, argv[optind], flags) < 0) |
337 | 7a5ca864 | bellard | return 1; |
338 | 7a5ca864 | bellard | |
339 | 7a5ca864 | bellard | fd_size = bs->total_sectors * 512;
|
340 | 7a5ca864 | bellard | |
341 | 7a5ca864 | bellard | if (partition != -1 && |
342 | 7a5ca864 | bellard | find_partition(bs, partition, &dev_offset, &fd_size)) |
343 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Could not find partition %d", partition);
|
344 | 7a5ca864 | bellard | |
345 | cd831bd7 | ths | if (device) {
|
346 | 3b05a8e9 | ths | pid_t pid; |
347 | 3b05a8e9 | ths | int sock;
|
348 | 3b05a8e9 | ths | |
349 | f5de141b | Anthony Liguori | if (!verbose) {
|
350 | f5de141b | Anthony Liguori | /* detach client and server */
|
351 | f5de141b | Anthony Liguori | if (daemon(0, 0) == -1) { |
352 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Failed to daemonize");
|
353 | f5de141b | Anthony Liguori | } |
354 | f5de141b | Anthony Liguori | } |
355 | cd831bd7 | ths | |
356 | cd831bd7 | ths | if (socket == NULL) { |
357 | 22ff51ee | Blue Swirl | snprintf(sockpath, sizeof(sockpath), SOCKET_PATH,
|
358 | 22ff51ee | Blue Swirl | basename(device)); |
359 | cd831bd7 | ths | socket = sockpath; |
360 | cd831bd7 | ths | } |
361 | cd831bd7 | ths | |
362 | cd831bd7 | ths | pid = fork(); |
363 | cd831bd7 | ths | if (pid < 0) |
364 | cd831bd7 | ths | return 1; |
365 | cd831bd7 | ths | if (pid != 0) { |
366 | cd831bd7 | ths | off_t size; |
367 | cd831bd7 | ths | size_t blocksize; |
368 | cd831bd7 | ths | |
369 | cd831bd7 | ths | ret = 0;
|
370 | cd831bd7 | ths | bdrv_close(bs); |
371 | cd831bd7 | ths | |
372 | cd831bd7 | ths | do {
|
373 | cd831bd7 | ths | sock = unix_socket_outgoing(socket); |
374 | cd831bd7 | ths | if (sock == -1) { |
375 | cd831bd7 | ths | if (errno != ENOENT && errno != ECONNREFUSED)
|
376 | cd831bd7 | ths | goto out;
|
377 | cd831bd7 | ths | sleep(1); /* wait children */ |
378 | cd831bd7 | ths | } |
379 | cd831bd7 | ths | } while (sock == -1); |
380 | cd831bd7 | ths | |
381 | cd831bd7 | ths | fd = open(device, O_RDWR); |
382 | cd831bd7 | ths | if (fd == -1) { |
383 | cd831bd7 | ths | ret = 1;
|
384 | cd831bd7 | ths | goto out;
|
385 | cd831bd7 | ths | } |
386 | cd831bd7 | ths | |
387 | cd831bd7 | ths | ret = nbd_receive_negotiate(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 | ret = nbd_init(fd, sock, size, blocksize); |
394 | cd831bd7 | ths | if (ret == -1) { |
395 | cd831bd7 | ths | ret = 1;
|
396 | cd831bd7 | ths | goto out;
|
397 | cd831bd7 | ths | } |
398 | cd831bd7 | ths | |
399 | cd831bd7 | ths | printf("NBD device %s is now connected to file %s\n",
|
400 | cd831bd7 | ths | device, argv[optind]); |
401 | cd831bd7 | ths | |
402 | cd831bd7 | ths | /* update partition table */
|
403 | cd831bd7 | ths | |
404 | cd831bd7 | ths | show_parts(device); |
405 | cd831bd7 | ths | |
406 | cd831bd7 | ths | nbd_client(fd, sock); |
407 | cd831bd7 | ths | close(fd); |
408 | cd831bd7 | ths | out:
|
409 | cd831bd7 | ths | kill(pid, SIGTERM); |
410 | cd831bd7 | ths | unlink(socket); |
411 | cd831bd7 | ths | |
412 | cd831bd7 | ths | return ret;
|
413 | cd831bd7 | ths | } |
414 | cd831bd7 | ths | /* children */
|
415 | cd831bd7 | ths | } |
416 | cd831bd7 | ths | |
417 | 3b05a8e9 | ths | sharing_fds = qemu_malloc((shared + 1) * sizeof(int)); |
418 | 3b05a8e9 | ths | |
419 | cd831bd7 | ths | if (socket) {
|
420 | 3b05a8e9 | ths | sharing_fds[0] = unix_socket_incoming(socket);
|
421 | cd831bd7 | ths | } else {
|
422 | 3b05a8e9 | ths | sharing_fds[0] = tcp_socket_incoming(bindto, port);
|
423 | cd831bd7 | ths | } |
424 | cd831bd7 | ths | |
425 | 3b05a8e9 | ths | if (sharing_fds[0] == -1) |
426 | 7a5ca864 | bellard | return 1; |
427 | 3b05a8e9 | ths | max_fd = sharing_fds[0];
|
428 | 3b05a8e9 | ths | nb_fds++; |
429 | 7a5ca864 | bellard | |
430 | 3b05a8e9 | ths | data = qemu_memalign(512, NBD_BUFFER_SIZE);
|
431 | 3b05a8e9 | ths | if (data == NULL) |
432 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Cannot allocate data buffer");
|
433 | 7a5ca864 | bellard | |
434 | 3b05a8e9 | ths | do {
|
435 | 7a5ca864 | bellard | |
436 | 3b05a8e9 | ths | FD_ZERO(&fds); |
437 | 3b05a8e9 | ths | for (i = 0; i < nb_fds; i++) |
438 | 3b05a8e9 | ths | FD_SET(sharing_fds[i], &fds); |
439 | 3b05a8e9 | ths | |
440 | 3b05a8e9 | ths | ret = select(max_fd + 1, &fds, NULL, NULL, NULL); |
441 | 3b05a8e9 | ths | if (ret == -1) |
442 | 3b05a8e9 | ths | break;
|
443 | 3b05a8e9 | ths | |
444 | 3b05a8e9 | ths | if (FD_ISSET(sharing_fds[0], &fds)) |
445 | 3b05a8e9 | ths | ret--; |
446 | 3b05a8e9 | ths | for (i = 1; i < nb_fds && ret; i++) { |
447 | 3b05a8e9 | ths | if (FD_ISSET(sharing_fds[i], &fds)) {
|
448 | 3b05a8e9 | ths | if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
|
449 | 3b05a8e9 | ths | &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
|
450 | 3b05a8e9 | ths | close(sharing_fds[i]); |
451 | 3b05a8e9 | ths | nb_fds--; |
452 | 3b05a8e9 | ths | sharing_fds[i] = sharing_fds[nb_fds]; |
453 | 3b05a8e9 | ths | i--; |
454 | 3b05a8e9 | ths | } |
455 | 3b05a8e9 | ths | ret--; |
456 | 3b05a8e9 | ths | } |
457 | 3b05a8e9 | ths | } |
458 | 3b05a8e9 | ths | /* new connection ? */
|
459 | 3b05a8e9 | ths | if (FD_ISSET(sharing_fds[0], &fds)) { |
460 | 3b05a8e9 | ths | if (nb_fds < shared + 1) { |
461 | 3b05a8e9 | ths | sharing_fds[nb_fds] = accept(sharing_fds[0],
|
462 | 3b05a8e9 | ths | (struct sockaddr *)&addr,
|
463 | 3b05a8e9 | ths | &addr_len); |
464 | 3b05a8e9 | ths | if (sharing_fds[nb_fds] != -1 && |
465 | 27982661 | aliguori | nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) {
|
466 | 3b05a8e9 | ths | if (sharing_fds[nb_fds] > max_fd)
|
467 | 3b05a8e9 | ths | max_fd = sharing_fds[nb_fds]; |
468 | 3b05a8e9 | ths | nb_fds++; |
469 | 3b05a8e9 | ths | } |
470 | 3b05a8e9 | ths | } |
471 | 3b05a8e9 | ths | } |
472 | 75818250 | ths | } while (persistent || nb_fds > 1); |
473 | f8a83245 | Herve Poussineau | qemu_vfree(data); |
474 | 7a5ca864 | bellard | |
475 | 3b05a8e9 | ths | close(sharing_fds[0]);
|
476 | 7a5ca864 | bellard | bdrv_close(bs); |
477 | 3b05a8e9 | ths | qemu_free(sharing_fds); |
478 | cd831bd7 | ths | if (socket)
|
479 | cd831bd7 | ths | unlink(socket); |
480 | 7a5ca864 | bellard | |
481 | 7a5ca864 | bellard | return 0; |
482 | 7a5ca864 | bellard | } |