xref: /dragonfly/contrib/lvm2/dist/tools/lvrename.c (revision 36a3d1d6)
1 /*	$NetBSD: lvrename.c,v 1.1.1.2 2009/12/02 00:25:52 haad Exp $	*/
2 
3 /*
4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
6  *
7  * This file is part of LVM2.
8  *
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.
12  *
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
16  */
17 
18 #include "tools.h"
19 #include "lvm-types.h"
20 
21 
22 /*
23  * lvrename command implementation.
24  * Check arguments and call lv_rename() to execute the request.
25  */
26 int lvrename(struct cmd_context *cmd, int argc, char **argv)
27 {
28 	size_t maxlen;
29 	char *lv_name_old, *lv_name_new;
30 	const char *vg_name, *vg_name_new, *vg_name_old;
31 	char *st;
32 	int r = ECMD_FAILED;
33 
34 	struct volume_group *vg = NULL;
35 	struct lv_list *lvl;
36 
37 	if (argc == 3) {
38 		vg_name = skip_dev_dir(cmd, argv[0], NULL);
39 		lv_name_old = argv[1];
40 		lv_name_new = argv[2];
41 		if (strchr(lv_name_old, '/') &&
42 		    (vg_name_old = extract_vgname(cmd, lv_name_old)) &&
43 		    strcmp(vg_name_old, vg_name)) {
44 			log_error("Please use a single volume group name "
45 				  "(\"%s\" or \"%s\")", vg_name, vg_name_old);
46 			return EINVALID_CMD_LINE;
47 		}
48 	} else if (argc == 2) {
49 		lv_name_old = argv[0];
50 		lv_name_new = argv[1];
51 		vg_name = extract_vgname(cmd, lv_name_old);
52 	} else {
53 		log_error("Old and new logical volume names required");
54 		return EINVALID_CMD_LINE;
55 	}
56 
57 	if (!validate_name(vg_name)) {
58 		log_error("Please provide a valid volume group name");
59 		return EINVALID_CMD_LINE;
60 	}
61 
62 	if (strchr(lv_name_new, '/') &&
63 	    (vg_name_new = extract_vgname(cmd, lv_name_new)) &&
64 	    strcmp(vg_name, vg_name_new)) {
65 		log_error("Logical volume names must "
66 			  "have the same volume group (\"%s\" or \"%s\")",
67 			  vg_name, vg_name_new);
68 		return EINVALID_CMD_LINE;
69 	}
70 
71 	if ((st = strrchr(lv_name_old, '/')))
72 		lv_name_old = st + 1;
73 
74 	if ((st = strrchr(lv_name_new, '/')))
75 		lv_name_new = st + 1;
76 
77 	/* Check sanity of new name */
78 	maxlen = NAME_LEN - strlen(vg_name) - strlen(cmd->dev_dir) - 3;
79 	if (strlen(lv_name_new) > maxlen) {
80 		log_error("New logical volume path exceeds maximum length "
81 			  "of %" PRIsize_t "!", maxlen);
82 		return ECMD_FAILED;
83 	}
84 
85 	if (!*lv_name_new) {
86 		log_error("New logical volume name may not be blank");
87 		return ECMD_FAILED;
88 	}
89 
90 	if (!apply_lvname_restrictions(lv_name_new)) {
91 		stack;
92 		return ECMD_FAILED;
93 	}
94 
95 	if (!validate_name(lv_name_new)) {
96 		log_error("New logical volume name \"%s\" is invalid",
97 		     lv_name_new);
98 		return EINVALID_CMD_LINE;
99 	}
100 
101 	if (!strcmp(lv_name_old, lv_name_new)) {
102 		log_error("Old and new logical volume names must differ");
103 		return EINVALID_CMD_LINE;
104 	}
105 
106 	log_verbose("Checking for existing volume group \"%s\"", vg_name);
107 	vg = vg_read_for_update(cmd, vg_name, NULL, 0);
108 	if (vg_read_error(vg)) {
109 		vg_release(vg);
110 		stack;
111 		return ECMD_FAILED;
112 	}
113 
114 	if (!(lvl = find_lv_in_vg(vg, lv_name_old))) {
115 		log_error("Existing logical volume \"%s\" not found in "
116 			  "volume group \"%s\"", lv_name_old, vg_name);
117 		goto error;
118 	}
119 
120 	if (!lv_rename(cmd, lvl->lv, lv_name_new))
121 		goto error;
122 
123 	log_print("Renamed \"%s\" to \"%s\" in volume group \"%s\"",
124 		  lv_name_old, lv_name_new, vg_name);
125 
126 	r = ECMD_PROCESSED;
127 error:
128 	unlock_and_release_vg(cmd, vg, vg_name);
129 	return r;
130 }
131