From d9401b0bd25283250dfbfa5b81ea05ed6f477902 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 20:23:58 +0300 Subject: [PATCH 1/2] Rename snapshots together with the image in vitastor-cli modify Snapshots are separate inodes named @, tied to their image only by that name. `modify --rename` renamed the image alone, so its snapshots kept the old name: they no longer showed up as the image's snapshots (the OpenStack driver looks them up as @), and a new image created under the old name would look like their owner. Rename every @* inode to @* in the same etcd transaction as the image, and refuse the rename if any @* already exists. As a side effect, `--rename ` together with another change no longer fails with "duplicate key given in txn request". --- docs/usage/cli.en.md | 2 + docs/usage/cli.ru.md | 3 + src/cmd/cli_modify.cpp | 148 ++++++++++++++++++++---------- tests/run_tests.sh | 1 + tests/test_modify_image_rename.sh | 43 +++++++++ 5 files changed, 146 insertions(+), 51 deletions(-) create mode 100755 tests/test_modify_image_rename.sh diff --git a/docs/usage/cli.en.md b/docs/usage/cli.en.md index 8992be5d..cff3750b 100644 --- a/docs/usage/cli.en.md +++ b/docs/usage/cli.en.md @@ -168,6 +168,8 @@ See also about [how to export snapshots](qemu.en.md#exporting-snapshots). `vitastor-cli modify [--rename ] [--resize ] [--readonly | --readwrite] [-f|--force] [--down-ok]` Rename, resize image or change its readonly status. Images with children can't be made read-write. +Renaming an image also renames its snapshots `@` to `@` in the same +etcd transaction, and is refused if a `@*` image already exists. If the new size is smaller than the old size, extra data will be purged. You should resize file system in the image, if present, before shrinking it. diff --git a/docs/usage/cli.ru.md b/docs/usage/cli.ru.md index 7f0ed964..ae843fa3 100644 --- a/docs/usage/cli.ru.md +++ b/docs/usage/cli.ru.md @@ -175,6 +175,9 @@ vitastor-cli snap-create [ОПЦИИ] @ Изменить размер, имя образа или флаг "только для чтения". Снимать флаг "только для чтения" и уменьшать размер образов, у которых есть дочерние клоны, без `--force` нельзя. +Вместе с образом переименовываются его снимки: `@` становится `@` +в той же транзакции etcd. Если образ `@*` уже есть, переименование отклоняется. + Если новый размер меньше старого, "лишние" данные будут удалены, поэтому перед уменьшением образа сначала уменьшите файловую систему в нём. diff --git a/src/cmd/cli_modify.cpp b/src/cmd/cli_modify.cpp index 27cd8d43..eb2b3d76 100644 --- a/src/cmd/cli_modify.cpp +++ b/src/cmd/cli_modify.cpp @@ -26,6 +26,8 @@ struct image_changer_t uint64_t inode_num = 0; inode_config_t cfg; + // Snapshots @* renamed together with the image, with their old names + std::vector snaps; json11::Json::array checks, success; bool has_children = false; @@ -105,6 +107,28 @@ struct image_changer_t state = 100; return; } + if (new_name != "" && new_name != image_name && image_name.find('@') == std::string::npos && new_name.find('@') == std::string::npos) + { + for (auto & ic: parent->cli->st_cli->inode_config) + { + if (ic.second.name.rfind(new_name+"@", 0) == 0) + { + result = (cli_result_t){ .err = EEXIST, .text = "Snapshot "+ic.second.name+" already exists, refusing to rename "+image_name+" to "+new_name }; + state = 100; + return; + } + if (ic.second.name.rfind(image_name+"@", 0) == 0) + { + if (!parent->check_image_perm(ic.second, true)) + { + result = (cli_result_t){ .err = EACCES, .text = "Snapshot "+ic.second.name+" permission denied" }; + state = 100; + return; + } + snaps.push_back(ic.second); + } + } + } if ((new_size != 0 || force_size) && (cfg.size < new_size || !inc_size)) { if (cfg.size >= new_size) @@ -192,58 +216,13 @@ resume_1: } cfg.enc_key = enc_key; } + add_inode_put(cfg, image_name); + // One txn: etcd's default --max-txn-ops=128 allows ~40 snapshots here + for (auto snap: snaps) { - std::string cur_cfg_key = base64_encode(parent->cli->st_cli->etcd_prefix+ - "/config/inode/"+std::to_string(INODE_POOL(inode_num))+ - "/"+std::to_string(INODE_NO_POOL(inode_num))); - checks.push_back(json11::Json::object { - { "target", "MOD" }, - { "key", cur_cfg_key }, - { "result", "LESS" }, - { "mod_revision", cfg.mod_revision+1 }, - }); - success.push_back(json11::Json::object { - { "request_put", json11::Json::object { - { "key", cur_cfg_key }, - { "value", base64_encode(json11::Json( - parent->cli->st_cli->serialize_inode_cfg(&cfg) - ).dump()) }, - } } - }); - } - if (new_name != "") - { - std::string old_idx_key = base64_encode( - parent->cli->st_cli->etcd_prefix+"/index/image/"+image_name - ); - std::string new_idx_key = base64_encode( - parent->cli->st_cli->etcd_prefix+"/index/image/"+new_name - ); - checks.push_back(json11::Json::object { - { "target", "MOD" }, - { "key", old_idx_key }, - { "result", "LESS" }, - { "mod_revision", cfg.mod_revision+1 }, - }); - checks.push_back(json11::Json::object { - { "target", "VERSION" }, - { "version", 0 }, - { "key", new_idx_key }, - }); - success.push_back(json11::Json::object { - { "request_delete_range", json11::Json::object { - { "key", old_idx_key }, - } } - }); - success.push_back(json11::Json::object { - { "request_put", json11::Json::object { - { "key", new_idx_key }, - { "value", base64_encode(json11::Json(json11::Json::object{ - { "id", INODE_NO_POOL(inode_num) }, - { "pool_id", (uint64_t)INODE_POOL(inode_num) }, - }).dump()) }, - } } - }); + std::string old_name = snap.name; + snap.name = snap_new_name(old_name); + add_inode_put(snap, old_name); } parent->etcd_txn(json11::Json::object { { "compare", checks }, @@ -272,6 +251,13 @@ resume_2: parent->cli->st_cli->inode_by_name.erase(image_name); } parent->cli->st_cli->insert_inode_config(cfg); + for (auto & snap: snaps) + { + parent->cli->st_cli->inode_by_name.erase(snap.name); + snap.name = snap_new_name(snap.name); + snap.mod_revision = cfg.mod_revision; + parent->cli->st_cli->insert_inode_config(snap); + } result = (cli_result_t){ .err = 0, .text = "Image "+image_name+" modified", @@ -279,6 +265,66 @@ resume_2: }; state = 100; } + + std::string snap_new_name(const std::string & old_name) + { + return new_name+old_name.substr(image_name.size()); + } + + // Put the inode config and move its /index/image key when old_name != c.name, guarded by CAS + void add_inode_put(inode_config_t & c, const std::string & old_name) + { + std::string cur_cfg_key = base64_encode(parent->cli->st_cli->etcd_prefix+ + "/config/inode/"+std::to_string(INODE_POOL(c.num))+ + "/"+std::to_string(INODE_NO_POOL(c.num))); + checks.push_back(json11::Json::object { + { "target", "MOD" }, + { "key", cur_cfg_key }, + { "result", "LESS" }, + { "mod_revision", c.mod_revision+1 }, + }); + success.push_back(json11::Json::object { + { "request_put", json11::Json::object { + { "key", cur_cfg_key }, + { "value", base64_encode(json11::Json( + parent->cli->st_cli->serialize_inode_cfg(&c) + ).dump()) }, + } } + }); + if (c.name == old_name) + return; + std::string old_idx_key = base64_encode( + parent->cli->st_cli->etcd_prefix+"/index/image/"+old_name + ); + std::string new_idx_key = base64_encode( + parent->cli->st_cli->etcd_prefix+"/index/image/"+c.name + ); + checks.push_back(json11::Json::object { + { "target", "MOD" }, + { "key", old_idx_key }, + { "result", "LESS" }, + { "mod_revision", c.mod_revision+1 }, + }); + checks.push_back(json11::Json::object { + { "target", "VERSION" }, + { "version", 0 }, + { "key", new_idx_key }, + }); + success.push_back(json11::Json::object { + { "request_delete_range", json11::Json::object { + { "key", old_idx_key }, + } } + }); + success.push_back(json11::Json::object { + { "request_put", json11::Json::object { + { "key", new_idx_key }, + { "value", base64_encode(json11::Json(json11::Json::object{ + { "id", INODE_NO_POOL(c.num) }, + { "pool_id", (uint64_t)INODE_POOL(c.num) }, + }).dump()) }, + } } + }); + } }; std::function cli_tool_t::start_modify(json11::Json cfg) diff --git a/tests/run_tests.sh b/tests/run_tests.sh index caac41b7..1479fcce 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -67,6 +67,7 @@ OLD=1 ./test_move_reappear.sh ./test_rm.sh ./test_modify_image_resize.sh +./test_modify_image_rename.sh ./test_rm_degraded.sh ./test_describe.sh diff --git a/tests/test_modify_image_rename.sh b/tests/test_modify_image_rename.sh new file mode 100755 index 00000000..d86c9e2a --- /dev/null +++ b/tests/test_modify_image_rename.sh @@ -0,0 +1,43 @@ +#!/bin/bash -ex + +# Test that renaming an image renames its snapshots @* with it + +. `dirname $0`/common.sh + +wait_etcd + +$ETCDCTL put /vitastor/config/pools '{"1":{"name":"testpool","scheme":"replicated","pg_size":2,"pg_minsize":1,"pg_count":1,"failure_domain":"osd"}}' + +$VITASTOR_CLI create -s 1G --pool testpool testimg +$VITASTOR_CLI snap-create testimg@snap1 +$VITASTOR_CLI snap-create testimg@snap2 +$VITASTOR_CLI create -s 1G --pool testpool testimg2 + +$VITASTOR_CLI modify testimg --rename newimg + +names=$($VITASTOR_CLI ls --json | jq -r '[.[].name] | sort | join(" ")') +[[ "$names" == "newimg newimg@snap1 newimg@snap2 testimg2" ]] || \ + format_error "Snapshots should follow the renamed image, got: $names" +idx=$($ETCDCTL get --prefix --keys-only /vitastor/index/image/ | grep . | sort | tr '\n' ' ') +[[ "$idx" == "/vitastor/index/image/newimg /vitastor/index/image/newimg@snap1 /vitastor/index/image/newimg@snap2 /vitastor/index/image/testimg2 " ]] || \ + format_error "Name index should follow the rename, got: $idx" +$VITASTOR_CLI ls --json | jq -e '[.[] | select(.name == "newimg" and .parent_name == "newimg@snap2")] | length == 1' || \ + format_error "newimg should keep newimg@snap2 as its parent" + +# The same name with another change is not a rename +$VITASTOR_CLI modify newimg --rename newimg --resize 2G +$ETCDCTL get --print-value-only /vitastor/config/inode/1/3 | jq -e '.name == "newimg" and .size == 2147483648' || \ + format_error "newimg should be resized to 2G" + +# A taken @* refuses the rename and changes nothing +$VITASTOR_CLI modify testimg2 --rename ghost@old +r=0 +$VITASTOR_CLI modify newimg --rename ghost || r=$? +if [[ $r != 17 ]]; then + format_error "Rename onto a name with existing snapshot ghost@old should fail with EEXIST (17), but exited with $r" +fi +names=$($VITASTOR_CLI ls --json | jq -r '[.[].name] | sort | join(" ")') +[[ "$names" == "ghost@old newimg newimg@snap1 newimg@snap2" ]] || \ + format_error "Refused rename should change nothing, got: $names" + +format_green OK -- 2.47.3 From b049e0f0545f0e809e1b4af611e652392b0af5e0 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 22:50:02 +0300 Subject: [PATCH 2/2] Rename snapshots in vitastor-cli modify only with --with-snapshots Keep the default rename as it was. With --with-snapshots, rename the snapshots @* to @* one by one after the image, through the usual single-image rename, instead of in one transaction. Still refuse the rename if any @* already exists. --- docs/usage/cli.en.md | 6 +- docs/usage/cli.ru.md | 7 +- src/cmd/cli.cpp | 5 +- src/cmd/cli_modify.cpp | 162 ++++++++++++++---------------- tests/test_modify_image_rename.sh | 17 ++-- 5 files changed, 97 insertions(+), 100 deletions(-) diff --git a/docs/usage/cli.en.md b/docs/usage/cli.en.md index cff3750b..9ad325d2 100644 --- a/docs/usage/cli.en.md +++ b/docs/usage/cli.en.md @@ -165,17 +165,17 @@ See also about [how to export snapshots](qemu.en.md#exporting-snapshots). ## modify -`vitastor-cli modify [--rename ] [--resize ] [--readonly | --readwrite] [-f|--force] [--down-ok]` +`vitastor-cli modify [--rename [--with-snapshots]] [--resize ] [--readonly | --readwrite] [-f|--force] [--down-ok]` Rename, resize image or change its readonly status. Images with children can't be made read-write. -Renaming an image also renames its snapshots `@` to `@` in the same -etcd transaction, and is refused if a `@*` image already exists. If the new size is smaller than the old size, extra data will be purged. You should resize file system in the image, if present, before shrinking it. * `--deleted 1|0` - Set/clear 'deleted image' flag (set automatically during unfinished deletes). * `-f|--force` - Proceed with shrinking or setting readwrite flag even if the image has children. * `--down-ok` - Proceed with shrinking even if some data will be left on unavailable OSDs. +* `--with-snapshots` - Also rename snapshots `@*` to `@*`, one by one after the image. + Refuse to rename if any `@*` already exists. * `--enc-key HEX` - Change image encryption key (allowed only with `--force`). * `--owner USERNAME` - Change image owner. * `--owner-group NAME` - Change image owner group name. diff --git a/docs/usage/cli.ru.md b/docs/usage/cli.ru.md index ae843fa3..6545eb08 100644 --- a/docs/usage/cli.ru.md +++ b/docs/usage/cli.ru.md @@ -170,20 +170,19 @@ vitastor-cli snap-create [ОПЦИИ] @ ## modify -`vitastor-cli modify [--rename ] [--resize ] [--readonly | --readwrite] [-f|--force] [--down-ok]` +`vitastor-cli modify [--rename [--with-snapshots]] [--resize ] [--readonly | --readwrite] [-f|--force] [--down-ok]` Изменить размер, имя образа или флаг "только для чтения". Снимать флаг "только для чтения" и уменьшать размер образов, у которых есть дочерние клоны, без `--force` нельзя. -Вместе с образом переименовываются его снимки: `@` становится `@` -в той же транзакции etcd. Если образ `@*` уже есть, переименование отклоняется. - Если новый размер меньше старого, "лишние" данные будут удалены, поэтому перед уменьшением образа сначала уменьшите файловую систему в нём. * `--deleted 1|0` - Установить/снять флаг "образ удалён" (устанавливается при незавершённом удалении). * `-f|--force` - Разрешить уменьшение или перевод в чтение-запись образа, у которого есть клоны. * `--down-ok` - Разрешить уменьшение, даже если часть данных останется неудалённой на недоступных OSD. +* `--with-snapshots` - Переименовать также снимки `@*` в `@*`, по одному после образа. + Если какой-либо `@*` уже есть, переименование отклоняется. * `--enc-key HEX` - Изменить ключ шифрования образа (разрешено только с `--force`). * `--owner USERNAME` - Изменить владельца образа. * `--owner-group NAME` - Изменить имя группы владельцев образа. diff --git a/src/cmd/cli.cpp b/src/cmd/cli.cpp index 9186bd98..eff8dd0d 100644 --- a/src/cmd/cli.cpp +++ b/src/cmd/cli.cpp @@ -62,13 +62,15 @@ static const char* help_text = " --enc-key KEY Change image encryption key to a specified key, Vault key or to an empty key.\n" " By default, the image retains its old key when taking a snapshot.\n" "\n" - "vitastor-cli modify [--rename ] [--resize ] [--readonly | --readwrite] [-f|--force] [--down-ok]\n" + "vitastor-cli modify [--rename [--with-snapshots]] [--resize ] [--readonly | --readwrite] [-f|--force] [--down-ok]\n" " Rename, resize image or change its readonly status. Images with children can't be made read-write.\n" " If the new size is smaller than the old size, extra data will be purged.\n" " You should resize file system in the image, if present, before shrinking it.\n" " --deleted 1|0 Set/clear 'deleted image' flag (set automatically during unfinished deletes).\n" " -f|--force Proceed with shrinking or setting readwrite flag even if the image has children.\n" " --down-ok Proceed with shrinking even if some data will be left on unavailable OSDs.\n" + " --with-snapshots Also rename snapshots @* to @*, one by one after the image.\n" + " Refuse to rename if any @* already exists.\n" " --enc-key HEX Change image encryption key (allowed only with --force).\n" " --owner username Change image owner.\n" " --owner_group name Change image owner group name.\n" @@ -341,6 +343,7 @@ static json11::Json::object parse_args(int narg, const char *args[]) !strcmp(opt, "allow-data-loss") || !strcmp(opt, "allow_data_loss") || !strcmp(opt, "allow-up") || !strcmp(opt, "allow_up") || !strcmp(opt, "down-ok") || !strcmp(opt, "down_ok") || + !strcmp(opt, "with-snapshots") || !strcmp(opt, "with_snapshots") || !strcmp(opt, "dry-run") || !strcmp(opt, "dry_run") || !strcmp(opt, "help") || !strcmp(opt, "all") || !strcmp(opt, "exact") || !strcmp(opt, "matching") || !strcmp(opt, "tree") || diff --git a/src/cmd/cli_modify.cpp b/src/cmd/cli_modify.cpp index eb2b3d76..2e313fc1 100644 --- a/src/cmd/cli_modify.cpp +++ b/src/cmd/cli_modify.cpp @@ -21,15 +21,16 @@ struct image_changer_t std::string enc_key; json11::Json new_owner, new_owner_group, new_reader_group; bool down_ok = false; + bool with_snapshots = false; // interval between fsyncs int fsync_interval = 128; uint64_t inode_num = 0; inode_config_t cfg; - // Snapshots @* renamed together with the image, with their old names - std::vector snaps; json11::Json::array checks, success; bool has_children = false; + std::vector snaps; + size_t snap_i = 0; int state = 0; std::function cb; @@ -46,6 +47,8 @@ struct image_changer_t goto resume_1; else if (state == 2) goto resume_2; + else if (state == 3) + goto resume_3; if (image_name == "") { result = (cli_result_t){ .err = EINVAL, .text = "Image name is missing" }; @@ -107,26 +110,18 @@ struct image_changer_t state = 100; return; } - if (new_name != "" && new_name != image_name && image_name.find('@') == std::string::npos && new_name.find('@') == std::string::npos) + if (with_snapshots && new_name != "" && new_name != image_name) { for (auto & ic: parent->cli->st_cli->inode_config) { if (ic.second.name.rfind(new_name+"@", 0) == 0) { - result = (cli_result_t){ .err = EEXIST, .text = "Snapshot "+ic.second.name+" already exists, refusing to rename "+image_name+" to "+new_name }; + result = (cli_result_t){ .err = EEXIST, .text = "Image "+ic.second.name+" already exists" }; state = 100; return; } if (ic.second.name.rfind(image_name+"@", 0) == 0) - { - if (!parent->check_image_perm(ic.second, true)) - { - result = (cli_result_t){ .err = EACCES, .text = "Snapshot "+ic.second.name+" permission denied" }; - state = 100; - return; - } - snaps.push_back(ic.second); - } + snaps.push_back(ic.second.name); } } if ((new_size != 0 || force_size) && (cfg.size < new_size || !inc_size)) @@ -216,13 +211,58 @@ resume_1: } cfg.enc_key = enc_key; } - add_inode_put(cfg, image_name); - // One txn: etcd's default --max-txn-ops=128 allows ~40 snapshots here - for (auto snap: snaps) { - std::string old_name = snap.name; - snap.name = snap_new_name(old_name); - add_inode_put(snap, old_name); + std::string cur_cfg_key = base64_encode(parent->cli->st_cli->etcd_prefix+ + "/config/inode/"+std::to_string(INODE_POOL(inode_num))+ + "/"+std::to_string(INODE_NO_POOL(inode_num))); + checks.push_back(json11::Json::object { + { "target", "MOD" }, + { "key", cur_cfg_key }, + { "result", "LESS" }, + { "mod_revision", cfg.mod_revision+1 }, + }); + success.push_back(json11::Json::object { + { "request_put", json11::Json::object { + { "key", cur_cfg_key }, + { "value", base64_encode(json11::Json( + parent->cli->st_cli->serialize_inode_cfg(&cfg) + ).dump()) }, + } } + }); + } + if (new_name != "") + { + std::string old_idx_key = base64_encode( + parent->cli->st_cli->etcd_prefix+"/index/image/"+image_name + ); + std::string new_idx_key = base64_encode( + parent->cli->st_cli->etcd_prefix+"/index/image/"+new_name + ); + checks.push_back(json11::Json::object { + { "target", "MOD" }, + { "key", old_idx_key }, + { "result", "LESS" }, + { "mod_revision", cfg.mod_revision+1 }, + }); + checks.push_back(json11::Json::object { + { "target", "VERSION" }, + { "version", 0 }, + { "key", new_idx_key }, + }); + success.push_back(json11::Json::object { + { "request_delete_range", json11::Json::object { + { "key", old_idx_key }, + } } + }); + success.push_back(json11::Json::object { + { "request_put", json11::Json::object { + { "key", new_idx_key }, + { "value", base64_encode(json11::Json(json11::Json::object{ + { "id", INODE_NO_POOL(inode_num) }, + { "pool_id", (uint64_t)INODE_POOL(inode_num) }, + }).dump()) }, + } } + }); } parent->etcd_txn(json11::Json::object { { "compare", checks }, @@ -251,12 +291,25 @@ resume_2: parent->cli->st_cli->inode_by_name.erase(image_name); } parent->cli->st_cli->insert_inode_config(cfg); - for (auto & snap: snaps) + for (; snap_i < snaps.size(); snap_i++) { - parent->cli->st_cli->inode_by_name.erase(snap.name); - snap.name = snap_new_name(snap.name); - snap.mod_revision = cfg.mod_revision; - parent->cli->st_cli->insert_inode_config(snap); + cb = parent->start_modify(json11::Json::object { + { "image", snaps[snap_i] }, + { "rename", new_name+snaps[snap_i].substr(image_name.size()) }, + }); +resume_3: + while (!cb(result)) + { + state = 3; + return; + } + cb = NULL; + if (result.err) + { + result.text = "Image "+image_name+" is renamed to "+new_name+", but its snapshot "+snaps[snap_i]+" is not: "+result.text; + state = 100; + return; + } } result = (cli_result_t){ .err = 0, @@ -265,66 +318,6 @@ resume_2: }; state = 100; } - - std::string snap_new_name(const std::string & old_name) - { - return new_name+old_name.substr(image_name.size()); - } - - // Put the inode config and move its /index/image key when old_name != c.name, guarded by CAS - void add_inode_put(inode_config_t & c, const std::string & old_name) - { - std::string cur_cfg_key = base64_encode(parent->cli->st_cli->etcd_prefix+ - "/config/inode/"+std::to_string(INODE_POOL(c.num))+ - "/"+std::to_string(INODE_NO_POOL(c.num))); - checks.push_back(json11::Json::object { - { "target", "MOD" }, - { "key", cur_cfg_key }, - { "result", "LESS" }, - { "mod_revision", c.mod_revision+1 }, - }); - success.push_back(json11::Json::object { - { "request_put", json11::Json::object { - { "key", cur_cfg_key }, - { "value", base64_encode(json11::Json( - parent->cli->st_cli->serialize_inode_cfg(&c) - ).dump()) }, - } } - }); - if (c.name == old_name) - return; - std::string old_idx_key = base64_encode( - parent->cli->st_cli->etcd_prefix+"/index/image/"+old_name - ); - std::string new_idx_key = base64_encode( - parent->cli->st_cli->etcd_prefix+"/index/image/"+c.name - ); - checks.push_back(json11::Json::object { - { "target", "MOD" }, - { "key", old_idx_key }, - { "result", "LESS" }, - { "mod_revision", c.mod_revision+1 }, - }); - checks.push_back(json11::Json::object { - { "target", "VERSION" }, - { "version", 0 }, - { "key", new_idx_key }, - }); - success.push_back(json11::Json::object { - { "request_delete_range", json11::Json::object { - { "key", old_idx_key }, - } } - }); - success.push_back(json11::Json::object { - { "request_put", json11::Json::object { - { "key", new_idx_key }, - { "value", base64_encode(json11::Json(json11::Json::object{ - { "id", INODE_NO_POOL(c.num) }, - { "pool_id", (uint64_t)INODE_POOL(c.num) }, - }).dump()) }, - } } - }); - } }; std::function cli_tool_t::start_modify(json11::Json cfg) @@ -360,6 +353,7 @@ std::function cli_tool_t::start_modify(json11::Json cfg) if (!changer->fsync_interval) changer->fsync_interval = 128; changer->down_ok = cfg["down_ok"].bool_value(); + changer->with_snapshots = cfg["with_snapshots"].bool_value(); changer->new_owner = cfg["owner"]; changer->new_owner_group = cfg["owner_group"]; changer->new_reader_group = cfg["reader_group"]; diff --git a/tests/test_modify_image_rename.sh b/tests/test_modify_image_rename.sh index d86c9e2a..c66efa39 100755 --- a/tests/test_modify_image_rename.sh +++ b/tests/test_modify_image_rename.sh @@ -1,6 +1,6 @@ #!/bin/bash -ex -# Test that renaming an image renames its snapshots @* with it +# Test that modify --rename --with-snapshots renames snapshots @* with the image . `dirname $0`/common.sh @@ -13,7 +13,7 @@ $VITASTOR_CLI snap-create testimg@snap1 $VITASTOR_CLI snap-create testimg@snap2 $VITASTOR_CLI create -s 1G --pool testpool testimg2 -$VITASTOR_CLI modify testimg --rename newimg +$VITASTOR_CLI modify testimg --rename newimg --with-snapshots names=$($VITASTOR_CLI ls --json | jq -r '[.[].name] | sort | join(" ")') [[ "$names" == "newimg newimg@snap1 newimg@snap2 testimg2" ]] || \ @@ -24,15 +24,10 @@ idx=$($ETCDCTL get --prefix --keys-only /vitastor/index/image/ | grep . | sort | $VITASTOR_CLI ls --json | jq -e '[.[] | select(.name == "newimg" and .parent_name == "newimg@snap2")] | length == 1' || \ format_error "newimg should keep newimg@snap2 as its parent" -# The same name with another change is not a rename -$VITASTOR_CLI modify newimg --rename newimg --resize 2G -$ETCDCTL get --print-value-only /vitastor/config/inode/1/3 | jq -e '.name == "newimg" and .size == 2147483648' || \ - format_error "newimg should be resized to 2G" - # A taken @* refuses the rename and changes nothing $VITASTOR_CLI modify testimg2 --rename ghost@old r=0 -$VITASTOR_CLI modify newimg --rename ghost || r=$? +$VITASTOR_CLI modify newimg --rename ghost --with-snapshots || r=$? if [[ $r != 17 ]]; then format_error "Rename onto a name with existing snapshot ghost@old should fail with EEXIST (17), but exited with $r" fi @@ -40,4 +35,10 @@ names=$($VITASTOR_CLI ls --json | jq -r '[.[].name] | sort | join(" ")') [[ "$names" == "ghost@old newimg newimg@snap1 newimg@snap2" ]] || \ format_error "Refused rename should change nothing, got: $names" +# Without --with-snapshots only the image is renamed +$VITASTOR_CLI modify newimg --rename otherimg +names=$($VITASTOR_CLI ls --json | jq -r '[.[].name] | sort | join(" ")') +[[ "$names" == "ghost@old newimg@snap1 newimg@snap2 otherimg" ]] || \ + format_error "Rename without --with-snapshots should keep snapshot names, got: $names" + format_green OK -- 2.47.3