[sheepdog] [PATCH 3/5] dog: upgrade command for config file

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Sun Jul 12 07:54:30 CEST 2015


This patch adds a new upgrade command for config file. It can be used
as below:
$ dog upgrade config-convert <original config file path> <new file path>

Cc: Masahiro Tsuji <tuji at atworks.co.jp>
Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 dog/upgrade.c   | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/sheep.h | 15 +++++++++++++
 sheep/config.c  | 15 +------------
 3 files changed, 85 insertions(+), 14 deletions(-)

diff --git a/dog/upgrade.c b/dog/upgrade.c
index d1b55a4..15551a9 100644
--- a/dog/upgrade.c
+++ b/dog/upgrade.c
@@ -32,6 +32,71 @@ static struct upgrade_cmd_data {
 	enum orig_version orig;
 } upgrade_cmd_data = { ~0, };
 
+static int upgrade_config_convert(int argc, char **argv)
+{
+	const char *orig_file = argv[optind++], *dst_file = NULL;
+	struct stat config_stat;
+	int fd, ret, new_fd;
+	struct sheepdog_config config;
+
+	BUILD_BUG_ON(sizeof(config) != SD_CONFIG_SIZE);
+
+	if (optind < argc)
+		dst_file = argv[optind++];
+	else {
+		sd_info("please specify destination file path");
+		return EXIT_USAGE;
+	}
+
+	fd = open(orig_file, O_RDONLY);
+	if (fd < 0) {
+		sd_err("failed to open config file: %m");
+		return EXIT_SYSFAIL;
+	}
+
+	memset(&config_stat, 0, sizeof(config_stat));
+	ret = fstat(fd, &config_stat);
+	if (ret < 0) {
+		sd_err("failed to stat config file: %m");
+		return EXIT_SYSFAIL;
+	}
+
+	if (config_stat.st_size != SD_CONFIG_SIZE) {
+		sd_err("original config file has invalid size: %lu",
+		       config_stat.st_size);
+		return EXIT_USAGE;
+	}
+
+	ret = xread(fd, &config, sizeof(config));
+	if (ret != sizeof(config)) {
+		sd_err("failed to read config file: %m");
+		return EXIT_SYSFAIL;
+	}
+
+	if (!(config.version == 0x0002 || config.version == 0x0004)) {
+		/* 0x0002: v0.7.x, 0x0004: v0.8.x */
+		sd_err("unknown version config file: %x", config.version);
+		return EXIT_USAGE;
+	}
+
+	config.block_size_shift = SD_DEFAULT_BLOCK_SIZE_SHIFT;
+	config.version = 0x0006;
+
+	new_fd = open(dst_file, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
+	if (new_fd < 0) {
+		sd_err("failed to create a new config file: %m");
+		return EXIT_SYSFAIL;
+	}
+
+	ret = xwrite(new_fd, &config, sizeof(config));
+	if (ret != sizeof(config)) {
+		sd_err("failed to write to a new config file: %m");
+		return EXIT_SYSFAIL;
+	}
+
+	return EXIT_SUCCESS;
+}
+
 static int upgrade_epoch_convert(int argc, char **argv)
 {
 	const char *orig_file = argv[optind++], *dst_file = NULL;
@@ -265,6 +330,10 @@ static struct subcommand upgrade_cmd[] = {
 	 " <path of new epoch log file>",
 	 "hTo", "upgrade epoch log file",
 	 NULL, CMD_NEED_ARG, upgrade_epoch_convert, upgrade_options},
+	{"config-convert", "<path of original config file>"
+	 " <path of new config file>",
+	 "hT", "upgrade config file",
+	 NULL, CMD_NEED_ARG, upgrade_config_convert, upgrade_options},
 	{NULL,},
 };
 
diff --git a/include/sheep.h b/include/sheep.h
index 43f692f..7cab254 100644
--- a/include/sheep.h
+++ b/include/sheep.h
@@ -350,4 +350,19 @@ static inline __attribute__((used)) void __sd_proto_build_bug_ons(void)
 	BUILD_BUG_ON(sizeof(struct sd_rsp) != SD_RSP_SIZE);
 }
 
+#define SD_FORMAT_VERSION 0x0006
+#define SD_CONFIG_SIZE 40
+
+struct sheepdog_config {
+	uint64_t ctime;
+	uint16_t flags;
+	uint8_t copies;
+	uint8_t store[STORE_LEN];
+	uint8_t shutdown;
+	uint8_t copy_policy;
+	uint8_t block_size_shift;
+	uint16_t version;
+	uint64_t space;
+};
+
 #endif
diff --git a/sheep/config.c b/sheep/config.c
index 9518109..5eff7e5 100644
--- a/sheep/config.c
+++ b/sheep/config.c
@@ -11,20 +11,7 @@
 
 #include "sheep_priv.h"
 
-#define SD_FORMAT_VERSION 0x0006
-#define SD_CONFIG_SIZE 40
-
-static struct sheepdog_config {
-	uint64_t ctime;
-	uint16_t flags;
-	uint8_t copies;
-	uint8_t store[STORE_LEN];
-	uint8_t shutdown;
-	uint8_t copy_policy;
-	uint8_t block_size_shift;
-	uint16_t version;
-	uint64_t space;
-} config;
+static struct sheepdog_config config;
 
 char *config_path;
 
-- 
1.9.1



More information about the sheepdog mailing list