blob: 66e023f7fa44a03e0983af5d455d407cf872d229 [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
Markus Armbrusterda34e652016-03-14 09:01:28 +010024#include "qapi/error.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020025#include "qemu/cutils.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020026#include "sysemu/block-backend.h"
Peter Lievenb3838a42014-08-13 19:20:18 +020027#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010028#include "block/nbd.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010029#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010030#include "qemu/option.h"
Paolo Bonzini537b41f2014-02-17 14:43:51 +010031#include "qemu/error-report.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000032#include "qemu/config-file.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010033#include "qemu/bswap.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030034#include "qemu/log.h"
Paolo Bonzini53fabd42017-03-16 16:29:45 +010035#include "qemu/systemd.h"
Wenchao Xia8c116b02013-12-04 17:10:55 +080036#include "block/snapshot.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010037#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010038#include "qapi/qmp/qstring.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000039#include "qom/object_interfaces.h"
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000040#include "io/channel-socket.h"
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000041#include "io/net-listener.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010042#include "crypto/init.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030043#include "trace/control.h"
Eric Blakebe377132017-07-21 08:50:46 -050044#include "qemu-version.h"
bellard7a5ca862008-05-27 21:13:40 +000045
Peter Lieven713cc672014-08-13 19:20:19 +020046#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
Daniel P. Berrangefa8b7ce2016-02-17 10:10:21 +000047#define QEMU_NBD_OPT_CACHE 256
48#define QEMU_NBD_OPT_AIO 257
49#define QEMU_NBD_OPT_DISCARD 258
50#define QEMU_NBD_OPT_DETECT_ZEROES 259
51#define QEMU_NBD_OPT_OBJECT 260
52#define QEMU_NBD_OPT_TLSCREDS 261
53#define QEMU_NBD_OPT_IMAGE_OPTS 262
Max Reitzffb31e12016-09-28 22:46:42 +020054#define QEMU_NBD_OPT_FORK 263
bellard7a5ca862008-05-27 21:13:40 +000055
Eric Blakebd31c212016-05-06 10:26:42 -060056#define MBR_SIZE 512
57
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020058static NBDExport *exp;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +000059static bool newproto;
blueswir1b1d8e522008-10-26 13:43:07 +000060static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010061static char *srcpath;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020062static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020063static int persistent = 0;
64static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020065static int shared = 1;
66static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000067static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000068static QCryptoTLSCreds *tlscreds;
bellard7a5ca862008-05-27 21:13:40 +000069
70static void usage(const char *name)
71{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020072 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000073"Usage: %s [OPTIONS] FILE\n"
74"QEMU Disk Network Block Device Server\n"
75"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020076" -h, --help display this help and exit\n"
77" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000078"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020079"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020080" -p, --port=PORT port to listen on (default `%d')\n"
81" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
82" -k, --socket=PATH path to the unix socket\n"
83" (default '"SOCKET_PATH"')\n"
84" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
85" -t, --persistent don't exit on the last connection\n"
86" -v, --verbose display extra debugging information\n"
Eric Blake332a2542016-04-05 20:02:08 -060087" -x, --export-name=NAME expose export by name\n"
Eric Blakeb1a75b32016-10-14 13:33:03 -050088" -D, --description=TEXT with -x, also export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020089"\n"
90"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020091" -o, --offset=OFFSET offset into the image\n"
92" -P, --partition=NUM only expose partition NUM\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020093"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000094"General purpose options:\n"
95" --object type,id=ID,... define an object such as 'secret' for providing\n"
96" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -050097" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030098" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
99" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200100" --fork fork off the server process and exit the parent\n"
101" once the server is running\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200102#ifdef __linux__
103"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200104" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
105" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200106"\n"
107#endif
108"\n"
109"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200110" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
111" -r, --read-only export read-only\n"
112" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
113" file with backing_file=FILE, redirect the write to\n"
114" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800115" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200116" load an internal snapshot inside FILE and export it\n"
117" as an read-only device, SNAPSHOT_PARAM format is\n"
118" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
119" '[ID_OR_NAME]'\n"
120" -n, --nocache disable host cache\n"
121" --cache=MODE set cache mode (none, writeback, ...)\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200122" --aio=MODE set AIO mode (native or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200123" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300124" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000125" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200126"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500127QEMU_HELP_BOTTOM "\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +0200128 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000129}
130
131static void version(const char *name)
132{
133 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100134"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000135"Written by Anthony Liguori.\n"
136"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500137QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000138"This is free software; see the source for copying conditions. There is NO\n"
139"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000140 , name);
bellard7a5ca862008-05-27 21:13:40 +0000141}
142
143struct partition_record
144{
145 uint8_t bootable;
146 uint8_t start_head;
147 uint32_t start_cylinder;
148 uint8_t start_sector;
149 uint8_t system;
150 uint8_t end_head;
151 uint8_t end_cylinder;
152 uint8_t end_sector;
153 uint32_t start_sector_abs;
154 uint32_t nb_sectors_abs;
155};
156
157static void read_partition(uint8_t *p, struct partition_record *r)
158{
159 r->bootable = p[0];
160 r->start_head = p[1];
161 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
162 r->start_sector = p[2] & 0x3f;
163 r->system = p[4];
164 r->end_head = p[5];
165 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
166 r->end_sector = p[6] & 0x3f;
Max Reitzac973932015-02-25 13:08:23 -0500167
Peter Maydell773dce32016-06-10 16:00:36 +0100168 r->start_sector_abs = ldl_le_p(p + 8);
169 r->nb_sectors_abs = ldl_le_p(p + 12);
bellard7a5ca862008-05-27 21:13:40 +0000170}
171
Max Reitz4c58e802014-11-18 12:21:19 +0100172static int find_partition(BlockBackend *blk, int partition,
bellard7a5ca862008-05-27 21:13:40 +0000173 off_t *offset, off_t *size)
174{
175 struct partition_record mbr[4];
Eric Blakebd31c212016-05-06 10:26:42 -0600176 uint8_t data[MBR_SIZE];
bellard7a5ca862008-05-27 21:13:40 +0000177 int i;
178 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900179 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000180
Eric Blakebd31c212016-05-06 10:26:42 -0600181 ret = blk_pread(blk, 0, data, sizeof(data));
182 if (ret < 0) {
Markus Armbrustera4699e52015-12-18 16:35:10 +0100183 error_report("error while reading: %s", strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100184 exit(EXIT_FAILURE);
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900185 }
bellard7a5ca862008-05-27 21:13:40 +0000186
187 if (data[510] != 0x55 || data[511] != 0xaa) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100188 return -EINVAL;
bellard7a5ca862008-05-27 21:13:40 +0000189 }
190
191 for (i = 0; i < 4; i++) {
192 read_partition(&data[446 + 16 * i], &mbr[i]);
193
Max Reitz453b07b2015-02-25 13:08:15 -0500194 if (!mbr[i].system || !mbr[i].nb_sectors_abs) {
bellard7a5ca862008-05-27 21:13:40 +0000195 continue;
Max Reitz453b07b2015-02-25 13:08:15 -0500196 }
bellard7a5ca862008-05-27 21:13:40 +0000197
198 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
199 struct partition_record ext[4];
Eric Blakebd31c212016-05-06 10:26:42 -0600200 uint8_t data1[MBR_SIZE];
bellard7a5ca862008-05-27 21:13:40 +0000201 int j;
202
Eric Blakebd31c212016-05-06 10:26:42 -0600203 ret = blk_pread(blk, mbr[i].start_sector_abs * MBR_SIZE,
204 data1, sizeof(data1));
205 if (ret < 0) {
Markus Armbrustera4699e52015-12-18 16:35:10 +0100206 error_report("error while reading: %s", strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100207 exit(EXIT_FAILURE);
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900208 }
bellard7a5ca862008-05-27 21:13:40 +0000209
210 for (j = 0; j < 4; j++) {
211 read_partition(&data1[446 + 16 * j], &ext[j]);
Max Reitz453b07b2015-02-25 13:08:15 -0500212 if (!ext[j].system || !ext[j].nb_sectors_abs) {
bellard7a5ca862008-05-27 21:13:40 +0000213 continue;
Max Reitz453b07b2015-02-25 13:08:15 -0500214 }
bellard7a5ca862008-05-27 21:13:40 +0000215
216 if ((ext_partnum + j + 1) == partition) {
217 *offset = (uint64_t)ext[j].start_sector_abs << 9;
218 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
219 return 0;
220 }
221 }
222 ext_partnum += 4;
223 } else if ((i + 1) == partition) {
224 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
225 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
226 return 0;
227 }
228 }
229
Paolo Bonzini185b4332012-03-05 08:56:10 +0100230 return -ENOENT;
bellard7a5ca862008-05-27 21:13:40 +0000231}
232
Paolo Bonzinibb345112011-11-04 15:51:19 +0100233static void termsig_handler(int signum)
234{
Pavel Butsykin23994a52016-04-14 13:20:15 +0300235 atomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200236 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100237}
238
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100239
Paolo Bonzinia517e882011-11-04 15:51:21 +0100240static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000241{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100242 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100243 int nbd;
thscd831bd2008-07-03 10:23:51 +0000244
Paolo Bonzinia517e882011-11-04 15:51:21 +0100245 /* linux just needs an open() to trigger
246 * the partition table update
247 * but remember to load the module with max_part != 0 :
248 * modprobe nbd max_part=63
249 */
250 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100251 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100252 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000253 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100254 return NULL;
255}
256
257static void *nbd_client_thread(void *arg)
258{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100259 char *device = arg;
Eric Blake081dd1f2017-07-07 15:30:49 -0500260 NBDExportInfo info = { .request_sizes = false, };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000261 QIOChannelSocket *sioc;
262 int fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100263 int ret;
264 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500265 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100266
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000267 sioc = qio_channel_socket_new();
268 if (qio_channel_socket_connect_sync(sioc,
269 saddr,
270 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100271 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000272 goto out;
273 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100274
Eric Blake004a89f2017-07-07 15:30:41 -0500275 ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), NULL,
276 NULL, NULL, NULL, &info, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100277 if (ret < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500278 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100279 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500280 }
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100281 goto out_socket;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100282 }
283
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100284 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100285 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100286 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100287 error_report("Failed to open %s: %m", device);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100288 goto out_socket;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100289 }
290
Eric Blake004a89f2017-07-07 15:30:41 -0500291 ret = nbd_init(fd, sioc, &info, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100292 if (ret < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300293 error_report_err(local_error);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100294 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100295 }
296
297 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100298 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100299
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100300 if (verbose) {
301 fprintf(stderr, "NBD device %s is now connected to %s\n",
302 device, srcpath);
303 } else {
304 /* Close stderr so that the qemu-nbd process exits. */
305 dup2(STDOUT_FILENO, STDERR_FILENO);
306 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100307
308 ret = nbd_client(fd);
309 if (ret) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100310 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100311 }
312 close(fd);
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000313 object_unref(OBJECT(sioc));
Paolo Bonzinia517e882011-11-04 15:51:21 +0100314 kill(getpid(), SIGTERM);
315 return (void *) EXIT_SUCCESS;
316
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100317out_fd:
318 close(fd);
319out_socket:
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000320 object_unref(OBJECT(sioc));
Paolo Bonzinia517e882011-11-04 15:51:21 +0100321out:
322 kill(getpid(), SIGTERM);
323 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000324}
325
Fam Zhenge4afbf42015-05-19 10:50:59 +0000326static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200327{
Eric Blakedf8ad9f2017-05-26 22:04:21 -0500328 return state == RUNNING && nb_fds < shared;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200329}
330
Paolo Bonzini7860a382012-09-18 13:31:56 +0200331static void nbd_export_closed(NBDExport *exp)
332{
333 assert(state == TERMINATING);
334 state = TERMINATED;
335}
336
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000337static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000338
Eric Blake0c9390d2017-06-08 17:26:17 -0500339static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200340{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200341 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500342 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200343 state = TERMINATE;
344 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000345 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200346 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200347}
348
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000349static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
350 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200351{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200352 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000353 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200354 }
355
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800356 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000357 nbd_update_server_watch();
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000358 nbd_client_new(newproto ? NULL : exp, cioc,
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000359 tlscreds, NULL, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200360}
361
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000362static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000363{
364 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000365 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000366 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000367 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000368 }
369}
370
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100371
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200372static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100373 const char *bindto,
374 const char *port)
375{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200376 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100377
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200378 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100379 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200380 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
381 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100382 } else {
Eric Blake03992932016-03-03 09:16:48 -0700383 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200384 saddr->type = SOCKET_ADDRESS_TYPE_INET;
385 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700386 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100387 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700388 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100389 } else {
Eric Blake03992932016-03-03 09:16:48 -0700390 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100391 }
392 }
393
394 return saddr;
395}
396
397
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000398static QemuOptsList file_opts = {
399 .name = "file",
400 .implied_opt_name = "file",
401 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
402 .desc = {
403 /* no elements => accept any params */
404 { /* end of list */ }
405 },
406};
407
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000408static QemuOptsList qemu_object_opts = {
409 .name = "object",
410 .implied_opt_name = "qom-type",
411 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
412 .desc = {
413 { }
414 },
415};
416
417
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000418
419static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
420{
421 Object *obj;
422 QCryptoTLSCreds *creds;
423
424 obj = object_resolve_path_component(
425 object_get_objects_root(), id);
426 if (!obj) {
427 error_setg(errp, "No TLS credentials with id '%s'",
428 id);
429 return NULL;
430 }
431 creds = (QCryptoTLSCreds *)
432 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
433 if (!creds) {
434 error_setg(errp, "Object with id '%s' is not TLS credentials",
435 id);
436 return NULL;
437 }
438
439 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
440 error_setg(errp,
441 "Expecting TLS credentials with a server endpoint");
442 return NULL;
443 }
444 object_ref(obj);
445 return creds;
446}
447
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000448static void setup_address_and_port(const char **address, const char **port)
449{
450 if (*address == NULL) {
451 *address = "0.0.0.0";
452 }
453
454 if (*port == NULL) {
455 *port = stringify(NBD_DEFAULT_PORT);
456 }
457}
458
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000459/*
460 * Check socket parameters compatibility when socket activation is used.
461 */
462static const char *socket_activation_validate_opts(const char *device,
463 const char *sockpath,
464 const char *address,
465 const char *port)
466{
467 if (device != NULL) {
468 return "NBD device can't be set when using socket activation";
469 }
470
471 if (sockpath != NULL) {
472 return "Unix socket can't be set when using socket activation";
473 }
474
475 if (address != NULL) {
476 return "The interface can't be set when using socket activation";
477 }
478
479 if (port != NULL) {
480 return "TCP port number can't be set when using socket activation";
481 }
482
483 return NULL;
484}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000485
Kevin Wolfb3b52992018-05-16 13:46:37 +0200486static void qemu_nbd_shutdown(void)
487{
488 job_cancel_sync_all();
489 bdrv_close_all();
490}
491
bellard7a5ca862008-05-27 21:13:40 +0000492int main(int argc, char **argv)
493{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200494 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000495 BlockDriverState *bs;
496 off_t dev_offset = 0;
Eric Blake7423f412016-07-21 13:34:46 -0600497 uint16_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000498 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000499 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100500 const char *port = NULL;
501 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100502 char *device = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000503 off_t fd_size;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800504 QemuOpts *sn_opts = NULL;
505 const char *sn_id_or_name = NULL;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500506 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:x:T:D:";
bellard7a5ca862008-05-27 21:13:40 +0000507 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000508 { "help", no_argument, NULL, 'h' },
509 { "version", no_argument, NULL, 'V' },
510 { "bind", required_argument, NULL, 'b' },
511 { "port", required_argument, NULL, 'p' },
512 { "socket", required_argument, NULL, 'k' },
513 { "offset", required_argument, NULL, 'o' },
514 { "read-only", no_argument, NULL, 'r' },
515 { "partition", required_argument, NULL, 'P' },
516 { "connect", required_argument, NULL, 'c' },
517 { "disconnect", no_argument, NULL, 'd' },
518 { "snapshot", no_argument, NULL, 's' },
519 { "load-snapshot", required_argument, NULL, 'l' },
520 { "nocache", no_argument, NULL, 'n' },
521 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
522 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
523 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
524 { "detect-zeroes", required_argument, NULL,
525 QEMU_NBD_OPT_DETECT_ZEROES },
526 { "shared", required_argument, NULL, 'e' },
527 { "format", required_argument, NULL, 'f' },
528 { "persistent", no_argument, NULL, 't' },
529 { "verbose", no_argument, NULL, 'v' },
530 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
531 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500532 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000533 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
534 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300535 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200536 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Blue Swirl660f11b2009-07-31 21:16:51 +0000537 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000538 };
539 int ch;
540 int opt_ind = 0;
bellard7a5ca862008-05-27 21:13:40 +0000541 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200542 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000543 int partition = -1;
Max Reitz4fbec262015-02-05 13:58:19 -0500544 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200545 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100546 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200547 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100548 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000549 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200550 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200551 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500552 QDict *options = NULL;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000553 const char *export_name = NULL;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500554 const char *export_description = NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000555 const char *tlscredsid = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000556 bool imageOpts = false;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100557 bool writethrough = true;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300558 char *trace_file = NULL;
Max Reitzffb31e12016-09-28 22:46:42 +0200559 bool fork_process = false;
560 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000561 unsigned socket_activation;
bellard7a5ca862008-05-27 21:13:40 +0000562
Paolo Bonzinia517e882011-11-04 15:51:21 +0100563 /* The client thread uses SIGTERM to interrupt the server. A signal
564 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
565 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100566 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100567 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
568 sa_sigterm.sa_handler = termsig_handler;
569 sigaction(SIGTERM, &sa_sigterm, NULL);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100570
Max Reitz041e32b2017-06-11 14:37:14 +0200571#ifdef CONFIG_POSIX
572 signal(SIGPIPE, SIG_IGN);
573#endif
574
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100575 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300576 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100577
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000578 module_call_init(MODULE_INIT_QOM);
579 qemu_add_opts(&qemu_object_opts);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300580 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800581 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100582
bellard7a5ca862008-05-27 21:13:40 +0000583 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
584 switch (ch) {
585 case 's':
ths2f726482008-07-03 11:47:46 +0000586 flags |= BDRV_O_SNAPSHOT;
587 break;
588 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200589 optarg = (char *) "none";
590 /* fallthrough */
591 case QEMU_NBD_OPT_CACHE:
592 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100593 error_report("-n and --cache can only be specified once");
594 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200595 }
596 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100597 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100598 error_report("Invalid cache mode `%s'", optarg);
599 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200600 }
bellard7a5ca862008-05-27 21:13:40 +0000601 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200602 case QEMU_NBD_OPT_AIO:
603 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100604 error_report("--aio can only be specified once");
605 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200606 }
607 seen_aio = true;
608 if (!strcmp(optarg, "native")) {
609 flags |= BDRV_O_NATIVE_AIO;
610 } else if (!strcmp(optarg, "threads")) {
611 /* this is the default */
612 } else {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100613 error_report("invalid aio mode `%s'", optarg);
614 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200615 }
616 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100617 case QEMU_NBD_OPT_DISCARD:
618 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100619 error_report("--discard can only be specified once");
620 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100621 }
622 seen_discard = true;
623 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100624 error_report("Invalid discard mode `%s'", optarg);
625 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100626 }
627 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200628 case QEMU_NBD_OPT_DETECT_ZEROES:
629 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200630 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200631 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200632 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
633 &local_err);
634 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100635 error_reportf_err(local_err,
636 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100637 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200638 }
639 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
640 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100641 error_report("setting detect-zeroes to unmap is not allowed "
642 "without setting discard operation to unmap");
643 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200644 }
645 break;
bellard7a5ca862008-05-27 21:13:40 +0000646 case 'b':
647 bindto = optarg;
648 break;
649 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100650 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000651 break;
652 case 'o':
653 dev_offset = strtoll (optarg, &end, 0);
654 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100655 error_report("Invalid offset `%s'", optarg);
656 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000657 }
658 if (dev_offset < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100659 error_report("Offset must be positive `%s'", optarg);
660 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000661 }
662 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800663 case 'l':
664 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100665 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
666 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800667 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100668 error_report("Failed in parsing snapshot param `%s'",
669 optarg);
670 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800671 }
672 } else {
673 sn_id_or_name = optarg;
674 }
675 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000676 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200677 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200678 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000679 break;
680 case 'P':
681 partition = strtol(optarg, &end, 0);
Peter Lieven713cc672014-08-13 19:20:19 +0200682 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100683 error_report("Invalid partition `%s'", optarg);
684 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200685 }
686 if (partition < 1 || partition > 8) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100687 error_report("Invalid partition %d", partition);
688 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200689 }
bellard7a5ca862008-05-27 21:13:40 +0000690 break;
thscd831bd2008-07-03 10:23:51 +0000691 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100692 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200693 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100694 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100695 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200696 }
thscd831bd2008-07-03 10:23:51 +0000697 break;
698 case 'd':
699 disconnect = true;
700 break;
701 case 'c':
702 device = optarg;
703 break;
ths3b05a8e2008-07-03 12:45:02 +0000704 case 'e':
705 shared = strtol(optarg, &end, 0);
706 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100707 error_report("Invalid shared device number '%s'", optarg);
708 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000709 }
710 if (shared < 1) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100711 error_report("Shared device number must be greater than 0");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100712 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000713 }
714 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000715 case 'f':
716 fmt = optarg;
717 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200718 case 't':
719 persistent = 1;
720 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000721 case 'x':
722 export_name = optarg;
723 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500724 case 'D':
725 export_description = optarg;
726 break;
bellard7a5ca862008-05-27 21:13:40 +0000727 case 'v':
728 verbose = 1;
729 break;
730 case 'V':
731 version(argv[0]);
732 exit(0);
733 break;
734 case 'h':
735 usage(argv[0]);
736 exit(0);
737 break;
738 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100739 error_report("Try `%s --help' for more information.", argv[0]);
740 exit(EXIT_FAILURE);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000741 case QEMU_NBD_OPT_OBJECT: {
742 QemuOpts *opts;
743 opts = qemu_opts_parse_noisily(&qemu_object_opts,
744 optarg, true);
745 if (!opts) {
746 exit(EXIT_FAILURE);
747 }
748 } break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000749 case QEMU_NBD_OPT_TLSCREDS:
750 tlscredsid = optarg;
751 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000752 case QEMU_NBD_OPT_IMAGE_OPTS:
753 imageOpts = true;
754 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300755 case 'T':
756 g_free(trace_file);
757 trace_file = trace_opt_parse(optarg);
758 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200759 case QEMU_NBD_OPT_FORK:
760 fork_process = true;
761 break;
bellard7a5ca862008-05-27 21:13:40 +0000762 }
763 }
764
765 if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100766 error_report("Invalid number of arguments");
767 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100768 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000769 }
770
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000771 if (qemu_opts_foreach(&qemu_object_opts,
772 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200773 NULL, NULL)) {
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000774 exit(EXIT_FAILURE);
775 }
776
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300777 if (!trace_init_backends()) {
778 exit(1);
779 }
780 trace_init_file(trace_file);
781 qemu_set_log(LOG_TRACE);
782
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000783 socket_activation = check_socket_activation();
784 if (socket_activation == 0) {
785 setup_address_and_port(&bindto, &port);
786 } else {
787 /* Using socket activation - check user didn't use -p etc. */
788 const char *err_msg = socket_activation_validate_opts(device, sockpath,
789 bindto, port);
790 if (err_msg != NULL) {
791 error_report("%s", err_msg);
792 exit(EXIT_FAILURE);
793 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100794
795 /* qemu-nbd can only listen on a single socket. */
796 if (socket_activation > 1) {
797 error_report("qemu-nbd does not support socket activation with %s > 1",
798 "LISTEN_FDS");
799 exit(EXIT_FAILURE);
800 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000801 }
802
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000803 if (tlscredsid) {
804 if (sockpath) {
805 error_report("TLS is only supported with IPv4/IPv6");
806 exit(EXIT_FAILURE);
807 }
808 if (device) {
809 error_report("TLS is not supported with a host device");
810 exit(EXIT_FAILURE);
811 }
812 if (!export_name) {
813 /* Set the default NBD protocol export name, since
814 * we *must* use new style protocol for TLS */
815 export_name = "";
816 }
817 tlscreds = nbd_get_tls_creds(tlscredsid, &local_err);
818 if (local_err) {
819 error_report("Failed to get TLS creds %s",
820 error_get_pretty(local_err));
821 exit(EXIT_FAILURE);
822 }
823 }
824
thscd831bd2008-07-03 10:23:51 +0000825 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000826 int nbdfd = open(argv[optind], O_RDWR);
827 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100828 error_report("Cannot open %s: %s", argv[optind],
829 strerror(errno));
830 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100831 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000832 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000833
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000834 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000835
836 printf("%s disconnected\n", argv[optind]);
837
Peter Lieven713cc672014-08-13 19:20:19 +0200838 return 0;
thscd831bd2008-07-03 10:23:51 +0000839 }
840
Max Reitzffb31e12016-09-28 22:46:42 +0200841 if ((device && !verbose) || fork_process) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100842 int stderr_fd[2];
843 pid_t pid;
844 int ret;
845
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100846 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100847 error_report("Error setting up communication pipe: %s",
848 strerror(errno));
849 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100850 }
851
852 /* Now daemonize, but keep a communication channel open to
853 * print errors and exit with the proper status code.
854 */
855 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500856 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100857 error_report("Failed to fork: %s", strerror(errno));
858 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500859 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100860 close(stderr_fd[0]);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400861 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100862
863 /* Temporarily redirect stderr to the parent's pipe... */
Max Reitzffb31e12016-09-28 22:46:42 +0200864 old_stderr = dup(STDERR_FILENO);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100865 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100866 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100867 error_report("Failed to daemonize: %s", strerror(errno));
868 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100869 }
870
871 /* ... close the descriptor we inherited and go on. */
872 close(stderr_fd[1]);
873 } else {
874 bool errors = false;
875 char *buf;
876
877 /* In the parent. Print error messages from the child until
878 * it closes the pipe.
879 */
880 close(stderr_fd[1]);
881 buf = g_malloc(1024);
882 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
883 errors = true;
884 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100885 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100886 exit(EXIT_FAILURE);
887 }
888 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100889 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100890 error_report("Cannot read from daemon: %s",
891 strerror(errno));
892 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100893 }
894
895 /* Usually the daemon should not print any message.
896 * Exit with zero status in that case.
897 */
898 exit(errors);
899 }
900 }
901
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100902 if (device != NULL && sockpath == NULL) {
903 sockpath = g_malloc(128);
904 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000905 }
906
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000907 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000908 if (socket_activation == 0) {
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000909 saddr = nbd_build_socket_address(sockpath, bindto, port);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000910 if (qio_net_listener_open_sync(server, saddr, &local_err) < 0) {
911 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000912 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000913 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000914 }
915 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000916 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000917 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000918 for (i = 0; i < socket_activation; i++) {
919 QIOChannelSocket *sioc;
920 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
921 &local_err);
922 if (sioc == NULL) {
923 object_unref(OBJECT(server));
924 error_report("Failed to use socket activation: %s",
925 error_get_pretty(local_err));
926 exit(EXIT_FAILURE);
927 }
928 qio_net_listener_add(server, sioc);
929 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000930 }
931 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100932
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300933 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100934 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300935 exit(EXIT_FAILURE);
936 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100937 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200938 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100939
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000940 srcpath = argv[optind];
941 if (imageOpts) {
942 QemuOpts *opts;
943 if (fmt) {
944 error_report("--image-opts and -f are mutually exclusive");
945 exit(EXIT_FAILURE);
946 }
947 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
948 if (!opts) {
949 qemu_opts_reset(&file_opts);
950 exit(EXIT_FAILURE);
951 }
952 options = qemu_opts_to_qdict(opts, NULL);
953 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +0100954 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000955 } else {
956 if (fmt) {
957 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -0500958 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000959 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100960 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000961 }
962
Max Reitz4fbec262015-02-05 13:58:19 -0500963 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100964 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
965 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100966 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100967 }
Max Reitz4fbec262015-02-05 13:58:19 -0500968 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100969
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100970 blk_set_enable_write_cache(blk, !writethrough);
971
Wenchao Xia8c116b02013-12-04 17:10:55 +0800972 if (sn_opts) {
973 ret = bdrv_snapshot_load_tmp(bs,
974 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
975 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
976 &local_err);
977 } else if (sn_id_or_name) {
978 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
979 &local_err);
980 }
981 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100982 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100983 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800984 }
985
Peter Lievenb3838a42014-08-13 19:20:18 +0200986 bs->detect_zeroes = detect_zeroes;
Max Reitz4c58e802014-11-18 12:21:19 +0100987 fd_size = blk_getlength(blk);
Max Reitz98f44bb2015-02-25 13:08:21 -0500988 if (fd_size < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100989 error_report("Failed to determine the image length: %s",
990 strerror(-fd_size));
991 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -0500992 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100993
Tomáš Golembiovskýe424b652016-10-05 23:40:20 +0200994 if (dev_offset >= fd_size) {
995 error_report("Offset (%lld) has to be smaller than the image size "
996 "(%lld)",
997 (long long int)dev_offset, (long long int)fd_size);
998 exit(EXIT_FAILURE);
999 }
1000 fd_size -= dev_offset;
1001
Paolo Bonzini185b4332012-03-05 08:56:10 +01001002 if (partition != -1) {
Max Reitz4c58e802014-11-18 12:21:19 +01001003 ret = find_partition(blk, partition, &dev_offset, &fd_size);
Paolo Bonzini185b4332012-03-05 08:56:10 +01001004 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001005 error_report("Could not find partition %d: %s", partition,
Markus Armbrustera4699e52015-12-18 16:35:10 +01001006 strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +01001007 exit(EXIT_FAILURE);
Paolo Bonzini185b4332012-03-05 08:56:10 +01001008 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001009 }
1010
Kevin Wolfcd7fca92016-07-06 11:22:39 +02001011 exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed,
1012 writethrough, NULL, &local_err);
Max Reitz98f44bb2015-02-25 13:08:21 -05001013 if (!exp) {
Markus Armbruster4fffeb52015-12-18 16:35:05 +01001014 error_report_err(local_err);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001015 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -05001016 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +00001017 if (export_name) {
1018 nbd_export_set_name(exp, export_name);
Eric Blakeb1a75b32016-10-14 13:33:03 -05001019 nbd_export_set_description(exp, export_description);
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +00001020 newproto = true;
Eric Blakeb1a75b32016-10-14 13:33:03 -05001021 } else if (export_description) {
1022 error_report("Export description requires an export name");
1023 exit(EXIT_FAILURE);
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +00001024 }
ths3b05a8e2008-07-03 12:45:02 +00001025
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001026 if (device) {
1027 int ret;
1028
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001029 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001030 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001031 error_report("Failed to create client thread: %s", strerror(ret));
1032 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001033 }
1034 } else {
1035 /* Shut up GCC warnings. */
1036 memset(&client_thread, 0, sizeof(client_thread));
1037 }
1038
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001039 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001040
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001041 /* now when the initialization is (almost) complete, chdir("/")
1042 * to free any busy filesystems */
1043 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001044 error_report("Could not chdir to root directory: %s",
1045 strerror(errno));
1046 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001047 }
1048
Max Reitzffb31e12016-09-28 22:46:42 +02001049 if (fork_process) {
1050 dup2(old_stderr, STDERR_FILENO);
1051 close(old_stderr);
1052 }
1053
Paolo Bonzini7860a382012-09-18 13:31:56 +02001054 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001055 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001056 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001057 if (state == TERMINATE) {
1058 state = TERMINATING;
1059 nbd_export_close(exp);
1060 nbd_export_put(exp);
1061 exp = NULL;
1062 }
1063 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001064
Markus Armbruster26f54e92014-10-07 13:59:04 +02001065 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001066 if (sockpath) {
1067 unlink(sockpath);
1068 }
bellard7a5ca862008-05-27 21:13:40 +00001069
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001070 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001071
Paolo Bonzinia517e882011-11-04 15:51:21 +01001072 if (device) {
1073 void *ret;
1074 pthread_join(client_thread, &ret);
1075 exit(ret != NULL);
1076 } else {
1077 exit(EXIT_SUCCESS);
1078 }
bellard7a5ca862008-05-27 21:13:40 +00001079}