From 2266a1863f048899dc6f54c4d62ae05b33605349 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Mon, 21 Oct 2019 09:16:45 +0200 Subject: [PATCH] lv_manip: add lv_uniq_rename_update Add function to rename LV to either passed name or if the name is already in use, generate new lvol% name. --- lib/metadata/lv_manip.c | 24 ++++++++++++++++++++++++ lib/metadata/metadata-exported.h | 2 ++ 2 files changed, 26 insertions(+) diff --git a/lib/metadata/lv_manip.c b/lib/metadata/lv_manip.c index 815ea9976..ae103d31a 100644 --- a/lib/metadata/lv_manip.c +++ b/lib/metadata/lv_manip.c @@ -4625,6 +4625,30 @@ int lv_rename_update(struct cmd_context *cmd, struct logical_volume *lv, return 1; } +/* + * Rename LV to new name, if name is occupies, lvol% is generated. + * VG must be locked by caller. + */ +int lv_uniq_rename_update(struct cmd_context *cmd, struct logical_volume *lv, + const char *new_name, int update_mda) +{ + char uniq_name[NAME_LEN]; + + /* If the name is in use, generate new lvol%d */ + if (lv_name_is_used_in_vg(lv->vg, new_name, NULL)) { + if (!generate_lv_name(lv->vg, "lvol%d", uniq_name, sizeof(uniq_name))) { + log_error("Failed to generate unique name for unused logical volume."); + return 0; + } + new_name = uniq_name; + } + + if (!lv_rename_update(cmd, lv, new_name, 0)) + return_0; + + return 1; +} + /* * Core of LV renaming routine. * VG must be locked by caller. diff --git a/lib/metadata/metadata-exported.h b/lib/metadata/metadata-exported.h index f34009319..c61c85c7e 100644 --- a/lib/metadata/metadata-exported.h +++ b/lib/metadata/metadata-exported.h @@ -845,6 +845,8 @@ int lv_rename(struct cmd_context *cmd, struct logical_volume *lv, const char *new_name); int lv_rename_update(struct cmd_context *cmd, struct logical_volume *lv, const char *new_name, int update_mda); +int lv_uniq_rename_update(struct cmd_context *cmd, struct logical_volume *lv, + const char *new_name, int update_mda); /* Updates and reloads metadata for given lv */ int lv_update_and_reload(struct logical_volume *lv);