Add --with-snapshots to vitastor-cli modify --rename #191

Merged
vitalif merged 2 commits from koolkhel/vitastor:cli-rename-snapshots into master 2026-11-27 18:46:47 +03:00
6 changed files with 95 additions and 3 deletions

View File

@@ -165,7 +165,7 @@ See also about [how to export snapshots](qemu.en.md#exporting-snapshots).
## modify
`vitastor-cli modify <name> [--rename <new-name>] [--resize <size>] [--readonly | --readwrite] [-f|--force] [--down-ok]`
`vitastor-cli modify <name> [--rename <new-name> [--with-snapshots]] [--resize <size>] [--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 `<name>@*` to `<new-name>@*`, one by one after the image.
Refuse to rename if any `<new-name>@*` 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.

View File

@@ -170,7 +170,7 @@ vitastor-cli snap-create [ОПЦИИ] <image>@<snapshot>
## modify
`vitastor-cli modify <name> [--rename <new-name>] [--resize <size>] [--readonly | --readwrite] [-f|--force] [--down-ok]`
`vitastor-cli modify <name> [--rename <new-name> [--with-snapshots]] [--resize <size>] [--readonly | --readwrite] [-f|--force] [--down-ok]`
Изменить размер, имя образа или флаг "только для чтения". Снимать флаг "только для чтения"
и уменьшать размер образов, у которых есть дочерние клоны, без `--force` нельзя.
@@ -181,6 +181,8 @@ vitastor-cli snap-create [ОПЦИИ] <image>@<snapshot>
* `--deleted 1|0` - Установить/снять флаг "образ удалён" (устанавливается при незавершённом удалении).
* `-f|--force` - Разрешить уменьшение или перевод в чтение-запись образа, у которого есть клоны.
* `--down-ok` - Разрешить уменьшение, даже если часть данных останется неудалённой на недоступных OSD.
* `--with-snapshots` - Переименовать также снимки `<name>@*` в `<new-name>@*`, по одному после образа.
Если какой-либо `<new-name>@*` уже есть, переименование отклоняется.
* `--enc-key HEX` - Изменить ключ шифрования образа (разрешено только с `--force`).
* `--owner USERNAME` - Изменить владельца образа.
* `--owner-group NAME` - Изменить имя группы владельцев образа.

View File

@@ -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 <name> [--rename <new-name>] [--resize <size>] [--readonly | --readwrite] [-f|--force] [--down-ok]\n"
"vitastor-cli modify <name> [--rename <new-name> [--with-snapshots]] [--resize <size>] [--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 <name>@* to <new-name>@*, one by one after the image.\n"
" Refuse to rename if any <new-name>@* 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") ||

View File

@@ -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<std::string> snaps;
size_t snap_i = 0;
int state = 0;
std::function<bool(cli_result_t &)> 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<bool(cli_result_t &)> 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"];

View File

@@ -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

View File

@@ -0,0 +1,44 @@
#!/bin/bash -ex
# Test that modify --rename --with-snapshots renames snapshots <image>@* 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 <new name>@* 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