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