xref: /illumos-gate/usr/src/lib/libbe/common/be_rename.c (revision 2ad72058bc680e30f084e9f9ba8ceb0f77386821)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <assert.h>
27 #include <libintl.h>
28 #include <libnvpair.h>
29 #include <libzfs.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 
37 #include <libbe.h>
38 #include <libbe_priv.h>
39 
40 /* ******************************************************************** */
41 /*			Public Functions				*/
42 /* ******************************************************************** */
43 
44 /*
45  * Function:	be_rename
46  * Description:	Renames the BE from the original name to the new name
47  *		passed in through be_attrs. Also the entries in vfstab and
48  *		menu.lst are updated with the new name.
49  * Parameters:
50  *		be_attrs - pointer to nvlist_t of attributes being passed in.
51  *			   The following attribute values are used by
52  *			   this function:
53  *
54  *			   BE_ATTR_ORIG_BE_NAME		*required
55  *			   BE_ATTR_NEW_BE_NAME		*required
56  * Return:
57  *		BE_SUCCESS - Success
58  *		be_errno_t - Failure
59  * Scope:
60  *		Public
61  */
62 
63 int
64 be_rename(nvlist_t *be_attrs)
65 {
66 	be_transaction_data_t	bt = { 0 };
67 	be_transaction_data_t	cbt = { 0 };
68 	be_fs_list_data_t	fld = { 0 };
69 	zfs_handle_t	*zhp = NULL;
70 	char		root_ds[MAXPATHLEN];
71 	char		*mp = NULL;
72 	int		zret = 0, ret = BE_SUCCESS;
73 
74 	/* Initialize libzfs handle */
75 	if (!be_zfs_init())
76 		return (BE_ERR_INIT);
77 
78 	/* Get original BE name to rename from */
79 	if (nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_NAME, &bt.obe_name)
80 	    != 0) {
81 		be_print_err(gettext("be_rename: failed to "
82 		    "lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
83 		be_zfs_fini();
84 		return (BE_ERR_INVAL);
85 	}
86 
87 	/* Get new BE name to rename to */
88 	if (nvlist_lookup_string(be_attrs, BE_ATTR_NEW_BE_NAME, &bt.nbe_name)
89 	    != 0) {
90 		be_print_err(gettext("be_rename: failed to "
91 		    "lookup BE_ATTR_NEW_BE_NAME attribute\n"));
92 		be_zfs_fini();
93 		return (BE_ERR_INVAL);
94 	}
95 
96 	/*
97 	 * Get the currently active BE and check to see if this
98 	 * is an attempt to rename the currently active BE.
99 	 */
100 	if (be_find_current_be(&cbt) != BE_SUCCESS) {
101 		be_print_err(gettext("be_rename: failed to find the currently "
102 		    "active BE\n"));
103 		be_zfs_fini();
104 		return (BE_ERR_CURR_BE_NOT_FOUND);
105 	}
106 
107 	if (strncmp(bt.obe_name, cbt.obe_name,
108 	    MAX(strlen(bt.obe_name), strlen(cbt.obe_name))) == 0) {
109 		be_print_err(gettext("be_rename: This is an attempt to rename "
110 		    "the currently active BE, which is not supported\n"));
111 		be_zfs_fini();
112 		free(cbt.obe_name);
113 		return (BE_ERR_RENAME_ACTIVE);
114 	}
115 
116 	/* Validate original BE name */
117 	if (!be_valid_be_name(bt.obe_name)) {
118 		be_print_err(gettext("be_rename: "
119 		    "invalid BE name %s\n"), bt.obe_name);
120 		be_zfs_fini();
121 		return (BE_ERR_INVAL);
122 	}
123 
124 	/* Validate new BE name */
125 	if (!be_valid_be_name(bt.nbe_name)) {
126 		be_print_err(gettext("be_rename: invalid BE name %s\n"),
127 		    bt.nbe_name);
128 		be_zfs_fini();
129 		return (BE_ERR_INVAL);
130 	}
131 
132 	/* Find which zpool the BE is in */
133 	if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &bt)) == 0) {
134 		be_print_err(gettext("be_rename: failed to "
135 		    "find zpool for BE (%s)\n"), bt.obe_name);
136 		be_zfs_fini();
137 		return (BE_ERR_BE_NOENT);
138 	} else if (zret < 0) {
139 		be_print_err(gettext("be_rename: zpool_iter failed: %s\n"),
140 		    libzfs_error_description(g_zfs));
141 		ret = zfs_err_to_be_err(g_zfs);
142 		be_zfs_fini();
143 		return (ret);
144 	}
145 
146 	/* New BE will reside in the same zpool as orig BE */
147 	bt.nbe_zpool = bt.obe_zpool;
148 
149 	be_make_root_ds(bt.obe_zpool, bt.obe_name, root_ds, sizeof (root_ds));
150 	bt.obe_root_ds = strdup(root_ds);
151 	be_make_root_ds(bt.nbe_zpool, bt.nbe_name, root_ds, sizeof (root_ds));
152 	bt.nbe_root_ds = strdup(root_ds);
153 
154 	/*
155 	 * Generate a list of file systems from the BE that are legacy
156 	 * mounted before renaming.  We use this list to determine which
157 	 * entries in the vfstab we need to update after we've renamed the BE.
158 	 */
159 	if ((ret = be_get_legacy_fs(bt.obe_name, bt.obe_root_ds, NULL, NULL,
160 	    &fld)) != BE_SUCCESS) {
161 		be_print_err(gettext("be_rename: failed to "
162 		    "get legacy mounted file system list for %s\n"),
163 		    bt.obe_name);
164 		goto done;
165 	}
166 
167 	/* Get handle to BE's root dataset */
168 	if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_FILESYSTEM))
169 	    == NULL) {
170 		be_print_err(gettext("be_rename: failed to "
171 		    "open BE root dataset (%s): %s\n"),
172 		    bt.obe_root_ds, libzfs_error_description(g_zfs));
173 		ret = zfs_err_to_be_err(g_zfs);
174 		goto done;
175 	}
176 
177 	/* Rename of BE's root dataset. */
178 	if (zfs_rename(zhp, bt.nbe_root_ds, B_FALSE) != 0) {
179 		be_print_err(gettext("be_rename: failed to "
180 		    "rename dataset (%s): %s\n"), bt.obe_root_ds,
181 		    libzfs_error_description(g_zfs));
182 		ret = zfs_err_to_be_err(g_zfs);
183 		goto done;
184 	}
185 
186 	/* Refresh handle to BE's root dataset after the rename */
187 	ZFS_CLOSE(zhp);
188 	if ((zhp = zfs_open(g_zfs, bt.nbe_root_ds, ZFS_TYPE_FILESYSTEM))
189 	    == NULL) {
190 		be_print_err(gettext("be_rename: failed to "
191 		    "open BE root dataset (%s): %s\n"),
192 		    bt.obe_root_ds, libzfs_error_description(g_zfs));
193 		ret = zfs_err_to_be_err(g_zfs);
194 		goto done;
195 	}
196 
197 	/* If BE is already mounted, get its mountpoint */
198 	if (zfs_is_mounted(zhp, &mp) && mp == NULL) {
199 		be_print_err(gettext("be_rename: failed to "
200 		    "get altroot of mounted BE %s: %s\n"),
201 		    bt.nbe_name, libzfs_error_description(g_zfs));
202 		ret = zfs_err_to_be_err(g_zfs);
203 		goto done;
204 	}
205 
206 	/* Update BE's vfstab */
207 	if ((ret = be_update_vfstab(bt.nbe_name, bt.obe_zpool, bt.nbe_zpool,
208 	    &fld, mp)) != BE_SUCCESS) {
209 		be_print_err(gettext("be_rename: "
210 		    "failed to update new BE's vfstab (%s)\n"), bt.nbe_name);
211 		goto done;
212 	}
213 
214 	/* Update this BE's GRUB menu entry */
215 	if (getzoneid() == GLOBAL_ZONEID && (ret = be_update_menu(bt.obe_name,
216 	    bt.nbe_name, bt.obe_zpool, NULL)) != BE_SUCCESS) {
217 		be_print_err(gettext("be_rename: "
218 		    "failed to update grub menu entry from %s to %s\n"),
219 		    bt.obe_name, bt.nbe_name);
220 	}
221 
222 done:
223 	be_free_fs_list(&fld);
224 
225 	ZFS_CLOSE(zhp);
226 
227 	be_zfs_fini();
228 
229 	free(bt.obe_root_ds);
230 	free(bt.nbe_root_ds);
231 	return (ret);
232 }
233