aboutsummaryrefslogtreecommitdiff
path: root/qemu-nbd.c
diff options
context:
space:
mode:
authorMax Reitz <mreitz@redhat.com>2016-09-28 22:46:42 +0200
committerKevin Wolf <kwolf@redhat.com>2016-10-24 17:54:03 +0200
commitffb31e1da7d3e044649717c4ebcb1768b5b44299 (patch)
treee925b31c89fc80a027cd36dd276480e8a85b2b75 /qemu-nbd.c
parenta26ddb43963e77aeebc2a4f011d27b2d9c017f21 (diff)
qemu-nbd: Add --fork option
Using the --fork option, one can make qemu-nbd fork the worker process. The original process will exit on error of the worker or once the worker enters the main loop. Suggested-by: Sascha Silbe <silbe@linux.vnet.ibm.com> Signed-off-by: Max Reitz <mreitz@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'qemu-nbd.c')
-rw-r--r--qemu-nbd.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/qemu-nbd.c b/qemu-nbd.c
index cca4a983b7..b757dc7621 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -48,6 +48,7 @@
#define QEMU_NBD_OPT_OBJECT 260
#define QEMU_NBD_OPT_TLSCREDS 261
#define QEMU_NBD_OPT_IMAGE_OPTS 262
+#define QEMU_NBD_OPT_FORK 263
#define MBR_SIZE 512
@@ -92,6 +93,8 @@ static void usage(const char *name)
" passwords and/or encryption keys\n"
" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
" specify tracing options\n"
+" --fork fork off the server process and exit the parent\n"
+" once the server is running\n"
#ifdef __linux__
"Kernel NBD client support:\n"
" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
@@ -503,6 +506,7 @@ int main(int argc, char **argv)
{ "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
{ "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
{ "trace", required_argument, NULL, 'T' },
+ { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
{ NULL, 0, NULL, 0 }
};
int ch;
@@ -524,6 +528,8 @@ int main(int argc, char **argv)
bool imageOpts = false;
bool writethrough = true;
char *trace_file = NULL;
+ bool fork_process = false;
+ int old_stderr = -1;
/* The client thread uses SIGTERM to interrupt the server. A signal
* handler ensures that "qemu-nbd -v -c" exits with a nice status code.
@@ -715,6 +721,9 @@ int main(int argc, char **argv)
g_free(trace_file);
trace_file = trace_opt_parse(optarg);
break;
+ case QEMU_NBD_OPT_FORK:
+ fork_process = true;
+ break;
}
}
@@ -774,7 +783,7 @@ int main(int argc, char **argv)
return 0;
}
- if (device && !verbose) {
+ if ((device && !verbose) || fork_process) {
int stderr_fd[2];
pid_t pid;
int ret;
@@ -797,6 +806,7 @@ int main(int argc, char **argv)
ret = qemu_daemon(1, 0);
/* Temporarily redirect stderr to the parent's pipe... */
+ old_stderr = dup(STDERR_FILENO);
dup2(stderr_fd[1], STDERR_FILENO);
if (ret < 0) {
error_report("Failed to daemonize: %s", strerror(errno));
@@ -960,6 +970,11 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE);
}
+ if (fork_process) {
+ dup2(old_stderr, STDERR_FILENO);
+ close(old_stderr);
+ }
+
state = RUNNING;
do {
main_loop_wait(false);