]>
Commit | Line | Data |
---|---|---|
cd831bd7 | 1 | /* |
7a5ca864 FB |
2 | * Copyright (C) 2005 Anthony Liguori <[email protected]> |
3 | * | |
4 | * Network Block Device | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; under version 2 of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
8167ee88 | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
7a5ca864 FB |
17 | */ |
18 | ||
d38ea87a | 19 | #include "qemu/osdep.h" |
5a61cb60 | 20 | #include "qemu-common.h" |
26f54e9a | 21 | #include "sysemu/block-backend.h" |
b3838a40 | 22 | #include "block/block_int.h" |
737e150e | 23 | #include "block/nbd.h" |
6a1751b7 | 24 | #include "qemu/main-loop.h" |
537b41f5 | 25 | #include "qemu/error-report.h" |
0ab3b337 | 26 | #include "qemu/config-file.h" |
8c116b0e | 27 | #include "block/snapshot.h" |
b3838a40 | 28 | #include "qapi/util.h" |
d49b6836 | 29 | #include "qapi/qmp/qstring.h" |
0ab3b337 | 30 | #include "qom/object_interfaces.h" |
d0d6ff58 | 31 | #include "io/channel-socket.h" |
7a5ca864 | 32 | |
7a5ca864 | 33 | #include <getopt.h> |
d0d6ff58 DB |
34 | #include <sys/types.h> |
35 | #include <signal.h> | |
2bff4b6f | 36 | #include <libgen.h> |
a517e88b | 37 | #include <pthread.h> |
cd831bd7 | 38 | |
713cc671 PL |
39 | #define SOCKET_PATH "/var/lock/qemu-nbd-%s" |
40 | #define QEMU_NBD_OPT_CACHE 1 | |
41 | #define QEMU_NBD_OPT_AIO 2 | |
42 | #define QEMU_NBD_OPT_DISCARD 3 | |
b3838a40 | 43 | #define QEMU_NBD_OPT_DETECT_ZEROES 4 |
0ab3b337 | 44 | #define QEMU_NBD_OPT_OBJECT 5 |
7a5ca864 | 45 | |
af49bbbe | 46 | static NBDExport *exp; |
3d4b2f9c | 47 | static bool newproto; |
b1d8e52e | 48 | static int verbose; |
a517e88b | 49 | static char *srcpath; |
48bec07e | 50 | static SocketAddress *saddr; |
7860a380 PB |
51 | static int persistent = 0; |
52 | static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state; | |
a61c6782 PB |
53 | static int shared = 1; |
54 | static int nb_fds; | |
d0d6ff58 DB |
55 | static QIOChannelSocket *server_ioc; |
56 | static int server_watch = -1; | |
7a5ca864 FB |
57 | |
58 | static void usage(const char *name) | |
59 | { | |
b033cd86 | 60 | (printf) ( |
7a5ca864 FB |
61 | "Usage: %s [OPTIONS] FILE\n" |
62 | "QEMU Disk Network Block Device Server\n" | |
63 | "\n" | |
713cc671 PL |
64 | " -h, --help display this help and exit\n" |
65 | " -V, --version output version information and exit\n" | |
b033cd86 PB |
66 | "\n" |
67 | "Connection properties:\n" | |
713cc671 PL |
68 | " -p, --port=PORT port to listen on (default `%d')\n" |
69 | " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n" | |
70 | " -k, --socket=PATH path to the unix socket\n" | |
71 | " (default '"SOCKET_PATH"')\n" | |
72 | " -e, --shared=NUM device can be shared by NUM clients (default '1')\n" | |
73 | " -t, --persistent don't exit on the last connection\n" | |
74 | " -v, --verbose display extra debugging information\n" | |
7a5ca864 | 75 | "\n" |
b033cd86 | 76 | "Exposing part of the image:\n" |
713cc671 PL |
77 | " -o, --offset=OFFSET offset into the image\n" |
78 | " -P, --partition=NUM only expose partition NUM\n" | |
b033cd86 | 79 | "\n" |
0ab3b337 DB |
80 | "General purpose options:\n" |
81 | " --object type,id=ID,... define an object such as 'secret' for providing\n" | |
82 | " passwords and/or encryption keys\n" | |
b033cd86 PB |
83 | #ifdef __linux__ |
84 | "Kernel NBD client support:\n" | |
713cc671 PL |
85 | " -c, --connect=DEV connect FILE to the local NBD device DEV\n" |
86 | " -d, --disconnect disconnect the specified device\n" | |
b033cd86 PB |
87 | "\n" |
88 | #endif | |
89 | "\n" | |
90 | "Block device options:\n" | |
713cc671 PL |
91 | " -f, --format=FORMAT set image format (raw, qcow2, ...)\n" |
92 | " -r, --read-only export read-only\n" | |
93 | " -s, --snapshot use FILE as an external snapshot, create a temporary\n" | |
94 | " file with backing_file=FILE, redirect the write to\n" | |
95 | " the temporary one\n" | |
8c116b0e | 96 | " -l, --load-snapshot=SNAPSHOT_PARAM\n" |
713cc671 PL |
97 | " load an internal snapshot inside FILE and export it\n" |
98 | " as an read-only device, SNAPSHOT_PARAM format is\n" | |
99 | " 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" | |
100 | " '[ID_OR_NAME]'\n" | |
101 | " -n, --nocache disable host cache\n" | |
102 | " --cache=MODE set cache mode (none, writeback, ...)\n" | |
713cc671 | 103 | " --aio=MODE set AIO mode (native or threads)\n" |
b3838a40 | 104 | " --discard=MODE set discard mode (ignore, unmap)\n" |
6883de6c | 105 | " --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n" |
b033cd86 PB |
106 | "\n" |
107 | "Report bugs to <[email protected]>\n" | |
c2e2872b | 108 | , name, NBD_DEFAULT_PORT, "DEVICE"); |
7a5ca864 FB |
109 | } |
110 | ||
111 | static void version(const char *name) | |
112 | { | |
113 | printf( | |
315bc7aa | 114 | "%s version 0.0.1\n" |
7a5ca864 FB |
115 | "Written by Anthony Liguori.\n" |
116 | "\n" | |
117 | "Copyright (C) 2006 Anthony Liguori <[email protected]>.\n" | |
118 | "This is free software; see the source for copying conditions. There is NO\n" | |
119 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" | |
315bc7aa | 120 | , name); |
7a5ca864 FB |
121 | } |
122 | ||
123 | struct partition_record | |
124 | { | |
125 | uint8_t bootable; | |
126 | uint8_t start_head; | |
127 | uint32_t start_cylinder; | |
128 | uint8_t start_sector; | |
129 | uint8_t system; | |
130 | uint8_t end_head; | |
131 | uint8_t end_cylinder; | |
132 | uint8_t end_sector; | |
133 | uint32_t start_sector_abs; | |
134 | uint32_t nb_sectors_abs; | |
135 | }; | |
136 | ||
137 | static void read_partition(uint8_t *p, struct partition_record *r) | |
138 | { | |
139 | r->bootable = p[0]; | |
140 | r->start_head = p[1]; | |
141 | r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300); | |
142 | r->start_sector = p[2] & 0x3f; | |
143 | r->system = p[4]; | |
144 | r->end_head = p[5]; | |
145 | r->end_cylinder = p[7] | ((p[6] << 2) & 0x300); | |
146 | r->end_sector = p[6] & 0x3f; | |
ac97393d HR |
147 | |
148 | r->start_sector_abs = le32_to_cpup((uint32_t *)(p + 8)); | |
149 | r->nb_sectors_abs = le32_to_cpup((uint32_t *)(p + 12)); | |
7a5ca864 FB |
150 | } |
151 | ||
4c58e80a | 152 | static int find_partition(BlockBackend *blk, int partition, |
7a5ca864 FB |
153 | off_t *offset, off_t *size) |
154 | { | |
155 | struct partition_record mbr[4]; | |
156 | uint8_t data[512]; | |
157 | int i; | |
158 | int ext_partnum = 4; | |
cb7cf0e3 | 159 | int ret; |
7a5ca864 | 160 | |
4c58e80a | 161 | if ((ret = blk_read(blk, 0, data, 1)) < 0) { |
a4699e55 | 162 | error_report("error while reading: %s", strerror(-ret)); |
85b01e09 | 163 | exit(EXIT_FAILURE); |
cb7cf0e3 | 164 | } |
7a5ca864 FB |
165 | |
166 | if (data[510] != 0x55 || data[511] != 0xaa) { | |
185b4338 | 167 | return -EINVAL; |
7a5ca864 FB |
168 | } |
169 | ||
170 | for (i = 0; i < 4; i++) { | |
171 | read_partition(&data[446 + 16 * i], &mbr[i]); | |
172 | ||
453b07b1 | 173 | if (!mbr[i].system || !mbr[i].nb_sectors_abs) { |
7a5ca864 | 174 | continue; |
453b07b1 | 175 | } |
7a5ca864 FB |
176 | |
177 | if (mbr[i].system == 0xF || mbr[i].system == 0x5) { | |
178 | struct partition_record ext[4]; | |
179 | uint8_t data1[512]; | |
180 | int j; | |
181 | ||
4c58e80a | 182 | if ((ret = blk_read(blk, mbr[i].start_sector_abs, data1, 1)) < 0) { |
a4699e55 | 183 | error_report("error while reading: %s", strerror(-ret)); |
85b01e09 | 184 | exit(EXIT_FAILURE); |
cb7cf0e3 | 185 | } |
7a5ca864 FB |
186 | |
187 | for (j = 0; j < 4; j++) { | |
188 | read_partition(&data1[446 + 16 * j], &ext[j]); | |
453b07b1 | 189 | if (!ext[j].system || !ext[j].nb_sectors_abs) { |
7a5ca864 | 190 | continue; |
453b07b1 | 191 | } |
7a5ca864 FB |
192 | |
193 | if ((ext_partnum + j + 1) == partition) { | |
194 | *offset = (uint64_t)ext[j].start_sector_abs << 9; | |
195 | *size = (uint64_t)ext[j].nb_sectors_abs << 9; | |
196 | return 0; | |
197 | } | |
198 | } | |
199 | ext_partnum += 4; | |
200 | } else if ((i + 1) == partition) { | |
201 | *offset = (uint64_t)mbr[i].start_sector_abs << 9; | |
202 | *size = (uint64_t)mbr[i].nb_sectors_abs << 9; | |
203 | return 0; | |
204 | } | |
205 | } | |
206 | ||
185b4338 | 207 | return -ENOENT; |
7a5ca864 FB |
208 | } |
209 | ||
bb345110 PB |
210 | static void termsig_handler(int signum) |
211 | { | |
7860a380 | 212 | state = TERMINATE; |
a61c6782 | 213 | qemu_notify_event(); |
bb345110 PB |
214 | } |
215 | ||
537b41f5 | 216 | |
a517e88b | 217 | static void *show_parts(void *arg) |
cd831bd7 | 218 | { |
a6ac2313 | 219 | char *device = arg; |
a517e88b PB |
220 | int nbd; |
221 | ||
222 | /* linux just needs an open() to trigger | |
223 | * the partition table update | |
224 | * but remember to load the module with max_part != 0 : | |
225 | * modprobe nbd max_part=63 | |
226 | */ | |
227 | nbd = open(device, O_RDWR); | |
fc19f8a0 | 228 | if (nbd >= 0) { |
a517e88b PB |
229 | close(nbd); |
230 | } | |
231 | return NULL; | |
232 | } | |
cd831bd7 | 233 | |
a517e88b PB |
234 | static void *nbd_client_thread(void *arg) |
235 | { | |
a6ac2313 | 236 | char *device = arg; |
a517e88b | 237 | off_t size; |
a517e88b | 238 | uint32_t nbdflags; |
d0d6ff58 DB |
239 | QIOChannelSocket *sioc; |
240 | int fd; | |
a517e88b PB |
241 | int ret; |
242 | pthread_t show_parts_thread; | |
1ce52846 | 243 | Error *local_error = NULL; |
a517e88b | 244 | |
d0d6ff58 DB |
245 | sioc = qio_channel_socket_new(); |
246 | if (qio_channel_socket_connect_sync(sioc, | |
247 | saddr, | |
248 | &local_error) < 0) { | |
48bec07e | 249 | error_report_err(local_error); |
dc10e8b3 SH |
250 | goto out; |
251 | } | |
a517e88b | 252 | |
1c778ef7 | 253 | ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), NULL, &nbdflags, |
3f472659 | 254 | &size, &local_error); |
fc19f8a0 | 255 | if (ret < 0) { |
1ce52846 | 256 | if (local_error) { |
78288671 | 257 | error_report_err(local_error); |
1ce52846 | 258 | } |
0c544d73 | 259 | goto out_socket; |
a517e88b PB |
260 | } |
261 | ||
a6ac2313 | 262 | fd = open(device, O_RDWR); |
fc19f8a0 | 263 | if (fd < 0) { |
a6ac2313 | 264 | /* Linux-only, we can use %m in printf. */ |
b9884681 | 265 | error_report("Failed to open %s: %m", device); |
0c544d73 | 266 | goto out_socket; |
a6ac2313 PB |
267 | } |
268 | ||
1c778ef7 | 269 | ret = nbd_init(fd, sioc, nbdflags, size); |
fc19f8a0 | 270 | if (ret < 0) { |
0c544d73 | 271 | goto out_fd; |
a517e88b PB |
272 | } |
273 | ||
274 | /* update partition table */ | |
a6ac2313 | 275 | pthread_create(&show_parts_thread, NULL, show_parts, device); |
a517e88b | 276 | |
c1f8fdc3 PB |
277 | if (verbose) { |
278 | fprintf(stderr, "NBD device %s is now connected to %s\n", | |
279 | device, srcpath); | |
280 | } else { | |
281 | /* Close stderr so that the qemu-nbd process exits. */ | |
282 | dup2(STDOUT_FILENO, STDERR_FILENO); | |
283 | } | |
a517e88b PB |
284 | |
285 | ret = nbd_client(fd); | |
286 | if (ret) { | |
0c544d73 | 287 | goto out_fd; |
cd831bd7 | 288 | } |
a517e88b | 289 | close(fd); |
d0d6ff58 | 290 | object_unref(OBJECT(sioc)); |
a517e88b PB |
291 | kill(getpid(), SIGTERM); |
292 | return (void *) EXIT_SUCCESS; | |
293 | ||
0c544d73 PB |
294 | out_fd: |
295 | close(fd); | |
296 | out_socket: | |
d0d6ff58 | 297 | object_unref(OBJECT(sioc)); |
a517e88b PB |
298 | out: |
299 | kill(getpid(), SIGTERM); | |
300 | return (void *) EXIT_FAILURE; | |
cd831bd7 TS |
301 | } |
302 | ||
e4afbf4f | 303 | static int nbd_can_accept(void) |
a61c6782 PB |
304 | { |
305 | return nb_fds < shared; | |
306 | } | |
307 | ||
7860a380 PB |
308 | static void nbd_export_closed(NBDExport *exp) |
309 | { | |
310 | assert(state == TERMINATING); | |
311 | state = TERMINATED; | |
312 | } | |
313 | ||
d0d6ff58 | 314 | static void nbd_update_server_watch(void); |
e4afbf4f | 315 | |
1743b515 | 316 | static void nbd_client_closed(NBDClient *client) |
a61c6782 | 317 | { |
1743b515 | 318 | nb_fds--; |
7860a380 PB |
319 | if (nb_fds == 0 && !persistent && state == RUNNING) { |
320 | state = TERMINATE; | |
321 | } | |
d0d6ff58 | 322 | nbd_update_server_watch(); |
7860a380 | 323 | nbd_client_put(client); |
a61c6782 PB |
324 | } |
325 | ||
d0d6ff58 | 326 | static gboolean nbd_accept(QIOChannel *ioc, GIOCondition cond, gpointer opaque) |
a61c6782 | 327 | { |
d0d6ff58 | 328 | QIOChannelSocket *cioc; |
a61c6782 | 329 | |
d0d6ff58 DB |
330 | cioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), |
331 | NULL); | |
332 | if (!cioc) { | |
333 | return TRUE; | |
0c544d73 PB |
334 | } |
335 | ||
7860a380 | 336 | if (state >= TERMINATE) { |
d0d6ff58 DB |
337 | object_unref(OBJECT(cioc)); |
338 | return TRUE; | |
7860a380 PB |
339 | } |
340 | ||
ee7d7aab | 341 | nb_fds++; |
d0d6ff58 | 342 | nbd_update_server_watch(); |
3d4b2f9c | 343 | nbd_client_new(newproto ? NULL : exp, cioc, nbd_client_closed); |
d0d6ff58 DB |
344 | object_unref(OBJECT(cioc)); |
345 | ||
346 | return TRUE; | |
a61c6782 PB |
347 | } |
348 | ||
d0d6ff58 | 349 | static void nbd_update_server_watch(void) |
e4afbf4f FZ |
350 | { |
351 | if (nbd_can_accept()) { | |
d0d6ff58 DB |
352 | if (server_watch == -1) { |
353 | server_watch = qio_channel_add_watch(QIO_CHANNEL(server_ioc), | |
354 | G_IO_IN, | |
355 | nbd_accept, | |
356 | NULL, NULL); | |
357 | } | |
e4afbf4f | 358 | } else { |
d0d6ff58 DB |
359 | if (server_watch != -1) { |
360 | g_source_remove(server_watch); | |
361 | server_watch = -1; | |
362 | } | |
e4afbf4f FZ |
363 | } |
364 | } | |
365 | ||
48bec07e DB |
366 | |
367 | static SocketAddress *nbd_build_socket_address(const char *sockpath, | |
368 | const char *bindto, | |
369 | const char *port) | |
370 | { | |
371 | SocketAddress *saddr; | |
372 | ||
373 | saddr = g_new0(SocketAddress, 1); | |
374 | if (sockpath) { | |
2d32adda EB |
375 | saddr->type = SOCKET_ADDRESS_KIND_UNIX; |
376 | saddr->u.q_unix = g_new0(UnixSocketAddress, 1); | |
377 | saddr->u.q_unix->path = g_strdup(sockpath); | |
48bec07e | 378 | } else { |
2d32adda EB |
379 | saddr->type = SOCKET_ADDRESS_KIND_INET; |
380 | saddr->u.inet = g_new0(InetSocketAddress, 1); | |
381 | saddr->u.inet->host = g_strdup(bindto); | |
48bec07e | 382 | if (port) { |
2d32adda | 383 | saddr->u.inet->port = g_strdup(port); |
48bec07e | 384 | } else { |
2d32adda | 385 | saddr->u.inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT); |
48bec07e DB |
386 | } |
387 | } | |
388 | ||
389 | return saddr; | |
390 | } | |
391 | ||
392 | ||
0ab3b337 DB |
393 | static QemuOptsList qemu_object_opts = { |
394 | .name = "object", | |
395 | .implied_opt_name = "qom-type", | |
396 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), | |
397 | .desc = { | |
398 | { } | |
399 | }, | |
400 | }; | |
401 | ||
402 | ||
7a5ca864 FB |
403 | int main(int argc, char **argv) |
404 | { | |
26f54e9a | 405 | BlockBackend *blk; |
7a5ca864 FB |
406 | BlockDriverState *bs; |
407 | off_t dev_offset = 0; | |
b90fb4b8 | 408 | uint32_t nbdflags = 0; |
cd831bd7 | 409 | bool disconnect = false; |
7a5ca864 | 410 | const char *bindto = "0.0.0.0"; |
48bec07e DB |
411 | const char *port = NULL; |
412 | char *sockpath = NULL; | |
a6ac2313 | 413 | char *device = NULL; |
7a5ca864 | 414 | off_t fd_size; |
8c116b0e WX |
415 | QemuOpts *sn_opts = NULL; |
416 | const char *sn_id_or_name = NULL; | |
3d4b2f9c | 417 | const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:x:"; |
7a5ca864 | 418 | struct option lopt[] = { |
660f11be BS |
419 | { "help", 0, NULL, 'h' }, |
420 | { "version", 0, NULL, 'V' }, | |
421 | { "bind", 1, NULL, 'b' }, | |
422 | { "port", 1, NULL, 'p' }, | |
423 | { "socket", 1, NULL, 'k' }, | |
424 | { "offset", 1, NULL, 'o' }, | |
425 | { "read-only", 0, NULL, 'r' }, | |
426 | { "partition", 1, NULL, 'P' }, | |
427 | { "connect", 1, NULL, 'c' }, | |
428 | { "disconnect", 0, NULL, 'd' }, | |
429 | { "snapshot", 0, NULL, 's' }, | |
8c116b0e | 430 | { "load-snapshot", 1, NULL, 'l' }, |
660f11be | 431 | { "nocache", 0, NULL, 'n' }, |
39a5235c | 432 | { "cache", 1, NULL, QEMU_NBD_OPT_CACHE }, |
39a5235c | 433 | { "aio", 1, NULL, QEMU_NBD_OPT_AIO }, |
ded9d2d5 | 434 | { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD }, |
b3838a40 | 435 | { "detect-zeroes", 1, NULL, QEMU_NBD_OPT_DETECT_ZEROES }, |
660f11be | 436 | { "shared", 1, NULL, 'e' }, |
e6b63677 | 437 | { "format", 1, NULL, 'f' }, |
660f11be BS |
438 | { "persistent", 0, NULL, 't' }, |
439 | { "verbose", 0, NULL, 'v' }, | |
0ab3b337 | 440 | { "object", 1, NULL, QEMU_NBD_OPT_OBJECT }, |
3d4b2f9c | 441 | { "export-name", 1, NULL, 'x' }, |
660f11be | 442 | { NULL, 0, NULL, 0 } |
7a5ca864 FB |
443 | }; |
444 | int ch; | |
445 | int opt_ind = 0; | |
7a5ca864 | 446 | char *end; |
f5edb014 | 447 | int flags = BDRV_O_RDWR; |
7a5ca864 | 448 | int partition = -1; |
4fbec260 | 449 | int ret = 0; |
39a5235c | 450 | bool seen_cache = false; |
ded9d2d5 | 451 | bool seen_discard = false; |
39a5235c | 452 | bool seen_aio = false; |
a517e88b | 453 | pthread_t client_thread; |
e6b63677 | 454 | const char *fmt = NULL; |
34b5d2c6 | 455 | Error *local_err = NULL; |
b3838a40 | 456 | BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; |
4fbec260 | 457 | QDict *options = NULL; |
3d4b2f9c | 458 | const char *export_name = NULL; |
7a5ca864 | 459 | |
a517e88b PB |
460 | /* The client thread uses SIGTERM to interrupt the server. A signal |
461 | * handler ensures that "qemu-nbd -v -c" exits with a nice status code. | |
462 | */ | |
bb345110 | 463 | struct sigaction sa_sigterm; |
bb345110 PB |
464 | memset(&sa_sigterm, 0, sizeof(sa_sigterm)); |
465 | sa_sigterm.sa_handler = termsig_handler; | |
466 | sigaction(SIGTERM, &sa_sigterm, NULL); | |
0ab3b337 DB |
467 | module_call_init(MODULE_INIT_QOM); |
468 | qemu_add_opts(&qemu_object_opts); | |
10f5bff6 | 469 | qemu_init_exec_dir(argv[0]); |
bb345110 | 470 | |
7a5ca864 FB |
471 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
472 | switch (ch) { | |
473 | case 's': | |
2f726488 TS |
474 | flags |= BDRV_O_SNAPSHOT; |
475 | break; | |
476 | case 'n': | |
39a5235c PB |
477 | optarg = (char *) "none"; |
478 | /* fallthrough */ | |
479 | case QEMU_NBD_OPT_CACHE: | |
480 | if (seen_cache) { | |
85b01e09 MA |
481 | error_report("-n and --cache can only be specified once"); |
482 | exit(EXIT_FAILURE); | |
39a5235c PB |
483 | } |
484 | seen_cache = true; | |
485 | if (bdrv_parse_cache_flags(optarg, &flags) == -1) { | |
85b01e09 MA |
486 | error_report("Invalid cache mode `%s'", optarg); |
487 | exit(EXIT_FAILURE); | |
39a5235c | 488 | } |
7a5ca864 | 489 | break; |
39a5235c PB |
490 | case QEMU_NBD_OPT_AIO: |
491 | if (seen_aio) { | |
85b01e09 MA |
492 | error_report("--aio can only be specified once"); |
493 | exit(EXIT_FAILURE); | |
39a5235c PB |
494 | } |
495 | seen_aio = true; | |
496 | if (!strcmp(optarg, "native")) { | |
497 | flags |= BDRV_O_NATIVE_AIO; | |
498 | } else if (!strcmp(optarg, "threads")) { | |
499 | /* this is the default */ | |
500 | } else { | |
85b01e09 MA |
501 | error_report("invalid aio mode `%s'", optarg); |
502 | exit(EXIT_FAILURE); | |
39a5235c PB |
503 | } |
504 | break; | |
ded9d2d5 PB |
505 | case QEMU_NBD_OPT_DISCARD: |
506 | if (seen_discard) { | |
85b01e09 MA |
507 | error_report("--discard can only be specified once"); |
508 | exit(EXIT_FAILURE); | |
ded9d2d5 PB |
509 | } |
510 | seen_discard = true; | |
511 | if (bdrv_parse_discard_flags(optarg, &flags) == -1) { | |
85b01e09 MA |
512 | error_report("Invalid discard mode `%s'", optarg); |
513 | exit(EXIT_FAILURE); | |
ded9d2d5 PB |
514 | } |
515 | break; | |
b3838a40 PL |
516 | case QEMU_NBD_OPT_DETECT_ZEROES: |
517 | detect_zeroes = | |
518 | qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, | |
519 | optarg, | |
7fb1cf16 | 520 | BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX, |
b3838a40 PL |
521 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, |
522 | &local_err); | |
523 | if (local_err) { | |
c29b77f9 MA |
524 | error_reportf_err(local_err, |
525 | "Failed to parse detect_zeroes mode: "); | |
85b01e09 | 526 | exit(EXIT_FAILURE); |
b3838a40 PL |
527 | } |
528 | if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && | |
529 | !(flags & BDRV_O_UNMAP)) { | |
85b01e09 MA |
530 | error_report("setting detect-zeroes to unmap is not allowed " |
531 | "without setting discard operation to unmap"); | |
532 | exit(EXIT_FAILURE); | |
b3838a40 PL |
533 | } |
534 | break; | |
7a5ca864 FB |
535 | case 'b': |
536 | bindto = optarg; | |
537 | break; | |
538 | case 'p': | |
48bec07e | 539 | port = optarg; |
7a5ca864 FB |
540 | break; |
541 | case 'o': | |
542 | dev_offset = strtoll (optarg, &end, 0); | |
543 | if (*end) { | |
85b01e09 MA |
544 | error_report("Invalid offset `%s'", optarg); |
545 | exit(EXIT_FAILURE); | |
7a5ca864 FB |
546 | } |
547 | if (dev_offset < 0) { | |
85b01e09 MA |
548 | error_report("Offset must be positive `%s'", optarg); |
549 | exit(EXIT_FAILURE); | |
7a5ca864 FB |
550 | } |
551 | break; | |
8c116b0e WX |
552 | case 'l': |
553 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
70b94331 MA |
554 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
555 | optarg, false); | |
8c116b0e | 556 | if (!sn_opts) { |
85b01e09 MA |
557 | error_report("Failed in parsing snapshot param `%s'", |
558 | optarg); | |
559 | exit(EXIT_FAILURE); | |
8c116b0e WX |
560 | } |
561 | } else { | |
562 | sn_id_or_name = optarg; | |
563 | } | |
564 | /* fall through */ | |
7a5ca864 | 565 | case 'r': |
b90fb4b8 | 566 | nbdflags |= NBD_FLAG_READ_ONLY; |
07108b29 | 567 | flags &= ~BDRV_O_RDWR; |
7a5ca864 FB |
568 | break; |
569 | case 'P': | |
570 | partition = strtol(optarg, &end, 0); | |
713cc671 | 571 | if (*end) { |
85b01e09 MA |
572 | error_report("Invalid partition `%s'", optarg); |
573 | exit(EXIT_FAILURE); | |
713cc671 PL |
574 | } |
575 | if (partition < 1 || partition > 8) { | |
85b01e09 MA |
576 | error_report("Invalid partition %d", partition); |
577 | exit(EXIT_FAILURE); | |
713cc671 | 578 | } |
7a5ca864 | 579 | break; |
cd831bd7 | 580 | case 'k': |
b32f6c28 | 581 | sockpath = optarg; |
713cc671 | 582 | if (sockpath[0] != '/') { |
9af9e0fe | 583 | error_report("socket path must be absolute"); |
85b01e09 | 584 | exit(EXIT_FAILURE); |
713cc671 | 585 | } |
cd831bd7 TS |
586 | break; |
587 | case 'd': | |
588 | disconnect = true; | |
589 | break; | |
590 | case 'c': | |
591 | device = optarg; | |
592 | break; | |
3b05a8e9 TS |
593 | case 'e': |
594 | shared = strtol(optarg, &end, 0); | |
595 | if (*end) { | |
85b01e09 MA |
596 | error_report("Invalid shared device number '%s'", optarg); |
597 | exit(EXIT_FAILURE); | |
3b05a8e9 TS |
598 | } |
599 | if (shared < 1) { | |
9af9e0fe | 600 | error_report("Shared device number must be greater than 0"); |
85b01e09 | 601 | exit(EXIT_FAILURE); |
3b05a8e9 TS |
602 | } |
603 | break; | |
e6b63677 DB |
604 | case 'f': |
605 | fmt = optarg; | |
606 | break; | |
713cc671 PL |
607 | case 't': |
608 | persistent = 1; | |
609 | break; | |
3d4b2f9c DB |
610 | case 'x': |
611 | export_name = optarg; | |
612 | break; | |
7a5ca864 FB |
613 | case 'v': |
614 | verbose = 1; | |
615 | break; | |
616 | case 'V': | |
617 | version(argv[0]); | |
618 | exit(0); | |
619 | break; | |
620 | case 'h': | |
621 | usage(argv[0]); | |
622 | exit(0); | |
623 | break; | |
624 | case '?': | |
85b01e09 MA |
625 | error_report("Try `%s --help' for more information.", argv[0]); |
626 | exit(EXIT_FAILURE); | |
0ab3b337 DB |
627 | case QEMU_NBD_OPT_OBJECT: { |
628 | QemuOpts *opts; | |
629 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
630 | optarg, true); | |
631 | if (!opts) { | |
632 | exit(EXIT_FAILURE); | |
633 | } | |
634 | } break; | |
7a5ca864 FB |
635 | } |
636 | } | |
637 | ||
638 | if ((argc - optind) != 1) { | |
433672b0 MA |
639 | error_report("Invalid number of arguments"); |
640 | error_printf("Try `%s --help' for more information.\n", argv[0]); | |
85b01e09 | 641 | exit(EXIT_FAILURE); |
7a5ca864 FB |
642 | } |
643 | ||
0ab3b337 DB |
644 | if (qemu_opts_foreach(&qemu_object_opts, |
645 | user_creatable_add_opts_foreach, | |
646 | NULL, &local_err)) { | |
647 | error_report_err(local_err); | |
648 | exit(EXIT_FAILURE); | |
649 | } | |
650 | ||
cd831bd7 | 651 | if (disconnect) { |
d0d6ff58 DB |
652 | int nbdfd = open(argv[optind], O_RDWR); |
653 | if (nbdfd < 0) { | |
85b01e09 MA |
654 | error_report("Cannot open %s: %s", argv[optind], |
655 | strerror(errno)); | |
656 | exit(EXIT_FAILURE); | |
fc19f8a0 | 657 | } |
d0d6ff58 | 658 | nbd_disconnect(nbdfd); |
cd831bd7 | 659 | |
d0d6ff58 | 660 | close(nbdfd); |
cd831bd7 TS |
661 | |
662 | printf("%s disconnected\n", argv[optind]); | |
663 | ||
713cc671 | 664 | return 0; |
cd831bd7 TS |
665 | } |
666 | ||
c1f8fdc3 PB |
667 | if (device && !verbose) { |
668 | int stderr_fd[2]; | |
669 | pid_t pid; | |
670 | int ret; | |
671 | ||
fc19f8a0 | 672 | if (qemu_pipe(stderr_fd) < 0) { |
85b01e09 MA |
673 | error_report("Error setting up communication pipe: %s", |
674 | strerror(errno)); | |
675 | exit(EXIT_FAILURE); | |
c1f8fdc3 PB |
676 | } |
677 | ||
678 | /* Now daemonize, but keep a communication channel open to | |
679 | * print errors and exit with the proper status code. | |
680 | */ | |
681 | pid = fork(); | |
70d4739e | 682 | if (pid < 0) { |
85b01e09 MA |
683 | error_report("Failed to fork: %s", strerror(errno)); |
684 | exit(EXIT_FAILURE); | |
70d4739e | 685 | } else if (pid == 0) { |
c1f8fdc3 | 686 | close(stderr_fd[0]); |
9faf31b6 | 687 | ret = qemu_daemon(1, 0); |
c1f8fdc3 PB |
688 | |
689 | /* Temporarily redirect stderr to the parent's pipe... */ | |
690 | dup2(stderr_fd[1], STDERR_FILENO); | |
fc19f8a0 | 691 | if (ret < 0) { |
85b01e09 MA |
692 | error_report("Failed to daemonize: %s", strerror(errno)); |
693 | exit(EXIT_FAILURE); | |
c1f8fdc3 PB |
694 | } |
695 | ||
696 | /* ... close the descriptor we inherited and go on. */ | |
697 | close(stderr_fd[1]); | |
698 | } else { | |
699 | bool errors = false; | |
700 | char *buf; | |
701 | ||
702 | /* In the parent. Print error messages from the child until | |
703 | * it closes the pipe. | |
704 | */ | |
705 | close(stderr_fd[1]); | |
706 | buf = g_malloc(1024); | |
707 | while ((ret = read(stderr_fd[0], buf, 1024)) > 0) { | |
708 | errors = true; | |
709 | ret = qemu_write_full(STDERR_FILENO, buf, ret); | |
fc19f8a0 | 710 | if (ret < 0) { |
c1f8fdc3 PB |
711 | exit(EXIT_FAILURE); |
712 | } | |
713 | } | |
fc19f8a0 | 714 | if (ret < 0) { |
85b01e09 MA |
715 | error_report("Cannot read from daemon: %s", |
716 | strerror(errno)); | |
717 | exit(EXIT_FAILURE); | |
c1f8fdc3 PB |
718 | } |
719 | ||
720 | /* Usually the daemon should not print any message. | |
721 | * Exit with zero status in that case. | |
722 | */ | |
723 | exit(errors); | |
724 | } | |
725 | } | |
726 | ||
a6ac2313 PB |
727 | if (device != NULL && sockpath == NULL) { |
728 | sockpath = g_malloc(128); | |
729 | snprintf(sockpath, 128, SOCKET_PATH, basename(device)); | |
cd831bd7 TS |
730 | } |
731 | ||
48bec07e DB |
732 | saddr = nbd_build_socket_address(sockpath, bindto, port); |
733 | ||
2f78e491 | 734 | if (qemu_init_main_loop(&local_err)) { |
565f65d2 | 735 | error_report_err(local_err); |
2f78e491 CN |
736 | exit(EXIT_FAILURE); |
737 | } | |
802ddc37 PB |
738 | bdrv_init(); |
739 | atexit(bdrv_close_all); | |
740 | ||
e6b63677 | 741 | if (fmt) { |
4fbec260 HR |
742 | options = qdict_new(); |
743 | qdict_put(options, "driver", qstring_from_str(fmt)); | |
e6b63677 DB |
744 | } |
745 | ||
802ddc37 | 746 | srcpath = argv[optind]; |
4fbec260 HR |
747 | blk = blk_new_open("hda", srcpath, NULL, options, flags, &local_err); |
748 | if (!blk) { | |
c29b77f9 MA |
749 | error_reportf_err(local_err, "Failed to blk_new_open '%s': ", |
750 | argv[optind]); | |
85b01e09 | 751 | exit(EXIT_FAILURE); |
802ddc37 | 752 | } |
4fbec260 | 753 | bs = blk_bs(blk); |
802ddc37 | 754 | |
8c116b0e WX |
755 | if (sn_opts) { |
756 | ret = bdrv_snapshot_load_tmp(bs, | |
757 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), | |
758 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
759 | &local_err); | |
760 | } else if (sn_id_or_name) { | |
761 | ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name, | |
762 | &local_err); | |
763 | } | |
764 | if (ret < 0) { | |
c29b77f9 | 765 | error_reportf_err(local_err, "Failed to load snapshot: "); |
85b01e09 | 766 | exit(EXIT_FAILURE); |
8c116b0e WX |
767 | } |
768 | ||
b3838a40 | 769 | bs->detect_zeroes = detect_zeroes; |
4c58e80a | 770 | fd_size = blk_getlength(blk); |
98f44bbe | 771 | if (fd_size < 0) { |
85b01e09 MA |
772 | error_report("Failed to determine the image length: %s", |
773 | strerror(-fd_size)); | |
774 | exit(EXIT_FAILURE); | |
98f44bbe | 775 | } |
802ddc37 | 776 | |
185b4338 | 777 | if (partition != -1) { |
4c58e80a | 778 | ret = find_partition(blk, partition, &dev_offset, &fd_size); |
185b4338 | 779 | if (ret < 0) { |
85b01e09 | 780 | error_report("Could not find partition %d: %s", partition, |
a4699e55 | 781 | strerror(-ret)); |
85b01e09 | 782 | exit(EXIT_FAILURE); |
185b4338 | 783 | } |
802ddc37 PB |
784 | } |
785 | ||
98f44bbe HR |
786 | exp = nbd_export_new(blk, dev_offset, fd_size, nbdflags, nbd_export_closed, |
787 | &local_err); | |
788 | if (!exp) { | |
4fffeb5e | 789 | error_report_err(local_err); |
85b01e09 | 790 | exit(EXIT_FAILURE); |
98f44bbe | 791 | } |
3d4b2f9c DB |
792 | if (export_name) { |
793 | nbd_export_set_name(exp, export_name); | |
794 | newproto = true; | |
795 | } | |
3b05a8e9 | 796 | |
d0d6ff58 DB |
797 | server_ioc = qio_channel_socket_new(); |
798 | if (qio_channel_socket_listen_sync(server_ioc, saddr, &local_err) < 0) { | |
799 | object_unref(OBJECT(server_ioc)); | |
48bec07e | 800 | error_report_err(local_err); |
7a5ca864 | 801 | return 1; |
a61c6782 | 802 | } |
f1ef5555 PB |
803 | |
804 | if (device) { | |
805 | int ret; | |
806 | ||
a6ac2313 | 807 | ret = pthread_create(&client_thread, NULL, nbd_client_thread, device); |
f1ef5555 | 808 | if (ret != 0) { |
85b01e09 MA |
809 | error_report("Failed to create client thread: %s", strerror(ret)); |
810 | exit(EXIT_FAILURE); | |
f1ef5555 PB |
811 | } |
812 | } else { | |
813 | /* Shut up GCC warnings. */ | |
814 | memset(&client_thread, 0, sizeof(client_thread)); | |
815 | } | |
816 | ||
d0d6ff58 | 817 | nbd_update_server_watch(); |
7a5ca864 | 818 | |
9faf31b6 MT |
819 | /* now when the initialization is (almost) complete, chdir("/") |
820 | * to free any busy filesystems */ | |
821 | if (chdir("/") < 0) { | |
85b01e09 MA |
822 | error_report("Could not chdir to root directory: %s", |
823 | strerror(errno)); | |
824 | exit(EXIT_FAILURE); | |
9faf31b6 MT |
825 | } |
826 | ||
7860a380 | 827 | state = RUNNING; |
3b05a8e9 | 828 | do { |
a61c6782 | 829 | main_loop_wait(false); |
7860a380 PB |
830 | if (state == TERMINATE) { |
831 | state = TERMINATING; | |
832 | nbd_export_close(exp); | |
833 | nbd_export_put(exp); | |
834 | exp = NULL; | |
835 | } | |
836 | } while (state != TERMINATED); | |
7a5ca864 | 837 | |
26f54e9a | 838 | blk_unref(blk); |
b32f6c28 PB |
839 | if (sockpath) { |
840 | unlink(sockpath); | |
841 | } | |
7a5ca864 | 842 | |
fbf28a43 | 843 | qemu_opts_del(sn_opts); |
8c116b0e | 844 | |
a517e88b PB |
845 | if (device) { |
846 | void *ret; | |
847 | pthread_join(client_thread, &ret); | |
848 | exit(ret != NULL); | |
849 | } else { | |
850 | exit(EXIT_SUCCESS); | |
851 | } | |
7a5ca864 | 852 | } |