[sheepdog] [PATCH v3 13/15] sheepfs: add options to pass the address and port of the sheep

Liu Yuan namei.unix at gmail.com
Mon May 21 17:25:57 CEST 2012


From: Liu Yuan <tailai.ly at taobao.com>


Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheepfs/cluster.c |    5 ++++-
 sheepfs/core.c    |   21 ++++++++++++++++++++-
 sheepfs/node.c    |   10 ++++++++--
 sheepfs/sheepfs.h |    4 ++++
 sheepfs/vdi.c     |    5 ++++-
 sheepfs/volume.c  |    7 ++++---
 6 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/sheepfs/cluster.c b/sheepfs/cluster.c
index 1f645ba..39415f8 100644
--- a/sheepfs/cluster.c
+++ b/sheepfs/cluster.c
@@ -47,9 +47,12 @@ int cluster_info_read(const char *path, char *buf, size_t size, off_t ignore)
 
 size_t cluster_info_get_size(const char *path)
 {
-	struct strbuf *buf = sheepfs_run_cmd("collie cluster info");
+	struct strbuf *buf;
 	size_t len;
+	char cmd[COMMAND_LEN];
 
+	sprintf(cmd, "collie cluster info -a %s -p %d", sdhost, sdport);
+	buf = sheepfs_run_cmd(cmd);
 	if (!buf)
 		return 0;
 
diff --git a/sheepfs/core.c b/sheepfs/core.c
index 3a56333..5bd96ac 100644
--- a/sheepfs/core.c
+++ b/sheepfs/core.c
@@ -19,10 +19,12 @@
 #include <pthread.h>
 #include <getopt.h>
 #include <syslog.h>
+#include <stdlib.h>
 
 #include "strbuf.h"
 #include "util.h"
 #include "sheepfs.h"
+#include "sheepdog_proto.h"
 
 #define SH_OP_NAME   "user.sheepfs.opcode"
 #define SH_OP_SIZE   sizeof(uint32_t)
@@ -32,16 +34,20 @@ char sheepfs_shadow[PATH_MAX];
 static int sheepfs_debug;
 static int sheepfs_fg;
 int sheepfs_page_cache = 0;
+const char *sdhost = "localhost";
+int sdport = SD_LISTEN_PORT;
 
 static struct option const long_options[] = {
+	{"address", required_argument, NULL, 'a'},
 	{"debug", no_argument, NULL, 'd'},
 	{"help", no_argument, NULL, 'h'},
 	{"foreground", no_argument, NULL, 'f'},
 	{"pagecache", no_argument, NULL, 'k'},
+	{"port", required_argument, NULL, 'p'},
 	{NULL, 0, NULL, 0},
 };
 
-static const char *short_options = "dfhk";
+static const char *short_options = "a:dfhkp:";
 
 static struct sheepfs_file_operation {
 	int (*read)(const char *path, char *buf, size_t size, off_t);
@@ -254,9 +260,11 @@ static void usage(int inval)
 		printf("\
 Usage: sheepfs [OPTION]... MOUNTPOINT\n\
 Options:\n\
+  -a  --address           specify the sheep address (default: localhost)\n\
   -d, --debug             enable debug output (implies -f)\n\
   -f, --foreground        sheepfs run in the foreground\n\
   -k, --pagecache         use local kernel's page cache to access volume\n\
+  -p  --port              specify the sheep port (default: 7000)\n\
   -h, --help              display this help and exit\n\
 ");
 	exit(inval);
@@ -272,6 +280,9 @@ int main(int argc, char **argv)
 	while ((ch = getopt_long(argc, argv, short_options, long_options,
 				 &longindex)) >= 0) {
 		switch (ch) {
+		case 'a':
+			sdhost = optarg;
+			break;
 		case 'd':
 			sheepfs_debug = 1;
 			break;
@@ -284,6 +295,14 @@ int main(int argc, char **argv)
 		case 'k':
 			sheepfs_page_cache = 1;
 			break;
+		case 'p':
+			sdport = strtol(optarg, NULL, 10);
+			if (sdport < 1 || sdport > UINT16_MAX) {
+				fprintf(stderr,
+				"Invalid port number '%s'\n", optarg);
+				exit(1);
+			}
+			break;
 		default:
 			usage(1);
 		}
diff --git a/sheepfs/node.c b/sheepfs/node.c
index 328ee79..38472e7 100644
--- a/sheepfs/node.c
+++ b/sheepfs/node.c
@@ -54,9 +54,12 @@ int node_info_read(const char *path, char *buf, size_t size, off_t ignore)
 
 size_t node_info_get_size(const char *path)
 {
-	struct strbuf *buf = sheepfs_run_cmd("collie node info");
+	struct strbuf *buf;
 	size_t len;
+	char cmd[COMMAND_LEN];
 
+	sprintf(cmd, "collie node info -a %s -p %d", sdhost, sdport);
+	buf = sheepfs_run_cmd(cmd);
 	if (!buf)
 		return 0;
 
@@ -73,9 +76,12 @@ int node_list_read(const char *path, char *buf, size_t size, off_t ignore)
 
 size_t node_list_get_size(const char *path)
 {
-	struct strbuf *buf = sheepfs_run_cmd("collie node list");
+	struct strbuf *buf;
 	size_t len;
+	char cmd[COMMAND_LEN];
 
+	sprintf(cmd, "collie node list -a %s -p %d", sdhost, sdport);
+	buf = sheepfs_run_cmd(cmd);
 	if (!buf)
 		return 0;
 
diff --git a/sheepfs/sheepfs.h b/sheepfs/sheepfs.h
index b8eb6cd..c17ec55 100644
--- a/sheepfs/sheepfs.h
+++ b/sheepfs/sheepfs.h
@@ -14,8 +14,12 @@ enum sheepfs_opcode {
 	OP_VOLUME,
 };
 
+#define COMMAND_LEN  512
+
 extern char sheepfs_shadow[];
 extern int sheepfs_page_cache;
+extern const char *sdhost;
+extern int sdport;
 
 extern struct strbuf *sheepfs_run_cmd(const char *command);
 extern int sheepfs_set_op(const char *path, unsigned opcode);
diff --git a/sheepfs/vdi.c b/sheepfs/vdi.c
index ccb241c..980dfaa 100644
--- a/sheepfs/vdi.c
+++ b/sheepfs/vdi.c
@@ -59,9 +59,12 @@ int vdi_list_read(const char *path, char *buf, size_t size, off_t ignore)
 
 size_t vdi_list_get_size(const char *path)
 {
-	struct strbuf *buf = sheepfs_run_cmd("collie vdi list");
+	struct strbuf *buf;
 	size_t len;
+	char cmd[COMMAND_LEN];
 
+	sprintf(cmd, "collie vdi list -a %s -p %d", sdhost, sdport);
+	buf = sheepfs_run_cmd(cmd);
 	if (!buf)
 		return 0;
 
diff --git a/sheepfs/volume.c b/sheepfs/volume.c
index 8bce6e5..d56fcd1 100644
--- a/sheepfs/volume.c
+++ b/sheepfs/volume.c
@@ -336,7 +336,7 @@ static int setup_socket_pool(int array[], int len)
 	int fd, i, ret;
 
 	for (i = 0; i < len; i++) {
-		fd = connect_to("localhost", 7000);
+		fd = connect_to(sdhost, sdport);
 		if (fd < 0) {
 			syslog(LOG_ERR, "[%s] connect_to %m\n", __func__);
 			destroy_socket_pool(array, --i);
@@ -361,9 +361,10 @@ static int init_vdi_info(const char *entry, uint32_t *vid, size_t *size)
 	struct strbuf *buf;
 	void *inode_buf = NULL;
 	struct vdi_inode *inode = NULL, *dummy;
-	char command[256] = { 0 };
+	char command[COMMAND_LEN];
 
-	sprintf(command, "%s %s\n", "collie vdi list -r", entry);
+	sprintf(command, "collie vdi list -r %s -a %s -p %d",
+		entry, sdhost, sdport);
 	buf = sheepfs_run_cmd(command);
 	if (!buf)
 		return -1;
-- 
1.7.10.2




More information about the sheepdog mailing list