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.
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.
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]
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
29 #include <libnvpair.h>
34 #include <sys/types.h>
39 #include <libbe_priv.h>
41 /* ******************************************************************** */
42 /* Public Functions */
43 /* ******************************************************************** */
47 * Description: Renames the BE from the original name to the new name
48 * passed in through be_attrs. Also the entries in vfstab and
49 * menu.lst are updated with the new name.
51 * be_attrs - pointer to nvlist_t of attributes being passed in.
52 * The following attribute values are used by
55 * BE_ATTR_ORIG_BE_NAME *required
56 * BE_ATTR_NEW_BE_NAME *required
58 * BE_SUCCESS - Success
59 * be_errno_t - Failure
65 be_rename(nvlist_t
*be_attrs
)
67 be_transaction_data_t bt
= { 0 };
68 be_transaction_data_t cbt
= { 0 };
69 be_fs_list_data_t fld
= { 0 };
70 zfs_handle_t
*zhp
= NULL
;
71 char root_ds
[MAXPATHLEN
];
73 int zret
= 0, ret
= BE_SUCCESS
;
75 /* Initialize libzfs handle */
79 /* Get original BE name to rename from */
80 if (nvlist_lookup_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, &bt
.obe_name
)
82 be_print_err(gettext("be_rename: failed to "
83 "lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
85 return (BE_ERR_INVAL
);
88 /* Get new BE name to rename to */
89 if (nvlist_lookup_string(be_attrs
, BE_ATTR_NEW_BE_NAME
, &bt
.nbe_name
)
91 be_print_err(gettext("be_rename: failed to "
92 "lookup BE_ATTR_NEW_BE_NAME attribute\n"));
94 return (BE_ERR_INVAL
);
98 * Get the currently active BE and check to see if this
99 * is an attempt to rename the currently active BE.
101 if (be_find_current_be(&cbt
) != BE_SUCCESS
) {
102 be_print_err(gettext("be_rename: failed to find the currently "
105 return (BE_ERR_CURR_BE_NOT_FOUND
);
108 if (strncmp(bt
.obe_name
, cbt
.obe_name
,
109 MAX(strlen(bt
.obe_name
), strlen(cbt
.obe_name
))) == 0) {
110 be_print_err(gettext("be_rename: This is an attempt to rename "
111 "the currently active BE, which is not supported\n"));
114 return (BE_ERR_RENAME_ACTIVE
);
117 /* Validate original BE name */
118 if (!be_valid_be_name(bt
.obe_name
)) {
119 be_print_err(gettext("be_rename: "
120 "invalid BE name %s\n"), bt
.obe_name
);
122 return (BE_ERR_INVAL
);
125 /* Validate new BE name */
126 if (!be_valid_be_name(bt
.nbe_name
)) {
127 be_print_err(gettext("be_rename: invalid BE name %s\n"),
130 return (BE_ERR_INVAL
);
133 /* Find which zpool the BE is in */
134 if ((zret
= zpool_iter(g_zfs
, be_find_zpool_callback
, &bt
)) == 0) {
135 be_print_err(gettext("be_rename: failed to "
136 "find zpool for BE (%s)\n"), bt
.obe_name
);
138 return (BE_ERR_BE_NOENT
);
139 } else if (zret
< 0) {
140 be_print_err(gettext("be_rename: zpool_iter failed: %s\n"),
141 libzfs_error_description(g_zfs
));
142 ret
= zfs_err_to_be_err(g_zfs
);
147 /* New BE will reside in the same zpool as orig BE */
148 bt
.nbe_zpool
= bt
.obe_zpool
;
150 be_make_root_ds(bt
.obe_zpool
, bt
.obe_name
, root_ds
, sizeof (root_ds
));
151 bt
.obe_root_ds
= strdup(root_ds
);
152 be_make_root_ds(bt
.nbe_zpool
, bt
.nbe_name
, root_ds
, sizeof (root_ds
));
153 bt
.nbe_root_ds
= strdup(root_ds
);
156 * Generate a list of file systems from the BE that are legacy
157 * mounted before renaming. We use this list to determine which
158 * entries in the vfstab we need to update after we've renamed the BE.
160 if ((ret
= be_get_legacy_fs(bt
.obe_name
, bt
.obe_root_ds
, NULL
, NULL
,
161 &fld
)) != BE_SUCCESS
) {
162 be_print_err(gettext("be_rename: failed to "
163 "get legacy mounted file system list for %s\n"),
168 /* Get handle to BE's root dataset */
169 if ((zhp
= zfs_open(g_zfs
, bt
.obe_root_ds
, ZFS_TYPE_FILESYSTEM
))
171 be_print_err(gettext("be_rename: failed to "
172 "open BE root dataset (%s): %s\n"),
173 bt
.obe_root_ds
, libzfs_error_description(g_zfs
));
174 ret
= zfs_err_to_be_err(g_zfs
);
178 /* Rename of BE's root dataset. */
179 if (zfs_rename(zhp
, bt
.nbe_root_ds
, B_FALSE
, B_FALSE
) != 0) {
180 be_print_err(gettext("be_rename: failed to "
181 "rename dataset (%s): %s\n"), bt
.obe_root_ds
,
182 libzfs_error_description(g_zfs
));
183 ret
= zfs_err_to_be_err(g_zfs
);
187 /* Refresh handle to BE's root dataset after the rename */
189 if ((zhp
= zfs_open(g_zfs
, bt
.nbe_root_ds
, ZFS_TYPE_FILESYSTEM
))
191 be_print_err(gettext("be_rename: failed to "
192 "open BE root dataset (%s): %s\n"),
193 bt
.obe_root_ds
, libzfs_error_description(g_zfs
));
194 ret
= zfs_err_to_be_err(g_zfs
);
198 /* If BE is already mounted, get its mountpoint */
199 if (zfs_is_mounted(zhp
, &mp
) && mp
== NULL
) {
200 be_print_err(gettext("be_rename: failed to "
201 "get altroot of mounted BE %s: %s\n"),
202 bt
.nbe_name
, libzfs_error_description(g_zfs
));
203 ret
= zfs_err_to_be_err(g_zfs
);
207 /* Update BE's vfstab */
208 if ((ret
= be_update_vfstab(bt
.nbe_name
, bt
.obe_zpool
, bt
.nbe_zpool
,
209 &fld
, mp
)) != BE_SUCCESS
) {
210 be_print_err(gettext("be_rename: "
211 "failed to update new BE's vfstab (%s)\n"), bt
.nbe_name
);
215 /* Update this BE's menu entry */
216 if (getzoneid() == GLOBAL_ZONEID
&& (ret
= be_update_menu(bt
.obe_name
,
217 bt
.nbe_name
, bt
.obe_zpool
, NULL
)) != BE_SUCCESS
) {
218 be_print_err(gettext("be_rename: "
219 "failed to update menu entry from %s to %s\n"),
220 bt
.obe_name
, bt
.nbe_name
);
224 be_free_fs_list(&fld
);
230 free(bt
.obe_root_ds
);
231 free(bt
.nbe_root_ds
);