]> sourceware.org Git - lvm2.git/blob - tools/vgrename.c
thin: tighten discard string conversions
[lvm2.git] / tools / vgrename.c
1 /*
2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
4 *
5 * This file is part of LVM2.
6 *
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
10 *
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
16 #include "tools.h"
17
18 static struct volume_group *_get_old_vg_for_rename(struct cmd_context *cmd,
19 const char *vg_name_old,
20 const char *vgid)
21 {
22 struct volume_group *vg;
23
24 /* FIXME we used to print an error about EXPORTED, but proceeded
25 nevertheless. */
26 vg = vg_read_for_update(cmd, vg_name_old, vgid, READ_ALLOW_EXPORTED);
27 if (vg_read_error(vg)) {
28 release_vg(vg);
29 return_NULL;
30 }
31
32 return vg;
33 }
34
35 static int _lock_new_vg_for_rename(struct cmd_context *cmd,
36 const char *vg_name_new)
37 {
38 int rc;
39
40 log_verbose("Checking for new volume group \"%s\"", vg_name_new);
41
42 rc = vg_lock_newname(cmd, vg_name_new);
43
44 if (rc == FAILED_LOCKING) {
45 log_error("Can't get lock for %s", vg_name_new);
46 return 0;
47 }
48
49 if (rc == FAILED_EXIST) {
50 log_error("New volume group \"%s\" already exists",
51 vg_name_new);
52 return 0;
53 }
54 return 1;
55 }
56
57 static int vg_rename_path(struct cmd_context *cmd, const char *old_vg_path,
58 const char *new_vg_path)
59 {
60 char *dev_dir;
61 struct id id;
62 int match = 0;
63 int found_id = 0;
64 struct dm_list *vgids;
65 struct str_list *sl;
66 const char *vg_name_new;
67 const char *vgid = NULL, *vg_name, *vg_name_old;
68 char old_path[NAME_LEN], new_path[NAME_LEN];
69 struct volume_group *vg = NULL;
70 int lock_vg_old_first = 1;
71
72 vg_name_old = skip_dev_dir(cmd, old_vg_path, NULL);
73 vg_name_new = skip_dev_dir(cmd, new_vg_path, NULL);
74
75 dev_dir = cmd->dev_dir;
76
77 if (!validate_vg_rename_params(cmd, vg_name_old, vg_name_new))
78 return_0;
79
80 log_verbose("Checking for existing volume group \"%s\"", vg_name_old);
81
82 /* populate lvmcache */
83 if (!lvmetad_vg_list_to_lvmcache(cmd))
84 stack;
85
86 /* Avoid duplicates */
87 if (!(vgids = get_vgids(cmd, 0)) || dm_list_empty(vgids)) {
88 log_error("No complete volume groups found");
89 return 0;
90 }
91
92 dm_list_iterate_items(sl, vgids) {
93 vgid = sl->str;
94 if (!vgid || !(vg_name = lvmcache_vgname_from_vgid(NULL, vgid)))
95 continue;
96 if (!strcmp(vg_name, vg_name_old)) {
97 if (match) {
98 log_error("Found more than one VG called %s. "
99 "Please supply VG uuid.", vg_name_old);
100 return 0;
101 }
102 match = 1;
103 }
104 }
105
106 log_suppress(2);
107 found_id = id_read_format(&id, vg_name_old);
108 log_suppress(0);
109 if (found_id && (vg_name = lvmcache_vgname_from_vgid(cmd->mem, (char *)id.uuid))) {
110 vg_name_old = vg_name;
111 vgid = (char *)id.uuid;
112 } else
113 vgid = NULL;
114
115 if (strcmp(vg_name_new, vg_name_old) < 0)
116 lock_vg_old_first = 0;
117
118 if (lock_vg_old_first) {
119 vg = _get_old_vg_for_rename(cmd, vg_name_old, vgid);
120 if (!vg)
121 return_0;
122
123 if (!_lock_new_vg_for_rename(cmd, vg_name_new)) {
124 unlock_and_release_vg(cmd, vg, vg_name_old);
125 return_0;
126 }
127 } else {
128 if (!_lock_new_vg_for_rename(cmd, vg_name_new))
129 return_0;
130
131 vg = _get_old_vg_for_rename(cmd, vg_name_old, vgid);
132 if (!vg) {
133 unlock_vg(cmd, vg_name_new);
134 return_0;
135 }
136 }
137
138 if (!archive(vg))
139 goto error;
140
141 /* Remove references based on old name */
142 if (!drop_cached_metadata(vg))
143 stack;
144
145 /* Change the volume group name */
146 vg_rename(cmd, vg, vg_name_new);
147
148 /* store it on disks */
149 log_verbose("Writing out updated volume group");
150 if (!vg_write(vg) || !vg_commit(vg)) {
151 goto error;
152 }
153
154 sprintf(old_path, "%s%s", dev_dir, vg_name_old);
155 sprintf(new_path, "%s%s", dev_dir, vg_name_new);
156
157 if (activation() && dir_exists(old_path)) {
158 log_verbose("Renaming \"%s\" to \"%s\"", old_path, new_path);
159
160 if (test_mode())
161 log_verbose("Test mode: Skipping rename.");
162
163 else if (lvs_in_vg_activated(vg)) {
164 if (!vg_refresh_visible(cmd, vg)) {
165 log_error("Renaming \"%s\" to \"%s\" failed",
166 old_path, new_path);
167 goto error;
168 }
169 }
170 }
171
172 if (!backup(vg))
173 stack;
174 if (!backup_remove(cmd, vg_name_old))
175 stack;
176
177 unlock_vg(cmd, vg_name_new);
178 unlock_and_release_vg(cmd, vg, vg_name_old);
179
180 log_print("Volume group \"%s\" successfully renamed to \"%s\"",
181 vg_name_old, vg_name_new);
182
183 /* FIXME lvmcache corruption - vginfo duplicated instead of renamed */
184 persistent_filter_wipe(cmd->filter);
185 lvmcache_destroy(cmd, 1);
186
187 return 1;
188
189 error:
190 if (lock_vg_old_first) {
191 unlock_vg(cmd, vg_name_new);
192 unlock_and_release_vg(cmd, vg, vg_name_old);
193 } else {
194 unlock_and_release_vg(cmd, vg, vg_name_old);
195 unlock_vg(cmd, vg_name_new);
196 }
197 return 0;
198 }
199
200 int vgrename(struct cmd_context *cmd, int argc, char **argv)
201 {
202 if (argc != 2) {
203 log_error("Old and new volume group names need specifying");
204 return EINVALID_CMD_LINE;
205 }
206
207 if (!vg_rename_path(cmd, argv[0], argv[1])) {
208 stack;
209 return ECMD_FAILED;
210 }
211
212 return ECMD_PROCESSED;
213 }
214
This page took 0.047251 seconds and 5 git commands to generate.