]>
Commit | Line | Data |
---|---|---|
d9bc7ffe | 1 | /* |
67cdbd7e | 2 | * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. |
be684599 | 3 | * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. |
d9bc7ffe | 4 | * |
6606c3ae | 5 | * This file is part of LVM2. |
d9bc7ffe | 6 | * |
6606c3ae AK |
7 | * This copyrighted material is made available to anyone wishing to use, |
8 | * modify, copy, or redistribute it subject to the terms and conditions | |
be684599 | 9 | * of the GNU Lesser General Public License v.2.1. |
d9bc7ffe | 10 | * |
be684599 | 11 | * You should have received a copy of the GNU Lesser General Public License |
6606c3ae AK |
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 | |
d9bc7ffe AK |
14 | */ |
15 | ||
16 | #include "tools.h" | |
4970ebfb | 17 | #include "lvm-types.h" |
d9bc7ffe | 18 | |
b7cd307f DW |
19 | |
20 | /* | |
21 | * lvrename command implementation. | |
22 | * Check arguments and call lv_rename() to execute the request. | |
23 | */ | |
60274aba | 24 | int lvrename(struct cmd_context *cmd, int argc, char **argv) |
d9bc7ffe | 25 | { |
8ef2b021 | 26 | size_t maxlen; |
d9bc7ffe | 27 | char *lv_name_old, *lv_name_new; |
8ef2b021 | 28 | const char *vg_name, *vg_name_new, *vg_name_old; |
d9bc7ffe | 29 | char *st; |
043b1362 | 30 | int r = ECMD_FAILED; |
d9bc7ffe | 31 | |
043b1362 | 32 | struct volume_group *vg = NULL; |
e586401e | 33 | struct lv_list *lvl; |
d9bc7ffe | 34 | |
faaedea2 | 35 | if (argc == 3) { |
9397354a | 36 | vg_name = skip_dev_dir(cmd, argv[0], NULL); |
faaedea2 AK |
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"); | |
d9bc7ffe AK |
52 | return EINVALID_CMD_LINE; |
53 | } | |
54 | ||
7e4867f7 | 55 | if (!validate_name(vg_name)) { |
faaedea2 | 56 | log_error("Please provide a valid volume group name"); |
d9bc7ffe AK |
57 | return EINVALID_CMD_LINE; |
58 | } | |
59 | ||
60 | if (strchr(lv_name_new, '/') && | |
25b73380 | 61 | (vg_name_new = extract_vgname(cmd, lv_name_new)) && |
d9bc7ffe AK |
62 | strcmp(vg_name, vg_name_new)) { |
63 | log_error("Logical volume names must " | |
08907484 | 64 | "have the same volume group (\"%s\" or \"%s\")", |
d9bc7ffe AK |
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 */ | |
60274aba | 76 | maxlen = NAME_LEN - strlen(vg_name) - strlen(cmd->dev_dir) - 3; |
d9bc7ffe AK |
77 | if (strlen(lv_name_new) > maxlen) { |
78 | log_error("New logical volume path exceeds maximum length " | |
4970ebfb | 79 | "of %" PRIsize_t "!", maxlen); |
d9bc7ffe AK |
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 | ||
8211a13c AK |
88 | if (!apply_lvname_restrictions(lv_name_new)) { |
89 | stack; | |
5a52dca9 AK |
90 | return ECMD_FAILED; |
91 | } | |
92 | ||
7e4867f7 | 93 | if (!validate_name(lv_name_new)) { |
8387b473 | 94 | log_error("New logical volume name \"%s\" is invalid", |
6fda126d | 95 | lv_name_new); |
d9bc7ffe AK |
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 | ||
08907484 | 104 | log_verbose("Checking for existing volume group \"%s\"", vg_name); |
b8b3508c | 105 | vg = vg_read_for_update(cmd, vg_name, NULL, 0); |
4c611a22 | 106 | if (vg_read_error(vg)) { |
077a6755 | 107 | release_vg(vg); |
651ff9b3 | 108 | stack; |
7d0e6e80 | 109 | return ECMD_FAILED; |
4c611a22 | 110 | } |
f53c6aa6 | 111 | |
e586401e | 112 | if (!(lvl = find_lv_in_vg(vg, lv_name_old))) { |
08907484 HM |
113 | log_error("Existing logical volume \"%s\" not found in " |
114 | "volume group \"%s\"", lv_name_old, vg_name); | |
7d0e6e80 | 115 | goto error; |
d9bc7ffe AK |
116 | } |
117 | ||
a927e401 JEB |
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 | ||
b7cd307f | 133 | if (!lv_rename(cmd, lvl->lv, lv_name_new)) |
d68336a5 | 134 | goto error; |
914c9723 | 135 | |
08907484 HM |
136 | log_print("Renamed \"%s\" to \"%s\" in volume group \"%s\"", |
137 | lv_name_old, lv_name_new, vg_name); | |
d9bc7ffe | 138 | |
043b1362 MB |
139 | r = ECMD_PROCESSED; |
140 | error: | |
077a6755 | 141 | unlock_and_release_vg(cmd, vg, vg_name); |
043b1362 | 142 | return r; |
d9bc7ffe | 143 | } |