vitastor-disk: make update-sb write only the given parameters and check them #188

Open
koolkhel wants to merge 4 commits from koolkhel/vitastor:disk-update-sb-checks into master
4 changed files with 44 additions and 7 deletions

View File

@@ -117,6 +117,7 @@ struct disk_tool_t
void dump_heap_entry(blockstore_heap_t *heap, heap_entry_t *obj);
int dump_load_check_superblock(const std::string & device);
int load_check_params(const json11::Json::object & params, bool check_lengths = false);
int write_json_journal(json11::Json entries);
int write_json_meta(json11::Json meta);

View File

@@ -247,15 +247,22 @@ int disk_tool_t::dump_load_check_superblock(const std::string & device)
json11::Json sb = read_osd_superblock(device, true, false);
if (sb.is_null())
return 1;
return load_check_params(sb["params"].object_items());
}
int disk_tool_t::load_check_params(const json11::Json::object & params, bool check_lengths)
{
try
{
auto cfg = json_to_string_map(sb["params"].object_items());
auto cfg = json_to_string_map(params);
dsk.parse_config(cfg);
dsk.data_io = dsk.meta_io = dsk.journal_io = "cached";
dsk.open_data();
dsk.open_meta();
dsk.open_journal();
dsk.calc_lengths();
if (check_lengths)
dsk.check_lengths();
}
catch (std::exception & e)
{

View File

@@ -90,18 +90,27 @@ int disk_tool_t::write_sb(std::string device)
int disk_tool_t::update_sb(std::string device)
{
json11::Json sb = read_osd_superblock(device, true, options.find("force") != options.end());
auto new_params = options;
bool force = new_params.erase("force") > 0;
// --io is the I/O mode of this tool, not an OSD parameter
new_params.erase("io");
json11::Json sb = read_osd_superblock(device, true, force);
if (sb.is_null())
{
return 1;
}
auto sb_obj = sb["params"].object_items();
for (auto & kv: options)
for (auto & kv: new_params)
{
if (kv.first != "force")
{
sb_obj[kv.first] = kv.second;
}
sb_obj[kv.first] = kv.second;
}
auto check_params = sb_obj;
// The OSD may be running and hold the device lock
check_params["disable_device_lock"] = "1";
if (!force && load_check_params(check_params, true) != 0)
{
fprintf(stderr, "Superblock not changed, use --force to write these parameters anyway\n");
return 1;
}
return !write_osd_superblock(device, sb_obj);
}

View File

@@ -179,4 +179,24 @@ fi
[[ "$(sudo -E build/src/disk_tool/vitastor-disk-test prepare --dry-run --json /dev/nonexistent | jq -c .devices)" == '[]' ]] || \
format_error "prepare --dry-run printed the wrong JSON without a suitable disk"
# update-sb should change only the given parameters
sudo build/src/disk_tool/vitastor-disk read-sb ${LOOP1}p1 | jq -S '. + {"journal_no_same_sector_overwrites": "true"}' > ./testdata/sb1.json
sudo build/src/disk_tool/vitastor-disk update-sb ${LOOP1}p1 --io cached --journal_no_same_sector_overwrites true
sudo build/src/disk_tool/vitastor-disk read-sb ${LOOP1}p1 | jq -S . > ./testdata/sb2.json
diff ./testdata/sb1.json ./testdata/sb2.json || format_error "update-sb changed parameters it wasn't given"
# ...refuse parameters the OSD can't start with, unless --force is given
if sudo build/src/disk_tool/vitastor-disk update-sb ${LOOP1}p1 --meta_block_size 4294967296; then
format_error "update-sb wrote a meta_block_size the OSD can't start with"
fi
if sudo build/src/disk_tool/vitastor-disk update-sb ${LOOP1}p1 --journal_size 1T; then
format_error "update-sb wrote a journal_size the OSD can't start with"
fi
sudo build/src/disk_tool/vitastor-disk read-sb ${LOOP1}p1 | jq -S . | diff ./testdata/sb2.json - || \
format_error "update-sb changed the superblock after refusing the parameters"
# ...also while the device is locked by a running OSD
sudo flock ${LOOP1}p1 build/src/disk_tool/vitastor-disk update-sb ${LOOP1}p1 --journal_no_same_sector_overwrites false || \
format_error "update-sb refused to change a locked device"
sudo build/src/disk_tool/vitastor-disk update-sb --force ${LOOP1}p1 --meta_block_size 4294967296 || \
format_error "update-sb --force refused to write the parameters"
format_green OK