root / qemu-nbd.c @ 33087598
History | View | Annotate | Download (13.7 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 | cb7cf0e3 | Ryota Ozaki | int ret;
|
116 | 7a5ca864 | bellard | |
117 | cb7cf0e3 | Ryota Ozaki | if ((ret = bdrv_read(bs, 0, data, 1)) < 0) { |
118 | cb7cf0e3 | Ryota Ozaki | errno = -ret; |
119 | cb7cf0e3 | Ryota Ozaki | err(EXIT_FAILURE, "error while reading");
|
120 | cb7cf0e3 | Ryota Ozaki | } |
121 | 7a5ca864 | bellard | |
122 | 7a5ca864 | bellard | if (data[510] != 0x55 || data[511] != 0xaa) { |
123 | 7a5ca864 | bellard | errno = -EINVAL; |
124 | 7a5ca864 | bellard | return -1; |
125 | 7a5ca864 | bellard | } |
126 | 7a5ca864 | bellard | |
127 | 7a5ca864 | bellard | for (i = 0; i < 4; i++) { |
128 | 7a5ca864 | bellard | read_partition(&data[446 + 16 * i], &mbr[i]); |
129 | 7a5ca864 | bellard | |
130 | 7a5ca864 | bellard | if (!mbr[i].nb_sectors_abs)
|
131 | 7a5ca864 | bellard | continue;
|
132 | 7a5ca864 | bellard | |
133 | 7a5ca864 | bellard | if (mbr[i].system == 0xF || mbr[i].system == 0x5) { |
134 | 7a5ca864 | bellard | struct partition_record ext[4]; |
135 | 7a5ca864 | bellard | uint8_t data1[512];
|
136 | 7a5ca864 | bellard | int j;
|
137 | 7a5ca864 | bellard | |
138 | cb7cf0e3 | Ryota Ozaki | if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) { |
139 | cb7cf0e3 | Ryota Ozaki | errno = -ret; |
140 | cb7cf0e3 | Ryota Ozaki | err(EXIT_FAILURE, "error while reading");
|
141 | cb7cf0e3 | Ryota Ozaki | } |
142 | 7a5ca864 | bellard | |
143 | 7a5ca864 | bellard | for (j = 0; j < 4; j++) { |
144 | 7a5ca864 | bellard | read_partition(&data1[446 + 16 * j], &ext[j]); |
145 | 7a5ca864 | bellard | if (!ext[j].nb_sectors_abs)
|
146 | 7a5ca864 | bellard | continue;
|
147 | 7a5ca864 | bellard | |
148 | 7a5ca864 | bellard | if ((ext_partnum + j + 1) == partition) { |
149 | 7a5ca864 | bellard | *offset = (uint64_t)ext[j].start_sector_abs << 9;
|
150 | 7a5ca864 | bellard | *size = (uint64_t)ext[j].nb_sectors_abs << 9;
|
151 | 7a5ca864 | bellard | return 0; |
152 | 7a5ca864 | bellard | } |
153 | 7a5ca864 | bellard | } |
154 | 7a5ca864 | bellard | ext_partnum += 4;
|
155 | 7a5ca864 | bellard | } else if ((i + 1) == partition) { |
156 | 7a5ca864 | bellard | *offset = (uint64_t)mbr[i].start_sector_abs << 9;
|
157 | 7a5ca864 | bellard | *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
|
158 | 7a5ca864 | bellard | return 0; |
159 | 7a5ca864 | bellard | } |
160 | 7a5ca864 | bellard | } |
161 | 7a5ca864 | bellard | |
162 | 7a5ca864 | bellard | errno = -ENOENT; |
163 | 7a5ca864 | bellard | return -1; |
164 | 7a5ca864 | bellard | } |
165 | 7a5ca864 | bellard | |
166 | cd831bd7 | ths | static void show_parts(const char *device) |
167 | cd831bd7 | ths | { |
168 | cd831bd7 | ths | if (fork() == 0) { |
169 | cd831bd7 | ths | int nbd;
|
170 | cd831bd7 | ths | |
171 | cd831bd7 | ths | /* linux just needs an open() to trigger
|
172 | cd831bd7 | ths | * the partition table update
|
173 | cd831bd7 | ths | * but remember to load the module with max_part != 0 :
|
174 | cd831bd7 | ths | * modprobe nbd max_part=63
|
175 | cd831bd7 | ths | */
|
176 | cd831bd7 | ths | nbd = open(device, O_RDWR); |
177 | cd831bd7 | ths | if (nbd != -1) |
178 | cd831bd7 | ths | close(nbd); |
179 | cd831bd7 | ths | exit(0);
|
180 | cd831bd7 | ths | } |
181 | cd831bd7 | ths | } |
182 | cd831bd7 | ths | |
183 | 7a5ca864 | bellard | int main(int argc, char **argv) |
184 | 7a5ca864 | bellard | { |
185 | 7a5ca864 | bellard | BlockDriverState *bs; |
186 | 7a5ca864 | bellard | off_t dev_offset = 0;
|
187 | 7a5ca864 | bellard | off_t offset = 0;
|
188 | 7a5ca864 | bellard | bool readonly = false; |
189 | cd831bd7 | ths | bool disconnect = false; |
190 | 7a5ca864 | bellard | const char *bindto = "0.0.0.0"; |
191 | 7a5ca864 | bellard | int port = 1024; |
192 | 7a5ca864 | bellard | struct sockaddr_in addr;
|
193 | 7a5ca864 | bellard | socklen_t addr_len = sizeof(addr);
|
194 | 7a5ca864 | bellard | off_t fd_size; |
195 | cd831bd7 | ths | char *device = NULL; |
196 | cd831bd7 | ths | char *socket = NULL; |
197 | cd831bd7 | ths | char sockpath[128]; |
198 | d6aa671f | aliguori | const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; |
199 | 7a5ca864 | bellard | struct option lopt[] = {
|
200 | 660f11be | Blue Swirl | { "help", 0, NULL, 'h' }, |
201 | 660f11be | Blue Swirl | { "version", 0, NULL, 'V' }, |
202 | 660f11be | Blue Swirl | { "bind", 1, NULL, 'b' }, |
203 | 660f11be | Blue Swirl | { "port", 1, NULL, 'p' }, |
204 | 660f11be | Blue Swirl | { "socket", 1, NULL, 'k' }, |
205 | 660f11be | Blue Swirl | { "offset", 1, NULL, 'o' }, |
206 | 660f11be | Blue Swirl | { "read-only", 0, NULL, 'r' }, |
207 | 660f11be | Blue Swirl | { "partition", 1, NULL, 'P' }, |
208 | 660f11be | Blue Swirl | { "connect", 1, NULL, 'c' }, |
209 | 660f11be | Blue Swirl | { "disconnect", 0, NULL, 'd' }, |
210 | 660f11be | Blue Swirl | { "snapshot", 0, NULL, 's' }, |
211 | 660f11be | Blue Swirl | { "nocache", 0, NULL, 'n' }, |
212 | 660f11be | Blue Swirl | { "shared", 1, NULL, 'e' }, |
213 | 660f11be | Blue Swirl | { "persistent", 0, NULL, 't' }, |
214 | 660f11be | Blue Swirl | { "verbose", 0, NULL, 'v' }, |
215 | 660f11be | Blue Swirl | { NULL, 0, NULL, 0 } |
216 | 7a5ca864 | bellard | }; |
217 | 7a5ca864 | bellard | int ch;
|
218 | 7a5ca864 | bellard | int opt_ind = 0; |
219 | 7a5ca864 | bellard | int li;
|
220 | 7a5ca864 | bellard | char *end;
|
221 | f5edb014 | Naphtali Sprei | int flags = BDRV_O_RDWR;
|
222 | 7a5ca864 | bellard | int partition = -1; |
223 | cd831bd7 | ths | int ret;
|
224 | 3b05a8e9 | ths | int shared = 1; |
225 | 2f726488 | ths | uint8_t *data; |
226 | 3b05a8e9 | ths | fd_set fds; |
227 | 3b05a8e9 | ths | int *sharing_fds;
|
228 | 3b05a8e9 | ths | int fd;
|
229 | 3b05a8e9 | ths | int i;
|
230 | 3b05a8e9 | ths | int nb_fds = 0; |
231 | 3b05a8e9 | ths | int max_fd;
|
232 | 75818250 | ths | int persistent = 0; |
233 | 7a5ca864 | bellard | |
234 | 7a5ca864 | bellard | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
235 | 7a5ca864 | bellard | switch (ch) {
|
236 | 7a5ca864 | bellard | case 's': |
237 | 2f726488 | ths | flags |= BDRV_O_SNAPSHOT; |
238 | 2f726488 | ths | break;
|
239 | 2f726488 | ths | case 'n': |
240 | 9f7965c7 | aliguori | flags |= BDRV_O_NOCACHE; |
241 | 7a5ca864 | bellard | break;
|
242 | 7a5ca864 | bellard | case 'b': |
243 | 7a5ca864 | bellard | bindto = optarg; |
244 | 7a5ca864 | bellard | break;
|
245 | 7a5ca864 | bellard | case 'p': |
246 | 7a5ca864 | bellard | li = strtol(optarg, &end, 0);
|
247 | 7a5ca864 | bellard | if (*end) {
|
248 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
|
249 | 7a5ca864 | bellard | } |
250 | 7a5ca864 | bellard | if (li < 1 || li > 65535) { |
251 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
|
252 | 7a5ca864 | bellard | } |
253 | 7a5ca864 | bellard | port = (uint16_t)li; |
254 | 7a5ca864 | bellard | break;
|
255 | 7a5ca864 | bellard | case 'o': |
256 | 7a5ca864 | bellard | dev_offset = strtoll (optarg, &end, 0);
|
257 | 7a5ca864 | bellard | if (*end) {
|
258 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
|
259 | 7a5ca864 | bellard | } |
260 | 7a5ca864 | bellard | if (dev_offset < 0) { |
261 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
|
262 | 7a5ca864 | bellard | } |
263 | 7a5ca864 | bellard | break;
|
264 | 7a5ca864 | bellard | case 'r': |
265 | 7a5ca864 | bellard | readonly = true;
|
266 | 07108b29 | Naphtali Sprei | flags &= ~BDRV_O_RDWR; |
267 | 7a5ca864 | bellard | break;
|
268 | 7a5ca864 | bellard | case 'P': |
269 | 7a5ca864 | bellard | partition = strtol(optarg, &end, 0);
|
270 | 7a5ca864 | bellard | if (*end)
|
271 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
|
272 | 7a5ca864 | bellard | if (partition < 1 || partition > 8) |
273 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid partition %d", partition);
|
274 | 7a5ca864 | bellard | break;
|
275 | cd831bd7 | ths | case 'k': |
276 | cd831bd7 | ths | socket = optarg; |
277 | cd831bd7 | ths | if (socket[0] != '/') |
278 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "socket path must be absolute\n");
|
279 | cd831bd7 | ths | break;
|
280 | cd831bd7 | ths | case 'd': |
281 | cd831bd7 | ths | disconnect = true;
|
282 | cd831bd7 | ths | break;
|
283 | cd831bd7 | ths | case 'c': |
284 | cd831bd7 | ths | device = optarg; |
285 | cd831bd7 | ths | break;
|
286 | 3b05a8e9 | ths | case 'e': |
287 | 3b05a8e9 | ths | shared = strtol(optarg, &end, 0);
|
288 | 3b05a8e9 | ths | if (*end) {
|
289 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
|
290 | 3b05a8e9 | ths | } |
291 | 3b05a8e9 | ths | if (shared < 1) { |
292 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
|
293 | 3b05a8e9 | ths | } |
294 | 3b05a8e9 | ths | break;
|
295 | 75818250 | ths | case 't': |
296 | 75818250 | ths | persistent = 1;
|
297 | 75818250 | ths | break;
|
298 | 7a5ca864 | bellard | case 'v': |
299 | 7a5ca864 | bellard | verbose = 1;
|
300 | 7a5ca864 | bellard | break;
|
301 | 7a5ca864 | bellard | case 'V': |
302 | 7a5ca864 | bellard | version(argv[0]);
|
303 | 7a5ca864 | bellard | exit(0);
|
304 | 7a5ca864 | bellard | break;
|
305 | 7a5ca864 | bellard | case 'h': |
306 | 7a5ca864 | bellard | usage(argv[0]);
|
307 | 7a5ca864 | bellard | exit(0);
|
308 | 7a5ca864 | bellard | break;
|
309 | 7a5ca864 | bellard | case '?': |
310 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Try `%s --help' for more information.",
|
311 | 7a5ca864 | bellard | argv[0]);
|
312 | 7a5ca864 | bellard | } |
313 | 7a5ca864 | bellard | } |
314 | 7a5ca864 | bellard | |
315 | 7a5ca864 | bellard | if ((argc - optind) != 1) { |
316 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Invalid number of argument.\n"
|
317 | 7a5ca864 | bellard | "Try `%s --help' for more information.",
|
318 | 7a5ca864 | bellard | argv[0]);
|
319 | 7a5ca864 | bellard | } |
320 | 7a5ca864 | bellard | |
321 | cd831bd7 | ths | if (disconnect) {
|
322 | cd831bd7 | ths | fd = open(argv[optind], O_RDWR); |
323 | cd831bd7 | ths | if (fd == -1) |
324 | cb7cf0e3 | Ryota Ozaki | err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
|
325 | cd831bd7 | ths | |
326 | cd831bd7 | ths | nbd_disconnect(fd); |
327 | cd831bd7 | ths | |
328 | cd831bd7 | ths | close(fd); |
329 | cd831bd7 | ths | |
330 | cd831bd7 | ths | printf("%s disconnected\n", argv[optind]);
|
331 | cd831bd7 | ths | |
332 | cd831bd7 | ths | return 0; |
333 | cd831bd7 | ths | } |
334 | cd831bd7 | ths | |
335 | 7a5ca864 | bellard | bdrv_init(); |
336 | 7a5ca864 | bellard | |
337 | 7a5ca864 | bellard | bs = bdrv_new("hda");
|
338 | 7a5ca864 | bellard | if (bs == NULL) |
339 | 7a5ca864 | bellard | return 1; |
340 | 7a5ca864 | bellard | |
341 | cb7cf0e3 | Ryota Ozaki | if ((ret = bdrv_open(bs, argv[optind], flags, NULL)) < 0) { |
342 | cb7cf0e3 | Ryota Ozaki | errno = -ret; |
343 | cb7cf0e3 | Ryota Ozaki | err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
|
344 | cb7cf0e3 | Ryota Ozaki | } |
345 | 7a5ca864 | bellard | |
346 | 7a5ca864 | bellard | fd_size = bs->total_sectors * 512;
|
347 | 7a5ca864 | bellard | |
348 | 7a5ca864 | bellard | if (partition != -1 && |
349 | 7a5ca864 | bellard | find_partition(bs, partition, &dev_offset, &fd_size)) |
350 | cb7cf0e3 | Ryota Ozaki | err(EXIT_FAILURE, "Could not find partition %d", partition);
|
351 | 7a5ca864 | bellard | |
352 | cd831bd7 | ths | if (device) {
|
353 | 3b05a8e9 | ths | pid_t pid; |
354 | 3b05a8e9 | ths | int sock;
|
355 | 3b05a8e9 | ths | |
356 | cb7cf0e3 | Ryota Ozaki | /* want to fail before daemonizing */
|
357 | cb7cf0e3 | Ryota Ozaki | if (access(device, R_OK|W_OK) == -1) { |
358 | cb7cf0e3 | Ryota Ozaki | err(EXIT_FAILURE, "Could not access '%s'", device);
|
359 | cb7cf0e3 | Ryota Ozaki | } |
360 | cb7cf0e3 | Ryota Ozaki | |
361 | f5de141b | Anthony Liguori | if (!verbose) {
|
362 | f5de141b | Anthony Liguori | /* detach client and server */
|
363 | f5de141b | Anthony Liguori | if (daemon(0, 0) == -1) { |
364 | cb7cf0e3 | Ryota Ozaki | err(EXIT_FAILURE, "Failed to daemonize");
|
365 | f5de141b | Anthony Liguori | } |
366 | f5de141b | Anthony Liguori | } |
367 | cd831bd7 | ths | |
368 | cd831bd7 | ths | if (socket == NULL) { |
369 | 22ff51ee | Blue Swirl | snprintf(sockpath, sizeof(sockpath), SOCKET_PATH,
|
370 | 22ff51ee | Blue Swirl | basename(device)); |
371 | cd831bd7 | ths | socket = sockpath; |
372 | cd831bd7 | ths | } |
373 | cd831bd7 | ths | |
374 | cd831bd7 | ths | pid = fork(); |
375 | cd831bd7 | ths | if (pid < 0) |
376 | cd831bd7 | ths | return 1; |
377 | cd831bd7 | ths | if (pid != 0) { |
378 | cd831bd7 | ths | off_t size; |
379 | cd831bd7 | ths | size_t blocksize; |
380 | cd831bd7 | ths | |
381 | cd831bd7 | ths | ret = 0;
|
382 | cd831bd7 | ths | bdrv_close(bs); |
383 | cd831bd7 | ths | |
384 | cd831bd7 | ths | do {
|
385 | cd831bd7 | ths | sock = unix_socket_outgoing(socket); |
386 | cd831bd7 | ths | if (sock == -1) { |
387 | cb7cf0e3 | Ryota Ozaki | if (errno != ENOENT && errno != ECONNREFUSED) {
|
388 | cb7cf0e3 | Ryota Ozaki | ret = 1;
|
389 | cd831bd7 | ths | goto out;
|
390 | cb7cf0e3 | Ryota Ozaki | } |
391 | cd831bd7 | ths | sleep(1); /* wait children */ |
392 | cd831bd7 | ths | } |
393 | cd831bd7 | ths | } while (sock == -1); |
394 | cd831bd7 | ths | |
395 | cd831bd7 | ths | fd = open(device, O_RDWR); |
396 | cd831bd7 | ths | if (fd == -1) { |
397 | cd831bd7 | ths | ret = 1;
|
398 | cd831bd7 | ths | goto out;
|
399 | cd831bd7 | ths | } |
400 | cd831bd7 | ths | |
401 | cd831bd7 | ths | ret = nbd_receive_negotiate(sock, &size, &blocksize); |
402 | cd831bd7 | ths | if (ret == -1) { |
403 | cd831bd7 | ths | ret = 1;
|
404 | cd831bd7 | ths | goto out;
|
405 | cd831bd7 | ths | } |
406 | cd831bd7 | ths | |
407 | cd831bd7 | ths | ret = nbd_init(fd, sock, size, blocksize); |
408 | cd831bd7 | ths | if (ret == -1) { |
409 | cd831bd7 | ths | ret = 1;
|
410 | cd831bd7 | ths | goto out;
|
411 | cd831bd7 | ths | } |
412 | cd831bd7 | ths | |
413 | cd831bd7 | ths | printf("NBD device %s is now connected to file %s\n",
|
414 | cd831bd7 | ths | device, argv[optind]); |
415 | cd831bd7 | ths | |
416 | cd831bd7 | ths | /* update partition table */
|
417 | cd831bd7 | ths | |
418 | cd831bd7 | ths | show_parts(device); |
419 | cd831bd7 | ths | |
420 | cd831bd7 | ths | nbd_client(fd, sock); |
421 | cd831bd7 | ths | close(fd); |
422 | cd831bd7 | ths | out:
|
423 | cd831bd7 | ths | kill(pid, SIGTERM); |
424 | cd831bd7 | ths | unlink(socket); |
425 | cd831bd7 | ths | |
426 | cd831bd7 | ths | return ret;
|
427 | cd831bd7 | ths | } |
428 | cd831bd7 | ths | /* children */
|
429 | cd831bd7 | ths | } |
430 | cd831bd7 | ths | |
431 | 3b05a8e9 | ths | sharing_fds = qemu_malloc((shared + 1) * sizeof(int)); |
432 | 3b05a8e9 | ths | |
433 | cd831bd7 | ths | if (socket) {
|
434 | 3b05a8e9 | ths | sharing_fds[0] = unix_socket_incoming(socket);
|
435 | cd831bd7 | ths | } else {
|
436 | 3b05a8e9 | ths | sharing_fds[0] = tcp_socket_incoming(bindto, port);
|
437 | cd831bd7 | ths | } |
438 | cd831bd7 | ths | |
439 | 3b05a8e9 | ths | if (sharing_fds[0] == -1) |
440 | 7a5ca864 | bellard | return 1; |
441 | 3b05a8e9 | ths | max_fd = sharing_fds[0];
|
442 | 3b05a8e9 | ths | nb_fds++; |
443 | 7a5ca864 | bellard | |
444 | 3b05a8e9 | ths | data = qemu_memalign(512, NBD_BUFFER_SIZE);
|
445 | 3b05a8e9 | ths | if (data == NULL) |
446 | b6353bea | Ryota Ozaki | errx(EXIT_FAILURE, "Cannot allocate data buffer");
|
447 | 7a5ca864 | bellard | |
448 | 3b05a8e9 | ths | do {
|
449 | 7a5ca864 | bellard | |
450 | 3b05a8e9 | ths | FD_ZERO(&fds); |
451 | 3b05a8e9 | ths | for (i = 0; i < nb_fds; i++) |
452 | 3b05a8e9 | ths | FD_SET(sharing_fds[i], &fds); |
453 | 3b05a8e9 | ths | |
454 | 3b05a8e9 | ths | ret = select(max_fd + 1, &fds, NULL, NULL, NULL); |
455 | 3b05a8e9 | ths | if (ret == -1) |
456 | 3b05a8e9 | ths | break;
|
457 | 3b05a8e9 | ths | |
458 | 3b05a8e9 | ths | if (FD_ISSET(sharing_fds[0], &fds)) |
459 | 3b05a8e9 | ths | ret--; |
460 | 3b05a8e9 | ths | for (i = 1; i < nb_fds && ret; i++) { |
461 | 3b05a8e9 | ths | if (FD_ISSET(sharing_fds[i], &fds)) {
|
462 | 3b05a8e9 | ths | if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
|
463 | 3b05a8e9 | ths | &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
|
464 | 3b05a8e9 | ths | close(sharing_fds[i]); |
465 | 3b05a8e9 | ths | nb_fds--; |
466 | 3b05a8e9 | ths | sharing_fds[i] = sharing_fds[nb_fds]; |
467 | 3b05a8e9 | ths | i--; |
468 | 3b05a8e9 | ths | } |
469 | 3b05a8e9 | ths | ret--; |
470 | 3b05a8e9 | ths | } |
471 | 3b05a8e9 | ths | } |
472 | 3b05a8e9 | ths | /* new connection ? */
|
473 | 3b05a8e9 | ths | if (FD_ISSET(sharing_fds[0], &fds)) { |
474 | 3b05a8e9 | ths | if (nb_fds < shared + 1) { |
475 | 3b05a8e9 | ths | sharing_fds[nb_fds] = accept(sharing_fds[0],
|
476 | 3b05a8e9 | ths | (struct sockaddr *)&addr,
|
477 | 3b05a8e9 | ths | &addr_len); |
478 | 3b05a8e9 | ths | if (sharing_fds[nb_fds] != -1 && |
479 | 27982661 | aliguori | nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) {
|
480 | 3b05a8e9 | ths | if (sharing_fds[nb_fds] > max_fd)
|
481 | 3b05a8e9 | ths | max_fd = sharing_fds[nb_fds]; |
482 | 3b05a8e9 | ths | nb_fds++; |
483 | 3b05a8e9 | ths | } |
484 | 3b05a8e9 | ths | } |
485 | 3b05a8e9 | ths | } |
486 | 75818250 | ths | } while (persistent || nb_fds > 1); |
487 | f8a83245 | Herve Poussineau | qemu_vfree(data); |
488 | 7a5ca864 | bellard | |
489 | 3b05a8e9 | ths | close(sharing_fds[0]);
|
490 | 7a5ca864 | bellard | bdrv_close(bs); |
491 | 3b05a8e9 | ths | qemu_free(sharing_fds); |
492 | cd831bd7 | ths | if (socket)
|
493 | cd831bd7 | ths | unlink(socket); |
494 | 7a5ca864 | bellard | |
495 | 7a5ca864 | bellard | return 0; |
496 | 7a5ca864 | bellard | } |