4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 static struct volume_group
*vg_rename_old(struct cmd_context
*cmd
,
21 const char *vg_name_old
,
24 struct volume_group
*vg
;
26 /* FIXME we used to print an error about EXPORTED, but proceeded
28 vg
= vg_read_for_update(cmd
, vg_name_old
, vgid
, READ_ALLOW_EXPORTED
);
29 if (vg_read_error(vg
)) {
34 if (lvs_in_vg_activated_by_uuid_only(vg
)) {
35 unlock_and_release_vg(cmd
, vg
, vg_name_old
);
36 log_error("Volume group \"%s\" still has active LVs",
38 /* FIXME Remove this restriction */
44 static int vg_rename_new(struct cmd_context
*cmd
,
45 const char *vg_name_new
)
49 log_verbose("Checking for new volume group \"%s\"", vg_name_new
);
51 rc
= vg_lock_newname(cmd
, vg_name_new
);
53 if (rc
== FAILED_LOCKING
) {
54 log_error("Can't get lock for %s", vg_name_new
);
58 if (rc
== FAILED_EXIST
) {
59 log_error("New volume group \"%s\" already exists",
66 static int vg_rename_path(struct cmd_context
*cmd
, const char *old_vg_path
,
67 const char *new_vg_path
)
73 struct dm_list
*vgids
;
76 const char *vgid
= NULL
, *vg_name
, *vg_name_old
;
77 char old_path
[NAME_LEN
], new_path
[NAME_LEN
];
78 struct volume_group
*vg
= NULL
;
79 int lock_vg_old_first
= 1;
81 vg_name_old
= skip_dev_dir(cmd
, old_vg_path
, NULL
);
82 vg_name_new
= skip_dev_dir(cmd
, new_vg_path
, NULL
);
84 dev_dir
= cmd
->dev_dir
;
86 if (!validate_vg_rename_params(cmd
, vg_name_old
, vg_name_new
))
89 log_verbose("Checking for existing volume group \"%s\"", vg_name_old
);
91 /* Avoid duplicates */
92 if (!(vgids
= get_vgids(cmd
, 0)) || dm_list_empty(vgids
)) {
93 log_error("No complete volume groups found");
97 dm_list_iterate_items(sl
, vgids
) {
99 if (!vgid
|| !(vg_name
= vgname_from_vgid(NULL
, vgid
)) ||
100 is_orphan_vg(vg_name
))
102 if (!strcmp(vg_name
, vg_name_old
)) {
104 log_error("Found more than one VG called %s. "
105 "Please supply VG uuid.", vg_name_old
);
113 found_id
= id_read_format(&id
, vg_name_old
);
115 if (found_id
&& (vg_name
= vgname_from_vgid(cmd
->mem
, (char *)id
.uuid
))) {
116 vg_name_old
= vg_name
;
117 vgid
= (char *)id
.uuid
;
121 if (strcmp(vg_name_new
, vg_name_old
) < 0)
122 lock_vg_old_first
= 0;
124 if (lock_vg_old_first
) {
125 vg
= vg_rename_old(cmd
, vg_name_old
, vgid
);
129 if (!vg_rename_new(cmd
, vg_name_new
)) {
130 unlock_and_release_vg(cmd
, vg
, vg_name_old
);
134 if (!vg_rename_new(cmd
, vg_name_new
)) {
138 vg
= vg_rename_old(cmd
, vg_name_old
, vgid
);
146 /* Remove references based on old name */
147 drop_cached_metadata(vg
);
149 /* Change the volume group name */
150 vg_rename(cmd
, vg
, vg_name_new
);
152 /* store it on disks */
153 log_verbose("Writing out updated volume group");
154 if (!vg_write(vg
) || !vg_commit(vg
)) {
158 sprintf(old_path
, "%s%s", dev_dir
, vg_name_old
);
159 sprintf(new_path
, "%s%s", dev_dir
, vg_name_new
);
161 if (activation() && dir_exists(old_path
)) {
162 log_verbose("Renaming \"%s\" to \"%s\"", old_path
, new_path
);
165 log_verbose("Test mode: Skipping rename.");
167 else if (lvs_in_vg_activated_by_uuid_only(vg
)) {
168 if (!vg_refresh_visible(cmd
, vg
)) {
169 log_error("Renaming \"%s\" to \"%s\" failed",
176 /******* FIXME Rename any active LVs! *****/
179 backup_remove(cmd
, vg_name_old
);
181 unlock_vg(cmd
, vg_name_new
);
182 unlock_and_release_vg(cmd
, vg
, vg_name_old
);
184 log_print("Volume group \"%s\" successfully renamed to \"%s\"",
185 vg_name_old
, vg_name_new
);
187 /* FIXME lvmcache corruption - vginfo duplicated instead of renamed */
188 persistent_filter_wipe(cmd
->filter
);
189 lvmcache_destroy(cmd
, 1);
194 if (lock_vg_old_first
) {
195 unlock_vg(cmd
, vg_name_new
);
196 unlock_and_release_vg(cmd
, vg
, vg_name_old
);
198 unlock_and_release_vg(cmd
, vg
, vg_name_old
);
199 unlock_vg(cmd
, vg_name_new
);
204 int vgrename(struct cmd_context
*cmd
, int argc
, char **argv
)
207 log_error("Old and new volume group names need specifying");
208 return EINVALID_CMD_LINE
;
211 if (!vg_rename_path(cmd
, argv
[0], argv
[1])) {
216 return ECMD_PROCESSED
;