]> sourceware.org Git - lvm2.git/blob - tools/lvrename.c
spacing
[lvm2.git] / tools / lvrename.c
1 /*
2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2007 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 #include "lvm-types.h"
18
19
20 /*
21 * lvrename command implementation.
22 * Check arguments and call lv_rename() to execute the request.
23 */
24 int lvrename(struct cmd_context *cmd, int argc, char **argv)
25 {
26 size_t maxlen;
27 char *lv_name_old, *lv_name_new;
28 const char *vg_name, *vg_name_new, *vg_name_old;
29 char *st;
30 int r = ECMD_FAILED;
31
32 struct volume_group *vg = NULL;
33 struct lv_list *lvl;
34
35 if (argc == 3) {
36 vg_name = skip_dev_dir(cmd, argv[0], NULL);
37 lv_name_old = argv[1];
38 lv_name_new = argv[2];
39 if (strchr(lv_name_old, '/') &&
40 (vg_name_old = extract_vgname(cmd, lv_name_old)) &&
41 strcmp(vg_name_old, vg_name)) {
42 log_error("Please use a single volume group name "
43 "(\"%s\" or \"%s\")", vg_name, vg_name_old);
44 return EINVALID_CMD_LINE;
45 }
46 } else if (argc == 2) {
47 lv_name_old = argv[0];
48 lv_name_new = argv[1];
49 vg_name = extract_vgname(cmd, lv_name_old);
50 } else {
51 log_error("Old and new logical volume names required");
52 return EINVALID_CMD_LINE;
53 }
54
55 if (!validate_name(vg_name)) {
56 log_error("Please provide a valid volume group name");
57 return EINVALID_CMD_LINE;
58 }
59
60 if (strchr(lv_name_new, '/') &&
61 (vg_name_new = extract_vgname(cmd, lv_name_new)) &&
62 strcmp(vg_name, vg_name_new)) {
63 log_error("Logical volume names must "
64 "have the same volume group (\"%s\" or \"%s\")",
65 vg_name, vg_name_new);
66 return EINVALID_CMD_LINE;
67 }
68
69 if ((st = strrchr(lv_name_old, '/')))
70 lv_name_old = st + 1;
71
72 if ((st = strrchr(lv_name_new, '/')))
73 lv_name_new = st + 1;
74
75 /* Check sanity of new name */
76 maxlen = NAME_LEN - strlen(vg_name) - strlen(cmd->dev_dir) - 3;
77 if (strlen(lv_name_new) > maxlen) {
78 log_error("New logical volume path exceeds maximum length "
79 "of %" PRIsize_t "!", maxlen);
80 return ECMD_FAILED;
81 }
82
83 if (!*lv_name_new) {
84 log_error("New logical volume name may not be blank");
85 return ECMD_FAILED;
86 }
87
88 if (!apply_lvname_restrictions(lv_name_new)) {
89 stack;
90 return ECMD_FAILED;
91 }
92
93 if (!validate_name(lv_name_new)) {
94 log_error("New logical volume name \"%s\" is invalid",
95 lv_name_new);
96 return EINVALID_CMD_LINE;
97 }
98
99 if (!strcmp(lv_name_old, lv_name_new)) {
100 log_error("Old and new logical volume names must differ");
101 return EINVALID_CMD_LINE;
102 }
103
104 log_verbose("Checking for existing volume group \"%s\"", vg_name);
105 vg = vg_read_for_update(cmd, vg_name, NULL, 0);
106 if (vg_read_error(vg)) {
107 release_vg(vg);
108 stack;
109 return ECMD_FAILED;
110 }
111
112 if (!(lvl = find_lv_in_vg(vg, lv_name_old))) {
113 log_error("Existing logical volume \"%s\" not found in "
114 "volume group \"%s\"", lv_name_old, vg_name);
115 goto error;
116 }
117
118 if (lvl->lv->status & (RAID_IMAGE | RAID_META)) {
119 log_error("Cannot rename a RAID %s directly",
120 (lvl->lv->status & RAID_IMAGE) ? "image" :
121 "metadata area");
122 r = ECMD_FAILED;
123 goto error;
124 }
125
126 if (lv_is_raid_with_tracking(lvl->lv)) {
127 log_error("Cannot rename %s while it is tracking a split image",
128 lvl->lv->name);
129 r = ECMD_FAILED;
130 goto error;
131 }
132
133 if (!lv_rename(cmd, lvl->lv, lv_name_new))
134 goto error;
135
136 log_print("Renamed \"%s\" to \"%s\" in volume group \"%s\"",
137 lv_name_old, lv_name_new, vg_name);
138
139 r = ECMD_PROCESSED;
140 error:
141 unlock_and_release_vg(cmd, vg, vg_name);
142 return r;
143 }
This page took 0.044206 seconds and 5 git commands to generate.