From: Chunyan Liu <cyliu@suse.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH] Add -f option to qemu-nbd
Date: Wed, 16 Nov 2011 14:57:49 +0800 [thread overview]
Message-ID: <1321426669-7337-1-git-send-email-cyliu@suse.com> (raw)
Currently qemu-nbd does not support finding free nbd device for users like
"losetup -f" and issuing "qemu-nbd -c /dev/nbdX disk.img" won't report error
message when /dev/nbd is already in use. It makes things a little confusing.
This patch adds "-f" option to qemu-nbd to support finding a free nbd device
for users. Please review and share your comments. Thanks.
Signed-off-by: Chunyan Liu <cyliu@suse.com>
---
qemu-nbd.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 64 insertions(+), 1 deletions(-)
diff --git a/qemu-nbd.c b/qemu-nbd.c
index 291cba2..5c43a68 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -244,6 +244,60 @@ out:
return (void *) EXIT_FAILURE;
}
+static int is_nbd_used(int minor)
+{
+ FILE *proc;
+ int NBDMAJOR = 43;
+ char buf[BUFSIZ];
+ int find = 0;
+
+ proc = fopen("/proc/partitions", "r");
+ if (proc != NULL) {
+ while (fgets(buf, sizeof(buf), proc)) {
+ int m, n;
+ unsigned long long sz;
+ char name[16];
+ char *pname = name;
+ char *end;
+
+ if (sscanf(buf, " %d %d %llu %128[^\n ]",
+ &m, &n, &sz, name) != 4)
+ continue;
+ if (m != NBDMAJOR)
+ continue;
+ if (strncmp(name, "nbd", 3))
+ continue;
+ pname += 3;
+ n = strtol(pname, &end, 10);
+ if (end && end != pname && *end == '\0' && n == minor) {
+ find = 1;
+ break;
+ }
+ }
+ fclose(proc);
+ }
+
+ return find;
+}
+
+static char *find_free_nbd_device(void)
+{
+ int i;
+ int nbds_max = 16;
+ char name[16];
+ char *devname = NULL;
+
+ for (i = 0; i < nbds_max; i++) {
+ if (!is_nbd_used(i)) {
+ snprintf(name, sizeof(name), "/dev/nbd%d", i);
+ devname = strdup(name);
+ break;
+ }
+ }
+
+ return devname;
+}
+
int main(int argc, char **argv)
{
BlockDriverState *bs;
@@ -256,7 +310,7 @@ int main(int argc, char **argv)
struct sockaddr_in addr;
socklen_t addr_len = sizeof(addr);
off_t fd_size;
- const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
+ const char *sopt = "hVb:o:p:rsnP:c:dvk:e:tf";
struct option lopt[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
@@ -273,6 +327,7 @@ int main(int argc, char **argv)
{ "shared", 1, NULL, 'e' },
{ "persistent", 0, NULL, 't' },
{ "verbose", 0, NULL, 'v' },
+ { "find", 0, NULL, 'f' },
{ NULL, 0, NULL, 0 }
};
int ch;
@@ -292,6 +347,7 @@ int main(int argc, char **argv)
int max_fd;
int persistent = 0;
pthread_t client_thread;
+ char *devname = NULL;
/* The client thread uses SIGTERM to interrupt the server. A signal
* handler ensures that "qemu-nbd -v -c" exits with a nice status code.
@@ -374,6 +430,13 @@ int main(int argc, char **argv)
case 'v':
verbose = 1;
break;
+ case 'f':
+ devname = find_free_nbd_device();
+ if (devname) {
+ printf("%s\n", devname);
+ }
+ exit(0);
+ break;
case 'V':
version(argv[0]);
exit(0);
--
1.7.3.4
next reply other threads:[~2011-11-16 6:58 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-11-16 6:57 Chunyan Liu [this message]
2011-11-16 10:34 ` [Qemu-devel] [PATCH] Add -f option to qemu-nbd Stefan Hajnoczi
2011-11-16 11:49 ` Zhi Yong Wu
2011-11-16 14:27 ` Stefan Hajnoczi
2011-11-17 4:38 ` Zhi Yong Wu
2011-11-16 17:23 ` Ian Campbell
2011-11-17 10:14 ` Stefan Hajnoczi
2011-11-17 11:34 ` Chun Yan Liu
2011-11-17 13:33 ` Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1321426669-7337-1-git-send-email-cyliu@suse.com \
--to=cyliu@suse.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).