From c72e8e649e9e57dda9883e316ea41303e9113ccf Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Tue, 5 Nov 2024 02:43:49 +0300 Subject: [PATCH] Support test mode for vitastor-disk --- src/disk_tool/disk_tool.cpp | 4 ++++ src/disk_tool/disk_tool.h | 4 +++- src/disk_tool/disk_tool_prepare.cpp | 25 +++++++++++++++++++++---- src/disk_tool/disk_tool_utils.cpp | 6 +++--- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/disk_tool/disk_tool.cpp b/src/disk_tool/disk_tool.cpp index f1ed13b4..c5a4eab9 100644 --- a/src/disk_tool/disk_tool.cpp +++ b/src/disk_tool/disk_tool.cpp @@ -224,6 +224,10 @@ int main(int argc, char *argv[]) cmd.push_back((char*)"dump-journal"); aliased = true; } + else if (!strcmp(exe_name, "vitastor-disk-test")) + { + self.test_mode = true; + } for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "--all")) diff --git a/src/disk_tool/disk_tool.h b/src/disk_tool/disk_tool.h index cd45412b..287432f1 100644 --- a/src/disk_tool/disk_tool.h +++ b/src/disk_tool/disk_tool.h @@ -41,6 +41,7 @@ struct disk_tool_t /**** Parameters ****/ std::map options; + bool test_mode = false; bool all, json, now; bool dump_with_blocks, dump_with_data; blockstore_disk_t dsk; @@ -128,6 +129,7 @@ struct disk_tool_t int prepare_one(std::map options, int is_hdd = -1); int check_existing_partition(const std::string & dev); + int fix_partition_type(const std::string & dev_by_uuid); int prepare(std::vector devices); std::vector collect_devices(const std::vector & devices); json11::Json add_partitions(vitastor_dev_info_t & devinfo, std::vector sizes); @@ -149,6 +151,6 @@ int write_zero(int fd, uint64_t offset, uint64_t size); json11::Json read_parttable(std::string dev); uint64_t dev_size_from_parttable(json11::Json pt); uint64_t free_from_parttable(json11::Json pt); -int fix_partition_type(std::string dev_by_uuid); +int fix_partition_type_uuid(std::string dev_by_uuid, const std::string & type_uuid); std::string csum_type_str(uint32_t data_csum_type); uint32_t csum_type_from_str(std::string data_csum_type); diff --git a/src/disk_tool/disk_tool_prepare.cpp b/src/disk_tool/disk_tool_prepare.cpp index 058483c6..1447d822 100644 --- a/src/disk_tool/disk_tool_prepare.cpp +++ b/src/disk_tool/disk_tool_prepare.cpp @@ -159,7 +159,11 @@ int disk_tool_t::prepare_one(std::map options, int is_ return 1; } std::string osd_num_str; - if (shell_exec({ "vitastor-cli", "alloc-osd" }, "", &osd_num_str, NULL) != 0) + if (test_mode && options.find("osd_num") != options.end()) + { + osd_num_str = options["osd_num"]; + } + else if (shell_exec({ "vitastor-cli", "alloc-osd" }, "", &osd_num_str, NULL) != 0) { dsk.close_all(); return 1; @@ -199,10 +203,13 @@ int disk_tool_t::prepare_one(std::map options, int is_ if (sep_j) desc += (sep_m ? " and journal on " : " with journal on ") + realpath_str(options["journal_device"]); fprintf(stderr, "Initialized OSD %ju on %s\n", osd_num, desc.c_str()); - if (shell_exec({ "systemctl", "enable", "--now", "vitastor-osd@"+std::to_string(osd_num) }, "", NULL, NULL) != 0) + if (!test_mode || options.find("no_init") == options.end()) { - fprintf(stderr, "Failed to enable systemd unit vitastor-osd@%ju\n", osd_num); - return 1; + if (shell_exec({ "systemctl", "enable", "--now", "vitastor-osd@"+std::to_string(osd_num) }, "", NULL, NULL) != 0) + { + fprintf(stderr, "Failed to enable systemd unit vitastor-osd@%ju\n", osd_num); + return 1; + } } return 0; } @@ -229,6 +236,16 @@ int disk_tool_t::check_existing_partition(const std::string & dev) return 0; } +int disk_tool_t::fix_partition_type(const std::string & dev) +{ + std::string type_uuid = VITASTOR_PART_TYPE; + if (test_mode && options.find("part_type_uuid") != options.end()) + { + type_uuid = options["part_type_uuid"]; + } + return fix_partition_type_uuid(dev, type_uuid); +} + std::vector disk_tool_t::collect_devices(const std::vector & devices) { std::vector devinfo; diff --git a/src/disk_tool/disk_tool_utils.cpp b/src/disk_tool/disk_tool_utils.cpp index 408eeef5..28318077 100644 --- a/src/disk_tool/disk_tool_utils.cpp +++ b/src/disk_tool/disk_tool_utils.cpp @@ -343,7 +343,7 @@ uint64_t free_from_parttable(json11::Json pt) return free; } -int fix_partition_type(std::string dev_by_uuid) +int fix_partition_type_uuid(std::string dev_by_uuid, const std::string & type_uuid) { auto uuid = strtolower(dev_by_uuid.substr(dev_by_uuid.rfind('/')+1)); std::string parent_dev = get_parent_device(realpath_str(dev_by_uuid, false)); @@ -356,7 +356,7 @@ int fix_partition_type(std::string dev_by_uuid) for (const auto & part: pt["partitions"].array_items()) { bool this_part = (strtolower(part["uuid"].string_value()) == uuid); - if (this_part && strtolower(part["type"].string_value()) == "e7009fac-a5a1-4d72-af72-53de13059903") + if (this_part && strtolower(part["type"].string_value()) == type_uuid) { // Already correct type return 0; @@ -369,7 +369,7 @@ int fix_partition_type(std::string dev_by_uuid) { script += (first ? "" : ", ")+kv.first+"="+ (kv.first == "type" && this_part - ? "e7009fac-a5a1-4d72-af72-53de13059903" + ? type_uuid : (kv.second.is_string() ? kv.second.string_value() : kv.second.dump())); first = false; }