From 150f3693467f1c0b0068c37ede51f9a8049e9b18 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Mon, 5 Sep 2022 10:15:08 +0300 Subject: [PATCH] Hotfixes for vitastor-disk prepare: max_other, get device size, older sfdisk --- src/disk_tool.h | 1 + src/disk_tool_prepare.cpp | 35 ++++++++++++++++++++++++++++------- src/disk_tool_utils.cpp | 17 ++++++++++++++--- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/disk_tool.h b/src/disk_tool.h index 956e0639..f69bf0b7 100644 --- a/src/disk_tool.h +++ b/src/disk_tool.h @@ -137,4 +137,5 @@ bool json_is_true(const json11::Json & val); int shell_exec(const std::vector & cmd, const std::string & in, std::string *out, std::string *err); 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); diff --git a/src/disk_tool_prepare.cpp b/src/disk_tool_prepare.cpp index d5a95da5..b9b60be7 100644 --- a/src/disk_tool_prepare.cpp +++ b/src/disk_tool_prepare.cpp @@ -207,11 +207,28 @@ std::vector disk_tool_t::collect_devices(const std::vector< if (errno == ENOENT) { fprintf(stderr, "%s does not exist, skipping\n", dev.c_str()); - return {}; + continue; } fprintf(stderr, "Error checking %s: %s\n", dev.c_str(), strerror(errno)); return {}; } + uint64_t dev_size = dev_st.st_size; + if (S_ISBLK(dev_st.st_mode)) + { + int fd = open(dev.c_str(), O_DIRECT|O_RDWR); + if (fd < 0) + { + fprintf(stderr, "Failed to open %s: %s\n", dev.c_str(), strerror(errno)); + return {}; + } + if (ioctl(fd, BLKGETSIZE64, &dev_size) < 0) + { + fprintf(stderr, "Failed to get %s size: %s\n", dev.c_str(), strerror(errno)); + close(fd); + return {}; + } + close(fd); + } if (stat(("/sys/block/"+dev.substr(5)).c_str(), &sys_st) < 0) { if (errno == ENOENT) @@ -251,8 +268,8 @@ std::vector disk_tool_t::collect_devices(const std::vector< .is_hdd = is_hdd, .pt = pt, .osd_part_count = osds, - .size = (uint64_t)dev_st.st_size, - .free = !pt.is_null() ? free_from_parttable(pt) : dev_st.st_size, + .size = !pt.is_null() ? dev_size_from_parttable(pt) : dev_size, + .free = !pt.is_null() ? free_from_parttable(pt) : dev_size, }); } if (!devinfo.size()) @@ -385,7 +402,7 @@ std::vector disk_tool_t::get_new_data_parts(vitastor_dev_info_t & d { want_parts = osd_per_disk; } - else if (dev.pt["partitions"].array_items().size() > 0) + else { // Disk already has partitions. If these are empty Vitastor OSD partitions, we can use them uint64_t osds_exist = 0, osds_size = 0; @@ -529,9 +546,13 @@ int disk_tool_t::prepare(std::vector devices) uint64_t osd_per_disk = stoull_full(options["osd_per_disk"]); if (!osd_per_disk) osd_per_disk = 1; - uint64_t max_other_percent = stoull_full(trim(options["max_other"], " \n\r\t%")); - if (max_other_percent > 100) - max_other_percent = 100; + uint64_t max_other_percent = 10; + if (options.find("max_other") != options.end()) + { + max_other_percent = stoull_full(trim(options["max_other"], " \n\r\t%")); + if (max_other_percent > 100) + max_other_percent = 100; + } std::vector ssds; if (options.find("disable_data_fsync") == options.end()) options["disable_data_fsync"] = "auto"; diff --git a/src/disk_tool_utils.cpp b/src/disk_tool_utils.cpp index cc5b76d2..c1a7731d 100644 --- a/src/disk_tool_utils.cpp +++ b/src/disk_tool_utils.cpp @@ -333,13 +333,24 @@ json11::Json read_parttable(std::string dev) return pt; } +uint64_t dev_size_from_parttable(json11::Json pt) +{ + uint64_t free = pt["lastlba"].uint64_value() + 1 - pt["firstlba"].uint64_value(); + if (!pt["sectorsize"].uint64_value()) + free *= 512; + else + free *= pt["sectorsize"].uint64_value(); + return free; +} + uint64_t free_from_parttable(json11::Json pt) { uint64_t free = pt["lastlba"].uint64_value() + 1 - pt["firstlba"].uint64_value(); for (const auto & part: pt["partitions"].array_items()) - { free -= part["size"].uint64_value(); - } - free *= pt["sectorsize"].uint64_value(); + if (!pt["sectorsize"].uint64_value()) + free *= 512; + else + free *= pt["sectorsize"].uint64_value(); return free; }