]>
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" |
c2a3d7da EB |
20 | #include <getopt.h> |
21 | #include <libgen.h> | |
22 | #include <pthread.h> | |
23 | ||
a8d25326 | 24 | #include "qemu-common.h" |
da34e65c | 25 | #include "qapi/error.h" |
f348b6d1 | 26 | #include "qemu/cutils.h" |
26f54e9a | 27 | #include "sysemu/block-backend.h" |
cbc20bfb | 28 | #include "sysemu/runstate.h" /* for qemu_system_killed() prototype */ |
b3838a40 | 29 | #include "block/block_int.h" |
737e150e | 30 | #include "block/nbd.h" |
6a1751b7 | 31 | #include "qemu/main-loop.h" |
0b8fa32f | 32 | #include "qemu/module.h" |
922a01a0 | 33 | #include "qemu/option.h" |
537b41f5 | 34 | #include "qemu/error-report.h" |
0ab3b337 | 35 | #include "qemu/config-file.h" |
58369e22 | 36 | #include "qemu/bswap.h" |
39ca463e | 37 | #include "qemu/log.h" |
53fabd4b | 38 | #include "qemu/systemd.h" |
8c116b0e | 39 | #include "block/snapshot.h" |
452fcdbc | 40 | #include "qapi/qmp/qdict.h" |
d49b6836 | 41 | #include "qapi/qmp/qstring.h" |
0ab3b337 | 42 | #include "qom/object_interfaces.h" |
d0d6ff58 | 43 | #include "io/channel-socket.h" |
e4849c1d | 44 | #include "io/net-listener.h" |
c2297088 | 45 | #include "crypto/init.h" |
0279cd95 | 46 | #include "crypto/tlscreds.h" |
39ca463e | 47 | #include "trace/control.h" |
be377133 | 48 | #include "qemu-version.h" |
7a5ca864 | 49 | |
3c1fa35d EB |
50 | #ifdef __linux__ |
51 | #define HAVE_NBD_DEVICE 1 | |
52 | #else | |
53 | #define HAVE_NBD_DEVICE 0 | |
54 | #endif | |
55 | ||
713cc671 | 56 | #define SOCKET_PATH "/var/lock/qemu-nbd-%s" |
fa8b7ce2 DB |
57 | #define QEMU_NBD_OPT_CACHE 256 |
58 | #define QEMU_NBD_OPT_AIO 257 | |
59 | #define QEMU_NBD_OPT_DISCARD 258 | |
60 | #define QEMU_NBD_OPT_DETECT_ZEROES 259 | |
61 | #define QEMU_NBD_OPT_OBJECT 260 | |
62 | #define QEMU_NBD_OPT_TLSCREDS 261 | |
63 | #define QEMU_NBD_OPT_IMAGE_OPTS 262 | |
ffb31e1d | 64 | #define QEMU_NBD_OPT_FORK 263 |
b25e12da | 65 | #define QEMU_NBD_OPT_TLSAUTHZ 264 |
637bc5a5 | 66 | #define QEMU_NBD_OPT_PID_FILE 265 |
7a5ca864 | 67 | |
bd31c214 EB |
68 | #define MBR_SIZE 512 |
69 | ||
b1d8e52e | 70 | static int verbose; |
a517e88b | 71 | static char *srcpath; |
bd269ebc | 72 | static SocketAddress *saddr; |
7860a380 | 73 | static int persistent = 0; |
d794f7f3 | 74 | static enum { RUNNING, TERMINATE, TERMINATED } state; |
a61c6782 PB |
75 | static int shared = 1; |
76 | static int nb_fds; | |
e4849c1d | 77 | static QIONetListener *server; |
145614a1 | 78 | static QCryptoTLSCreds *tlscreds; |
b25e12da | 79 | static const char *tlsauthz; |
7a5ca864 FB |
80 | |
81 | static void usage(const char *name) | |
82 | { | |
b033cd86 | 83 | (printf) ( |
7a5ca864 | 84 | "Usage: %s [OPTIONS] FILE\n" |
68b96f15 EB |
85 | " or: %s -L [OPTIONS]\n" |
86 | "QEMU Disk Network Block Device Utility\n" | |
7a5ca864 | 87 | "\n" |
713cc671 PL |
88 | " -h, --help display this help and exit\n" |
89 | " -V, --version output version information and exit\n" | |
b033cd86 PB |
90 | "\n" |
91 | "Connection properties:\n" | |
713cc671 PL |
92 | " -p, --port=PORT port to listen on (default `%d')\n" |
93 | " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n" | |
94 | " -k, --socket=PATH path to the unix socket\n" | |
95 | " (default '"SOCKET_PATH"')\n" | |
96 | " -e, --shared=NUM device can be shared by NUM clients (default '1')\n" | |
97 | " -t, --persistent don't exit on the last connection\n" | |
98 | " -v, --verbose display extra debugging information\n" | |
f5cd0bb5 VSO |
99 | " -x, --export-name=NAME expose export by name (default is empty string)\n" |
100 | " -D, --description=TEXT export a human-readable description\n" | |
7a5ca864 | 101 | "\n" |
b033cd86 | 102 | "Exposing part of the image:\n" |
713cc671 | 103 | " -o, --offset=OFFSET offset into the image\n" |
dbc7b014 | 104 | " -A, --allocation-depth expose the allocation depth\n" |
636192c4 | 105 | " -B, --bitmap=NAME expose a persistent dirty bitmap\n" |
b033cd86 | 106 | "\n" |
0ab3b337 | 107 | "General purpose options:\n" |
68b96f15 | 108 | " -L, --list list exports available from another NBD server\n" |
0ab3b337 DB |
109 | " --object type,id=ID,... define an object such as 'secret' for providing\n" |
110 | " passwords and/or encryption keys\n" | |
f7812df7 | 111 | " --tls-creds=ID use id of an earlier --object to provide TLS\n" |
b25e12da DB |
112 | " --tls-authz=ID use id of an earlier --object to provide\n" |
113 | " authorization\n" | |
39ca463e DL |
114 | " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
115 | " specify tracing options\n" | |
ffb31e1d HR |
116 | " --fork fork off the server process and exit the parent\n" |
117 | " once the server is running\n" | |
637bc5a5 | 118 | " --pid-file=PATH store the server's process ID in the given file\n" |
3c1fa35d EB |
119 | #if HAVE_NBD_DEVICE |
120 | "\n" | |
b033cd86 | 121 | "Kernel NBD client support:\n" |
713cc671 PL |
122 | " -c, --connect=DEV connect FILE to the local NBD device DEV\n" |
123 | " -d, --disconnect disconnect the specified device\n" | |
b033cd86 PB |
124 | #endif |
125 | "\n" | |
126 | "Block device options:\n" | |
713cc671 PL |
127 | " -f, --format=FORMAT set image format (raw, qcow2, ...)\n" |
128 | " -r, --read-only export read-only\n" | |
129 | " -s, --snapshot use FILE as an external snapshot, create a temporary\n" | |
130 | " file with backing_file=FILE, redirect the write to\n" | |
131 | " the temporary one\n" | |
8c116b0e | 132 | " -l, --load-snapshot=SNAPSHOT_PARAM\n" |
713cc671 PL |
133 | " load an internal snapshot inside FILE and export it\n" |
134 | " as an read-only device, SNAPSHOT_PARAM format is\n" | |
135 | " 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" | |
136 | " '[ID_OR_NAME]'\n" | |
137 | " -n, --nocache disable host cache\n" | |
09615257 NS |
138 | " --cache=MODE set cache mode used to access the disk image, the\n" |
139 | " valid options are: 'none', 'writeback' (default),\n" | |
140 | " 'writethrough', 'directsync' and 'unsafe'\n" | |
7680274d | 141 | " --aio=MODE set AIO mode (native, io_uring or threads)\n" |
b3838a40 | 142 | " --discard=MODE set discard mode (ignore, unmap)\n" |
6883de6c | 143 | " --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n" |
77c9aaef | 144 | " --image-opts treat FILE as a full set of image options\n" |
b033cd86 | 145 | "\n" |
f5048cb7 | 146 | QEMU_HELP_BOTTOM "\n" |
68b96f15 | 147 | , name, name, NBD_DEFAULT_PORT, "DEVICE"); |
7a5ca864 FB |
148 | } |
149 | ||
150 | static void version(const char *name) | |
151 | { | |
152 | printf( | |
7e563bfb | 153 | "%s " QEMU_FULL_VERSION "\n" |
7a5ca864 FB |
154 | "Written by Anthony Liguori.\n" |
155 | "\n" | |
be377133 | 156 | QEMU_COPYRIGHT "\n" |
7a5ca864 FB |
157 | "This is free software; see the source for copying conditions. There is NO\n" |
158 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" | |
315bc7aa | 159 | , name); |
7a5ca864 FB |
160 | } |
161 | ||
029a88c9 | 162 | #ifdef CONFIG_POSIX |
cbc20bfb SH |
163 | /* |
164 | * The client thread uses SIGTERM to interrupt the server. A signal | |
165 | * handler ensures that "qemu-nbd -v -c" exits with a nice status code. | |
166 | */ | |
167 | void qemu_system_killed(int signum, pid_t pid) | |
bb345110 | 168 | { |
d73415a3 | 169 | qatomic_cmpxchg(&state, RUNNING, TERMINATE); |
a61c6782 | 170 | qemu_notify_event(); |
bb345110 | 171 | } |
029a88c9 | 172 | #endif /* CONFIG_POSIX */ |
537b41f5 | 173 | |
68b96f15 EB |
174 | static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls, |
175 | const char *hostname) | |
176 | { | |
177 | int ret = EXIT_FAILURE; | |
178 | int rc; | |
179 | Error *err = NULL; | |
180 | QIOChannelSocket *sioc; | |
181 | NBDExportInfo *list; | |
182 | int i, j; | |
183 | ||
184 | sioc = qio_channel_socket_new(); | |
185 | if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) { | |
186 | error_report_err(err); | |
992809bf | 187 | goto out; |
68b96f15 EB |
188 | } |
189 | rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list, | |
190 | &err); | |
191 | if (rc < 0) { | |
192 | if (err) { | |
193 | error_report_err(err); | |
194 | } | |
195 | goto out; | |
196 | } | |
197 | printf("exports available: %d\n", rc); | |
198 | for (i = 0; i < rc; i++) { | |
199 | printf(" export: '%s'\n", list[i].name); | |
200 | if (list[i].description && *list[i].description) { | |
201 | printf(" description: %s\n", list[i].description); | |
202 | } | |
203 | if (list[i].flags & NBD_FLAG_HAS_FLAGS) { | |
c4e2aff8 HR |
204 | static const char *const flag_names[] = { |
205 | [NBD_FLAG_READ_ONLY_BIT] = "readonly", | |
206 | [NBD_FLAG_SEND_FLUSH_BIT] = "flush", | |
207 | [NBD_FLAG_SEND_FUA_BIT] = "fua", | |
208 | [NBD_FLAG_ROTATIONAL_BIT] = "rotational", | |
209 | [NBD_FLAG_SEND_TRIM_BIT] = "trim", | |
210 | [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes", | |
211 | [NBD_FLAG_SEND_DF_BIT] = "df", | |
212 | [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi", | |
213 | [NBD_FLAG_SEND_RESIZE_BIT] = "resize", | |
214 | [NBD_FLAG_SEND_CACHE_BIT] = "cache", | |
0a479545 | 215 | [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero", |
c4e2aff8 HR |
216 | }; |
217 | ||
68b96f15 EB |
218 | printf(" size: %" PRIu64 "\n", list[i].size); |
219 | printf(" flags: 0x%x (", list[i].flags); | |
c4e2aff8 HR |
220 | for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) { |
221 | if (flag_names[bit] && (list[i].flags & (1 << bit))) { | |
222 | printf(" %s", flag_names[bit]); | |
223 | } | |
68b96f15 EB |
224 | } |
225 | printf(" )\n"); | |
226 | } | |
227 | if (list[i].min_block) { | |
228 | printf(" min block: %u\n", list[i].min_block); | |
229 | printf(" opt block: %u\n", list[i].opt_block); | |
230 | printf(" max block: %u\n", list[i].max_block); | |
231 | } | |
232 | if (list[i].n_contexts) { | |
233 | printf(" available meta contexts: %d\n", list[i].n_contexts); | |
234 | for (j = 0; j < list[i].n_contexts; j++) { | |
235 | printf(" %s\n", list[i].contexts[j]); | |
236 | } | |
237 | } | |
238 | } | |
239 | nbd_free_export_list(list, rc); | |
240 | ||
241 | ret = EXIT_SUCCESS; | |
242 | out: | |
243 | object_unref(OBJECT(sioc)); | |
244 | return ret; | |
245 | } | |
246 | ||
247 | ||
3c1fa35d | 248 | #if HAVE_NBD_DEVICE |
a517e88b | 249 | static void *show_parts(void *arg) |
cd831bd7 | 250 | { |
a6ac2313 | 251 | char *device = arg; |
a517e88b PB |
252 | int nbd; |
253 | ||
254 | /* linux just needs an open() to trigger | |
255 | * the partition table update | |
256 | * but remember to load the module with max_part != 0 : | |
257 | * modprobe nbd max_part=63 | |
258 | */ | |
259 | nbd = open(device, O_RDWR); | |
fc19f8a0 | 260 | if (nbd >= 0) { |
a517e88b PB |
261 | close(nbd); |
262 | } | |
263 | return NULL; | |
264 | } | |
cd831bd7 | 265 | |
a517e88b PB |
266 | static void *nbd_client_thread(void *arg) |
267 | { | |
a6ac2313 | 268 | char *device = arg; |
6dc1667d | 269 | NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") }; |
d0d6ff58 | 270 | QIOChannelSocket *sioc; |
af74b550 AC |
271 | int fd = -1; |
272 | int ret = EXIT_FAILURE; | |
a517e88b | 273 | pthread_t show_parts_thread; |
1ce52846 | 274 | Error *local_error = NULL; |
a517e88b | 275 | |
d0d6ff58 DB |
276 | sioc = qio_channel_socket_new(); |
277 | if (qio_channel_socket_connect_sync(sioc, | |
278 | saddr, | |
279 | &local_error) < 0) { | |
48bec07e | 280 | error_report_err(local_error); |
dc10e8b3 SH |
281 | goto out; |
282 | } | |
a517e88b | 283 | |
af74b550 AC |
284 | if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc), |
285 | NULL, NULL, NULL, &info, &local_error) < 0) { | |
1ce52846 | 286 | if (local_error) { |
78288671 | 287 | error_report_err(local_error); |
1ce52846 | 288 | } |
af74b550 | 289 | goto out; |
a517e88b PB |
290 | } |
291 | ||
a6ac2313 | 292 | fd = open(device, O_RDWR); |
fc19f8a0 | 293 | if (fd < 0) { |
a6ac2313 | 294 | /* Linux-only, we can use %m in printf. */ |
b9884681 | 295 | error_report("Failed to open %s: %m", device); |
af74b550 | 296 | goto out; |
a6ac2313 PB |
297 | } |
298 | ||
af74b550 | 299 | if (nbd_init(fd, sioc, &info, &local_error) < 0) { |
be41c100 | 300 | error_report_err(local_error); |
af74b550 | 301 | goto out; |
a517e88b PB |
302 | } |
303 | ||
304 | /* update partition table */ | |
a6ac2313 | 305 | pthread_create(&show_parts_thread, NULL, show_parts, device); |
a517e88b | 306 | |
c1f8fdc3 PB |
307 | if (verbose) { |
308 | fprintf(stderr, "NBD device %s is now connected to %s\n", | |
309 | device, srcpath); | |
310 | } else { | |
311 | /* Close stderr so that the qemu-nbd process exits. */ | |
312 | dup2(STDOUT_FILENO, STDERR_FILENO); | |
313 | } | |
a517e88b | 314 | |
af74b550 AC |
315 | if (nbd_client(fd) < 0) { |
316 | goto out; | |
cd831bd7 | 317 | } |
a517e88b | 318 | |
af74b550 AC |
319 | ret = EXIT_SUCCESS; |
320 | ||
321 | out: | |
322 | if (fd >= 0) { | |
323 | close(fd); | |
324 | } | |
d0d6ff58 | 325 | object_unref(OBJECT(sioc)); |
6dc1667d | 326 | g_free(info.name); |
a517e88b | 327 | kill(getpid(), SIGTERM); |
af74b550 | 328 | return (void *) (intptr_t) ret; |
cd831bd7 | 329 | } |
3c1fa35d | 330 | #endif /* HAVE_NBD_DEVICE */ |
cd831bd7 | 331 | |
e4afbf4f | 332 | static int nbd_can_accept(void) |
a61c6782 | 333 | { |
3dcf56e6 | 334 | return state == RUNNING && (shared == 0 || nb_fds < shared); |
a61c6782 PB |
335 | } |
336 | ||
d0d6ff58 | 337 | static void nbd_update_server_watch(void); |
e4afbf4f | 338 | |
0c9390d9 | 339 | static void nbd_client_closed(NBDClient *client, bool negotiated) |
a61c6782 | 340 | { |
1743b515 | 341 | nb_fds--; |
0c9390d9 | 342 | if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) { |
7860a380 PB |
343 | state = TERMINATE; |
344 | } | |
d0d6ff58 | 345 | nbd_update_server_watch(); |
7860a380 | 346 | nbd_client_put(client); |
a61c6782 PB |
347 | } |
348 | ||
e4849c1d DB |
349 | static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc, |
350 | gpointer opaque) | |
a61c6782 | 351 | { |
7860a380 | 352 | if (state >= TERMINATE) { |
e4849c1d | 353 | return; |
7860a380 PB |
354 | } |
355 | ||
ee7d7aab | 356 | nb_fds++; |
d0d6ff58 | 357 | nbd_update_server_watch(); |
b25e12da | 358 | nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed); |
a61c6782 PB |
359 | } |
360 | ||
d0d6ff58 | 361 | static void nbd_update_server_watch(void) |
e4afbf4f FZ |
362 | { |
363 | if (nbd_can_accept()) { | |
e4849c1d | 364 | qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL); |
e4afbf4f | 365 | } else { |
e4849c1d | 366 | qio_net_listener_set_client_func(server, NULL, NULL, NULL); |
e4afbf4f FZ |
367 | } |
368 | } | |
369 | ||
48bec07e | 370 | |
bd269ebc | 371 | static SocketAddress *nbd_build_socket_address(const char *sockpath, |
48bec07e DB |
372 | const char *bindto, |
373 | const char *port) | |
374 | { | |
bd269ebc | 375 | SocketAddress *saddr; |
48bec07e | 376 | |
bd269ebc | 377 | saddr = g_new0(SocketAddress, 1); |
48bec07e | 378 | if (sockpath) { |
bd269ebc MA |
379 | saddr->type = SOCKET_ADDRESS_TYPE_UNIX; |
380 | saddr->u.q_unix.path = g_strdup(sockpath); | |
48bec07e | 381 | } else { |
0399293e | 382 | InetSocketAddress *inet; |
bd269ebc MA |
383 | saddr->type = SOCKET_ADDRESS_TYPE_INET; |
384 | inet = &saddr->u.inet; | |
0399293e | 385 | inet->host = g_strdup(bindto); |
48bec07e | 386 | if (port) { |
0399293e | 387 | inet->port = g_strdup(port); |
48bec07e | 388 | } else { |
0399293e | 389 | inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT); |
48bec07e DB |
390 | } |
391 | } | |
392 | ||
393 | return saddr; | |
394 | } | |
395 | ||
396 | ||
77c9aaef DB |
397 | static QemuOptsList file_opts = { |
398 | .name = "file", | |
399 | .implied_opt_name = "file", | |
400 | .head = QTAILQ_HEAD_INITIALIZER(file_opts.head), | |
401 | .desc = { | |
402 | /* no elements => accept any params */ | |
403 | { /* end of list */ } | |
404 | }, | |
405 | }; | |
406 | ||
68b96f15 EB |
407 | static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list, |
408 | Error **errp) | |
145614a1 DB |
409 | { |
410 | Object *obj; | |
411 | QCryptoTLSCreds *creds; | |
412 | ||
413 | obj = object_resolve_path_component( | |
414 | object_get_objects_root(), id); | |
415 | if (!obj) { | |
416 | error_setg(errp, "No TLS credentials with id '%s'", | |
417 | id); | |
418 | return NULL; | |
419 | } | |
420 | creds = (QCryptoTLSCreds *) | |
421 | object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); | |
422 | if (!creds) { | |
423 | error_setg(errp, "Object with id '%s' is not TLS credentials", | |
424 | id); | |
425 | return NULL; | |
426 | } | |
427 | ||
0279cd95 PMD |
428 | if (!qcrypto_tls_creds_check_endpoint(creds, |
429 | list | |
430 | ? QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT | |
431 | : QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, | |
432 | errp)) { | |
433 | return NULL; | |
145614a1 DB |
434 | } |
435 | object_ref(obj); | |
436 | return creds; | |
437 | } | |
438 | ||
a721f53b RJ |
439 | static void setup_address_and_port(const char **address, const char **port) |
440 | { | |
441 | if (*address == NULL) { | |
442 | *address = "0.0.0.0"; | |
443 | } | |
444 | ||
445 | if (*port == NULL) { | |
446 | *port = stringify(NBD_DEFAULT_PORT); | |
447 | } | |
448 | } | |
449 | ||
a721f53b RJ |
450 | /* |
451 | * Check socket parameters compatibility when socket activation is used. | |
452 | */ | |
453 | static const char *socket_activation_validate_opts(const char *device, | |
454 | const char *sockpath, | |
455 | const char *address, | |
68b96f15 EB |
456 | const char *port, |
457 | bool list) | |
a721f53b RJ |
458 | { |
459 | if (device != NULL) { | |
460 | return "NBD device can't be set when using socket activation"; | |
461 | } | |
462 | ||
463 | if (sockpath != NULL) { | |
464 | return "Unix socket can't be set when using socket activation"; | |
465 | } | |
466 | ||
467 | if (address != NULL) { | |
468 | return "The interface can't be set when using socket activation"; | |
469 | } | |
470 | ||
471 | if (port != NULL) { | |
472 | return "TCP port number can't be set when using socket activation"; | |
473 | } | |
474 | ||
68b96f15 EB |
475 | if (list) { |
476 | return "List mode is incompatible with socket activation"; | |
477 | } | |
478 | ||
a721f53b RJ |
479 | return NULL; |
480 | } | |
145614a1 | 481 | |
b3b5299d KW |
482 | static void qemu_nbd_shutdown(void) |
483 | { | |
484 | job_cancel_sync_all(); | |
1895b977 | 485 | blk_exp_close_all(); |
b3b5299d KW |
486 | bdrv_close_all(); |
487 | } | |
488 | ||
7a5ca864 FB |
489 | int main(int argc, char **argv) |
490 | { | |
26f54e9a | 491 | BlockBackend *blk; |
7a5ca864 | 492 | BlockDriverState *bs; |
9d26dfcb | 493 | uint64_t dev_offset = 0; |
dbb38caa | 494 | bool readonly = false; |
cd831bd7 | 495 | bool disconnect = false; |
a721f53b | 496 | const char *bindto = NULL; |
48bec07e DB |
497 | const char *port = NULL; |
498 | char *sockpath = NULL; | |
a6ac2313 | 499 | char *device = NULL; |
8c116b0e WX |
500 | QemuOpts *sn_opts = NULL; |
501 | const char *sn_id_or_name = NULL; | |
dbc7b014 | 502 | const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L"; |
7a5ca864 | 503 | struct option lopt[] = { |
aa6e546c DB |
504 | { "help", no_argument, NULL, 'h' }, |
505 | { "version", no_argument, NULL, 'V' }, | |
506 | { "bind", required_argument, NULL, 'b' }, | |
507 | { "port", required_argument, NULL, 'p' }, | |
508 | { "socket", required_argument, NULL, 'k' }, | |
509 | { "offset", required_argument, NULL, 'o' }, | |
510 | { "read-only", no_argument, NULL, 'r' }, | |
dbc7b014 | 511 | { "allocation-depth", no_argument, NULL, 'A' }, |
636192c4 | 512 | { "bitmap", required_argument, NULL, 'B' }, |
aa6e546c DB |
513 | { "connect", required_argument, NULL, 'c' }, |
514 | { "disconnect", no_argument, NULL, 'd' }, | |
68b96f15 | 515 | { "list", no_argument, NULL, 'L' }, |
aa6e546c DB |
516 | { "snapshot", no_argument, NULL, 's' }, |
517 | { "load-snapshot", required_argument, NULL, 'l' }, | |
518 | { "nocache", no_argument, NULL, 'n' }, | |
519 | { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE }, | |
520 | { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO }, | |
521 | { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD }, | |
522 | { "detect-zeroes", required_argument, NULL, | |
523 | QEMU_NBD_OPT_DETECT_ZEROES }, | |
524 | { "shared", required_argument, NULL, 'e' }, | |
525 | { "format", required_argument, NULL, 'f' }, | |
526 | { "persistent", no_argument, NULL, 't' }, | |
527 | { "verbose", no_argument, NULL, 'v' }, | |
528 | { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT }, | |
529 | { "export-name", required_argument, NULL, 'x' }, | |
b1a75b33 | 530 | { "description", required_argument, NULL, 'D' }, |
aa6e546c | 531 | { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS }, |
b25e12da | 532 | { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ }, |
aa6e546c | 533 | { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS }, |
39ca463e | 534 | { "trace", required_argument, NULL, 'T' }, |
ffb31e1d | 535 | { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK }, |
637bc5a5 | 536 | { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE }, |
660f11be | 537 | { NULL, 0, NULL, 0 } |
7a5ca864 FB |
538 | }; |
539 | int ch; | |
540 | int opt_ind = 0; | |
f5edb014 | 541 | int flags = BDRV_O_RDWR; |
4fbec260 | 542 | int ret = 0; |
39a5235c | 543 | bool seen_cache = false; |
ded9d2d5 | 544 | bool seen_discard = false; |
39a5235c | 545 | bool seen_aio = false; |
a517e88b | 546 | pthread_t client_thread; |
e6b63677 | 547 | const char *fmt = NULL; |
34b5d2c6 | 548 | Error *local_err = NULL; |
b3838a40 | 549 | BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; |
4fbec260 | 550 | QDict *options = NULL; |
68b96f15 | 551 | const char *export_name = NULL; /* defaults to "" later for server mode */ |
b1a75b33 | 552 | const char *export_description = NULL; |
cbad81ce | 553 | strList *bitmaps = NULL; |
dbc7b014 | 554 | bool alloc_depth = false; |
145614a1 | 555 | const char *tlscredsid = NULL; |
77c9aaef | 556 | bool imageOpts = false; |
09615257 | 557 | bool writethrough = false; /* Client will flush as needed. */ |
ffb31e1d | 558 | bool fork_process = false; |
68b96f15 | 559 | bool list = false; |
ffb31e1d | 560 | int old_stderr = -1; |
a721f53b | 561 | unsigned socket_activation; |
637bc5a5 | 562 | const char *pid_file_name = NULL; |
00917172 | 563 | BlockExportOptions *export_opts; |
7a5ca864 | 564 | |
029a88c9 | 565 | #ifdef CONFIG_POSIX |
cbc20bfb SH |
566 | os_setup_early_signal_handling(); |
567 | os_setup_signal_handling(); | |
041e32b8 HR |
568 | #endif |
569 | ||
98c5d2e7 | 570 | socket_init(); |
f5852efa | 571 | error_init(argv[0]); |
fe4db84d | 572 | module_call_init(MODULE_INIT_TRACE); |
e8f2d272 | 573 | qcrypto_init(&error_fatal); |
c2297088 | 574 | |
0ab3b337 | 575 | module_call_init(MODULE_INIT_QOM); |
39ca463e | 576 | qemu_add_opts(&qemu_trace_opts); |
10f5bff6 | 577 | qemu_init_exec_dir(argv[0]); |
bb345110 | 578 | |
7a5ca864 FB |
579 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
580 | switch (ch) { | |
581 | case 's': | |
2f726488 TS |
582 | flags |= BDRV_O_SNAPSHOT; |
583 | break; | |
584 | case 'n': | |
39a5235c PB |
585 | optarg = (char *) "none"; |
586 | /* fallthrough */ | |
587 | case QEMU_NBD_OPT_CACHE: | |
588 | if (seen_cache) { | |
85b01e09 MA |
589 | error_report("-n and --cache can only be specified once"); |
590 | exit(EXIT_FAILURE); | |
39a5235c PB |
591 | } |
592 | seen_cache = true; | |
6effd5bf | 593 | if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) { |
85b01e09 MA |
594 | error_report("Invalid cache mode `%s'", optarg); |
595 | exit(EXIT_FAILURE); | |
39a5235c | 596 | } |
7a5ca864 | 597 | break; |
39a5235c PB |
598 | case QEMU_NBD_OPT_AIO: |
599 | if (seen_aio) { | |
85b01e09 MA |
600 | error_report("--aio can only be specified once"); |
601 | exit(EXIT_FAILURE); | |
39a5235c PB |
602 | } |
603 | seen_aio = true; | |
7680274d AM |
604 | if (bdrv_parse_aio(optarg, &flags) < 0) { |
605 | error_report("Invalid aio mode '%s'", optarg); | |
606 | exit(EXIT_FAILURE); | |
39a5235c PB |
607 | } |
608 | break; | |
ded9d2d5 PB |
609 | case QEMU_NBD_OPT_DISCARD: |
610 | if (seen_discard) { | |
85b01e09 MA |
611 | error_report("--discard can only be specified once"); |
612 | exit(EXIT_FAILURE); | |
ded9d2d5 PB |
613 | } |
614 | seen_discard = true; | |
615 | if (bdrv_parse_discard_flags(optarg, &flags) == -1) { | |
85b01e09 MA |
616 | error_report("Invalid discard mode `%s'", optarg); |
617 | exit(EXIT_FAILURE); | |
ded9d2d5 PB |
618 | } |
619 | break; | |
b3838a40 PL |
620 | case QEMU_NBD_OPT_DETECT_ZEROES: |
621 | detect_zeroes = | |
f7abe0ec | 622 | qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, |
b3838a40 | 623 | optarg, |
b3838a40 PL |
624 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, |
625 | &local_err); | |
626 | if (local_err) { | |
c29b77f9 MA |
627 | error_reportf_err(local_err, |
628 | "Failed to parse detect_zeroes mode: "); | |
85b01e09 | 629 | exit(EXIT_FAILURE); |
b3838a40 PL |
630 | } |
631 | if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && | |
632 | !(flags & BDRV_O_UNMAP)) { | |
85b01e09 MA |
633 | error_report("setting detect-zeroes to unmap is not allowed " |
634 | "without setting discard operation to unmap"); | |
635 | exit(EXIT_FAILURE); | |
b3838a40 PL |
636 | } |
637 | break; | |
7a5ca864 FB |
638 | case 'b': |
639 | bindto = optarg; | |
640 | break; | |
641 | case 'p': | |
48bec07e | 642 | port = optarg; |
7a5ca864 FB |
643 | break; |
644 | case 'o': | |
43b51011 EB |
645 | if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) { |
646 | error_report("Invalid offset '%s'", optarg); | |
85b01e09 | 647 | exit(EXIT_FAILURE); |
7a5ca864 | 648 | } |
7a5ca864 | 649 | break; |
8c116b0e WX |
650 | case 'l': |
651 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
70b94331 MA |
652 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
653 | optarg, false); | |
8c116b0e | 654 | if (!sn_opts) { |
85b01e09 MA |
655 | error_report("Failed in parsing snapshot param `%s'", |
656 | optarg); | |
657 | exit(EXIT_FAILURE); | |
8c116b0e WX |
658 | } |
659 | } else { | |
660 | sn_id_or_name = optarg; | |
661 | } | |
662 | /* fall through */ | |
7a5ca864 | 663 | case 'r': |
dbb38caa | 664 | readonly = true; |
07108b29 | 665 | flags &= ~BDRV_O_RDWR; |
7a5ca864 | 666 | break; |
dbc7b014 EB |
667 | case 'A': |
668 | alloc_depth = true; | |
669 | break; | |
636192c4 | 670 | case 'B': |
cbad81ce | 671 | QAPI_LIST_PREPEND(bitmaps, g_strdup(optarg)); |
636192c4 | 672 | break; |
cd831bd7 | 673 | case 'k': |
b32f6c28 | 674 | sockpath = optarg; |
713cc671 | 675 | if (sockpath[0] != '/') { |
9af9e0fe | 676 | error_report("socket path must be absolute"); |
85b01e09 | 677 | exit(EXIT_FAILURE); |
713cc671 | 678 | } |
cd831bd7 TS |
679 | break; |
680 | case 'd': | |
681 | disconnect = true; | |
682 | break; | |
683 | case 'c': | |
684 | device = optarg; | |
685 | break; | |
3b05a8e9 | 686 | case 'e': |
43b51011 | 687 | if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 || |
3dcf56e6 | 688 | shared < 0) { |
85b01e09 MA |
689 | error_report("Invalid shared device number '%s'", optarg); |
690 | exit(EXIT_FAILURE); | |
3b05a8e9 | 691 | } |
3b05a8e9 | 692 | break; |
e6b63677 DB |
693 | case 'f': |
694 | fmt = optarg; | |
695 | break; | |
713cc671 PL |
696 | case 't': |
697 | persistent = 1; | |
698 | break; | |
3d4b2f9c DB |
699 | case 'x': |
700 | export_name = optarg; | |
93676c88 EB |
701 | if (strlen(export_name) > NBD_MAX_STRING_SIZE) { |
702 | error_report("export name '%s' too long", export_name); | |
703 | exit(EXIT_FAILURE); | |
704 | } | |
3d4b2f9c | 705 | break; |
b1a75b33 EB |
706 | case 'D': |
707 | export_description = optarg; | |
93676c88 EB |
708 | if (strlen(export_description) > NBD_MAX_STRING_SIZE) { |
709 | error_report("export description '%s' too long", | |
710 | export_description); | |
711 | exit(EXIT_FAILURE); | |
712 | } | |
b1a75b33 | 713 | break; |
7a5ca864 FB |
714 | case 'v': |
715 | verbose = 1; | |
716 | break; | |
717 | case 'V': | |
718 | version(argv[0]); | |
719 | exit(0); | |
720 | break; | |
721 | case 'h': | |
722 | usage(argv[0]); | |
723 | exit(0); | |
724 | break; | |
725 | case '?': | |
85b01e09 MA |
726 | error_report("Try `%s --help' for more information.", argv[0]); |
727 | exit(EXIT_FAILURE); | |
fa40e43c KW |
728 | case QEMU_NBD_OPT_OBJECT: |
729 | user_creatable_process_cmdline(optarg); | |
730 | break; | |
145614a1 DB |
731 | case QEMU_NBD_OPT_TLSCREDS: |
732 | tlscredsid = optarg; | |
733 | break; | |
77c9aaef DB |
734 | case QEMU_NBD_OPT_IMAGE_OPTS: |
735 | imageOpts = true; | |
736 | break; | |
39ca463e | 737 | case 'T': |
92eecfff | 738 | trace_opt_parse(optarg); |
39ca463e | 739 | break; |
b25e12da DB |
740 | case QEMU_NBD_OPT_TLSAUTHZ: |
741 | tlsauthz = optarg; | |
742 | break; | |
ffb31e1d HR |
743 | case QEMU_NBD_OPT_FORK: |
744 | fork_process = true; | |
745 | break; | |
68b96f15 EB |
746 | case 'L': |
747 | list = true; | |
748 | break; | |
637bc5a5 HR |
749 | case QEMU_NBD_OPT_PID_FILE: |
750 | pid_file_name = optarg; | |
751 | break; | |
7a5ca864 FB |
752 | } |
753 | } | |
754 | ||
68b96f15 EB |
755 | if (list) { |
756 | if (argc != optind) { | |
757 | error_report("List mode is incompatible with a file name"); | |
758 | exit(EXIT_FAILURE); | |
759 | } | |
0bc16997 | 760 | if (export_name || export_description || dev_offset || |
cbad81ce | 761 | device || disconnect || fmt || sn_id_or_name || bitmaps || |
dbc7b014 | 762 | alloc_depth || seen_aio || seen_discard || seen_cache) { |
68b96f15 EB |
763 | error_report("List mode is incompatible with per-device settings"); |
764 | exit(EXIT_FAILURE); | |
765 | } | |
766 | if (fork_process) { | |
767 | error_report("List mode is incompatible with forking"); | |
768 | exit(EXIT_FAILURE); | |
769 | } | |
770 | } else if ((argc - optind) != 1) { | |
433672b0 MA |
771 | error_report("Invalid number of arguments"); |
772 | error_printf("Try `%s --help' for more information.\n", argv[0]); | |
85b01e09 | 773 | exit(EXIT_FAILURE); |
68b96f15 EB |
774 | } else if (!export_name) { |
775 | export_name = ""; | |
7a5ca864 FB |
776 | } |
777 | ||
39ca463e DL |
778 | if (!trace_init_backends()) { |
779 | exit(1); | |
780 | } | |
92eecfff | 781 | trace_init_file(); |
39ca463e DL |
782 | qemu_set_log(LOG_TRACE); |
783 | ||
a721f53b RJ |
784 | socket_activation = check_socket_activation(); |
785 | if (socket_activation == 0) { | |
786 | setup_address_and_port(&bindto, &port); | |
787 | } else { | |
788 | /* Using socket activation - check user didn't use -p etc. */ | |
789 | const char *err_msg = socket_activation_validate_opts(device, sockpath, | |
68b96f15 EB |
790 | bindto, port, |
791 | list); | |
a721f53b RJ |
792 | if (err_msg != NULL) { |
793 | error_report("%s", err_msg); | |
794 | exit(EXIT_FAILURE); | |
795 | } | |
53fabd4b PB |
796 | |
797 | /* qemu-nbd can only listen on a single socket. */ | |
798 | if (socket_activation > 1) { | |
799 | error_report("qemu-nbd does not support socket activation with %s > 1", | |
800 | "LISTEN_FDS"); | |
801 | exit(EXIT_FAILURE); | |
802 | } | |
a721f53b RJ |
803 | } |
804 | ||
145614a1 DB |
805 | if (tlscredsid) { |
806 | if (sockpath) { | |
807 | error_report("TLS is only supported with IPv4/IPv6"); | |
808 | exit(EXIT_FAILURE); | |
809 | } | |
810 | if (device) { | |
811 | error_report("TLS is not supported with a host device"); | |
812 | exit(EXIT_FAILURE); | |
813 | } | |
b25e12da DB |
814 | if (tlsauthz && list) { |
815 | error_report("TLS authorization is incompatible with export list"); | |
816 | exit(EXIT_FAILURE); | |
817 | } | |
68b96f15 | 818 | tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err); |
145614a1 | 819 | if (local_err) { |
5217f188 | 820 | error_reportf_err(local_err, "Failed to get TLS creds: "); |
145614a1 DB |
821 | exit(EXIT_FAILURE); |
822 | } | |
b25e12da DB |
823 | } else { |
824 | if (tlsauthz) { | |
825 | error_report("--tls-authz is not permitted without --tls-creds"); | |
826 | exit(EXIT_FAILURE); | |
827 | } | |
145614a1 DB |
828 | } |
829 | ||
68b96f15 EB |
830 | if (list) { |
831 | saddr = nbd_build_socket_address(sockpath, bindto, port); | |
832 | return qemu_nbd_client_list(saddr, tlscreds, bindto); | |
833 | } | |
834 | ||
3c1fa35d EB |
835 | #if !HAVE_NBD_DEVICE |
836 | if (disconnect || device) { | |
837 | error_report("Kernel /dev/nbdN support not available"); | |
838 | exit(EXIT_FAILURE); | |
839 | } | |
840 | #else /* HAVE_NBD_DEVICE */ | |
cd831bd7 | 841 | if (disconnect) { |
d0d6ff58 DB |
842 | int nbdfd = open(argv[optind], O_RDWR); |
843 | if (nbdfd < 0) { | |
85b01e09 MA |
844 | error_report("Cannot open %s: %s", argv[optind], |
845 | strerror(errno)); | |
846 | exit(EXIT_FAILURE); | |
fc19f8a0 | 847 | } |
d0d6ff58 | 848 | nbd_disconnect(nbdfd); |
cd831bd7 | 849 | |
d0d6ff58 | 850 | close(nbdfd); |
cd831bd7 TS |
851 | |
852 | printf("%s disconnected\n", argv[optind]); | |
853 | ||
713cc671 | 854 | return 0; |
cd831bd7 | 855 | } |
3c1fa35d | 856 | #endif |
cd831bd7 | 857 | |
ffb31e1d | 858 | if ((device && !verbose) || fork_process) { |
eb705985 | 859 | #ifndef WIN32 |
c1f8fdc3 PB |
860 | int stderr_fd[2]; |
861 | pid_t pid; | |
862 | int ret; | |
863 | ||
fc19f8a0 | 864 | if (qemu_pipe(stderr_fd) < 0) { |
85b01e09 MA |
865 | error_report("Error setting up communication pipe: %s", |
866 | strerror(errno)); | |
867 | exit(EXIT_FAILURE); | |
c1f8fdc3 PB |
868 | } |
869 | ||
870 | /* Now daemonize, but keep a communication channel open to | |
871 | * print errors and exit with the proper status code. | |
872 | */ | |
873 | pid = fork(); | |
70d4739e | 874 | if (pid < 0) { |
85b01e09 MA |
875 | error_report("Failed to fork: %s", strerror(errno)); |
876 | exit(EXIT_FAILURE); | |
70d4739e | 877 | } else if (pid == 0) { |
c1f8fdc3 | 878 | close(stderr_fd[0]); |
e6df58a5 | 879 | |
0eaf453e RP |
880 | /* Remember parent's stderr if we will be restoring it. */ |
881 | if (fork_process) { | |
882 | old_stderr = dup(STDERR_FILENO); | |
883 | } | |
884 | ||
9faf31b6 | 885 | ret = qemu_daemon(1, 0); |
c1f8fdc3 PB |
886 | |
887 | /* Temporarily redirect stderr to the parent's pipe... */ | |
888 | dup2(stderr_fd[1], STDERR_FILENO); | |
fc19f8a0 | 889 | if (ret < 0) { |
85b01e09 MA |
890 | error_report("Failed to daemonize: %s", strerror(errno)); |
891 | exit(EXIT_FAILURE); | |
c1f8fdc3 PB |
892 | } |
893 | ||
894 | /* ... close the descriptor we inherited and go on. */ | |
895 | close(stderr_fd[1]); | |
896 | } else { | |
897 | bool errors = false; | |
898 | char *buf; | |
899 | ||
900 | /* In the parent. Print error messages from the child until | |
901 | * it closes the pipe. | |
902 | */ | |
903 | close(stderr_fd[1]); | |
904 | buf = g_malloc(1024); | |
905 | while ((ret = read(stderr_fd[0], buf, 1024)) > 0) { | |
906 | errors = true; | |
907 | ret = qemu_write_full(STDERR_FILENO, buf, ret); | |
fc19f8a0 | 908 | if (ret < 0) { |
c1f8fdc3 PB |
909 | exit(EXIT_FAILURE); |
910 | } | |
911 | } | |
fc19f8a0 | 912 | if (ret < 0) { |
85b01e09 MA |
913 | error_report("Cannot read from daemon: %s", |
914 | strerror(errno)); | |
915 | exit(EXIT_FAILURE); | |
c1f8fdc3 PB |
916 | } |
917 | ||
918 | /* Usually the daemon should not print any message. | |
919 | * Exit with zero status in that case. | |
920 | */ | |
921 | exit(errors); | |
922 | } | |
eb705985 DB |
923 | #else /* WIN32 */ |
924 | error_report("Unable to fork into background on Windows hosts"); | |
925 | exit(EXIT_FAILURE); | |
926 | #endif /* WIN32 */ | |
c1f8fdc3 PB |
927 | } |
928 | ||
a6ac2313 PB |
929 | if (device != NULL && sockpath == NULL) { |
930 | sockpath = g_malloc(128); | |
931 | snprintf(sockpath, 128, SOCKET_PATH, basename(device)); | |
cd831bd7 TS |
932 | } |
933 | ||
e4849c1d | 934 | server = qio_net_listener_new(); |
a721f53b | 935 | if (socket_activation == 0) { |
582d4210 EB |
936 | int backlog; |
937 | ||
3dcf56e6 | 938 | if (persistent || shared == 0) { |
582d4210 EB |
939 | backlog = SOMAXCONN; |
940 | } else { | |
941 | backlog = MIN(shared, SOMAXCONN); | |
942 | } | |
a721f53b | 943 | saddr = nbd_build_socket_address(sockpath, bindto, port); |
582d4210 EB |
944 | if (qio_net_listener_open_sync(server, saddr, backlog, |
945 | &local_err) < 0) { | |
e4849c1d | 946 | object_unref(OBJECT(server)); |
a721f53b | 947 | error_report_err(local_err); |
e4849c1d | 948 | exit(EXIT_FAILURE); |
a721f53b RJ |
949 | } |
950 | } else { | |
e4849c1d | 951 | size_t i; |
a721f53b | 952 | /* See comment in check_socket_activation above. */ |
e4849c1d DB |
953 | for (i = 0; i < socket_activation; i++) { |
954 | QIOChannelSocket *sioc; | |
955 | sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i, | |
956 | &local_err); | |
957 | if (sioc == NULL) { | |
958 | object_unref(OBJECT(server)); | |
5217f188 MA |
959 | error_reportf_err(local_err, |
960 | "Failed to use socket activation: "); | |
e4849c1d DB |
961 | exit(EXIT_FAILURE); |
962 | } | |
963 | qio_net_listener_add(server, sioc); | |
964 | object_unref(OBJECT(sioc)); | |
a721f53b RJ |
965 | } |
966 | } | |
48bec07e | 967 | |
f9734d5d | 968 | qemu_init_main_loop(&error_fatal); |
802ddc37 | 969 | bdrv_init(); |
b3b5299d | 970 | atexit(qemu_nbd_shutdown); |
802ddc37 | 971 | |
77c9aaef DB |
972 | srcpath = argv[optind]; |
973 | if (imageOpts) { | |
974 | QemuOpts *opts; | |
975 | if (fmt) { | |
976 | error_report("--image-opts and -f are mutually exclusive"); | |
977 | exit(EXIT_FAILURE); | |
978 | } | |
979 | opts = qemu_opts_parse_noisily(&file_opts, srcpath, true); | |
980 | if (!opts) { | |
981 | qemu_opts_reset(&file_opts); | |
982 | exit(EXIT_FAILURE); | |
983 | } | |
984 | options = qemu_opts_to_qdict(opts, NULL); | |
985 | qemu_opts_reset(&file_opts); | |
efaa7c4e | 986 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
77c9aaef DB |
987 | } else { |
988 | if (fmt) { | |
989 | options = qdict_new(); | |
46f5ac20 | 990 | qdict_put_str(options, "driver", fmt); |
77c9aaef | 991 | } |
efaa7c4e | 992 | blk = blk_new_open(srcpath, NULL, options, flags, &local_err); |
e6b63677 DB |
993 | } |
994 | ||
4fbec260 | 995 | if (!blk) { |
c29b77f9 MA |
996 | error_reportf_err(local_err, "Failed to blk_new_open '%s': ", |
997 | argv[optind]); | |
85b01e09 | 998 | exit(EXIT_FAILURE); |
802ddc37 | 999 | } |
4fbec260 | 1000 | bs = blk_bs(blk); |
802ddc37 | 1001 | |
b57e4de0 KW |
1002 | if (dev_offset) { |
1003 | QDict *raw_opts = qdict_new(); | |
1004 | qdict_put_str(raw_opts, "driver", "raw"); | |
1005 | qdict_put_str(raw_opts, "file", bs->node_name); | |
1006 | qdict_put_int(raw_opts, "offset", dev_offset); | |
1007 | bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal); | |
1008 | blk_remove_bs(blk); | |
1009 | blk_insert_bs(blk, bs, &error_fatal); | |
1010 | bdrv_unref(bs); | |
1011 | } | |
1012 | ||
6effd5bf KW |
1013 | blk_set_enable_write_cache(blk, !writethrough); |
1014 | ||
8c116b0e WX |
1015 | if (sn_opts) { |
1016 | ret = bdrv_snapshot_load_tmp(bs, | |
1017 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), | |
1018 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
1019 | &local_err); | |
1020 | } else if (sn_id_or_name) { | |
1021 | ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name, | |
1022 | &local_err); | |
1023 | } | |
1024 | if (ret < 0) { | |
c29b77f9 | 1025 | error_reportf_err(local_err, "Failed to load snapshot: "); |
85b01e09 | 1026 | exit(EXIT_FAILURE); |
8c116b0e WX |
1027 | } |
1028 | ||
b3838a40 | 1029 | bs->detect_zeroes = detect_zeroes; |
e424b655 | 1030 | |
00917172 KW |
1031 | nbd_server_is_qemu_nbd(true); |
1032 | ||
1033 | export_opts = g_new(BlockExportOptions, 1); | |
1034 | *export_opts = (BlockExportOptions) { | |
1035 | .type = BLOCK_EXPORT_TYPE_NBD, | |
d53be9ce | 1036 | .id = g_strdup("qemu-nbd-export"), |
b6076afc | 1037 | .node_name = g_strdup(bdrv_get_node_name(bs)), |
00917172 KW |
1038 | .has_writethrough = true, |
1039 | .writethrough = writethrough, | |
30dbc81d KW |
1040 | .has_writable = true, |
1041 | .writable = !readonly, | |
00917172 | 1042 | .u.nbd = { |
cbad81ce EB |
1043 | .has_name = true, |
1044 | .name = g_strdup(export_name), | |
1045 | .has_description = !!export_description, | |
1046 | .description = g_strdup(export_description), | |
1047 | .has_bitmaps = !!bitmaps, | |
1048 | .bitmaps = bitmaps, | |
dbc7b014 EB |
1049 | .has_allocation_depth = alloc_depth, |
1050 | .allocation_depth = alloc_depth, | |
00917172 KW |
1051 | }, |
1052 | }; | |
1053 | blk_exp_add(export_opts, &error_fatal); | |
1054 | qapi_free_BlockExportOptions(export_opts); | |
3b05a8e9 | 1055 | |
f1ef5555 | 1056 | if (device) { |
3c1fa35d | 1057 | #if HAVE_NBD_DEVICE |
f1ef5555 PB |
1058 | int ret; |
1059 | ||
a6ac2313 | 1060 | ret = pthread_create(&client_thread, NULL, nbd_client_thread, device); |
f1ef5555 | 1061 | if (ret != 0) { |
85b01e09 MA |
1062 | error_report("Failed to create client thread: %s", strerror(ret)); |
1063 | exit(EXIT_FAILURE); | |
f1ef5555 | 1064 | } |
3c1fa35d | 1065 | #endif |
f1ef5555 PB |
1066 | } else { |
1067 | /* Shut up GCC warnings. */ | |
1068 | memset(&client_thread, 0, sizeof(client_thread)); | |
1069 | } | |
1070 | ||
d0d6ff58 | 1071 | nbd_update_server_watch(); |
7a5ca864 | 1072 | |
637bc5a5 HR |
1073 | if (pid_file_name) { |
1074 | qemu_write_pidfile(pid_file_name, &error_fatal); | |
1075 | } | |
1076 | ||
9faf31b6 MT |
1077 | /* now when the initialization is (almost) complete, chdir("/") |
1078 | * to free any busy filesystems */ | |
1079 | if (chdir("/") < 0) { | |
85b01e09 MA |
1080 | error_report("Could not chdir to root directory: %s", |
1081 | strerror(errno)); | |
1082 | exit(EXIT_FAILURE); | |
9faf31b6 MT |
1083 | } |
1084 | ||
ffb31e1d HR |
1085 | if (fork_process) { |
1086 | dup2(old_stderr, STDERR_FILENO); | |
1087 | close(old_stderr); | |
1088 | } | |
1089 | ||
7860a380 | 1090 | state = RUNNING; |
3b05a8e9 | 1091 | do { |
a61c6782 | 1092 | main_loop_wait(false); |
7860a380 | 1093 | if (state == TERMINATE) { |
bc4ee65b | 1094 | blk_exp_close_all(); |
d794f7f3 | 1095 | state = TERMINATED; |
7860a380 PB |
1096 | } |
1097 | } while (state != TERMINATED); | |
7a5ca864 | 1098 | |
26f54e9a | 1099 | blk_unref(blk); |
b32f6c28 PB |
1100 | if (sockpath) { |
1101 | unlink(sockpath); | |
1102 | } | |
7a5ca864 | 1103 | |
fbf28a43 | 1104 | qemu_opts_del(sn_opts); |
8c116b0e | 1105 | |
a517e88b PB |
1106 | if (device) { |
1107 | void *ret; | |
1108 | pthread_join(client_thread, &ret); | |
1109 | exit(ret != NULL); | |
1110 | } else { | |
1111 | exit(EXIT_SUCCESS); | |
1112 | } | |
7a5ca864 | 1113 | } |