diff --git a/docs/usage/cli.en.md b/docs/usage/cli.en.md index 8992be5d..9ad325d2 100644 --- a/docs/usage/cli.en.md +++ b/docs/usage/cli.en.md @@ -165,7 +165,7 @@ 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. If the new size is smaller than the old size, extra data will be purged. @@ -174,6 +174,8 @@ 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 7f0ed964..6545eb08 100644 --- a/docs/usage/cli.ru.md +++ b/docs/usage/cli.ru.md @@ -170,7 +170,7 @@ 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` нельзя. @@ -181,6 +181,8 @@ vitastor-cli snap-create [ОПЦИИ] @ * `--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 27cd8d43..2e313fc1 100644 --- a/src/cmd/cli_modify.cpp +++ b/src/cmd/cli_modify.cpp @@ -21,6 +21,7 @@ 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; @@ -28,6 +29,8 @@ struct image_changer_t inode_config_t cfg; json11::Json::array checks, success; bool has_children = false; + std::vector snaps; + size_t snap_i = 0; int state = 0; std::function cb; @@ -44,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" }; @@ -105,6 +110,20 @@ struct image_changer_t state = 100; return; } + 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 = "Image "+ic.second.name+" already exists" }; + state = 100; + return; + } + if (ic.second.name.rfind(image_name+"@", 0) == 0) + snaps.push_back(ic.second.name); + } + } if ((new_size != 0 || force_size) && (cfg.size < new_size || !inc_size)) { if (cfg.size >= new_size) @@ -272,6 +291,26 @@ resume_2: parent->cli->st_cli->inode_by_name.erase(image_name); } parent->cli->st_cli->insert_inode_config(cfg); + for (; snap_i < snaps.size(); snap_i++) + { + 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, .text = "Image "+image_name+" modified", @@ -314,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/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..c66efa39 --- /dev/null +++ b/tests/test_modify_image_rename.sh @@ -0,0 +1,44 @@ +#!/bin/bash -ex + +# Test that modify --rename --with-snapshots renames snapshots @* with the image + +. `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 --with-snapshots + +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" + +# A taken @* refuses the rename and changes nothing +$VITASTOR_CLI modify testimg2 --rename ghost@old +r=0 +$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 +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