blob: a186d2e1190b39c7dbc39c917fdc5bcd20a1ec1d [file] [log] [blame]
thscd831bd2008-07-03 10:23:51 +00001/*
bellard7a5ca862008-05-27 21:13:40 +00002 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
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
Blue Swirl8167ee82009-07-16 20:47:01 +000016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard7a5ca862008-05-27 21:13:40 +000017 */
18
Peter Maydelld38ea872016-01-29 17:50:05 +000019#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050020#include <getopt.h>
21#include <libgen.h>
22#include <pthread.h>
23
Marc-André Lureau49f95222022-04-20 17:25:49 +040024#include "qemu/help-texts.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020026#include "qemu/cutils.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020027#include "sysemu/block-backend.h"
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +010028#include "sysemu/runstate.h" /* for qemu_system_killed() prototype */
Peter Lievenb3838a42014-08-13 19:20:18 +020029#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010030#include "block/nbd.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010031#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020032#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010033#include "qemu/option.h"
Paolo Bonzini537b41f2014-02-17 14:43:51 +010034#include "qemu/error-report.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000035#include "qemu/config-file.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010036#include "qemu/bswap.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030037#include "qemu/log.h"
Paolo Bonzini53fabd42017-03-16 16:29:45 +010038#include "qemu/systemd.h"
Wenchao Xia8c116b02013-12-04 17:10:55 +080039#include "block/snapshot.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010040#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010041#include "qapi/qmp/qstring.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000042#include "qom/object_interfaces.h"
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000043#include "io/channel-socket.h"
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000044#include "io/net-listener.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010045#include "crypto/init.h"
Philippe Mathieu-Daudé0279cd92021-06-28 18:09:10 +020046#include "crypto/tlscreds.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030047#include "trace/control.h"
Eric Blakebe377132017-07-21 08:50:46 -050048#include "qemu-version.h"
bellard7a5ca862008-05-27 21:13:40 +000049
Richard W.M. Jones3d212b42021-11-15 14:29:43 -060050#ifdef CONFIG_SELINUX
51#include <selinux/selinux.h>
52#endif
53
Eric Blake3c1fa352018-12-15 07:53:08 -060054#ifdef __linux__
55#define HAVE_NBD_DEVICE 1
56#else
57#define HAVE_NBD_DEVICE 0
58#endif
59
Peter Lieven713cc672014-08-13 19:20:19 +020060#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
Daniel P. Berrangefa8b7ce2016-02-17 10:10:21 +000061#define QEMU_NBD_OPT_CACHE 256
62#define QEMU_NBD_OPT_AIO 257
63#define QEMU_NBD_OPT_DISCARD 258
64#define QEMU_NBD_OPT_DETECT_ZEROES 259
65#define QEMU_NBD_OPT_OBJECT 260
66#define QEMU_NBD_OPT_TLSCREDS 261
67#define QEMU_NBD_OPT_IMAGE_OPTS 262
Max Reitzffb31e12016-09-28 22:46:42 +020068#define QEMU_NBD_OPT_FORK 263
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000069#define QEMU_NBD_OPT_TLSAUTHZ 264
Max Reitz637bc5a2019-05-08 23:18:16 +020070#define QEMU_NBD_OPT_PID_FILE 265
Richard W.M. Jones3d212b42021-11-15 14:29:43 -060071#define QEMU_NBD_OPT_SELINUX_LABEL 266
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +000072#define QEMU_NBD_OPT_TLSHOSTNAME 267
bellard7a5ca862008-05-27 21:13:40 +000073
Eric Blakebd31c212016-05-06 10:26:42 -060074#define MBR_SIZE 512
75
Paolo Bonzini7860a382012-09-18 13:31:56 +020076static int persistent = 0;
Kevin Wolfd794f7f2020-09-24 17:26:56 +020077static enum { RUNNING, TERMINATE, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020078static int shared = 1;
79static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000080static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000081static QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000082static const char *tlsauthz;
bellard7a5ca862008-05-27 21:13:40 +000083
84static void usage(const char *name)
85{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020086 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000087"Usage: %s [OPTIONS] FILE\n"
Eric Blake68b96f12019-01-17 13:36:56 -060088" or: %s -L [OPTIONS]\n"
89"QEMU Disk Network Block Device Utility\n"
bellard7a5ca862008-05-27 21:13:40 +000090"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020091" -h, --help display this help and exit\n"
92" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000093"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020094"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020095" -p, --port=PORT port to listen on (default `%d')\n"
96" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
97" -k, --socket=PATH path to the unix socket\n"
98" (default '"SOCKET_PATH"')\n"
99" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
100" -t, --persistent don't exit on the last connection\n"
101" -v, --verbose display extra debugging information\n"
Vladimir Sementsov-Ogievskiyf5cd0bb2018-10-03 20:02:27 +0300102" -x, --export-name=NAME expose export by name (default is empty string)\n"
103" -D, --description=TEXT export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200104"\n"
105"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200106" -o, --offset=OFFSET offset into the image\n"
Eric Blakedbc7b012020-10-27 00:05:55 -0500107" -A, --allocation-depth expose the allocation depth\n"
Eric Blake636192c2019-01-11 13:47:20 -0600108" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200109"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000110"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600111" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000112" --object type,id=ID,... define an object such as 'secret' for providing\n"
113" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500114" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000115" --tls-authz=ID use id of an earlier --object to provide\n"
116" authorization\n"
Michael Tokarevd81b7972024-02-07 10:29:40 +0300117" --tls-hostname=HOSTNAME override hostname used to check x509 certificate\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300118" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
119" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200120" --fork fork off the server process and exit the parent\n"
121" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200122" --pid-file=PATH store the server's process ID in the given file\n"
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600123#ifdef CONFIG_SELINUX
124" --selinux-label=LABEL set SELinux process label on listening socket\n"
125#endif
Eric Blake3c1fa352018-12-15 07:53:08 -0600126#if HAVE_NBD_DEVICE
127"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200128"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200129" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
130" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200131#endif
132"\n"
133"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200134" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
135" -r, --read-only export read-only\n"
136" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
137" file with backing_file=FILE, redirect the write to\n"
138" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800139" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200140" load an internal snapshot inside FILE and export it\n"
141" as an read-only device, SNAPSHOT_PARAM format is\n"
142" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
143" '[ID_OR_NAME]'\n"
144" -n, --nocache disable host cache\n"
Nir Soffer09615252021-08-13 23:55:19 +0300145" --cache=MODE set cache mode used to access the disk image, the\n"
146" valid options are: 'none', 'writeback' (default),\n"
147" 'writethrough', 'directsync' and 'unsafe'\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000148" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200149" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300150" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000151" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200152"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500153QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600154 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000155}
156
157static void version(const char *name)
158{
159 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100160"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000161"Written by Anthony Liguori.\n"
162"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500163QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000164"This is free software; see the source for copying conditions. There is NO\n"
165"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000166 , name);
bellard7a5ca862008-05-27 21:13:40 +0000167}
168
Eric Blake029a88c2020-09-30 07:11:01 -0500169#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100170/*
171 * The client thread uses SIGTERM to interrupt the server. A signal
172 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
173 */
174void qemu_system_killed(int signum, pid_t pid)
Paolo Bonzinibb345112011-11-04 15:51:19 +0100175{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100176 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200177 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100178}
Eric Blake029a88c2020-09-30 07:11:01 -0500179#endif /* CONFIG_POSIX */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100180
Eric Blake68b96f12019-01-17 13:36:56 -0600181static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
182 const char *hostname)
183{
184 int ret = EXIT_FAILURE;
185 int rc;
186 Error *err = NULL;
187 QIOChannelSocket *sioc;
188 NBDExportInfo *list;
189 int i, j;
190
191 sioc = qio_channel_socket_new();
192 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
193 error_report_err(err);
Alex Chen992809b2020-11-30 12:36:51 +0000194 goto out;
Eric Blake68b96f12019-01-17 13:36:56 -0600195 }
196 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
197 &err);
198 if (rc < 0) {
199 if (err) {
200 error_report_err(err);
201 }
202 goto out;
203 }
204 printf("exports available: %d\n", rc);
205 for (i = 0; i < rc; i++) {
206 printf(" export: '%s'\n", list[i].name);
207 if (list[i].description && *list[i].description) {
208 printf(" description: %s\n", list[i].description);
209 }
210 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200211 static const char *const flag_names[] = {
212 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
213 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
214 [NBD_FLAG_SEND_FUA_BIT] = "fua",
215 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
216 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
217 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
218 [NBD_FLAG_SEND_DF_BIT] = "df",
219 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
220 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
221 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500222 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Eric Blake2dcbb112023-09-25 14:22:42 -0500223 [NBD_FLAG_BLOCK_STAT_PAYLOAD_BIT] = "block-status-payload",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200224 };
225
Eric Blake68b96f12019-01-17 13:36:56 -0600226 printf(" size: %" PRIu64 "\n", list[i].size);
227 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200228 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
229 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
230 printf(" %s", flag_names[bit]);
231 }
Eric Blake68b96f12019-01-17 13:36:56 -0600232 }
233 printf(" )\n");
234 }
235 if (list[i].min_block) {
236 printf(" min block: %u\n", list[i].min_block);
237 printf(" opt block: %u\n", list[i].opt_block);
238 printf(" max block: %u\n", list[i].max_block);
239 }
Eric Blake56cf9d02023-09-25 14:22:39 -0500240 printf(" transaction size: %s\n",
241 list[i].mode >= NBD_MODE_EXTENDED ?
242 "64-bit" : "32-bit");
Eric Blake68b96f12019-01-17 13:36:56 -0600243 if (list[i].n_contexts) {
244 printf(" available meta contexts: %d\n", list[i].n_contexts);
245 for (j = 0; j < list[i].n_contexts; j++) {
246 printf(" %s\n", list[i].contexts[j]);
247 }
248 }
249 }
250 nbd_free_export_list(list, rc);
251
252 ret = EXIT_SUCCESS;
253 out:
254 object_unref(OBJECT(sioc));
255 return ret;
256}
257
258
Denis V. Lunev3484f672023-09-06 11:32:04 +0200259struct NbdClientOpts {
260 char *device;
Denis V. Lunevb18d72d2023-09-06 11:32:05 +0200261 char *srcpath;
Denis V. Lunevb2cecdf2023-09-06 11:32:06 +0200262 SocketAddress *saddr;
Denis V. Lunevf6f46202023-09-06 11:32:08 +0200263 int old_stderr;
Denis V. Lunev3484f672023-09-06 11:32:04 +0200264 bool fork_process;
265 bool verbose;
266};
267
Denis V. Lunevf6f46202023-09-06 11:32:08 +0200268static void nbd_client_release_pipe(int old_stderr)
Denis V. Lunev2eb7c2a2023-09-06 11:32:07 +0200269{
270 /* Close stderr so that the qemu-nbd process exits. */
Denis V. Lunevf6f46202023-09-06 11:32:08 +0200271 if (dup2(old_stderr, STDERR_FILENO) < 0) {
Denis V. Lunev2eb7c2a2023-09-06 11:32:07 +0200272 error_report("Could not release pipe to parent: %s",
273 strerror(errno));
274 exit(EXIT_FAILURE);
275 }
Denis V. Lunevf6f46202023-09-06 11:32:08 +0200276 if (old_stderr != STDOUT_FILENO && close(old_stderr) < 0) {
277 error_report("Could not release qemu-nbd: %s", strerror(errno));
278 exit(EXIT_FAILURE);
279 }
Denis V. Lunev2eb7c2a2023-09-06 11:32:07 +0200280}
281
Eric Blake3c1fa352018-12-15 07:53:08 -0600282#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100283static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000284{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100285 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100286 int nbd;
thscd831bd2008-07-03 10:23:51 +0000287
Paolo Bonzinia517e882011-11-04 15:51:21 +0100288 /* linux just needs an open() to trigger
289 * the partition table update
290 * but remember to load the module with max_part != 0 :
291 * modprobe nbd max_part=63
292 */
293 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100294 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100295 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000296 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100297 return NULL;
298}
299
300static void *nbd_client_thread(void *arg)
301{
Denis V. Lunev03b67622023-07-17 16:55:40 +0200302 struct NbdClientOpts *opts = arg;
Eric Blakeac132d02023-08-29 12:58:28 -0500303 /* TODO: Revisit this if nbd.ko ever gains support for structured reply */
304 NBDExportInfo info = { .request_sizes = false, .name = g_strdup(""),
305 .mode = NBD_MODE_SIMPLE };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000306 QIOChannelSocket *sioc;
Alex Chenaf74b552020-12-08 13:49:44 +0000307 int fd = -1;
308 int ret = EXIT_FAILURE;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100309 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500310 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100311
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000312 sioc = qio_channel_socket_new();
313 if (qio_channel_socket_connect_sync(sioc,
Denis V. Lunevb2cecdf2023-09-06 11:32:06 +0200314 opts->saddr,
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000315 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100316 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000317 goto out;
318 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100319
Stefan Hajnoczib84ca912023-08-30 18:47:59 -0400320 if (nbd_receive_negotiate(QIO_CHANNEL(sioc), NULL, NULL, NULL,
321 &info, &local_error) < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500322 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100323 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500324 }
Alex Chenaf74b552020-12-08 13:49:44 +0000325 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100326 }
327
Denis V. Lunev03b67622023-07-17 16:55:40 +0200328 fd = open(opts->device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100329 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100330 /* Linux-only, we can use %m in printf. */
Denis V. Lunev03b67622023-07-17 16:55:40 +0200331 error_report("Failed to open %s: %m", opts->device);
Alex Chenaf74b552020-12-08 13:49:44 +0000332 goto out;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100333 }
334
Alex Chenaf74b552020-12-08 13:49:44 +0000335 if (nbd_init(fd, sioc, &info, &local_error) < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300336 error_report_err(local_error);
Alex Chenaf74b552020-12-08 13:49:44 +0000337 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100338 }
339
340 /* update partition table */
Denis V. Lunev03b67622023-07-17 16:55:40 +0200341 pthread_create(&show_parts_thread, NULL, show_parts, opts->device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100342
Denis V. Lunev414c0cf2023-07-17 22:25:20 +0200343 if (opts->verbose && !opts->fork_process) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100344 fprintf(stderr, "NBD device %s is now connected to %s\n",
Denis V. Lunevb18d72d2023-09-06 11:32:05 +0200345 opts->device, opts->srcpath);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100346 } else {
Denis V. Lunevf6f46202023-09-06 11:32:08 +0200347 nbd_client_release_pipe(opts->old_stderr);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100348 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100349
Alex Chenaf74b552020-12-08 13:49:44 +0000350 if (nbd_client(fd) < 0) {
351 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100352 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100353
Alex Chenaf74b552020-12-08 13:49:44 +0000354 ret = EXIT_SUCCESS;
355
356 out:
357 if (fd >= 0) {
358 close(fd);
359 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000360 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600361 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100362 kill(getpid(), SIGTERM);
Alex Chenaf74b552020-12-08 13:49:44 +0000363 return (void *) (intptr_t) ret;
thscd831bd2008-07-03 10:23:51 +0000364}
Eric Blake3c1fa352018-12-15 07:53:08 -0600365#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000366
Fam Zhenge4afbf42015-05-19 10:50:59 +0000367static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200368{
Eric Blake3dcf56e2021-02-09 09:27:59 -0600369 return state == RUNNING && (shared == 0 || nb_fds < shared);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200370}
371
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000372static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000373
Eric Blake0c9390d2017-06-08 17:26:17 -0500374static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200375{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200376 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500377 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200378 state = TERMINATE;
379 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000380 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200381 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200382}
383
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000384static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
385 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200386{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200387 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000388 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200389 }
390
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800391 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000392 nbd_update_server_watch();
Eric Blakefb1c2aa2024-08-07 08:50:01 -0500393 /* TODO - expose handshake timeout as command line option */
394 nbd_client_new(cioc, NBD_DEFAULT_HANDSHAKE_MAX_SECS,
395 tlscreds, tlsauthz, nbd_client_closed, NULL);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200396}
397
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000398static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000399{
400 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000401 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000402 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000403 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000404 }
405}
406
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100407
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200408static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100409 const char *bindto,
410 const char *port)
411{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200412 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100413
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200414 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100415 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200416 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
417 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100418 } else {
Eric Blake03992932016-03-03 09:16:48 -0700419 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200420 saddr->type = SOCKET_ADDRESS_TYPE_INET;
421 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700422 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100423 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700424 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100425 } else {
Eric Blake03992932016-03-03 09:16:48 -0700426 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100427 }
428 }
429
430 return saddr;
431}
432
433
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000434static QemuOptsList file_opts = {
435 .name = "file",
436 .implied_opt_name = "file",
437 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
438 .desc = {
439 /* no elements => accept any params */
440 { /* end of list */ }
441 },
442};
443
Eric Blake68b96f12019-01-17 13:36:56 -0600444static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
445 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000446{
447 Object *obj;
448 QCryptoTLSCreds *creds;
449
450 obj = object_resolve_path_component(
451 object_get_objects_root(), id);
452 if (!obj) {
453 error_setg(errp, "No TLS credentials with id '%s'",
454 id);
455 return NULL;
456 }
457 creds = (QCryptoTLSCreds *)
458 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
459 if (!creds) {
460 error_setg(errp, "Object with id '%s' is not TLS credentials",
461 id);
462 return NULL;
463 }
464
Philippe Mathieu-Daudé0279cd92021-06-28 18:09:10 +0200465 if (!qcrypto_tls_creds_check_endpoint(creds,
466 list
467 ? QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
468 : QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
469 errp)) {
470 return NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000471 }
472 object_ref(obj);
473 return creds;
474}
475
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000476static void setup_address_and_port(const char **address, const char **port)
477{
478 if (*address == NULL) {
479 *address = "0.0.0.0";
480 }
481
482 if (*port == NULL) {
483 *port = stringify(NBD_DEFAULT_PORT);
484 }
485}
486
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000487/*
488 * Check socket parameters compatibility when socket activation is used.
489 */
490static const char *socket_activation_validate_opts(const char *device,
491 const char *sockpath,
492 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600493 const char *port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600494 const char *selinux,
Eric Blake68b96f12019-01-17 13:36:56 -0600495 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000496{
497 if (device != NULL) {
498 return "NBD device can't be set when using socket activation";
499 }
500
501 if (sockpath != NULL) {
502 return "Unix socket can't be set when using socket activation";
503 }
504
505 if (address != NULL) {
506 return "The interface can't be set when using socket activation";
507 }
508
509 if (port != NULL) {
510 return "TCP port number can't be set when using socket activation";
511 }
512
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600513 if (selinux != NULL) {
514 return "SELinux label can't be set when using socket activation";
515 }
516
Eric Blake68b96f12019-01-17 13:36:56 -0600517 if (list) {
518 return "List mode is incompatible with socket activation";
519 }
520
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000521 return NULL;
522}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000523
Kevin Wolfb3b52992018-05-16 13:46:37 +0200524static void qemu_nbd_shutdown(void)
525{
526 job_cancel_sync_all();
Sergio Lopez1895b972021-02-01 13:50:32 +0100527 blk_exp_close_all();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200528 bdrv_close_all();
529}
530
bellard7a5ca862008-05-27 21:13:40 +0000531int main(int argc, char **argv)
532{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200533 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000534 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600535 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500536 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000537 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000538 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100539 const char *port = NULL;
540 char *sockpath = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800541 QemuOpts *sn_opts = NULL;
542 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500543 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000544 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000545 { "help", no_argument, NULL, 'h' },
546 { "version", no_argument, NULL, 'V' },
547 { "bind", required_argument, NULL, 'b' },
548 { "port", required_argument, NULL, 'p' },
549 { "socket", required_argument, NULL, 'k' },
550 { "offset", required_argument, NULL, 'o' },
551 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500552 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600553 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000554 { "connect", required_argument, NULL, 'c' },
555 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600556 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000557 { "snapshot", no_argument, NULL, 's' },
558 { "load-snapshot", required_argument, NULL, 'l' },
559 { "nocache", no_argument, NULL, 'n' },
560 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
561 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
562 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
563 { "detect-zeroes", required_argument, NULL,
564 QEMU_NBD_OPT_DETECT_ZEROES },
565 { "shared", required_argument, NULL, 'e' },
566 { "format", required_argument, NULL, 'f' },
567 { "persistent", no_argument, NULL, 't' },
568 { "verbose", no_argument, NULL, 'v' },
569 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
570 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500571 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000572 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000573 { "tls-hostname", required_argument, NULL, QEMU_NBD_OPT_TLSHOSTNAME },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000574 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000575 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300576 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200577 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200578 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600579 { "selinux-label", required_argument, NULL,
580 QEMU_NBD_OPT_SELINUX_LABEL },
Blue Swirl660f11b2009-07-31 21:16:51 +0000581 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000582 };
583 int ch;
584 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200585 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500586 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200587 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100588 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200589 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100590 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000591 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200592 Error *local_err = NULL;
Eric Blakec7195732024-08-01 16:49:20 -0500593 BlockdevDetectZeroesOptions detect_zeroes =
594 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500595 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600596 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500597 const char *export_description = NULL;
Vladimir Sementsov-Ogievskiye5fb29d2022-03-15 00:32:25 +0300598 BlockDirtyBitmapOrStrList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500599 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000600 const char *tlscredsid = NULL;
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000601 const char *tlshostname = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000602 bool imageOpts = false;
Nir Soffer09615252021-08-13 23:55:19 +0300603 bool writethrough = false; /* Client will flush as needed. */
Eric Blake68b96f12019-01-17 13:36:56 -0600604 bool list = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000605 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200606 const char *pid_file_name = NULL;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600607 const char *selinux_label = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200608 BlockExportOptions *export_opts;
Denis V. Lunev3484f672023-09-06 11:32:04 +0200609 struct NbdClientOpts opts = {
610 .fork_process = false,
611 .verbose = false,
612 .device = NULL,
Denis V. Lunevb18d72d2023-09-06 11:32:05 +0200613 .srcpath = NULL,
Denis V. Lunevb2cecdf2023-09-06 11:32:06 +0200614 .saddr = NULL,
Denis V. Lunevf6f46202023-09-06 11:32:08 +0200615 .old_stderr = STDOUT_FILENO,
Denis V. Lunev3484f672023-09-06 11:32:04 +0200616 };
bellard7a5ca862008-05-27 21:13:40 +0000617
Eric Blake029a88c2020-09-30 07:11:01 -0500618#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100619 os_setup_early_signal_handling();
620 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200621#endif
622
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100623 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100624 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100625 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300626 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100627
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000628 module_call_init(MODULE_INIT_QOM);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300629 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800630 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100631
bellard7a5ca862008-05-27 21:13:40 +0000632 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
633 switch (ch) {
634 case 's':
ths2f726482008-07-03 11:47:46 +0000635 flags |= BDRV_O_SNAPSHOT;
636 break;
637 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200638 optarg = (char *) "none";
639 /* fallthrough */
640 case QEMU_NBD_OPT_CACHE:
641 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100642 error_report("-n and --cache can only be specified once");
643 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200644 }
645 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100646 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100647 error_report("Invalid cache mode `%s'", optarg);
648 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200649 }
bellard7a5ca862008-05-27 21:13:40 +0000650 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200651 case QEMU_NBD_OPT_AIO:
652 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100653 error_report("--aio can only be specified once");
654 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200655 }
656 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000657 if (bdrv_parse_aio(optarg, &flags) < 0) {
658 error_report("Invalid aio mode '%s'", optarg);
659 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200660 }
661 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100662 case QEMU_NBD_OPT_DISCARD:
663 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100664 error_report("--discard can only be specified once");
665 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100666 }
667 seen_discard = true;
668 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100669 error_report("Invalid discard mode `%s'", optarg);
670 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100671 }
672 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200673 case QEMU_NBD_OPT_DETECT_ZEROES:
674 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200675 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200676 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200677 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
678 &local_err);
679 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100680 error_reportf_err(local_err,
681 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100682 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200683 }
684 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
685 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100686 error_report("setting detect-zeroes to unmap is not allowed "
687 "without setting discard operation to unmap");
688 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200689 }
690 break;
bellard7a5ca862008-05-27 21:13:40 +0000691 case 'b':
692 bindto = optarg;
693 break;
694 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100695 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000696 break;
697 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600698 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
699 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100700 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000701 }
bellard7a5ca862008-05-27 21:13:40 +0000702 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800703 case 'l':
704 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100705 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
706 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800707 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100708 error_report("Failed in parsing snapshot param `%s'",
709 optarg);
710 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800711 }
712 } else {
713 sn_id_or_name = optarg;
714 }
715 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000716 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500717 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200718 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000719 break;
Eric Blakedbc7b012020-10-27 00:05:55 -0500720 case 'A':
721 alloc_depth = true;
722 break;
Eric Blake636192c2019-01-11 13:47:20 -0600723 case 'B':
Vladimir Sementsov-Ogievskiye5fb29d2022-03-15 00:32:25 +0300724 {
725 BlockDirtyBitmapOrStr *el = g_new(BlockDirtyBitmapOrStr, 1);
726 *el = (BlockDirtyBitmapOrStr) {
727 .type = QTYPE_QSTRING,
728 .u.local = g_strdup(optarg),
729 };
730 QAPI_LIST_PREPEND(bitmaps, el);
731 }
Eric Blake636192c2019-01-11 13:47:20 -0600732 break;
thscd831bd2008-07-03 10:23:51 +0000733 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100734 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200735 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100736 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100737 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200738 }
thscd831bd2008-07-03 10:23:51 +0000739 break;
740 case 'd':
741 disconnect = true;
742 break;
743 case 'c':
Denis V. Lunev3484f672023-09-06 11:32:04 +0200744 opts.device = optarg;
thscd831bd2008-07-03 10:23:51 +0000745 break;
ths3b05a8e2008-07-03 12:45:02 +0000746 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600747 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
Eric Blake3dcf56e2021-02-09 09:27:59 -0600748 shared < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100749 error_report("Invalid shared device number '%s'", optarg);
750 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000751 }
ths3b05a8e2008-07-03 12:45:02 +0000752 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000753 case 'f':
754 fmt = optarg;
755 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200756 case 't':
757 persistent = 1;
758 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000759 case 'x':
760 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600761 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
762 error_report("export name '%s' too long", export_name);
763 exit(EXIT_FAILURE);
764 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000765 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500766 case 'D':
767 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600768 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
769 error_report("export description '%s' too long",
770 export_description);
771 exit(EXIT_FAILURE);
772 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500773 break;
bellard7a5ca862008-05-27 21:13:40 +0000774 case 'v':
Denis V. Lunev3484f672023-09-06 11:32:04 +0200775 opts.verbose = true;
bellard7a5ca862008-05-27 21:13:40 +0000776 break;
777 case 'V':
778 version(argv[0]);
779 exit(0);
780 break;
781 case 'h':
782 usage(argv[0]);
783 exit(0);
784 break;
785 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100786 error_report("Try `%s --help' for more information.", argv[0]);
787 exit(EXIT_FAILURE);
Kevin Wolffa40e432021-02-17 12:56:45 +0100788 case QEMU_NBD_OPT_OBJECT:
789 user_creatable_process_cmdline(optarg);
790 break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000791 case QEMU_NBD_OPT_TLSCREDS:
792 tlscredsid = optarg;
793 break;
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000794 case QEMU_NBD_OPT_TLSHOSTNAME:
795 tlshostname = optarg;
796 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000797 case QEMU_NBD_OPT_IMAGE_OPTS:
798 imageOpts = true;
799 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300800 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500801 trace_opt_parse(optarg);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300802 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000803 case QEMU_NBD_OPT_TLSAUTHZ:
804 tlsauthz = optarg;
805 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200806 case QEMU_NBD_OPT_FORK:
Denis V. Lunev3484f672023-09-06 11:32:04 +0200807 opts.fork_process = true;
Max Reitzffb31e12016-09-28 22:46:42 +0200808 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600809 case 'L':
810 list = true;
811 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200812 case QEMU_NBD_OPT_PID_FILE:
813 pid_file_name = optarg;
814 break;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600815 case QEMU_NBD_OPT_SELINUX_LABEL:
816 selinux_label = optarg;
817 break;
bellard7a5ca862008-05-27 21:13:40 +0000818 }
819 }
820
Eric Blake68b96f12019-01-17 13:36:56 -0600821 if (list) {
822 if (argc != optind) {
823 error_report("List mode is incompatible with a file name");
824 exit(EXIT_FAILURE);
825 }
Eric Blake0bc16992020-01-23 10:46:50 -0600826 if (export_name || export_description || dev_offset ||
Denis V. Lunev3484f672023-09-06 11:32:04 +0200827 opts.device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blakedbc7b012020-10-27 00:05:55 -0500828 alloc_depth || seen_aio || seen_discard || seen_cache) {
Eric Blake68b96f12019-01-17 13:36:56 -0600829 error_report("List mode is incompatible with per-device settings");
830 exit(EXIT_FAILURE);
831 }
Denis V. Lunev3484f672023-09-06 11:32:04 +0200832 if (opts.fork_process) {
Eric Blake68b96f12019-01-17 13:36:56 -0600833 error_report("List mode is incompatible with forking");
834 exit(EXIT_FAILURE);
835 }
836 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100837 error_report("Invalid number of arguments");
838 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100839 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600840 } else if (!export_name) {
841 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000842 }
843
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300844 if (!trace_init_backends()) {
845 exit(1);
846 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500847 trace_init_file();
Richard Hendersonc5955f42022-04-17 11:29:44 -0700848 qemu_set_log(LOG_TRACE, &error_fatal);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300849
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000850 socket_activation = check_socket_activation();
851 if (socket_activation == 0) {
Daniel P. Berrangée8ae8b12022-03-04 19:36:03 +0000852 if (!sockpath) {
853 setup_address_and_port(&bindto, &port);
854 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000855 } else {
856 /* Using socket activation - check user didn't use -p etc. */
Denis V. Lunev3484f672023-09-06 11:32:04 +0200857 const char *err_msg = socket_activation_validate_opts(opts.device,
858 sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600859 bindto, port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600860 selinux_label,
Eric Blake68b96f12019-01-17 13:36:56 -0600861 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000862 if (err_msg != NULL) {
863 error_report("%s", err_msg);
864 exit(EXIT_FAILURE);
865 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100866
867 /* qemu-nbd can only listen on a single socket. */
868 if (socket_activation > 1) {
869 error_report("qemu-nbd does not support socket activation with %s > 1",
870 "LISTEN_FDS");
871 exit(EXIT_FAILURE);
872 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000873 }
874
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000875 if (tlscredsid) {
Denis V. Lunev3484f672023-09-06 11:32:04 +0200876 if (opts.device) {
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000877 error_report("TLS is not supported with a host device");
878 exit(EXIT_FAILURE);
879 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000880 if (tlsauthz && list) {
881 error_report("TLS authorization is incompatible with export list");
882 exit(EXIT_FAILURE);
883 }
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000884 if (tlshostname && !list) {
885 error_report("TLS hostname is only supported with export list");
886 exit(EXIT_FAILURE);
887 }
Eric Blake68b96f12019-01-17 13:36:56 -0600888 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000889 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200890 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000891 exit(EXIT_FAILURE);
892 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000893 } else {
894 if (tlsauthz) {
895 error_report("--tls-authz is not permitted without --tls-creds");
896 exit(EXIT_FAILURE);
897 }
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000898 if (tlshostname) {
899 error_report("--tls-hostname is not permitted without --tls-creds");
900 exit(EXIT_FAILURE);
901 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000902 }
903
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600904 if (selinux_label) {
905#ifdef CONFIG_SELINUX
Denis V. Lunev3484f672023-09-06 11:32:04 +0200906 if (sockpath == NULL && opts.device == NULL) {
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600907 error_report("--selinux-label is not permitted without --socket");
908 exit(EXIT_FAILURE);
909 }
910#else
911 error_report("SELinux support not enabled in this binary");
912 exit(EXIT_FAILURE);
913#endif
914 }
915
Eric Blake68b96f12019-01-17 13:36:56 -0600916 if (list) {
Denis V. Lunevb2cecdf2023-09-06 11:32:06 +0200917 opts.saddr = nbd_build_socket_address(sockpath, bindto, port);
918 return qemu_nbd_client_list(opts.saddr, tlscreds,
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000919 tlshostname ? tlshostname : bindto);
Eric Blake68b96f12019-01-17 13:36:56 -0600920 }
921
Eric Blake3c1fa352018-12-15 07:53:08 -0600922#if !HAVE_NBD_DEVICE
Denis V. Lunev3484f672023-09-06 11:32:04 +0200923 if (disconnect || opts.device) {
Eric Blake3c1fa352018-12-15 07:53:08 -0600924 error_report("Kernel /dev/nbdN support not available");
925 exit(EXIT_FAILURE);
926 }
927#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000928 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000929 int nbdfd = open(argv[optind], O_RDWR);
930 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100931 error_report("Cannot open %s: %s", argv[optind],
932 strerror(errno));
933 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100934 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000935 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000936
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000937 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000938
939 printf("%s disconnected\n", argv[optind]);
940
Peter Lieven713cc672014-08-13 19:20:19 +0200941 return 0;
thscd831bd2008-07-03 10:23:51 +0000942 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600943#endif
thscd831bd2008-07-03 10:23:51 +0000944
Denis V. Lunev3484f672023-09-06 11:32:04 +0200945 if ((opts.device && !opts.verbose) || opts.fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100946#ifndef WIN32
Marc-André Lureaua7241972022-03-29 15:21:00 +0400947 g_autoptr(GError) err = NULL;
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100948 int stderr_fd[2];
949 pid_t pid;
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100950
Marc-André Lureaua7241972022-03-29 15:21:00 +0400951 if (!g_unix_open_pipe(stderr_fd, FD_CLOEXEC, &err)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100952 error_report("Error setting up communication pipe: %s",
Marc-André Lureaua7241972022-03-29 15:21:00 +0400953 err->message);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100954 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100955 }
956
957 /* Now daemonize, but keep a communication channel open to
958 * print errors and exit with the proper status code.
959 */
960 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500961 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100962 error_report("Failed to fork: %s", strerror(errno));
963 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500964 } else if (pid == 0) {
Denis V. Lunev1dc82152023-07-17 16:55:43 +0200965 int saved_errno;
966
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100967 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200968
Denis V. Lunevf6f46202023-09-06 11:32:08 +0200969 /* Remember parent's stderr if we will be restoring it. */
970 if (opts.verbose /* fork_process is set */) {
971 opts.old_stderr = dup(STDERR_FILENO);
972 if (opts.old_stderr < 0) {
973 error_report("Could not dup original stderr: %s",
974 strerror(errno));
975 exit(EXIT_FAILURE);
976 }
977 }
978
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400979 ret = qemu_daemon(1, 0);
Denis V. Lunev1dc82152023-07-17 16:55:43 +0200980 saved_errno = errno; /* dup2 will overwrite error below */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100981
982 /* Temporarily redirect stderr to the parent's pipe... */
Denis V. Luneve0892ce2023-07-17 16:55:42 +0200983 if (dup2(stderr_fd[1], STDERR_FILENO) < 0) {
984 char str[256];
985 snprintf(str, sizeof(str),
986 "%s: Failed to link stderr to the pipe: %s\n",
987 g_get_prgname(), strerror(errno));
988 /*
989 * We are unable to use error_report() here as we need to get
990 * stderr pointed to the parent's pipe. Write to that pipe
991 * manually.
992 */
993 ret = write(stderr_fd[1], str, strlen(str));
994 exit(EXIT_FAILURE);
995 }
996
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100997 if (ret < 0) {
Denis V. Lunev1dc82152023-07-17 16:55:43 +0200998 error_report("Failed to daemonize: %s", strerror(saved_errno));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100999 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +01001000 }
1001
1002 /* ... close the descriptor we inherited and go on. */
1003 close(stderr_fd[1]);
1004 } else {
1005 bool errors = false;
1006 char *buf;
1007
1008 /* In the parent. Print error messages from the child until
1009 * it closes the pipe.
1010 */
1011 close(stderr_fd[1]);
1012 buf = g_malloc(1024);
1013 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
1014 errors = true;
1015 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001016 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +01001017 exit(EXIT_FAILURE);
1018 }
1019 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +01001020 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001021 error_report("Cannot read from daemon: %s",
1022 strerror(errno));
1023 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +01001024 }
1025
1026 /* Usually the daemon should not print any message.
1027 * Exit with zero status in that case.
1028 */
1029 exit(errors);
1030 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +01001031#else /* WIN32 */
1032 error_report("Unable to fork into background on Windows hosts");
1033 exit(EXIT_FAILURE);
1034#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +01001035 }
1036
Denis V. Lunev3484f672023-09-06 11:32:04 +02001037 if (opts.device != NULL && sockpath == NULL) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001038 sockpath = g_malloc(128);
Denis V. Lunev3484f672023-09-06 11:32:04 +02001039 snprintf(sockpath, 128, SOCKET_PATH, basename(opts.device));
thscd831bd2008-07-03 10:23:51 +00001040 }
1041
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001042 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001043 if (socket_activation == 0) {
Eric Blake582d4212021-02-09 09:27:58 -06001044 int backlog;
1045
Eric Blake3dcf56e2021-02-09 09:27:59 -06001046 if (persistent || shared == 0) {
Eric Blake582d4212021-02-09 09:27:58 -06001047 backlog = SOMAXCONN;
1048 } else {
1049 backlog = MIN(shared, SOMAXCONN);
1050 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -06001051#ifdef CONFIG_SELINUX
1052 if (selinux_label && setsockcreatecon_raw(selinux_label) == -1) {
1053 error_report("Cannot set SELinux socket create context to %s: %s",
1054 selinux_label, strerror(errno));
1055 exit(EXIT_FAILURE);
1056 }
1057#endif
Denis V. Lunevb2cecdf2023-09-06 11:32:06 +02001058 opts.saddr = nbd_build_socket_address(sockpath, bindto, port);
1059 if (qio_net_listener_open_sync(server, opts.saddr, backlog,
Eric Blake582d4212021-02-09 09:27:58 -06001060 &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001061 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001062 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001063 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001064 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -06001065#ifdef CONFIG_SELINUX
1066 if (selinux_label && setsockcreatecon_raw(NULL) == -1) {
1067 error_report("Cannot clear SELinux socket create context: %s",
1068 strerror(errno));
1069 exit(EXIT_FAILURE);
1070 }
1071#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001072 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001073 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001074 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001075 for (i = 0; i < socket_activation; i++) {
1076 QIOChannelSocket *sioc;
1077 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
1078 &local_err);
1079 if (sioc == NULL) {
1080 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +02001081 error_reportf_err(local_err,
1082 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001083 exit(EXIT_FAILURE);
1084 }
1085 qio_net_listener_add(server, sioc);
1086 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001087 }
1088 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +01001089
Markus Armbrusterf9734d52021-07-20 14:53:53 +02001090 qemu_init_main_loop(&error_fatal);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001091 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +02001092 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001093
Denis V. Lunevb18d72d2023-09-06 11:32:05 +02001094 opts.srcpath = argv[optind];
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001095 if (imageOpts) {
Denis V. Lunevb18d72d2023-09-06 11:32:05 +02001096 QemuOpts *o;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001097 if (fmt) {
1098 error_report("--image-opts and -f are mutually exclusive");
1099 exit(EXIT_FAILURE);
1100 }
Denis V. Lunevb18d72d2023-09-06 11:32:05 +02001101 o = qemu_opts_parse_noisily(&file_opts, opts.srcpath, true);
1102 if (!o) {
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001103 qemu_opts_reset(&file_opts);
1104 exit(EXIT_FAILURE);
1105 }
Denis V. Lunevb18d72d2023-09-06 11:32:05 +02001106 options = qemu_opts_to_qdict(o, NULL);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001107 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001108 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001109 } else {
1110 if (fmt) {
1111 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001112 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001113 }
Denis V. Lunevb18d72d2023-09-06 11:32:05 +02001114 blk = blk_new_open(opts.srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001115 }
1116
Max Reitz4fbec262015-02-05 13:58:19 -05001117 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001118 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1119 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001120 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001121 }
Max Reitz4fbec262015-02-05 13:58:19 -05001122 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001123
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001124 if (dev_offset) {
1125 QDict *raw_opts = qdict_new();
1126 qdict_put_str(raw_opts, "driver", "raw");
1127 qdict_put_str(raw_opts, "file", bs->node_name);
1128 qdict_put_int(raw_opts, "offset", dev_offset);
Kevin Wolfc6e0a6d2023-05-25 14:47:04 +02001129
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001130 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
Kevin Wolfc6e0a6d2023-05-25 14:47:04 +02001131
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001132 blk_remove_bs(blk);
1133 blk_insert_bs(blk, bs, &error_fatal);
1134 bdrv_unref(bs);
1135 }
1136
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001137 blk_set_enable_write_cache(blk, !writethrough);
1138
Wenchao Xia8c116b02013-12-04 17:10:55 +08001139 if (sn_opts) {
1140 ret = bdrv_snapshot_load_tmp(bs,
1141 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1142 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1143 &local_err);
1144 } else if (sn_id_or_name) {
1145 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1146 &local_err);
1147 }
1148 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001149 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001150 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001151 }
1152
Peter Lievenb3838a42014-08-13 19:20:18 +02001153 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001154
Eric Blakea5fced42022-05-11 19:49:23 -05001155 nbd_server_is_qemu_nbd(shared);
Kevin Wolf00917172020-09-24 17:26:57 +02001156
1157 export_opts = g_new(BlockExportOptions, 1);
1158 *export_opts = (BlockExportOptions) {
1159 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001160 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001161 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001162 .has_writethrough = true,
1163 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001164 .has_writable = true,
1165 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001166 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001167 .name = g_strdup(export_name),
Eric Blakecbad81c2020-10-27 00:05:49 -05001168 .description = g_strdup(export_description),
1169 .has_bitmaps = !!bitmaps,
1170 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001171 .has_allocation_depth = alloc_depth,
1172 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001173 },
1174 };
1175 blk_exp_add(export_opts, &error_fatal);
1176 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001177
Denis V. Lunev3484f672023-09-06 11:32:04 +02001178 if (opts.device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001179#if HAVE_NBD_DEVICE
Denis V. Lunev03b67622023-07-17 16:55:40 +02001180 ret = pthread_create(&client_thread, NULL, nbd_client_thread, &opts);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001181 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001182 error_report("Failed to create client thread: %s", strerror(ret));
1183 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001184 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001185#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001186 } else {
1187 /* Shut up GCC warnings. */
1188 memset(&client_thread, 0, sizeof(client_thread));
1189 }
1190
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001191 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001192
Max Reitz637bc5a2019-05-08 23:18:16 +02001193 if (pid_file_name) {
1194 qemu_write_pidfile(pid_file_name, &error_fatal);
1195 }
1196
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001197 /* now when the initialization is (almost) complete, chdir("/")
1198 * to free any busy filesystems */
1199 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001200 error_report("Could not chdir to root directory: %s",
1201 strerror(errno));
1202 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001203 }
1204
Denis V. Lunev3484f672023-09-06 11:32:04 +02001205 if (opts.fork_process) {
Denis V. Lunevf6f46202023-09-06 11:32:08 +02001206 nbd_client_release_pipe(opts.old_stderr);
Max Reitzffb31e12016-09-28 22:46:42 +02001207 }
1208
Paolo Bonzini7860a382012-09-18 13:31:56 +02001209 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001210 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001211 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001212 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001213 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001214 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001215 }
1216 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001217
Markus Armbruster26f54e92014-10-07 13:59:04 +02001218 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001219 if (sockpath) {
1220 unlink(sockpath);
1221 }
bellard7a5ca862008-05-27 21:13:40 +00001222
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001223 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001224
Denis V. Lunev3484f672023-09-06 11:32:04 +02001225 if (opts.device) {
Eric Blakee1617852023-09-22 15:50:20 -05001226 void *result;
1227 pthread_join(client_thread, &result);
1228 ret = (intptr_t)result;
1229 exit(ret);
Paolo Bonzinia517e882011-11-04 15:51:21 +01001230 } else {
1231 exit(EXIT_SUCCESS);
1232 }
bellard7a5ca862008-05-27 21:13:40 +00001233}