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