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.
24f169c0eaSGlenn Lagasse */
25f169c0eaSGlenn Lagasse
267e0e2549SAlexander Eremin /*
271a902ef8SHans Rosenfeld * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28*4fef13f1SAndy Fiddaman * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
297e0e2549SAlexander Eremin */
307e0e2549SAlexander Eremin
31f169c0eaSGlenn Lagasse #include <assert.h>
32f169c0eaSGlenn Lagasse #include <libintl.h>
33f169c0eaSGlenn Lagasse #include <libnvpair.h>
34f169c0eaSGlenn Lagasse #include <libzfs.h>
35f169c0eaSGlenn Lagasse #include <stdio.h>
36f169c0eaSGlenn Lagasse #include <stdlib.h>
37f169c0eaSGlenn Lagasse #include <string.h>
381a902ef8SHans Rosenfeld #include <strings.h>
39f169c0eaSGlenn Lagasse #include <errno.h>
40f169c0eaSGlenn Lagasse #include <sys/mnttab.h>
41f169c0eaSGlenn Lagasse #include <sys/types.h>
42f169c0eaSGlenn Lagasse #include <sys/stat.h>
431a902ef8SHans Rosenfeld #include <fcntl.h>
44f169c0eaSGlenn Lagasse #include <unistd.h>
451a902ef8SHans Rosenfeld #include <sys/efi_partition.h>
46f169c0eaSGlenn Lagasse
47f169c0eaSGlenn Lagasse #include <libbe.h>
48f169c0eaSGlenn Lagasse #include <libbe_priv.h>
49f169c0eaSGlenn Lagasse
50f169c0eaSGlenn Lagasse char *mnttab = MNTTAB;
51f169c0eaSGlenn Lagasse
52f169c0eaSGlenn Lagasse /*
53f169c0eaSGlenn Lagasse * Private function prototypes
54f169c0eaSGlenn Lagasse */
55f169c0eaSGlenn Lagasse static int set_bootfs(char *boot_rpool, char *be_root_ds);
56f169c0eaSGlenn Lagasse static int set_canmount(be_node_list_t *, char *);
57a63c99a2SToomas Soome static boolean_t be_do_install_mbr(char *, nvlist_t *);
58a63c99a2SToomas Soome static int be_do_installboot_helper(zpool_handle_t *, nvlist_t *, char *,
59c7c0ceafSToomas Soome char *, uint16_t);
60c7c0ceafSToomas Soome static int be_do_installboot(be_transaction_data_t *, uint16_t);
61f169c0eaSGlenn Lagasse static int be_get_grub_vers(be_transaction_data_t *, char **, char **);
62f169c0eaSGlenn Lagasse static int get_ver_from_capfile(char *, char **);
63f169c0eaSGlenn Lagasse static int be_promote_zone_ds(char *, char *);
64f169c0eaSGlenn Lagasse static int be_promote_ds_callback(zfs_handle_t *, void *);
65f169c0eaSGlenn Lagasse
66f169c0eaSGlenn Lagasse /* ******************************************************************** */
67f169c0eaSGlenn Lagasse /* Public Functions */
68f169c0eaSGlenn Lagasse /* ******************************************************************** */
69f169c0eaSGlenn Lagasse
70f169c0eaSGlenn Lagasse /*
71f169c0eaSGlenn Lagasse * Function: be_activate
72f169c0eaSGlenn Lagasse * Description: Calls _be_activate which activates the BE named in the
73f169c0eaSGlenn Lagasse * attributes passed in through be_attrs. The process of
74f169c0eaSGlenn Lagasse * activation sets the bootfs property of the root pool, resets
75f169c0eaSGlenn Lagasse * the canmount property to noauto, and sets the default in the
76f169c0eaSGlenn Lagasse * grub menu to the entry corresponding to the entry for the named
77f169c0eaSGlenn Lagasse * BE.
78f169c0eaSGlenn Lagasse * Parameters:
79f169c0eaSGlenn Lagasse * be_attrs - pointer to nvlist_t of attributes being passed in.
80f169c0eaSGlenn Lagasse * The follow attribute values are used by this function:
81f169c0eaSGlenn Lagasse *
82f169c0eaSGlenn Lagasse * BE_ATTR_ORIG_BE_NAME *required
83f169c0eaSGlenn Lagasse * Return:
84f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
85f169c0eaSGlenn Lagasse * be_errno_t - Failure
86f169c0eaSGlenn Lagasse * Scope:
87f169c0eaSGlenn Lagasse * Public
88f169c0eaSGlenn Lagasse */
89f169c0eaSGlenn Lagasse int
be_activate(nvlist_t * be_attrs)90f169c0eaSGlenn Lagasse be_activate(nvlist_t *be_attrs)
91f169c0eaSGlenn Lagasse {
92f169c0eaSGlenn Lagasse int ret = BE_SUCCESS;
93f169c0eaSGlenn Lagasse char *be_name = NULL;
94f169c0eaSGlenn Lagasse
95f169c0eaSGlenn Lagasse /* Initialize libzfs handle */
96f169c0eaSGlenn Lagasse if (!be_zfs_init())
97f169c0eaSGlenn Lagasse return (BE_ERR_INIT);
98f169c0eaSGlenn Lagasse
99f169c0eaSGlenn Lagasse /* Get the BE name to activate */
100f169c0eaSGlenn Lagasse if (nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_NAME, &be_name)
101f169c0eaSGlenn Lagasse != 0) {
102f169c0eaSGlenn Lagasse be_print_err(gettext("be_activate: failed to "
103f169c0eaSGlenn Lagasse "lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
104f169c0eaSGlenn Lagasse be_zfs_fini();
105f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
106f169c0eaSGlenn Lagasse }
107f169c0eaSGlenn Lagasse
108f169c0eaSGlenn Lagasse /* Validate BE name */
109f169c0eaSGlenn Lagasse if (!be_valid_be_name(be_name)) {
110f169c0eaSGlenn Lagasse be_print_err(gettext("be_activate: invalid BE name %s\n"),
111f169c0eaSGlenn Lagasse be_name);
112f169c0eaSGlenn Lagasse be_zfs_fini();
113f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
114f169c0eaSGlenn Lagasse }
115f169c0eaSGlenn Lagasse
116f169c0eaSGlenn Lagasse ret = _be_activate(be_name);
117f169c0eaSGlenn Lagasse
118f169c0eaSGlenn Lagasse be_zfs_fini();
119f169c0eaSGlenn Lagasse
120f169c0eaSGlenn Lagasse return (ret);
121f169c0eaSGlenn Lagasse }
122f169c0eaSGlenn Lagasse
123c7c0ceafSToomas Soome /*
124c7c0ceafSToomas Soome * Function: be_installboot
125c7c0ceafSToomas Soome * Description: Calls be_do_installboot to install/update bootloader on
126c7c0ceafSToomas Soome * pool passed in through be_attrs. The primary consumer is
127c7c0ceafSToomas Soome * bootadm command to avoid duplication of the code.
128c7c0ceafSToomas Soome * Parameters:
129c7c0ceafSToomas Soome * be_attrs - pointer to nvlist_t of attributes being passed in.
130c7c0ceafSToomas Soome * The following attribute values are used:
131c7c0ceafSToomas Soome *
132c7c0ceafSToomas Soome * BE_ATTR_ORIG_BE_NAME *required
133c7c0ceafSToomas Soome * BE_ATTR_ORIG_BE_POOL *required
134c7c0ceafSToomas Soome * BE_ATTR_ORIG_BE_ROOT *required
135c7c0ceafSToomas Soome * BE_ATTR_INSTALL_FLAGS optional
136c7c0ceafSToomas Soome *
137c7c0ceafSToomas Soome * Return:
138c7c0ceafSToomas Soome * BE_SUCCESS - Success
139c7c0ceafSToomas Soome * be_errno_t - Failure
140c7c0ceafSToomas Soome * Scope:
141c7c0ceafSToomas Soome * Public
142c7c0ceafSToomas Soome */
143c7c0ceafSToomas Soome int
be_installboot(nvlist_t * be_attrs)144c7c0ceafSToomas Soome be_installboot(nvlist_t *be_attrs)
145c7c0ceafSToomas Soome {
146c7c0ceafSToomas Soome int ret = BE_SUCCESS;
147c7c0ceafSToomas Soome uint16_t flags = 0;
148c7c0ceafSToomas Soome uint16_t verbose;
149c7c0ceafSToomas Soome be_transaction_data_t bt = { 0 };
150c7c0ceafSToomas Soome
151c7c0ceafSToomas Soome /* Get flags */
152c7c0ceafSToomas Soome if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
153c7c0ceafSToomas Soome BE_ATTR_INSTALL_FLAGS, DATA_TYPE_UINT16, &flags, NULL) != 0) {
154c7c0ceafSToomas Soome be_print_err(gettext("be_installboot: failed to lookup "
155c7c0ceafSToomas Soome "BE_ATTR_INSTALL_FLAGS attribute\n"));
156c7c0ceafSToomas Soome return (BE_ERR_INVAL);
157c7c0ceafSToomas Soome }
158c7c0ceafSToomas Soome
159c7c0ceafSToomas Soome /* Set verbose early, so we get all messages */
160c7c0ceafSToomas Soome verbose = flags & BE_INSTALLBOOT_FLAG_VERBOSE;
161c7c0ceafSToomas Soome if (verbose == BE_INSTALLBOOT_FLAG_VERBOSE)
162c7c0ceafSToomas Soome libbe_print_errors(B_TRUE);
163c7c0ceafSToomas Soome
164c7c0ceafSToomas Soome ret = nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_NAME,
165c7c0ceafSToomas Soome &bt.obe_name);
166c7c0ceafSToomas Soome if (ret != 0) {
167c7c0ceafSToomas Soome be_print_err(gettext("be_installboot: failed to "
168c7c0ceafSToomas Soome "lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
169c7c0ceafSToomas Soome return (BE_ERR_INVAL);
170c7c0ceafSToomas Soome }
171c7c0ceafSToomas Soome
172c7c0ceafSToomas Soome ret = nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_POOL,
173c7c0ceafSToomas Soome &bt.obe_zpool);
174c7c0ceafSToomas Soome if (ret != 0) {
175c7c0ceafSToomas Soome be_print_err(gettext("be_installboot: failed to "
176c7c0ceafSToomas Soome "lookup BE_ATTR_ORIG_BE_POOL attribute\n"));
177c7c0ceafSToomas Soome return (BE_ERR_INVAL);
178c7c0ceafSToomas Soome }
179c7c0ceafSToomas Soome
180c7c0ceafSToomas Soome ret = nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_ROOT,
181c7c0ceafSToomas Soome &bt.obe_root_ds);
182c7c0ceafSToomas Soome if (ret != 0) {
183c7c0ceafSToomas Soome be_print_err(gettext("be_installboot: failed to "
184c7c0ceafSToomas Soome "lookup BE_ATTR_ORIG_BE_ROOT attribute\n"));
185c7c0ceafSToomas Soome return (BE_ERR_INVAL);
186c7c0ceafSToomas Soome }
187c7c0ceafSToomas Soome
188c7c0ceafSToomas Soome /* Initialize libzfs handle */
189c7c0ceafSToomas Soome if (!be_zfs_init())
190c7c0ceafSToomas Soome return (BE_ERR_INIT);
191c7c0ceafSToomas Soome
192c7c0ceafSToomas Soome ret = be_do_installboot(&bt, flags);
193c7c0ceafSToomas Soome
194c7c0ceafSToomas Soome be_zfs_fini();
195c7c0ceafSToomas Soome
196c7c0ceafSToomas Soome return (ret);
197c7c0ceafSToomas Soome }
198c7c0ceafSToomas Soome
199f169c0eaSGlenn Lagasse /* ******************************************************************** */
200f169c0eaSGlenn Lagasse /* Semi Private Functions */
201f169c0eaSGlenn Lagasse /* ******************************************************************** */
202f169c0eaSGlenn Lagasse
203f169c0eaSGlenn Lagasse /*
204f169c0eaSGlenn Lagasse * Function: _be_activate
205f169c0eaSGlenn Lagasse * Description: This does the actual work described in be_activate.
206f169c0eaSGlenn Lagasse * Parameters:
207f169c0eaSGlenn Lagasse * be_name - pointer to the name of BE to activate.
208f169c0eaSGlenn Lagasse *
209f169c0eaSGlenn Lagasse * Return:
210f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
211f169c0eaSGlenn Lagasse * be_errnot_t - Failure
212f169c0eaSGlenn Lagasse * Scope:
213f169c0eaSGlenn Lagasse * Public
214f169c0eaSGlenn Lagasse */
215f169c0eaSGlenn Lagasse int
_be_activate(char * be_name)216f169c0eaSGlenn Lagasse _be_activate(char *be_name)
217f169c0eaSGlenn Lagasse {
218f169c0eaSGlenn Lagasse be_transaction_data_t cb = { 0 };
219f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL;
220f169c0eaSGlenn Lagasse char root_ds[MAXPATHLEN];
2217e0e2549SAlexander Eremin char active_ds[MAXPATHLEN];
222f169c0eaSGlenn Lagasse be_node_list_t *be_nodes = NULL;
223f169c0eaSGlenn Lagasse uuid_t uu = {0};
224f169c0eaSGlenn Lagasse int entry, ret = BE_SUCCESS;
225f169c0eaSGlenn Lagasse int zret = 0;
226f169c0eaSGlenn Lagasse
227f169c0eaSGlenn Lagasse /*
228f169c0eaSGlenn Lagasse * TODO: The BE needs to be validated to make sure that it is actually
229f169c0eaSGlenn Lagasse * a bootable BE.
230f169c0eaSGlenn Lagasse */
231f169c0eaSGlenn Lagasse
232f169c0eaSGlenn Lagasse if (be_name == NULL)
233f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
234f169c0eaSGlenn Lagasse
235f169c0eaSGlenn Lagasse /* Set obe_name to be_name in the cb structure */
236f169c0eaSGlenn Lagasse cb.obe_name = be_name;
237f169c0eaSGlenn Lagasse
238f169c0eaSGlenn Lagasse /* find which zpool the be is in */
239f169c0eaSGlenn Lagasse if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &cb)) == 0) {
240f169c0eaSGlenn Lagasse be_print_err(gettext("be_activate: failed to "
241f169c0eaSGlenn Lagasse "find zpool for BE (%s)\n"), cb.obe_name);
242f169c0eaSGlenn Lagasse return (BE_ERR_BE_NOENT);
243f169c0eaSGlenn Lagasse } else if (zret < 0) {
244f169c0eaSGlenn Lagasse be_print_err(gettext("be_activate: "
245f169c0eaSGlenn Lagasse "zpool_iter failed: %s\n"),
246f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
247f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs);
248f169c0eaSGlenn Lagasse return (ret);
249f169c0eaSGlenn Lagasse }
250f169c0eaSGlenn Lagasse
251f169c0eaSGlenn Lagasse be_make_root_ds(cb.obe_zpool, cb.obe_name, root_ds, sizeof (root_ds));
252f169c0eaSGlenn Lagasse cb.obe_root_ds = strdup(root_ds);
253f169c0eaSGlenn Lagasse
254f169c0eaSGlenn Lagasse if (getzoneid() == GLOBAL_ZONEID) {
255c7c0ceafSToomas Soome ret = be_do_installboot(&cb, BE_INSTALLBOOT_FLAG_NULL);
256c7c0ceafSToomas Soome if (ret != BE_SUCCESS)
257f169c0eaSGlenn Lagasse return (ret);
258a63c99a2SToomas Soome
259f169c0eaSGlenn Lagasse if (!be_has_menu_entry(root_ds, cb.obe_zpool, &entry)) {
260f169c0eaSGlenn Lagasse if ((ret = be_append_menu(cb.obe_name, cb.obe_zpool,
261f169c0eaSGlenn Lagasse NULL, NULL, NULL)) != BE_SUCCESS) {
262f169c0eaSGlenn Lagasse be_print_err(gettext("be_activate: Failed to "
263a63c99a2SToomas Soome "add BE (%s) to the menu\n"),
264f169c0eaSGlenn Lagasse cb.obe_name);
265f169c0eaSGlenn Lagasse goto done;
266f169c0eaSGlenn Lagasse }
267f169c0eaSGlenn Lagasse }
268f169c0eaSGlenn Lagasse if (be_has_grub()) {
269f169c0eaSGlenn Lagasse if ((ret = be_change_grub_default(cb.obe_name,
270f169c0eaSGlenn Lagasse cb.obe_zpool)) != BE_SUCCESS) {
271f169c0eaSGlenn Lagasse be_print_err(gettext("be_activate: failed to "
272f169c0eaSGlenn Lagasse "change the default entry in menu.lst\n"));
273f169c0eaSGlenn Lagasse goto done;
274f169c0eaSGlenn Lagasse }
275f169c0eaSGlenn Lagasse }
276f169c0eaSGlenn Lagasse }
277f169c0eaSGlenn Lagasse
278*4fef13f1SAndy Fiddaman if ((ret = _be_list(cb.obe_name, &be_nodes, BE_LIST_DEFAULT))
279*4fef13f1SAndy Fiddaman != BE_SUCCESS) {
280f169c0eaSGlenn Lagasse return (ret);
281f169c0eaSGlenn Lagasse }
282f169c0eaSGlenn Lagasse
283f169c0eaSGlenn Lagasse if ((ret = set_canmount(be_nodes, "noauto")) != BE_SUCCESS) {
284f169c0eaSGlenn Lagasse be_print_err(gettext("be_activate: failed to set "
285f169c0eaSGlenn Lagasse "canmount dataset property\n"));
286f169c0eaSGlenn Lagasse goto done;
287f169c0eaSGlenn Lagasse }
288f169c0eaSGlenn Lagasse
2897e0e2549SAlexander Eremin if (getzoneid() == GLOBAL_ZONEID) {
2907e0e2549SAlexander Eremin if ((ret = set_bootfs(be_nodes->be_rpool,
2917e0e2549SAlexander Eremin root_ds)) != BE_SUCCESS) {
292f169c0eaSGlenn Lagasse be_print_err(gettext("be_activate: failed to set "
293f169c0eaSGlenn Lagasse "bootfs pool property for %s\n"), root_ds);
294f169c0eaSGlenn Lagasse goto done;
295f169c0eaSGlenn Lagasse }
2967e0e2549SAlexander Eremin }
297f169c0eaSGlenn Lagasse
298f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, root_ds, ZFS_TYPE_FILESYSTEM)) != NULL) {
299f169c0eaSGlenn Lagasse /*
300f169c0eaSGlenn Lagasse * We don't need to close the zfs handle at this
301f169c0eaSGlenn Lagasse * point because The callback funtion
302f169c0eaSGlenn Lagasse * be_promote_ds_callback() will close it for us.
303f169c0eaSGlenn Lagasse */
304f169c0eaSGlenn Lagasse if (be_promote_ds_callback(zhp, NULL) != 0) {
305f169c0eaSGlenn Lagasse be_print_err(gettext("be_activate: "
306f169c0eaSGlenn Lagasse "failed to activate the "
307f169c0eaSGlenn Lagasse "datasets for %s: %s\n"),
308f169c0eaSGlenn Lagasse root_ds,
309f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
310f169c0eaSGlenn Lagasse ret = BE_ERR_PROMOTE;
311f169c0eaSGlenn Lagasse goto done;
312f169c0eaSGlenn Lagasse }
313f169c0eaSGlenn Lagasse } else {
3147e0e2549SAlexander Eremin be_print_err(gettext("be_activate: failed to open "
315f169c0eaSGlenn Lagasse "dataset (%s): %s\n"), root_ds,
316f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
317f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs);
318f169c0eaSGlenn Lagasse goto done;
319f169c0eaSGlenn Lagasse }
320f169c0eaSGlenn Lagasse
321f169c0eaSGlenn Lagasse if (getzoneid() == GLOBAL_ZONEID &&
322f169c0eaSGlenn Lagasse be_get_uuid(cb.obe_root_ds, &uu) == BE_SUCCESS &&
323f169c0eaSGlenn Lagasse (ret = be_promote_zone_ds(cb.obe_name, cb.obe_root_ds))
324f169c0eaSGlenn Lagasse != BE_SUCCESS) {
325f169c0eaSGlenn Lagasse be_print_err(gettext("be_activate: failed to promote "
326f169c0eaSGlenn Lagasse "the active zonepath datasets for zones in BE %s\n"),
327f169c0eaSGlenn Lagasse cb.obe_name);
328f169c0eaSGlenn Lagasse }
329f169c0eaSGlenn Lagasse
3307e0e2549SAlexander Eremin if (getzoneid() != GLOBAL_ZONEID) {
3317e0e2549SAlexander Eremin if (!be_zone_compare_uuids(root_ds)) {
3327e0e2549SAlexander Eremin be_print_err(gettext("be_activate: activating zone "
3337e0e2549SAlexander Eremin "root dataset from non-active global BE is not "
3347e0e2549SAlexander Eremin "supported\n"));
3357e0e2549SAlexander Eremin ret = BE_ERR_NOTSUP;
3367e0e2549SAlexander Eremin goto done;
3377e0e2549SAlexander Eremin }
3387e0e2549SAlexander Eremin if ((zhp = zfs_open(g_zfs, root_ds,
3397e0e2549SAlexander Eremin ZFS_TYPE_FILESYSTEM)) == NULL) {
3407e0e2549SAlexander Eremin be_print_err(gettext("be_activate: failed to open "
3417e0e2549SAlexander Eremin "dataset (%s): %s\n"), root_ds,
3427e0e2549SAlexander Eremin libzfs_error_description(g_zfs));
3437e0e2549SAlexander Eremin ret = zfs_err_to_be_err(g_zfs);
3447e0e2549SAlexander Eremin goto done;
3457e0e2549SAlexander Eremin }
3467e0e2549SAlexander Eremin /* Find current active zone root dataset */
3477e0e2549SAlexander Eremin if ((ret = be_find_active_zone_root(zhp, cb.obe_zpool,
3487e0e2549SAlexander Eremin active_ds, sizeof (active_ds))) != BE_SUCCESS) {
3497e0e2549SAlexander Eremin be_print_err(gettext("be_activate: failed to find "
3507e0e2549SAlexander Eremin "active zone root dataset\n"));
3517e0e2549SAlexander Eremin ZFS_CLOSE(zhp);
3527e0e2549SAlexander Eremin goto done;
3537e0e2549SAlexander Eremin }
3547e0e2549SAlexander Eremin /* Do nothing if requested BE is already active */
3557e0e2549SAlexander Eremin if (strcmp(root_ds, active_ds) == 0) {
3567e0e2549SAlexander Eremin ret = BE_SUCCESS;
3577e0e2549SAlexander Eremin ZFS_CLOSE(zhp);
3587e0e2549SAlexander Eremin goto done;
3597e0e2549SAlexander Eremin }
3607e0e2549SAlexander Eremin
3617e0e2549SAlexander Eremin /* Set active property for BE */
3627e0e2549SAlexander Eremin if (zfs_prop_set(zhp, BE_ZONE_ACTIVE_PROPERTY, "on") != 0) {
3637e0e2549SAlexander Eremin be_print_err(gettext("be_activate: failed to set "
3647e0e2549SAlexander Eremin "active property (%s): %s\n"), root_ds,
3657e0e2549SAlexander Eremin libzfs_error_description(g_zfs));
3667e0e2549SAlexander Eremin ret = zfs_err_to_be_err(g_zfs);
3677e0e2549SAlexander Eremin ZFS_CLOSE(zhp);
3687e0e2549SAlexander Eremin goto done;
3697e0e2549SAlexander Eremin }
3707e0e2549SAlexander Eremin ZFS_CLOSE(zhp);
3717e0e2549SAlexander Eremin
3727e0e2549SAlexander Eremin /* Unset active property for old active root dataset */
3737e0e2549SAlexander Eremin if ((zhp = zfs_open(g_zfs, active_ds,
3747e0e2549SAlexander Eremin ZFS_TYPE_FILESYSTEM)) == NULL) {
3757e0e2549SAlexander Eremin be_print_err(gettext("be_activate: failed to open "
3767e0e2549SAlexander Eremin "dataset (%s): %s\n"), active_ds,
3777e0e2549SAlexander Eremin libzfs_error_description(g_zfs));
3787e0e2549SAlexander Eremin ret = zfs_err_to_be_err(g_zfs);
3797e0e2549SAlexander Eremin goto done;
3807e0e2549SAlexander Eremin }
3817e0e2549SAlexander Eremin if (zfs_prop_set(zhp, BE_ZONE_ACTIVE_PROPERTY, "off") != 0) {
3827e0e2549SAlexander Eremin be_print_err(gettext("be_activate: failed to unset "
3837e0e2549SAlexander Eremin "active property (%s): %s\n"), active_ds,
3847e0e2549SAlexander Eremin libzfs_error_description(g_zfs));
3857e0e2549SAlexander Eremin ret = zfs_err_to_be_err(g_zfs);
3867e0e2549SAlexander Eremin ZFS_CLOSE(zhp);
3877e0e2549SAlexander Eremin goto done;
3887e0e2549SAlexander Eremin }
3897e0e2549SAlexander Eremin ZFS_CLOSE(zhp);
3907e0e2549SAlexander Eremin }
391f169c0eaSGlenn Lagasse done:
392f169c0eaSGlenn Lagasse be_free_list(be_nodes);
393f169c0eaSGlenn Lagasse return (ret);
394f169c0eaSGlenn Lagasse }
395f169c0eaSGlenn Lagasse
396f169c0eaSGlenn Lagasse /*
397f169c0eaSGlenn Lagasse * Function: be_activate_current_be
398f169c0eaSGlenn Lagasse * Description: Set the currently "active" BE to be "active on boot"
399f169c0eaSGlenn Lagasse * Paramters:
400f169c0eaSGlenn Lagasse * none
401f169c0eaSGlenn Lagasse * Returns:
402f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
403f169c0eaSGlenn Lagasse * be_errnot_t - Failure
404f169c0eaSGlenn Lagasse * Scope:
405f169c0eaSGlenn Lagasse * Semi-private (library wide use only)
406f169c0eaSGlenn Lagasse */
407f169c0eaSGlenn Lagasse int
be_activate_current_be(void)408f169c0eaSGlenn Lagasse be_activate_current_be(void)
409f169c0eaSGlenn Lagasse {
410f169c0eaSGlenn Lagasse int ret = BE_SUCCESS;
411f169c0eaSGlenn Lagasse be_transaction_data_t bt = { 0 };
412f169c0eaSGlenn Lagasse
413f169c0eaSGlenn Lagasse if ((ret = be_find_current_be(&bt)) != BE_SUCCESS) {
414f169c0eaSGlenn Lagasse return (ret);
415f169c0eaSGlenn Lagasse }
416f169c0eaSGlenn Lagasse
417f169c0eaSGlenn Lagasse if ((ret = _be_activate(bt.obe_name)) != BE_SUCCESS) {
418f169c0eaSGlenn Lagasse be_print_err(gettext("be_activate_current_be: failed to "
419f169c0eaSGlenn Lagasse "activate %s\n"), bt.obe_name);
420f169c0eaSGlenn Lagasse return (ret);
421f169c0eaSGlenn Lagasse }
422f169c0eaSGlenn Lagasse
423f169c0eaSGlenn Lagasse return (BE_SUCCESS);
424f169c0eaSGlenn Lagasse }
425f169c0eaSGlenn Lagasse
426f169c0eaSGlenn Lagasse /*
427f169c0eaSGlenn Lagasse * Function: be_is_active_on_boot
428f169c0eaSGlenn Lagasse * Description: Checks if the BE name passed in has the "active on boot"
429f169c0eaSGlenn Lagasse * property set to B_TRUE.
430f169c0eaSGlenn Lagasse * Paramters:
431f169c0eaSGlenn Lagasse * be_name - the name of the BE to check
432f169c0eaSGlenn Lagasse * Returns:
433f169c0eaSGlenn Lagasse * B_TRUE - if active on boot.
434f169c0eaSGlenn Lagasse * B_FALSE - if not active on boot.
435f169c0eaSGlenn Lagasse * Scope:
436f169c0eaSGlenn Lagasse * Semi-private (library wide use only)
437f169c0eaSGlenn Lagasse */
438f169c0eaSGlenn Lagasse boolean_t
be_is_active_on_boot(char * be_name)439f169c0eaSGlenn Lagasse be_is_active_on_boot(char *be_name)
440f169c0eaSGlenn Lagasse {
441f169c0eaSGlenn Lagasse be_node_list_t *be_node = NULL;
442f169c0eaSGlenn Lagasse
443f169c0eaSGlenn Lagasse if (be_name == NULL) {
444f169c0eaSGlenn Lagasse be_print_err(gettext("be_is_active_on_boot: "
445f169c0eaSGlenn Lagasse "be_name must not be NULL\n"));
446f169c0eaSGlenn Lagasse return (B_FALSE);
447f169c0eaSGlenn Lagasse }
448f169c0eaSGlenn Lagasse
449*4fef13f1SAndy Fiddaman if (_be_list(be_name, &be_node, BE_LIST_DEFAULT) != BE_SUCCESS) {
450f169c0eaSGlenn Lagasse return (B_FALSE);
451f169c0eaSGlenn Lagasse }
452f169c0eaSGlenn Lagasse
453f169c0eaSGlenn Lagasse if (be_node == NULL) {
454f169c0eaSGlenn Lagasse return (B_FALSE);
455f169c0eaSGlenn Lagasse }
456f169c0eaSGlenn Lagasse
457f169c0eaSGlenn Lagasse if (be_node->be_active_on_boot) {
458f169c0eaSGlenn Lagasse be_free_list(be_node);
459f169c0eaSGlenn Lagasse return (B_TRUE);
460f169c0eaSGlenn Lagasse } else {
461f169c0eaSGlenn Lagasse be_free_list(be_node);
462f169c0eaSGlenn Lagasse return (B_FALSE);
463f169c0eaSGlenn Lagasse }
464f169c0eaSGlenn Lagasse }
465f169c0eaSGlenn Lagasse
466f169c0eaSGlenn Lagasse /* ******************************************************************** */
467f169c0eaSGlenn Lagasse /* Private Functions */
468f169c0eaSGlenn Lagasse /* ******************************************************************** */
469f169c0eaSGlenn Lagasse
470f169c0eaSGlenn Lagasse /*
471f169c0eaSGlenn Lagasse * Function: set_bootfs
472f169c0eaSGlenn Lagasse * Description: Sets the bootfs property on the boot pool to be the
473f169c0eaSGlenn Lagasse * root dataset of the activated BE.
474f169c0eaSGlenn Lagasse * Parameters:
475f169c0eaSGlenn Lagasse * boot_pool - The pool we're setting bootfs in.
476f169c0eaSGlenn Lagasse * be_root_ds - The main dataset for the BE.
477f169c0eaSGlenn Lagasse * Return:
478f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
479f169c0eaSGlenn Lagasse * be_errno_t - Failure
480f169c0eaSGlenn Lagasse * Scope:
481f169c0eaSGlenn Lagasse * Private
482f169c0eaSGlenn Lagasse */
483f169c0eaSGlenn Lagasse static int
set_bootfs(char * boot_rpool,char * be_root_ds)484f169c0eaSGlenn Lagasse set_bootfs(char *boot_rpool, char *be_root_ds)
485f169c0eaSGlenn Lagasse {
486f169c0eaSGlenn Lagasse zpool_handle_t *zhp;
487f169c0eaSGlenn Lagasse int err = BE_SUCCESS;
488f169c0eaSGlenn Lagasse
489f169c0eaSGlenn Lagasse if ((zhp = zpool_open(g_zfs, boot_rpool)) == NULL) {
490f169c0eaSGlenn Lagasse be_print_err(gettext("set_bootfs: failed to open pool "
491f169c0eaSGlenn Lagasse "(%s): %s\n"), boot_rpool, libzfs_error_description(g_zfs));
492f169c0eaSGlenn Lagasse err = zfs_err_to_be_err(g_zfs);
493f169c0eaSGlenn Lagasse return (err);
494f169c0eaSGlenn Lagasse }
495f169c0eaSGlenn Lagasse
496f169c0eaSGlenn Lagasse err = zpool_set_prop(zhp, "bootfs", be_root_ds);
497f169c0eaSGlenn Lagasse if (err) {
498f169c0eaSGlenn Lagasse be_print_err(gettext("set_bootfs: failed to set "
499f169c0eaSGlenn Lagasse "bootfs property for pool %s: %s\n"), boot_rpool,
500f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
501f169c0eaSGlenn Lagasse err = zfs_err_to_be_err(g_zfs);
502f169c0eaSGlenn Lagasse zpool_close(zhp);
503f169c0eaSGlenn Lagasse return (err);
504f169c0eaSGlenn Lagasse }
505f169c0eaSGlenn Lagasse
506f169c0eaSGlenn Lagasse zpool_close(zhp);
507f169c0eaSGlenn Lagasse return (BE_SUCCESS);
508f169c0eaSGlenn Lagasse }
509f169c0eaSGlenn Lagasse
510f169c0eaSGlenn Lagasse /*
511f169c0eaSGlenn Lagasse * Function: set_canmount
512f169c0eaSGlenn Lagasse * Description: Sets the canmount property on the datasets of the
513f169c0eaSGlenn Lagasse * activated BE.
514f169c0eaSGlenn Lagasse * Parameters:
515f169c0eaSGlenn Lagasse * be_nodes - The be_node_t returned from be_list
516f169c0eaSGlenn Lagasse * value - The value of canmount we setting, on|off|noauto.
517f169c0eaSGlenn Lagasse * Return:
518f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
519f169c0eaSGlenn Lagasse * be_errno_t - Failure
520f169c0eaSGlenn Lagasse * Scope:
521f169c0eaSGlenn Lagasse * Private
522f169c0eaSGlenn Lagasse */
523f169c0eaSGlenn Lagasse static int
set_canmount(be_node_list_t * be_nodes,char * value)524f169c0eaSGlenn Lagasse set_canmount(be_node_list_t *be_nodes, char *value)
525f169c0eaSGlenn Lagasse {
526f169c0eaSGlenn Lagasse char ds_path[MAXPATHLEN];
527f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL;
528f169c0eaSGlenn Lagasse be_node_list_t *list = be_nodes;
529f169c0eaSGlenn Lagasse int err = BE_SUCCESS;
530f169c0eaSGlenn Lagasse
531f169c0eaSGlenn Lagasse while (list != NULL) {
532f169c0eaSGlenn Lagasse be_dataset_list_t *datasets = list->be_node_datasets;
533f169c0eaSGlenn Lagasse
534f169c0eaSGlenn Lagasse be_make_root_ds(list->be_rpool, list->be_node_name, ds_path,
535f169c0eaSGlenn Lagasse sizeof (ds_path));
536f169c0eaSGlenn Lagasse
537f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, ds_path, ZFS_TYPE_DATASET)) ==
538f169c0eaSGlenn Lagasse NULL) {
539f169c0eaSGlenn Lagasse be_print_err(gettext("set_canmount: failed to open "
540f169c0eaSGlenn Lagasse "dataset (%s): %s\n"), ds_path,
541f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
542f169c0eaSGlenn Lagasse err = zfs_err_to_be_err(g_zfs);
543f169c0eaSGlenn Lagasse return (err);
544f169c0eaSGlenn Lagasse }
545f169c0eaSGlenn Lagasse if (zfs_prop_get_int(zhp, ZFS_PROP_MOUNTED)) {
546f169c0eaSGlenn Lagasse /*
547f169c0eaSGlenn Lagasse * it's already mounted so we can't change the
548f169c0eaSGlenn Lagasse * canmount property anyway.
549f169c0eaSGlenn Lagasse */
550f169c0eaSGlenn Lagasse err = BE_SUCCESS;
551f169c0eaSGlenn Lagasse } else {
552f169c0eaSGlenn Lagasse err = zfs_prop_set(zhp,
553f169c0eaSGlenn Lagasse zfs_prop_to_name(ZFS_PROP_CANMOUNT), value);
554f169c0eaSGlenn Lagasse if (err) {
555f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
556f169c0eaSGlenn Lagasse be_print_err(gettext("set_canmount: failed to "
557f169c0eaSGlenn Lagasse "set dataset property (%s): %s\n"),
558f169c0eaSGlenn Lagasse ds_path, libzfs_error_description(g_zfs));
559f169c0eaSGlenn Lagasse err = zfs_err_to_be_err(g_zfs);
560f169c0eaSGlenn Lagasse return (err);
561f169c0eaSGlenn Lagasse }
562f169c0eaSGlenn Lagasse }
563f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
564f169c0eaSGlenn Lagasse
565f169c0eaSGlenn Lagasse while (datasets != NULL) {
566f169c0eaSGlenn Lagasse be_make_root_ds(list->be_rpool,
567f169c0eaSGlenn Lagasse datasets->be_dataset_name, ds_path,
568f169c0eaSGlenn Lagasse sizeof (ds_path));
569f169c0eaSGlenn Lagasse
570f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, ds_path, ZFS_TYPE_DATASET))
571f169c0eaSGlenn Lagasse == NULL) {
572f169c0eaSGlenn Lagasse be_print_err(gettext("set_canmount: failed to "
573f169c0eaSGlenn Lagasse "open dataset %s: %s\n"), ds_path,
574f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
575f169c0eaSGlenn Lagasse err = zfs_err_to_be_err(g_zfs);
576f169c0eaSGlenn Lagasse return (err);
577f169c0eaSGlenn Lagasse }
578f169c0eaSGlenn Lagasse if (zfs_prop_get_int(zhp, ZFS_PROP_MOUNTED)) {
579f169c0eaSGlenn Lagasse /*
580f169c0eaSGlenn Lagasse * it's already mounted so we can't change the
581f169c0eaSGlenn Lagasse * canmount property anyway.
582f169c0eaSGlenn Lagasse */
583f169c0eaSGlenn Lagasse err = BE_SUCCESS;
584f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
585f169c0eaSGlenn Lagasse break;
586f169c0eaSGlenn Lagasse }
587f169c0eaSGlenn Lagasse err = zfs_prop_set(zhp,
588f169c0eaSGlenn Lagasse zfs_prop_to_name(ZFS_PROP_CANMOUNT), value);
589f169c0eaSGlenn Lagasse if (err) {
590f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
591f169c0eaSGlenn Lagasse be_print_err(gettext("set_canmount: "
592f169c0eaSGlenn Lagasse "Failed to set property value %s "
593f169c0eaSGlenn Lagasse "for dataset %s: %s\n"), value, ds_path,
594f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
595f169c0eaSGlenn Lagasse err = zfs_err_to_be_err(g_zfs);
596f169c0eaSGlenn Lagasse return (err);
597f169c0eaSGlenn Lagasse }
598f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
599f169c0eaSGlenn Lagasse datasets = datasets->be_next_dataset;
600f169c0eaSGlenn Lagasse }
601f169c0eaSGlenn Lagasse list = list->be_next_node;
602f169c0eaSGlenn Lagasse }
603f169c0eaSGlenn Lagasse return (err);
604f169c0eaSGlenn Lagasse }
605f169c0eaSGlenn Lagasse
606f169c0eaSGlenn Lagasse /*
607f169c0eaSGlenn Lagasse * Function: be_get_grub_vers
608f169c0eaSGlenn Lagasse * Description: Gets the grub version number from /boot/grub/capability. If
609f169c0eaSGlenn Lagasse * capability file doesn't exist NULL is returned.
610f169c0eaSGlenn Lagasse * Parameters:
611f169c0eaSGlenn Lagasse * bt - The transaction data for the BE we're getting the grub
612f169c0eaSGlenn Lagasse * version for.
613f169c0eaSGlenn Lagasse * cur_vers - used to return the current version of grub from
614f169c0eaSGlenn Lagasse * the root pool.
615f169c0eaSGlenn Lagasse * new_vers - used to return the grub version of the BE we're
616f169c0eaSGlenn Lagasse * activating.
617f169c0eaSGlenn Lagasse * Return:
618f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
619f169c0eaSGlenn Lagasse * be_errno_t - Failed to find version
620f169c0eaSGlenn Lagasse * Scope:
621f169c0eaSGlenn Lagasse * Private
622f169c0eaSGlenn Lagasse */
623f169c0eaSGlenn Lagasse static int
be_get_grub_vers(be_transaction_data_t * bt,char ** cur_vers,char ** new_vers)624f169c0eaSGlenn Lagasse be_get_grub_vers(be_transaction_data_t *bt, char **cur_vers, char **new_vers)
625f169c0eaSGlenn Lagasse {
626f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL;
627f169c0eaSGlenn Lagasse zfs_handle_t *pool_zhp = NULL;
628f169c0eaSGlenn Lagasse int ret = BE_SUCCESS;
629f169c0eaSGlenn Lagasse char cap_file[MAXPATHLEN];
630f169c0eaSGlenn Lagasse char *temp_mntpnt = NULL;
631f169c0eaSGlenn Lagasse char *zpool_mntpt = NULL;
632f169c0eaSGlenn Lagasse char *ptmp_mntpnt = NULL;
633f169c0eaSGlenn Lagasse char *orig_mntpnt = NULL;
634f169c0eaSGlenn Lagasse boolean_t be_mounted = B_FALSE;
635f169c0eaSGlenn Lagasse boolean_t pool_mounted = B_FALSE;
636f169c0eaSGlenn Lagasse
637f169c0eaSGlenn Lagasse if (!be_has_grub()) {
638f169c0eaSGlenn Lagasse be_print_err(gettext("be_get_grub_vers: Not supported on "
639f169c0eaSGlenn Lagasse "this architecture\n"));
640f169c0eaSGlenn Lagasse return (BE_ERR_NOTSUP);
641f169c0eaSGlenn Lagasse }
642f169c0eaSGlenn Lagasse
643f169c0eaSGlenn Lagasse if (bt == NULL || bt->obe_name == NULL || bt->obe_zpool == NULL ||
644f169c0eaSGlenn Lagasse bt->obe_root_ds == NULL) {
645f169c0eaSGlenn Lagasse be_print_err(gettext("be_get_grub_vers: Invalid BE\n"));
646f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
647f169c0eaSGlenn Lagasse }
648f169c0eaSGlenn Lagasse
649f169c0eaSGlenn Lagasse if ((pool_zhp = zfs_open(g_zfs, bt->obe_zpool, ZFS_TYPE_FILESYSTEM)) ==
650f169c0eaSGlenn Lagasse NULL) {
651f169c0eaSGlenn Lagasse be_print_err(gettext("be_get_grub_vers: zfs_open failed: %s\n"),
652f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
653f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs));
654f169c0eaSGlenn Lagasse }
655f169c0eaSGlenn Lagasse
656f169c0eaSGlenn Lagasse /*
657f169c0eaSGlenn Lagasse * Check to see if the pool's dataset is mounted. If it isn't we'll
658f169c0eaSGlenn Lagasse * attempt to mount it.
659f169c0eaSGlenn Lagasse */
660f169c0eaSGlenn Lagasse if ((ret = be_mount_pool(pool_zhp, &ptmp_mntpnt,
661f169c0eaSGlenn Lagasse &orig_mntpnt, &pool_mounted)) != BE_SUCCESS) {
662f169c0eaSGlenn Lagasse be_print_err(gettext("be_get_grub_vers: pool dataset "
663f169c0eaSGlenn Lagasse "(%s) could not be mounted\n"), bt->obe_zpool);
664f169c0eaSGlenn Lagasse ZFS_CLOSE(pool_zhp);
665f169c0eaSGlenn Lagasse return (ret);
666f169c0eaSGlenn Lagasse }
667f169c0eaSGlenn Lagasse
668f169c0eaSGlenn Lagasse /*
669f169c0eaSGlenn Lagasse * Get the mountpoint for the root pool dataset.
670f169c0eaSGlenn Lagasse */
671f169c0eaSGlenn Lagasse if (!zfs_is_mounted(pool_zhp, &zpool_mntpt)) {
672f169c0eaSGlenn Lagasse be_print_err(gettext("be_get_grub_vers: pool "
673a63c99a2SToomas Soome "dataset (%s) is not mounted. Can't read the "
674a63c99a2SToomas Soome "grub capability file.\n"), bt->obe_zpool);
675f169c0eaSGlenn Lagasse ret = BE_ERR_NO_MENU;
676f169c0eaSGlenn Lagasse goto cleanup;
677f169c0eaSGlenn Lagasse }
678f169c0eaSGlenn Lagasse
679f169c0eaSGlenn Lagasse /*
680f169c0eaSGlenn Lagasse * get the version of the most recent grub update.
681f169c0eaSGlenn Lagasse */
682f169c0eaSGlenn Lagasse (void) snprintf(cap_file, sizeof (cap_file), "%s%s",
683f169c0eaSGlenn Lagasse zpool_mntpt, BE_CAP_FILE);
684f169c0eaSGlenn Lagasse free(zpool_mntpt);
685f169c0eaSGlenn Lagasse zpool_mntpt = NULL;
686f169c0eaSGlenn Lagasse
687f169c0eaSGlenn Lagasse if ((ret = get_ver_from_capfile(cap_file, cur_vers)) != BE_SUCCESS)
688f169c0eaSGlenn Lagasse goto cleanup;
689f169c0eaSGlenn Lagasse
690f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, bt->obe_root_ds, ZFS_TYPE_FILESYSTEM)) ==
691f169c0eaSGlenn Lagasse NULL) {
692f169c0eaSGlenn Lagasse be_print_err(gettext("be_get_grub_vers: failed to "
693f169c0eaSGlenn Lagasse "open BE root dataset (%s): %s\n"), bt->obe_root_ds,
694f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
695f169c0eaSGlenn Lagasse free(cur_vers);
696f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs);
697f169c0eaSGlenn Lagasse goto cleanup;
698f169c0eaSGlenn Lagasse }
699f169c0eaSGlenn Lagasse if (!zfs_is_mounted(zhp, &temp_mntpnt)) {
700f169c0eaSGlenn Lagasse if ((ret = _be_mount(bt->obe_name, &temp_mntpnt,
701f169c0eaSGlenn Lagasse BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
702f169c0eaSGlenn Lagasse be_print_err(gettext("be_get_grub_vers: failed to "
703f169c0eaSGlenn Lagasse "mount BE (%s)\n"), bt->obe_name);
704f169c0eaSGlenn Lagasse free(*cur_vers);
705f169c0eaSGlenn Lagasse *cur_vers = NULL;
706f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
707f169c0eaSGlenn Lagasse goto cleanup;
708f169c0eaSGlenn Lagasse }
709f169c0eaSGlenn Lagasse be_mounted = B_TRUE;
710f169c0eaSGlenn Lagasse }
711f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
712f169c0eaSGlenn Lagasse
713f169c0eaSGlenn Lagasse /*
714f169c0eaSGlenn Lagasse * Now get the grub version for the BE being activated.
715f169c0eaSGlenn Lagasse */
716f169c0eaSGlenn Lagasse (void) snprintf(cap_file, sizeof (cap_file), "%s%s", temp_mntpnt,
717f169c0eaSGlenn Lagasse BE_CAP_FILE);
718f169c0eaSGlenn Lagasse ret = get_ver_from_capfile(cap_file, new_vers);
719f169c0eaSGlenn Lagasse if (ret != BE_SUCCESS) {
720f169c0eaSGlenn Lagasse free(*cur_vers);
721f169c0eaSGlenn Lagasse *cur_vers = NULL;
722f169c0eaSGlenn Lagasse }
723f169c0eaSGlenn Lagasse if (be_mounted)
724f169c0eaSGlenn Lagasse (void) _be_unmount(bt->obe_name, 0);
725f169c0eaSGlenn Lagasse
726f169c0eaSGlenn Lagasse cleanup:
727f169c0eaSGlenn Lagasse if (pool_mounted) {
728f169c0eaSGlenn Lagasse int iret = BE_SUCCESS;
729f169c0eaSGlenn Lagasse iret = be_unmount_pool(pool_zhp, ptmp_mntpnt, orig_mntpnt);
730f169c0eaSGlenn Lagasse if (ret == BE_SUCCESS)
731f169c0eaSGlenn Lagasse ret = iret;
732f169c0eaSGlenn Lagasse free(orig_mntpnt);
733f169c0eaSGlenn Lagasse free(ptmp_mntpnt);
734f169c0eaSGlenn Lagasse }
735f169c0eaSGlenn Lagasse ZFS_CLOSE(pool_zhp);
736f169c0eaSGlenn Lagasse
737f169c0eaSGlenn Lagasse free(temp_mntpnt);
738f169c0eaSGlenn Lagasse return (ret);
739f169c0eaSGlenn Lagasse }
740f169c0eaSGlenn Lagasse
741f169c0eaSGlenn Lagasse /*
742f169c0eaSGlenn Lagasse * Function: get_ver_from_capfile
743f169c0eaSGlenn Lagasse * Description: Parses the capability file passed in looking for the VERSION
744f169c0eaSGlenn Lagasse * line. If found the version is returned in vers, if not then
745f169c0eaSGlenn Lagasse * NULL is returned in vers.
746f169c0eaSGlenn Lagasse *
747f169c0eaSGlenn Lagasse * Parameters:
748f169c0eaSGlenn Lagasse * file - the path to the capability file we want to parse.
749f169c0eaSGlenn Lagasse * vers - the version string that will be passed back.
750f169c0eaSGlenn Lagasse * Return:
751f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
752f169c0eaSGlenn Lagasse * be_errno_t - Failed to find version
753f169c0eaSGlenn Lagasse * Scope:
754f169c0eaSGlenn Lagasse * Private
755f169c0eaSGlenn Lagasse */
756f169c0eaSGlenn Lagasse static int
get_ver_from_capfile(char * file,char ** vers)757f169c0eaSGlenn Lagasse get_ver_from_capfile(char *file, char **vers)
758f169c0eaSGlenn Lagasse {
759f169c0eaSGlenn Lagasse FILE *fp = NULL;
760f169c0eaSGlenn Lagasse char line[BUFSIZ];
761f169c0eaSGlenn Lagasse char *last = NULL;
762f169c0eaSGlenn Lagasse int err = BE_SUCCESS;
763f169c0eaSGlenn Lagasse errno = 0;
764f169c0eaSGlenn Lagasse
765f169c0eaSGlenn Lagasse if (!be_has_grub()) {
766f169c0eaSGlenn Lagasse be_print_err(gettext("get_ver_from_capfile: Not supported "
767f169c0eaSGlenn Lagasse "on this architecture\n"));
768f169c0eaSGlenn Lagasse return (BE_ERR_NOTSUP);
769f169c0eaSGlenn Lagasse }
770f169c0eaSGlenn Lagasse
771f169c0eaSGlenn Lagasse /*
772f169c0eaSGlenn Lagasse * Set version string to NULL; the only case this shouldn't be set
773f169c0eaSGlenn Lagasse * to be NULL is when we've actually found a version in the capability
774f169c0eaSGlenn Lagasse * file, which is set below.
775f169c0eaSGlenn Lagasse */
776f169c0eaSGlenn Lagasse *vers = NULL;
777f169c0eaSGlenn Lagasse
778f169c0eaSGlenn Lagasse /*
779f169c0eaSGlenn Lagasse * If the capability file doesn't exist, we're returning success
780f169c0eaSGlenn Lagasse * because on older releases, the capability file did not exist
781f169c0eaSGlenn Lagasse * so this is a valid scenario.
782f169c0eaSGlenn Lagasse */
783f169c0eaSGlenn Lagasse if (access(file, F_OK) == 0) {
784f169c0eaSGlenn Lagasse if ((fp = fopen(file, "r")) == NULL) {
785f169c0eaSGlenn Lagasse err = errno;
786f169c0eaSGlenn Lagasse be_print_err(gettext("get_ver_from_capfile: failed to "
787f169c0eaSGlenn Lagasse "open file %s with error %s\n"), file,
788f169c0eaSGlenn Lagasse strerror(err));
789f169c0eaSGlenn Lagasse err = errno_to_be_err(err);
790f169c0eaSGlenn Lagasse return (err);
791f169c0eaSGlenn Lagasse }
792f169c0eaSGlenn Lagasse
793f169c0eaSGlenn Lagasse while (fgets(line, BUFSIZ, fp)) {
794f169c0eaSGlenn Lagasse char *tok = strtok_r(line, "=", &last);
795f169c0eaSGlenn Lagasse
796f169c0eaSGlenn Lagasse if (tok == NULL || tok[0] == '#') {
797f169c0eaSGlenn Lagasse continue;
798f169c0eaSGlenn Lagasse } else if (strcmp(tok, "VERSION") == 0) {
799f169c0eaSGlenn Lagasse *vers = strdup(last);
800f169c0eaSGlenn Lagasse break;
801f169c0eaSGlenn Lagasse }
802f169c0eaSGlenn Lagasse }
803f169c0eaSGlenn Lagasse (void) fclose(fp);
804f169c0eaSGlenn Lagasse }
805f169c0eaSGlenn Lagasse
806f169c0eaSGlenn Lagasse return (BE_SUCCESS);
807f169c0eaSGlenn Lagasse }
808f169c0eaSGlenn Lagasse
809f169c0eaSGlenn Lagasse /*
810a63c99a2SToomas Soome * To be able to boot EFI labeled disks, stage1 needs to be written
8111a902ef8SHans Rosenfeld * into the MBR. We do not do this if we're on disks with a traditional
8121a902ef8SHans Rosenfeld * fdisk partition table only, or if any foreign EFI partitions exist.
8131a902ef8SHans Rosenfeld * In the trivial case of a whole-disk vdev we always write stage1 into
8141a902ef8SHans Rosenfeld * the MBR.
8151a902ef8SHans Rosenfeld */
8161a902ef8SHans Rosenfeld static boolean_t
be_do_install_mbr(char * diskname,nvlist_t * child)817a63c99a2SToomas Soome be_do_install_mbr(char *diskname, nvlist_t *child)
8181a902ef8SHans Rosenfeld {
8191a902ef8SHans Rosenfeld struct uuid allowed_uuids[] = {
8201a902ef8SHans Rosenfeld EFI_UNUSED,
8211a902ef8SHans Rosenfeld EFI_RESV1,
8221a902ef8SHans Rosenfeld EFI_BOOT,
8231a902ef8SHans Rosenfeld EFI_ROOT,
8241a902ef8SHans Rosenfeld EFI_SWAP,
8251a902ef8SHans Rosenfeld EFI_USR,
8261a902ef8SHans Rosenfeld EFI_BACKUP,
8271a902ef8SHans Rosenfeld EFI_RESV2,
8281a902ef8SHans Rosenfeld EFI_VAR,
8291a902ef8SHans Rosenfeld EFI_HOME,
8301a902ef8SHans Rosenfeld EFI_ALTSCTR,
8311a902ef8SHans Rosenfeld EFI_RESERVED,
8321a902ef8SHans Rosenfeld EFI_SYSTEM,
8331a902ef8SHans Rosenfeld EFI_BIOS_BOOT,
8341a902ef8SHans Rosenfeld EFI_SYMC_PUB,
8351a902ef8SHans Rosenfeld EFI_SYMC_CDS
8361a902ef8SHans Rosenfeld };
8371a902ef8SHans Rosenfeld
8381a902ef8SHans Rosenfeld uint64_t whole;
8391a902ef8SHans Rosenfeld struct dk_gpt *gpt;
8401a902ef8SHans Rosenfeld struct uuid *u;
8411a902ef8SHans Rosenfeld int fd, npart, i, j;
8421a902ef8SHans Rosenfeld
8431a902ef8SHans Rosenfeld (void) nvlist_lookup_uint64(child, ZPOOL_CONFIG_WHOLE_DISK,
8441a902ef8SHans Rosenfeld &whole);
8451a902ef8SHans Rosenfeld
8461a902ef8SHans Rosenfeld if (whole)
8471a902ef8SHans Rosenfeld return (B_TRUE);
8481a902ef8SHans Rosenfeld
8491a902ef8SHans Rosenfeld if ((fd = open(diskname, O_RDONLY|O_NDELAY)) < 0)
8501a902ef8SHans Rosenfeld return (B_FALSE);
8511a902ef8SHans Rosenfeld
8521a902ef8SHans Rosenfeld if ((npart = efi_alloc_and_read(fd, &gpt)) <= 0)
8531a902ef8SHans Rosenfeld return (B_FALSE);
8541a902ef8SHans Rosenfeld
8551a902ef8SHans Rosenfeld for (i = 0; i != npart; i++) {
8561a902ef8SHans Rosenfeld int match = 0;
8571a902ef8SHans Rosenfeld
8581a902ef8SHans Rosenfeld u = &gpt->efi_parts[i].p_guid;
8591a902ef8SHans Rosenfeld
8601a902ef8SHans Rosenfeld for (j = 0;
8611a902ef8SHans Rosenfeld j != sizeof (allowed_uuids) / sizeof (struct uuid);
8621a902ef8SHans Rosenfeld j++)
8631a902ef8SHans Rosenfeld if (bcmp(u, &allowed_uuids[j],
8641a902ef8SHans Rosenfeld sizeof (struct uuid)) == 0)
8651a902ef8SHans Rosenfeld match++;
8661a902ef8SHans Rosenfeld
8671a902ef8SHans Rosenfeld if (match == 0)
8681a902ef8SHans Rosenfeld return (B_FALSE);
8691a902ef8SHans Rosenfeld }
8701a902ef8SHans Rosenfeld
8711a902ef8SHans Rosenfeld return (B_TRUE);
8721a902ef8SHans Rosenfeld }
8731a902ef8SHans Rosenfeld
8741a902ef8SHans Rosenfeld static int
be_do_installboot_helper(zpool_handle_t * zphp,nvlist_t * child,char * stage1,char * stage2,uint16_t flags)875a63c99a2SToomas Soome be_do_installboot_helper(zpool_handle_t *zphp, nvlist_t *child, char *stage1,
876c7c0ceafSToomas Soome char *stage2, uint16_t flags)
8771a902ef8SHans Rosenfeld {
878a63c99a2SToomas Soome char install_cmd[MAXPATHLEN];
8791a902ef8SHans Rosenfeld char be_run_cmd_errbuf[BUFSIZ];
880c7c0ceafSToomas Soome char be_run_cmd_outbuf[BUFSIZ];
8811a902ef8SHans Rosenfeld char diskname[MAXPATHLEN];
8821a902ef8SHans Rosenfeld char *vname;
8831a902ef8SHans Rosenfeld char *path, *dsk_ptr;
884a63c99a2SToomas Soome char *flag = "";
885c7c0ceafSToomas Soome int ret;
88608e9b2dfSHans Rosenfeld vdev_stat_t *vs;
88708e9b2dfSHans Rosenfeld uint_t vsc;
8881a902ef8SHans Rosenfeld
8891a902ef8SHans Rosenfeld if (nvlist_lookup_string(child, ZPOOL_CONFIG_PATH, &path) != 0) {
890a63c99a2SToomas Soome be_print_err(gettext("be_do_installboot: "
8911a902ef8SHans Rosenfeld "failed to get device path\n"));
8921a902ef8SHans Rosenfeld return (BE_ERR_NODEV);
8931a902ef8SHans Rosenfeld }
8941a902ef8SHans Rosenfeld
89508e9b2dfSHans Rosenfeld if ((nvlist_lookup_uint64_array(child, ZPOOL_CONFIG_VDEV_STATS,
89608e9b2dfSHans Rosenfeld (uint64_t **)&vs, &vsc) != 0) ||
89708e9b2dfSHans Rosenfeld vs->vs_state < VDEV_STATE_DEGRADED) {
89808e9b2dfSHans Rosenfeld /*
89908e9b2dfSHans Rosenfeld * Don't try to run installgrub on a vdev that is not ONLINE
90008e9b2dfSHans Rosenfeld * or DEGRADED. Try to print a warning for each such vdev.
90108e9b2dfSHans Rosenfeld */
90208e9b2dfSHans Rosenfeld be_print_err(gettext("be_do_installboot: "
90308e9b2dfSHans Rosenfeld "vdev %s is %s, can't install boot loader\n"),
90408e9b2dfSHans Rosenfeld path, zpool_state_to_name(vs->vs_state, vs->vs_aux));
90508e9b2dfSHans Rosenfeld free(path);
90608e9b2dfSHans Rosenfeld return (BE_SUCCESS);
90708e9b2dfSHans Rosenfeld }
90808e9b2dfSHans Rosenfeld
9091a902ef8SHans Rosenfeld /*
9101a902ef8SHans Rosenfeld * Modify the vdev path to point to the raw disk.
9111a902ef8SHans Rosenfeld */
9121a902ef8SHans Rosenfeld path = strdup(path);
9131a902ef8SHans Rosenfeld if (path == NULL)
9141a902ef8SHans Rosenfeld return (BE_ERR_NOMEM);
9151a902ef8SHans Rosenfeld
9161a902ef8SHans Rosenfeld dsk_ptr = strstr(path, "/dsk/");
9171a902ef8SHans Rosenfeld if (dsk_ptr != NULL) {
9181a902ef8SHans Rosenfeld *dsk_ptr = '\0';
9191a902ef8SHans Rosenfeld dsk_ptr++;
9201a902ef8SHans Rosenfeld } else {
9211a902ef8SHans Rosenfeld dsk_ptr = "";
9221a902ef8SHans Rosenfeld }
9231a902ef8SHans Rosenfeld
9241a902ef8SHans Rosenfeld (void) snprintf(diskname, sizeof (diskname), "%s/r%s", path, dsk_ptr);
9251a902ef8SHans Rosenfeld free(path);
9261a902ef8SHans Rosenfeld
9271a902ef8SHans Rosenfeld vname = zpool_vdev_name(g_zfs, zphp, child, B_FALSE);
9281a902ef8SHans Rosenfeld if (vname == NULL) {
929a63c99a2SToomas Soome be_print_err(gettext("be_do_installboot: "
9301a902ef8SHans Rosenfeld "failed to get device name: %s\n"),
9311a902ef8SHans Rosenfeld libzfs_error_description(g_zfs));
9321a902ef8SHans Rosenfeld return (zfs_err_to_be_err(g_zfs));
9331a902ef8SHans Rosenfeld }
9341a902ef8SHans Rosenfeld
935a63c99a2SToomas Soome if (be_is_isa("i386")) {
936c7c0ceafSToomas Soome uint16_t force = flags & BE_INSTALLBOOT_FLAG_FORCE;
937c7c0ceafSToomas Soome uint16_t mbr = flags & BE_INSTALLBOOT_FLAG_MBR;
938c7c0ceafSToomas Soome
939c7c0ceafSToomas Soome if (force == BE_INSTALLBOOT_FLAG_FORCE) {
940c7c0ceafSToomas Soome if (mbr == BE_INSTALLBOOT_FLAG_MBR ||
941c7c0ceafSToomas Soome be_do_install_mbr(diskname, child))
942c7c0ceafSToomas Soome flag = "-F -m -f";
943c7c0ceafSToomas Soome else
944c7c0ceafSToomas Soome flag = "-F";
945c7c0ceafSToomas Soome } else {
946c7c0ceafSToomas Soome if (mbr == BE_INSTALLBOOT_FLAG_MBR ||
947c7c0ceafSToomas Soome be_do_install_mbr(diskname, child))
948a63c99a2SToomas Soome flag = "-m -f";
949c7c0ceafSToomas Soome }
950c7c0ceafSToomas Soome
951a63c99a2SToomas Soome (void) snprintf(install_cmd, sizeof (install_cmd),
952a63c99a2SToomas Soome "%s %s %s %s %s", BE_INSTALL_GRUB, flag,
953a63c99a2SToomas Soome stage1, stage2, diskname);
95466f18efaSToomas Soome } else if (be_is_isa("sparc")) {
955c7c0ceafSToomas Soome if ((flags & BE_INSTALLBOOT_FLAG_FORCE) ==
956c7c0ceafSToomas Soome BE_INSTALLBOOT_FLAG_FORCE)
957c7c0ceafSToomas Soome flag = "-f -F zfs";
958c7c0ceafSToomas Soome else
959a63c99a2SToomas Soome flag = "-F zfs";
960c7c0ceafSToomas Soome
961a63c99a2SToomas Soome (void) snprintf(install_cmd, sizeof (install_cmd),
962a63c99a2SToomas Soome "%s %s %s %s", BE_INSTALL_BOOT, flag, stage2, diskname);
96366f18efaSToomas Soome } else {
96466f18efaSToomas Soome be_print_err(gettext("be_do_installboot: unsupported "
96566f18efaSToomas Soome "architecture.\n"));
96666f18efaSToomas Soome return (BE_ERR_BOOTFILE_INST);
967a63c99a2SToomas Soome }
968a63c99a2SToomas Soome
969c7c0ceafSToomas Soome *be_run_cmd_outbuf = '\0';
970c7c0ceafSToomas Soome *be_run_cmd_errbuf = '\0';
971c7c0ceafSToomas Soome
972c7c0ceafSToomas Soome ret = be_run_cmd(install_cmd, be_run_cmd_errbuf, BUFSIZ,
973c7c0ceafSToomas Soome be_run_cmd_outbuf, BUFSIZ);
974c7c0ceafSToomas Soome
975c7c0ceafSToomas Soome if (ret != BE_SUCCESS) {
976a63c99a2SToomas Soome be_print_err(gettext("be_do_installboot: install "
9771a902ef8SHans Rosenfeld "failed for device %s.\n"), vname);
978c7c0ceafSToomas Soome ret = BE_ERR_BOOTFILE_INST;
979c7c0ceafSToomas Soome }
980c7c0ceafSToomas Soome
981c7c0ceafSToomas Soome be_print_err(gettext(" Command: \"%s\"\n"), install_cmd);
982c7c0ceafSToomas Soome if (be_run_cmd_outbuf[0] != 0) {
983c7c0ceafSToomas Soome be_print_err(gettext(" Output:\n"));
984c7c0ceafSToomas Soome be_print_err("%s", be_run_cmd_outbuf);
985c7c0ceafSToomas Soome }
986c7c0ceafSToomas Soome
987c7c0ceafSToomas Soome if (be_run_cmd_errbuf[0] != 0) {
988c7c0ceafSToomas Soome be_print_err(gettext(" Errors:\n"));
9891a902ef8SHans Rosenfeld be_print_err("%s", be_run_cmd_errbuf);
9901a902ef8SHans Rosenfeld }
9911a902ef8SHans Rosenfeld free(vname);
9921a902ef8SHans Rosenfeld
993c7c0ceafSToomas Soome return (ret);
9941a902ef8SHans Rosenfeld }
9951a902ef8SHans Rosenfeld
9961a902ef8SHans Rosenfeld /*
997a63c99a2SToomas Soome * Function: be_do_copy_grub_cap
998a63c99a2SToomas Soome * Description: This function will copy grub capability file to BE.
999f169c0eaSGlenn Lagasse *
1000f169c0eaSGlenn Lagasse * Parameters:
1001f169c0eaSGlenn Lagasse * bt - The transaction data for the BE we're activating.
1002f169c0eaSGlenn Lagasse * Return:
1003f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
1004f169c0eaSGlenn Lagasse * be_errno_t - Failure
1005f169c0eaSGlenn Lagasse *
1006f169c0eaSGlenn Lagasse * Scope:
1007f169c0eaSGlenn Lagasse * Private
1008f169c0eaSGlenn Lagasse */
1009f169c0eaSGlenn Lagasse static int
be_do_copy_grub_cap(be_transaction_data_t * bt)1010a63c99a2SToomas Soome be_do_copy_grub_cap(be_transaction_data_t *bt)
1011f169c0eaSGlenn Lagasse {
1012f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL;
1013f169c0eaSGlenn Lagasse char cap_file[MAXPATHLEN];
1014f169c0eaSGlenn Lagasse char zpool_cap_file[MAXPATHLEN];
1015a63c99a2SToomas Soome char line[BUFSIZ];
1016a63c99a2SToomas Soome char *tmp_mntpnt = NULL;
1017a63c99a2SToomas Soome char *orig_mntpnt = NULL;
1018a63c99a2SToomas Soome char *pool_mntpnt = NULL;
1019a63c99a2SToomas Soome FILE *cap_fp = NULL;
1020a63c99a2SToomas Soome FILE *zpool_cap_fp = NULL;
1021f169c0eaSGlenn Lagasse int err = 0;
1022a63c99a2SToomas Soome int ret = BE_SUCCESS;
1023f169c0eaSGlenn Lagasse boolean_t pool_mounted = B_FALSE;
1024a63c99a2SToomas Soome boolean_t be_mounted = B_FALSE;
1025f169c0eaSGlenn Lagasse
1026f169c0eaSGlenn Lagasse /*
1027a21e1692SToomas Soome * first get BE dataset mountpoint, we can free all the resources
1028a21e1692SToomas Soome * once cap_file is built, leaving only be unmount to be done.
1029f169c0eaSGlenn Lagasse */
1030a21e1692SToomas Soome if ((zhp = zfs_open(g_zfs, bt->obe_root_ds, ZFS_TYPE_FILESYSTEM)) ==
1031a21e1692SToomas Soome NULL) {
1032a21e1692SToomas Soome be_print_err(gettext("be_do_copy_grub_cap: failed to "
1033a21e1692SToomas Soome "open BE root dataset (%s): %s\n"), bt->obe_root_ds,
1034a21e1692SToomas Soome libzfs_error_description(g_zfs));
1035a21e1692SToomas Soome return (zfs_err_to_be_err(g_zfs));
1036a21e1692SToomas Soome }
1037a21e1692SToomas Soome
1038a21e1692SToomas Soome if (!zfs_is_mounted(zhp, &tmp_mntpnt)) {
1039a21e1692SToomas Soome if ((ret = _be_mount(bt->obe_name, &tmp_mntpnt,
1040a21e1692SToomas Soome BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
1041a21e1692SToomas Soome be_print_err(gettext("be_do_copy_grub_cap: failed to "
1042a21e1692SToomas Soome "mount BE (%s)\n"), bt->obe_name);
1043a21e1692SToomas Soome ZFS_CLOSE(zhp);
1044a21e1692SToomas Soome goto done;
1045a21e1692SToomas Soome }
1046a21e1692SToomas Soome be_mounted = B_TRUE;
1047a21e1692SToomas Soome }
1048a21e1692SToomas Soome ZFS_CLOSE(zhp); /* BE dataset handle is not needed any more */
1049a21e1692SToomas Soome
1050a21e1692SToomas Soome (void) snprintf(cap_file, sizeof (cap_file), "%s%s", tmp_mntpnt,
1051a21e1692SToomas Soome BE_CAP_FILE);
1052a21e1692SToomas Soome free(tmp_mntpnt);
1053a21e1692SToomas Soome
1054a21e1692SToomas Soome /* get pool root dataset mountpoint */
1055a63c99a2SToomas Soome zhp = zfs_open(g_zfs, bt->obe_zpool, ZFS_TYPE_FILESYSTEM);
1056a63c99a2SToomas Soome if (zhp == NULL) {
1057a21e1692SToomas Soome be_print_err(gettext("be_do_copy_grub_cap: zfs_open "
1058f169c0eaSGlenn Lagasse "failed: %s\n"), libzfs_error_description(g_zfs));
1059a21e1692SToomas Soome ret = zfs_err_to_be_err(g_zfs);
1060a21e1692SToomas Soome goto done;
1061f169c0eaSGlenn Lagasse }
1062f169c0eaSGlenn Lagasse
1063f169c0eaSGlenn Lagasse /*
1064f169c0eaSGlenn Lagasse * Check to see if the pool's dataset is mounted. If it isn't we'll
1065f169c0eaSGlenn Lagasse * attempt to mount it.
1066f169c0eaSGlenn Lagasse */
1067a21e1692SToomas Soome if ((ret = be_mount_pool(zhp, &tmp_mntpnt,
1068f169c0eaSGlenn Lagasse &orig_mntpnt, &pool_mounted)) != BE_SUCCESS) {
1069a21e1692SToomas Soome be_print_err(gettext("be_do_copy_grub_cap: pool dataset "
1070f169c0eaSGlenn Lagasse "(%s) could not be mounted\n"), bt->obe_zpool);
1071f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
1072a21e1692SToomas Soome goto done;
1073f169c0eaSGlenn Lagasse }
1074f169c0eaSGlenn Lagasse
1075f169c0eaSGlenn Lagasse /*
1076f169c0eaSGlenn Lagasse * Get the mountpoint for the root pool dataset.
1077a21e1692SToomas Soome * NOTE: zhp must be kept for _be_unmount_pool()
1078f169c0eaSGlenn Lagasse */
1079f169c0eaSGlenn Lagasse if (!zfs_is_mounted(zhp, &pool_mntpnt)) {
1080a21e1692SToomas Soome be_print_err(gettext("be_do_copy_grub_cap: pool "
1081f169c0eaSGlenn Lagasse "dataset (%s) is not mounted. Can't check the grub "
1082f169c0eaSGlenn Lagasse "version from the grub capability file.\n"), bt->obe_zpool);
1083f169c0eaSGlenn Lagasse ret = BE_ERR_NO_MENU;
1084f169c0eaSGlenn Lagasse goto done;
1085f169c0eaSGlenn Lagasse }
1086f169c0eaSGlenn Lagasse
1087f169c0eaSGlenn Lagasse (void) snprintf(zpool_cap_file, sizeof (zpool_cap_file), "%s%s",
1088f169c0eaSGlenn Lagasse pool_mntpnt, BE_CAP_FILE);
1089f169c0eaSGlenn Lagasse free(pool_mntpnt);
1090a63c99a2SToomas Soome
1091f169c0eaSGlenn Lagasse if ((cap_fp = fopen(cap_file, "r")) == NULL) {
1092f169c0eaSGlenn Lagasse err = errno;
1093a21e1692SToomas Soome be_print_err(gettext("be_do_copy_grub_cap: failed to open grub "
1094f169c0eaSGlenn Lagasse "capability file\n"));
1095f169c0eaSGlenn Lagasse ret = errno_to_be_err(err);
1096f169c0eaSGlenn Lagasse goto done;
1097f169c0eaSGlenn Lagasse }
1098f169c0eaSGlenn Lagasse if ((zpool_cap_fp = fopen(zpool_cap_file, "w")) == NULL) {
1099f169c0eaSGlenn Lagasse err = errno;
1100a21e1692SToomas Soome be_print_err(gettext("be_do_copy_grub_cap: failed to open new "
1101f169c0eaSGlenn Lagasse "grub capability file\n"));
1102f169c0eaSGlenn Lagasse ret = errno_to_be_err(err);
1103f169c0eaSGlenn Lagasse (void) fclose(cap_fp);
1104f169c0eaSGlenn Lagasse goto done;
1105f169c0eaSGlenn Lagasse }
1106f169c0eaSGlenn Lagasse
1107f169c0eaSGlenn Lagasse while (fgets(line, BUFSIZ, cap_fp)) {
1108f169c0eaSGlenn Lagasse (void) fputs(line, zpool_cap_fp);
1109f169c0eaSGlenn Lagasse }
1110f169c0eaSGlenn Lagasse
1111f169c0eaSGlenn Lagasse (void) fclose(zpool_cap_fp);
1112f169c0eaSGlenn Lagasse (void) fclose(cap_fp);
1113f169c0eaSGlenn Lagasse
1114f169c0eaSGlenn Lagasse done:
1115a63c99a2SToomas Soome if (be_mounted)
1116a63c99a2SToomas Soome (void) _be_unmount(bt->obe_name, 0);
1117a63c99a2SToomas Soome
1118f169c0eaSGlenn Lagasse if (pool_mounted) {
1119a21e1692SToomas Soome err = be_unmount_pool(zhp, tmp_mntpnt, orig_mntpnt);
1120f169c0eaSGlenn Lagasse if (ret == BE_SUCCESS)
1121a21e1692SToomas Soome ret = err;
1122f169c0eaSGlenn Lagasse free(orig_mntpnt);
1123a21e1692SToomas Soome free(tmp_mntpnt);
1124a21e1692SToomas Soome zfs_close(zhp);
1125f169c0eaSGlenn Lagasse }
1126a63c99a2SToomas Soome return (ret);
1127a63c99a2SToomas Soome }
1128a63c99a2SToomas Soome
1129a63c99a2SToomas Soome /*
1130a63c99a2SToomas Soome * Function: be_is_install_needed
1131a63c99a2SToomas Soome * Description: Check detached version files to detect if bootloader
1132a63c99a2SToomas Soome * install/update is needed.
1133a63c99a2SToomas Soome *
1134a63c99a2SToomas Soome * Parameters:
1135a63c99a2SToomas Soome * bt - The transaction data for the BE we're activating.
1136a63c99a2SToomas Soome * update - set B_TRUE is update is needed.
1137a63c99a2SToomas Soome * Return:
1138a63c99a2SToomas Soome * BE_SUCCESS - Success
1139a63c99a2SToomas Soome * be_errno_t - Failure
1140a63c99a2SToomas Soome *
1141a63c99a2SToomas Soome * Scope:
1142a63c99a2SToomas Soome * Private
1143a63c99a2SToomas Soome */
1144a63c99a2SToomas Soome static int
be_is_install_needed(be_transaction_data_t * bt,boolean_t * update)1145a63c99a2SToomas Soome be_is_install_needed(be_transaction_data_t *bt, boolean_t *update)
1146a63c99a2SToomas Soome {
1147a63c99a2SToomas Soome int ret = BE_SUCCESS;
1148a63c99a2SToomas Soome char *cur_vers = NULL, *new_vers = NULL;
1149a63c99a2SToomas Soome
1150a63c99a2SToomas Soome assert(bt != NULL);
1151a63c99a2SToomas Soome assert(update != NULL);
1152a63c99a2SToomas Soome
1153a63c99a2SToomas Soome if (!be_has_grub()) {
1154a63c99a2SToomas Soome /*
1155a63c99a2SToomas Soome * no detached versioning, let installboot to manage
1156a63c99a2SToomas Soome * versioning.
1157a63c99a2SToomas Soome */
1158a63c99a2SToomas Soome *update = B_TRUE;
1159a63c99a2SToomas Soome return (ret);
1160a63c99a2SToomas Soome }
1161a63c99a2SToomas Soome
1162a63c99a2SToomas Soome *update = B_FALSE; /* set default */
1163a63c99a2SToomas Soome
1164a63c99a2SToomas Soome /*
1165a63c99a2SToomas Soome * We need to check to see if the version number from
1166a63c99a2SToomas Soome * the BE being activated is greater than the current
1167a63c99a2SToomas Soome * one.
1168a63c99a2SToomas Soome */
1169a63c99a2SToomas Soome ret = be_get_grub_vers(bt, &cur_vers, &new_vers);
1170a63c99a2SToomas Soome if (ret != BE_SUCCESS) {
1171a63c99a2SToomas Soome be_print_err(gettext("be_activate: failed to get grub "
1172a63c99a2SToomas Soome "versions from capability files.\n"));
1173a63c99a2SToomas Soome return (ret);
1174a63c99a2SToomas Soome }
1175a63c99a2SToomas Soome /* update if we have both versions and can compare */
1176a63c99a2SToomas Soome if (cur_vers != NULL) {
1177a63c99a2SToomas Soome if (new_vers != NULL) {
1178a63c99a2SToomas Soome if (atof(cur_vers) < atof(new_vers))
1179a63c99a2SToomas Soome *update = B_TRUE;
1180a63c99a2SToomas Soome free(new_vers);
1181a63c99a2SToomas Soome }
1182a63c99a2SToomas Soome free(cur_vers);
1183a63c99a2SToomas Soome } else if (new_vers != NULL) {
1184a63c99a2SToomas Soome /* we only got new version - update */
1185a63c99a2SToomas Soome *update = B_TRUE;
1186a63c99a2SToomas Soome free(new_vers);
1187a63c99a2SToomas Soome }
1188a63c99a2SToomas Soome return (ret);
1189a63c99a2SToomas Soome }
1190a63c99a2SToomas Soome
1191a63c99a2SToomas Soome /*
1192a63c99a2SToomas Soome * Function: be_do_installboot
1193a63c99a2SToomas Soome * Description: This function runs installgrub/installboot using the boot
1194a63c99a2SToomas Soome * loader files from the BE we're activating and installing
1195a63c99a2SToomas Soome * them on the pool the BE lives in.
1196a63c99a2SToomas Soome *
1197a63c99a2SToomas Soome * Parameters:
1198a63c99a2SToomas Soome * bt - The transaction data for the BE we're activating.
1199c7c0ceafSToomas Soome * flags - flags for bootloader install
1200a63c99a2SToomas Soome * Return:
1201a63c99a2SToomas Soome * BE_SUCCESS - Success
1202a63c99a2SToomas Soome * be_errno_t - Failure
1203a63c99a2SToomas Soome *
1204a63c99a2SToomas Soome * Scope:
1205a63c99a2SToomas Soome * Private
1206a63c99a2SToomas Soome */
1207a63c99a2SToomas Soome static int
be_do_installboot(be_transaction_data_t * bt,uint16_t flags)1208c7c0ceafSToomas Soome be_do_installboot(be_transaction_data_t *bt, uint16_t flags)
1209a63c99a2SToomas Soome {
1210a63c99a2SToomas Soome zpool_handle_t *zphp = NULL;
1211a63c99a2SToomas Soome zfs_handle_t *zhp = NULL;
1212a63c99a2SToomas Soome nvlist_t **child, *nv, *config;
1213a63c99a2SToomas Soome uint_t c, children = 0;
1214a63c99a2SToomas Soome char *tmp_mntpt = NULL;
1215a63c99a2SToomas Soome char stage1[MAXPATHLEN];
1216a63c99a2SToomas Soome char stage2[MAXPATHLEN];
1217a63c99a2SToomas Soome char *vname;
1218a63c99a2SToomas Soome int ret = BE_SUCCESS;
1219a63c99a2SToomas Soome boolean_t be_mounted = B_FALSE;
1220a63c99a2SToomas Soome boolean_t update = B_FALSE;
1221a63c99a2SToomas Soome
1222a63c99a2SToomas Soome /*
1223a63c99a2SToomas Soome * check versions. This call is to support detached
1224a63c99a2SToomas Soome * version implementation like grub. Embedded versioning is
1225a63c99a2SToomas Soome * checked by actual installer.
1226a63c99a2SToomas Soome */
1227c7c0ceafSToomas Soome if ((flags & BE_INSTALLBOOT_FLAG_FORCE) != BE_INSTALLBOOT_FLAG_FORCE) {
1228a63c99a2SToomas Soome ret = be_is_install_needed(bt, &update);
1229a63c99a2SToomas Soome if (ret != BE_SUCCESS || update == B_FALSE)
1230a63c99a2SToomas Soome return (ret);
1231c7c0ceafSToomas Soome }
1232a63c99a2SToomas Soome
1233a63c99a2SToomas Soome if ((zhp = zfs_open(g_zfs, bt->obe_root_ds, ZFS_TYPE_FILESYSTEM)) ==
1234a63c99a2SToomas Soome NULL) {
1235a63c99a2SToomas Soome be_print_err(gettext("be_do_installboot: failed to "
1236a63c99a2SToomas Soome "open BE root dataset (%s): %s\n"), bt->obe_root_ds,
1237a63c99a2SToomas Soome libzfs_error_description(g_zfs));
1238a63c99a2SToomas Soome ret = zfs_err_to_be_err(g_zfs);
1239a63c99a2SToomas Soome return (ret);
1240a63c99a2SToomas Soome }
1241a63c99a2SToomas Soome if (!zfs_is_mounted(zhp, &tmp_mntpt)) {
1242a63c99a2SToomas Soome if ((ret = _be_mount(bt->obe_name, &tmp_mntpt,
1243a63c99a2SToomas Soome BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
1244a63c99a2SToomas Soome be_print_err(gettext("be_do_installboot: failed to "
1245a63c99a2SToomas Soome "mount BE (%s)\n"), bt->obe_name);
1246a63c99a2SToomas Soome ZFS_CLOSE(zhp);
1247a63c99a2SToomas Soome return (ret);
1248a63c99a2SToomas Soome }
1249a63c99a2SToomas Soome be_mounted = B_TRUE;
1250a63c99a2SToomas Soome }
1251a63c99a2SToomas Soome ZFS_CLOSE(zhp);
1252a63c99a2SToomas Soome
1253a63c99a2SToomas Soome if (be_has_grub()) {
1254a63c99a2SToomas Soome (void) snprintf(stage1, sizeof (stage1), "%s%s",
1255a63c99a2SToomas Soome tmp_mntpt, BE_GRUB_STAGE_1);
1256a63c99a2SToomas Soome (void) snprintf(stage2, sizeof (stage2), "%s%s",
1257a63c99a2SToomas Soome tmp_mntpt, BE_GRUB_STAGE_2);
1258a63c99a2SToomas Soome } else {
1259a63c99a2SToomas Soome char *platform = be_get_platform();
1260a63c99a2SToomas Soome
1261a63c99a2SToomas Soome if (platform == NULL) {
1262a63c99a2SToomas Soome be_print_err(gettext("be_do_installboot: failed to "
1263a63c99a2SToomas Soome "detect system platform name\n"));
1264a63c99a2SToomas Soome if (be_mounted)
1265a63c99a2SToomas Soome (void) _be_unmount(bt->obe_name, 0);
1266a63c99a2SToomas Soome free(tmp_mntpt);
1267a63c99a2SToomas Soome return (BE_ERR_BOOTFILE_INST);
1268a63c99a2SToomas Soome }
1269a63c99a2SToomas Soome
1270a63c99a2SToomas Soome stage1[0] = '\0'; /* sparc has no stage1 */
1271a63c99a2SToomas Soome (void) snprintf(stage2, sizeof (stage2),
1272a63c99a2SToomas Soome "%s/usr/platform/%s%s", tmp_mntpt,
1273a63c99a2SToomas Soome platform, BE_SPARC_BOOTBLK);
1274a63c99a2SToomas Soome }
1275a63c99a2SToomas Soome
1276a63c99a2SToomas Soome if ((zphp = zpool_open(g_zfs, bt->obe_zpool)) == NULL) {
1277a63c99a2SToomas Soome be_print_err(gettext("be_do_installboot: failed to open "
1278a63c99a2SToomas Soome "pool (%s): %s\n"), bt->obe_zpool,
1279a63c99a2SToomas Soome libzfs_error_description(g_zfs));
1280a63c99a2SToomas Soome ret = zfs_err_to_be_err(g_zfs);
1281a63c99a2SToomas Soome if (be_mounted)
1282a63c99a2SToomas Soome (void) _be_unmount(bt->obe_name, 0);
1283a63c99a2SToomas Soome free(tmp_mntpt);
1284a63c99a2SToomas Soome return (ret);
1285a63c99a2SToomas Soome }
1286a63c99a2SToomas Soome
1287a63c99a2SToomas Soome if ((config = zpool_get_config(zphp, NULL)) == NULL) {
1288a63c99a2SToomas Soome be_print_err(gettext("be_do_installboot: failed to get zpool "
1289a63c99a2SToomas Soome "configuration information. %s\n"),
1290a63c99a2SToomas Soome libzfs_error_description(g_zfs));
1291a63c99a2SToomas Soome ret = zfs_err_to_be_err(g_zfs);
1292a63c99a2SToomas Soome goto done;
1293a63c99a2SToomas Soome }
1294a63c99a2SToomas Soome
1295a63c99a2SToomas Soome /*
1296a63c99a2SToomas Soome * Get the vdev tree
1297a63c99a2SToomas Soome */
1298a63c99a2SToomas Soome if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) != 0) {
1299a63c99a2SToomas Soome be_print_err(gettext("be_do_installboot: failed to get vdev "
1300a63c99a2SToomas Soome "tree: %s\n"), libzfs_error_description(g_zfs));
1301a63c99a2SToomas Soome ret = zfs_err_to_be_err(g_zfs);
1302a63c99a2SToomas Soome goto done;
1303a63c99a2SToomas Soome }
1304a63c99a2SToomas Soome
1305a63c99a2SToomas Soome if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1306a63c99a2SToomas Soome &children) != 0) {
1307a63c99a2SToomas Soome be_print_err(gettext("be_do_installboot: failed to traverse "
1308a63c99a2SToomas Soome "the vdev tree: %s\n"), libzfs_error_description(g_zfs));
1309a63c99a2SToomas Soome ret = zfs_err_to_be_err(g_zfs);
1310a63c99a2SToomas Soome goto done;
1311a63c99a2SToomas Soome }
1312a63c99a2SToomas Soome for (c = 0; c < children; c++) {
1313a63c99a2SToomas Soome uint_t i, nchildren = 0;
1314a63c99a2SToomas Soome nvlist_t **nvchild;
1315a63c99a2SToomas Soome vname = zpool_vdev_name(g_zfs, zphp, child[c], B_FALSE);
1316a63c99a2SToomas Soome if (vname == NULL) {
1317a63c99a2SToomas Soome be_print_err(gettext(
1318a63c99a2SToomas Soome "be_do_installboot: "
1319a63c99a2SToomas Soome "failed to get device name: %s\n"),
1320a63c99a2SToomas Soome libzfs_error_description(g_zfs));
1321a63c99a2SToomas Soome ret = zfs_err_to_be_err(g_zfs);
1322a63c99a2SToomas Soome goto done;
1323a63c99a2SToomas Soome }
1324a63c99a2SToomas Soome if (strcmp(vname, "mirror") == 0 || vname[0] != 'c') {
1325a63c99a2SToomas Soome free(vname);
1326a63c99a2SToomas Soome
1327a63c99a2SToomas Soome if (nvlist_lookup_nvlist_array(child[c],
1328a63c99a2SToomas Soome ZPOOL_CONFIG_CHILDREN, &nvchild, &nchildren) != 0) {
1329a63c99a2SToomas Soome be_print_err(gettext("be_do_installboot: "
1330a63c99a2SToomas Soome "failed to traverse the vdev tree: %s\n"),
1331a63c99a2SToomas Soome libzfs_error_description(g_zfs));
1332a63c99a2SToomas Soome ret = zfs_err_to_be_err(g_zfs);
1333a63c99a2SToomas Soome goto done;
1334a63c99a2SToomas Soome }
1335a63c99a2SToomas Soome
1336a63c99a2SToomas Soome for (i = 0; i < nchildren; i++) {
1337a63c99a2SToomas Soome ret = be_do_installboot_helper(zphp, nvchild[i],
1338c7c0ceafSToomas Soome stage1, stage2, flags);
1339a63c99a2SToomas Soome if (ret != BE_SUCCESS)
1340a63c99a2SToomas Soome goto done;
1341a63c99a2SToomas Soome }
1342a63c99a2SToomas Soome } else {
1343a63c99a2SToomas Soome free(vname);
1344a63c99a2SToomas Soome
1345a63c99a2SToomas Soome ret = be_do_installboot_helper(zphp, child[c], stage1,
1346c7c0ceafSToomas Soome stage2, flags);
1347a63c99a2SToomas Soome if (ret != BE_SUCCESS)
1348a63c99a2SToomas Soome goto done;
1349a63c99a2SToomas Soome }
1350a63c99a2SToomas Soome }
1351a63c99a2SToomas Soome
1352a63c99a2SToomas Soome if (be_has_grub()) {
1353a63c99a2SToomas Soome ret = be_do_copy_grub_cap(bt);
1354a63c99a2SToomas Soome }
1355a63c99a2SToomas Soome
1356a63c99a2SToomas Soome done:
1357f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
1358f169c0eaSGlenn Lagasse if (be_mounted)
1359f169c0eaSGlenn Lagasse (void) _be_unmount(bt->obe_name, 0);
1360f169c0eaSGlenn Lagasse zpool_close(zphp);
1361f169c0eaSGlenn Lagasse free(tmp_mntpt);
1362f169c0eaSGlenn Lagasse return (ret);
1363f169c0eaSGlenn Lagasse }
1364f169c0eaSGlenn Lagasse
1365f169c0eaSGlenn Lagasse /*
1366f169c0eaSGlenn Lagasse * Function: be_promote_zone_ds
1367f169c0eaSGlenn Lagasse * Description: This function finds the zones for the BE being activated
1368f169c0eaSGlenn Lagasse * and the active zonepath dataset for each zone. Then each
1369f169c0eaSGlenn Lagasse * active zonepath dataset is promoted.
1370f169c0eaSGlenn Lagasse *
1371f169c0eaSGlenn Lagasse * Parameters:
1372f169c0eaSGlenn Lagasse * be_name - the name of the global zone BE that we need to
1373f169c0eaSGlenn Lagasse * find the zones for.
1374f169c0eaSGlenn Lagasse * be_root_ds - the root dataset for be_name.
1375f169c0eaSGlenn Lagasse * Return:
1376f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
1377f169c0eaSGlenn Lagasse * be_errno_t - Failure
1378f169c0eaSGlenn Lagasse *
1379f169c0eaSGlenn Lagasse * Scope:
1380f169c0eaSGlenn Lagasse * Private
1381f169c0eaSGlenn Lagasse */
1382f169c0eaSGlenn Lagasse static int
be_promote_zone_ds(char * be_name,char * be_root_ds)1383f169c0eaSGlenn Lagasse be_promote_zone_ds(char *be_name, char *be_root_ds)
1384f169c0eaSGlenn Lagasse {
1385f169c0eaSGlenn Lagasse char *zone_ds = NULL;
1386f169c0eaSGlenn Lagasse char *temp_mntpt = NULL;
1387f169c0eaSGlenn Lagasse char origin[MAXPATHLEN];
1388f169c0eaSGlenn Lagasse char zoneroot_ds[MAXPATHLEN];
1389f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL;
1390f169c0eaSGlenn Lagasse zfs_handle_t *z_zhp = NULL;
1391f169c0eaSGlenn Lagasse zoneList_t zone_list = NULL;
1392f169c0eaSGlenn Lagasse zoneBrandList_t *brands = NULL;
1393f169c0eaSGlenn Lagasse boolean_t be_mounted = B_FALSE;
1394f169c0eaSGlenn Lagasse int zone_index = 0;
1395f169c0eaSGlenn Lagasse int err = BE_SUCCESS;
1396f169c0eaSGlenn Lagasse
1397f169c0eaSGlenn Lagasse /*
1398f169c0eaSGlenn Lagasse * Get the supported zone brands so we can pass that
1399f169c0eaSGlenn Lagasse * to z_get_nonglobal_zone_list_by_brand. Currently
1400f169c0eaSGlenn Lagasse * only the ipkg and labeled brand zones are supported
1401f169c0eaSGlenn Lagasse *
1402f169c0eaSGlenn Lagasse */
1403f169c0eaSGlenn Lagasse if ((brands = be_get_supported_brandlist()) == NULL) {
1404f169c0eaSGlenn Lagasse be_print_err(gettext("be_promote_zone_ds: no supported "
1405f169c0eaSGlenn Lagasse "brands\n"));
1406f169c0eaSGlenn Lagasse return (BE_SUCCESS);
1407f169c0eaSGlenn Lagasse }
1408f169c0eaSGlenn Lagasse
1409f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, be_root_ds,
1410f169c0eaSGlenn Lagasse ZFS_TYPE_FILESYSTEM)) == NULL) {
1411f169c0eaSGlenn Lagasse be_print_err(gettext("be_promote_zone_ds: Failed to open "
1412f169c0eaSGlenn Lagasse "dataset (%s): %s\n"), be_root_ds,
1413f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
1414f169c0eaSGlenn Lagasse err = zfs_err_to_be_err(g_zfs);
1415f169c0eaSGlenn Lagasse z_free_brand_list(brands);
1416f169c0eaSGlenn Lagasse return (err);
1417f169c0eaSGlenn Lagasse }
1418f169c0eaSGlenn Lagasse
1419f169c0eaSGlenn Lagasse if (!zfs_is_mounted(zhp, &temp_mntpt)) {
1420f169c0eaSGlenn Lagasse if ((err = _be_mount(be_name, &temp_mntpt,
1421f169c0eaSGlenn Lagasse BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
1422f169c0eaSGlenn Lagasse be_print_err(gettext("be_promote_zone_ds: failed to "
1423f169c0eaSGlenn Lagasse "mount the BE for zones procesing.\n"));
1424f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
1425f169c0eaSGlenn Lagasse z_free_brand_list(brands);
1426f169c0eaSGlenn Lagasse return (err);
1427f169c0eaSGlenn Lagasse }
1428f169c0eaSGlenn Lagasse be_mounted = B_TRUE;
1429f169c0eaSGlenn Lagasse }
1430f169c0eaSGlenn Lagasse
1431f169c0eaSGlenn Lagasse /*
1432f169c0eaSGlenn Lagasse * Set the zone root to the temp mount point for the BE we just mounted.
1433f169c0eaSGlenn Lagasse */
1434f169c0eaSGlenn Lagasse z_set_zone_root(temp_mntpt);
1435f169c0eaSGlenn Lagasse
1436f169c0eaSGlenn Lagasse /*
1437f169c0eaSGlenn Lagasse * Get all the zones based on the brands we're looking for. If no zones
1438f169c0eaSGlenn Lagasse * are found that we're interested in unmount the BE and move on.
1439f169c0eaSGlenn Lagasse */
1440f169c0eaSGlenn Lagasse if ((zone_list = z_get_nonglobal_zone_list_by_brand(brands)) == NULL) {
1441f169c0eaSGlenn Lagasse if (be_mounted)
1442f169c0eaSGlenn Lagasse (void) _be_unmount(be_name, 0);
1443f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
1444f169c0eaSGlenn Lagasse z_free_brand_list(brands);
1445f169c0eaSGlenn Lagasse free(temp_mntpt);
1446f169c0eaSGlenn Lagasse return (BE_SUCCESS);
1447f169c0eaSGlenn Lagasse }
1448f169c0eaSGlenn Lagasse for (zone_index = 0; z_zlist_get_zonename(zone_list, zone_index)
1449f169c0eaSGlenn Lagasse != NULL; zone_index++) {
1450f169c0eaSGlenn Lagasse char *zone_path = NULL;
1451f169c0eaSGlenn Lagasse
1452f169c0eaSGlenn Lagasse /* Skip zones that aren't at least installed */
1453f169c0eaSGlenn Lagasse if (z_zlist_get_current_state(zone_list, zone_index) <
1454f169c0eaSGlenn Lagasse ZONE_STATE_INSTALLED)
1455f169c0eaSGlenn Lagasse continue;
1456f169c0eaSGlenn Lagasse
1457f169c0eaSGlenn Lagasse if (((zone_path =
1458f169c0eaSGlenn Lagasse z_zlist_get_zonepath(zone_list, zone_index)) == NULL) ||
1459f169c0eaSGlenn Lagasse ((zone_ds = be_get_ds_from_dir(zone_path)) == NULL) ||
1460f169c0eaSGlenn Lagasse !be_zone_supported(zone_ds))
1461f169c0eaSGlenn Lagasse continue;
1462f169c0eaSGlenn Lagasse
1463f169c0eaSGlenn Lagasse if (be_find_active_zone_root(zhp, zone_ds,
1464f169c0eaSGlenn Lagasse zoneroot_ds, sizeof (zoneroot_ds)) != 0) {
1465f169c0eaSGlenn Lagasse be_print_err(gettext("be_promote_zone_ds: "
1466f169c0eaSGlenn Lagasse "Zone does not have an active root "
1467f169c0eaSGlenn Lagasse "dataset, skipping this zone.\n"));
1468f169c0eaSGlenn Lagasse continue;
1469f169c0eaSGlenn Lagasse }
1470f169c0eaSGlenn Lagasse
1471f169c0eaSGlenn Lagasse if ((z_zhp = zfs_open(g_zfs, zoneroot_ds,
1472f169c0eaSGlenn Lagasse ZFS_TYPE_FILESYSTEM)) == NULL) {
1473f169c0eaSGlenn Lagasse be_print_err(gettext("be_promote_zone_ds: "
1474f169c0eaSGlenn Lagasse "Failed to open dataset "
1475f169c0eaSGlenn Lagasse "(%s): %s\n"), zoneroot_ds,
1476f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
1477f169c0eaSGlenn Lagasse err = zfs_err_to_be_err(g_zfs);
1478f169c0eaSGlenn Lagasse goto done;
1479f169c0eaSGlenn Lagasse }
1480f169c0eaSGlenn Lagasse
1481f169c0eaSGlenn Lagasse if (zfs_prop_get(z_zhp, ZFS_PROP_ORIGIN, origin,
1482f169c0eaSGlenn Lagasse sizeof (origin), NULL, NULL, 0, B_FALSE) != 0) {
1483f169c0eaSGlenn Lagasse ZFS_CLOSE(z_zhp);
1484f169c0eaSGlenn Lagasse continue;
1485f169c0eaSGlenn Lagasse }
1486f169c0eaSGlenn Lagasse
1487f169c0eaSGlenn Lagasse /*
1488f169c0eaSGlenn Lagasse * We don't need to close the zfs handle at this
1489f169c0eaSGlenn Lagasse * point because the callback funtion
1490f169c0eaSGlenn Lagasse * be_promote_ds_callback() will close it for us.
1491f169c0eaSGlenn Lagasse */
1492f169c0eaSGlenn Lagasse if (be_promote_ds_callback(z_zhp, NULL) != 0) {
1493f169c0eaSGlenn Lagasse be_print_err(gettext("be_promote_zone_ds: "
1494f169c0eaSGlenn Lagasse "failed to activate the "
1495f169c0eaSGlenn Lagasse "datasets for %s: %s\n"),
1496f169c0eaSGlenn Lagasse zoneroot_ds,
1497f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
1498f169c0eaSGlenn Lagasse err = BE_ERR_PROMOTE;
1499f169c0eaSGlenn Lagasse goto done;
1500f169c0eaSGlenn Lagasse }
1501f169c0eaSGlenn Lagasse }
1502f169c0eaSGlenn Lagasse done:
1503f169c0eaSGlenn Lagasse if (be_mounted)
1504f169c0eaSGlenn Lagasse (void) _be_unmount(be_name, 0);
1505f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
1506f169c0eaSGlenn Lagasse free(temp_mntpt);
1507f169c0eaSGlenn Lagasse z_free_brand_list(brands);
1508f169c0eaSGlenn Lagasse z_free_zone_list(zone_list);
1509f169c0eaSGlenn Lagasse return (err);
1510f169c0eaSGlenn Lagasse }
1511f169c0eaSGlenn Lagasse
1512f169c0eaSGlenn Lagasse /*
1513f169c0eaSGlenn Lagasse * Function: be_promote_ds_callback
1514f169c0eaSGlenn Lagasse * Description: This function is used to promote the datasets for the BE
1515f169c0eaSGlenn Lagasse * being activated as well as the datasets for the zones BE
1516f169c0eaSGlenn Lagasse * being activated.
1517f169c0eaSGlenn Lagasse *
1518f169c0eaSGlenn Lagasse * Parameters:
1519f169c0eaSGlenn Lagasse * zhp - the zfs handle for zone BE being activated.
1520f169c0eaSGlenn Lagasse * data - not used.
1521f169c0eaSGlenn Lagasse * Return:
1522f169c0eaSGlenn Lagasse * 0 - Success
1523f169c0eaSGlenn Lagasse * be_errno_t - Failure
1524f169c0eaSGlenn Lagasse *
1525f169c0eaSGlenn Lagasse * Scope:
1526f169c0eaSGlenn Lagasse * Private
1527f169c0eaSGlenn Lagasse */
1528f169c0eaSGlenn Lagasse static int
1529f169c0eaSGlenn Lagasse /* LINTED */
be_promote_ds_callback(zfs_handle_t * zhp,void * data)1530f169c0eaSGlenn Lagasse be_promote_ds_callback(zfs_handle_t *zhp, void *data)
1531f169c0eaSGlenn Lagasse {
1532f169c0eaSGlenn Lagasse char origin[MAXPATHLEN];
1533f169c0eaSGlenn Lagasse char *sub_dataset = NULL;
1534f169c0eaSGlenn Lagasse int ret = 0;
1535f169c0eaSGlenn Lagasse
1536f169c0eaSGlenn Lagasse if (zhp != NULL) {
1537f169c0eaSGlenn Lagasse sub_dataset = strdup(zfs_get_name(zhp));
1538f169c0eaSGlenn Lagasse if (sub_dataset == NULL) {
1539f169c0eaSGlenn Lagasse ret = BE_ERR_NOMEM;
1540f169c0eaSGlenn Lagasse goto done;
1541f169c0eaSGlenn Lagasse }
1542f169c0eaSGlenn Lagasse } else {
1543f169c0eaSGlenn Lagasse be_print_err(gettext("be_promote_ds_callback: "
1544f169c0eaSGlenn Lagasse "Invalid zfs handle passed into function\n"));
1545f169c0eaSGlenn Lagasse ret = BE_ERR_INVAL;
1546f169c0eaSGlenn Lagasse goto done;
1547f169c0eaSGlenn Lagasse }
1548f169c0eaSGlenn Lagasse
1549f169c0eaSGlenn Lagasse /*
1550f169c0eaSGlenn Lagasse * This loop makes sure that we promote the dataset to the
1551f169c0eaSGlenn Lagasse * top of the tree so that it is no longer a decendent of any
1552f169c0eaSGlenn Lagasse * dataset. The ZFS close and then open is used to make sure that
1553f169c0eaSGlenn Lagasse * the promotion is updated before we move on.
1554f169c0eaSGlenn Lagasse */
1555f169c0eaSGlenn Lagasse while (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
1556f169c0eaSGlenn Lagasse sizeof (origin), NULL, NULL, 0, B_FALSE) == 0) {
1557f169c0eaSGlenn Lagasse if (zfs_promote(zhp) != 0) {
1558f169c0eaSGlenn Lagasse if (libzfs_errno(g_zfs) != EZFS_EXISTS) {
1559f169c0eaSGlenn Lagasse be_print_err(gettext("be_promote_ds_callback: "
1560f169c0eaSGlenn Lagasse "promote of %s failed: %s\n"),
1561f169c0eaSGlenn Lagasse zfs_get_name(zhp),
1562f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
1563f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs);
1564f169c0eaSGlenn Lagasse goto done;
1565f169c0eaSGlenn Lagasse } else {
1566f169c0eaSGlenn Lagasse /*
1567f169c0eaSGlenn Lagasse * If the call to zfs_promote returns the
1568f169c0eaSGlenn Lagasse * error EZFS_EXISTS we've hit a snapshot name
1569f169c0eaSGlenn Lagasse * collision. This means we're probably
1570f169c0eaSGlenn Lagasse * attemping to promote a zone dataset above a
1571f169c0eaSGlenn Lagasse * parent dataset that belongs to another zone
1572f169c0eaSGlenn Lagasse * which this zone was cloned from.
1573f169c0eaSGlenn Lagasse *
1574f169c0eaSGlenn Lagasse * TODO: If this is a zone dataset at some
1575f169c0eaSGlenn Lagasse * point we should skip this if the zone
1576f169c0eaSGlenn Lagasse * paths for the dataset and the snapshot
1577f169c0eaSGlenn Lagasse * don't match.
1578f169c0eaSGlenn Lagasse */
1579f169c0eaSGlenn Lagasse be_print_err(gettext("be_promote_ds_callback: "
1580f169c0eaSGlenn Lagasse "promote of %s failed due to snapshot "
1581f169c0eaSGlenn Lagasse "name collision: %s\n"), zfs_get_name(zhp),
1582f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
1583f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs);
1584f169c0eaSGlenn Lagasse goto done;
1585f169c0eaSGlenn Lagasse }
1586f169c0eaSGlenn Lagasse }
1587f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
1588f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, sub_dataset,
1589f169c0eaSGlenn Lagasse ZFS_TYPE_FILESYSTEM)) == NULL) {
1590f169c0eaSGlenn Lagasse be_print_err(gettext("be_promote_ds_callback: "
1591f169c0eaSGlenn Lagasse "Failed to open dataset (%s): %s\n"), sub_dataset,
1592f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
1593f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs);
1594f169c0eaSGlenn Lagasse goto done;
1595f169c0eaSGlenn Lagasse }
1596f169c0eaSGlenn Lagasse }
1597f169c0eaSGlenn Lagasse
1598f169c0eaSGlenn Lagasse /* Iterate down this dataset's children and promote them */
1599f169c0eaSGlenn Lagasse ret = zfs_iter_filesystems(zhp, be_promote_ds_callback, NULL);
1600f169c0eaSGlenn Lagasse
1601f169c0eaSGlenn Lagasse done:
1602f169c0eaSGlenn Lagasse free(sub_dataset);
1603f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
1604f169c0eaSGlenn Lagasse return (ret);
1605f169c0eaSGlenn Lagasse }
1606