libbe: be_do_installboot() fails with vdev replacing
[unleashed.git] / usr / src / lib / libbe / common / be_activate.c
blob4b69ce38a2fc092fbda7f4244ef5f9825be3863f
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
30 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
31 * Copyright 2016 Toomas Soome <tsoome@me.com>
34 #include <assert.h>
35 #include <libintl.h>
36 #include <libnvpair.h>
37 #include <libzfs.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <errno.h>
43 #include <sys/mnttab.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <unistd.h>
48 #include <sys/efi_partition.h>
50 #include <libbe.h>
51 #include <libbe_priv.h>
53 char *mnttab = MNTTAB;
56 * Private function prototypes
58 static int set_bootfs(char *boot_rpool, char *be_root_ds);
59 static int set_canmount(be_node_list_t *, char *);
60 static boolean_t be_do_install_mbr(char *, nvlist_t *);
61 static int be_do_installboot_helper(zpool_handle_t *, nvlist_t *, char *,
62 char *, uint16_t);
63 static int be_do_installboot(be_transaction_data_t *, uint16_t);
64 static int be_promote_zone_ds(char *, char *);
65 static int be_promote_ds_callback(zfs_handle_t *, void *);
67 /* ******************************************************************** */
68 /* Public Functions */
69 /* ******************************************************************** */
72 * Function: be_activate
73 * Description: Calls _be_activate which activates the BE named in the
74 * attributes passed in through be_attrs. The process of
75 * activation sets the bootfs property of the root pool, resets
76 * the canmount property to noauto, and sets the default in the
77 * menu to the entry corresponding to the entry for the named BE.
78 * Parameters:
79 * be_attrs - pointer to nvlist_t of attributes being passed in.
80 * The follow attribute values are used by this function:
82 * BE_ATTR_ORIG_BE_NAME *required
83 * Return:
84 * BE_SUCCESS - Success
85 * be_errno_t - Failure
86 * Scope:
87 * Public
89 int
90 be_activate(nvlist_t *be_attrs)
92 int ret = BE_SUCCESS;
93 char *be_name = NULL;
95 /* Initialize libzfs handle */
96 if (!be_zfs_init())
97 return (BE_ERR_INIT);
99 /* Get the BE name to activate */
100 if (nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_NAME, &be_name)
101 != 0) {
102 be_print_err(gettext("be_activate: failed to "
103 "lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
104 be_zfs_fini();
105 return (BE_ERR_INVAL);
108 /* Validate BE name */
109 if (!be_valid_be_name(be_name)) {
110 be_print_err(gettext("be_activate: invalid BE name %s\n"),
111 be_name);
112 be_zfs_fini();
113 return (BE_ERR_INVAL);
116 ret = _be_activate(be_name);
118 be_zfs_fini();
120 return (ret);
124 * Function: be_installboot
125 * Description: Calls be_do_installboot to install/update bootloader on
126 * pool passed in through be_attrs. The primary consumer is
127 * bootadm command to avoid duplication of the code.
128 * Parameters:
129 * be_attrs - pointer to nvlist_t of attributes being passed in.
130 * The following attribute values are used:
132 * BE_ATTR_ORIG_BE_NAME *required
133 * BE_ATTR_ORIG_BE_POOL *required
134 * BE_ATTR_ORIG_BE_ROOT *required
135 * BE_ATTR_INSTALL_FLAGS optional
137 * Return:
138 * BE_SUCCESS - Success
139 * be_errno_t - Failure
140 * Scope:
141 * Public
144 be_installboot(nvlist_t *be_attrs)
146 int ret = BE_SUCCESS;
147 uint16_t flags = 0;
148 uint16_t verbose;
149 be_transaction_data_t bt = { 0 };
151 /* Get flags */
152 if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
153 BE_ATTR_INSTALL_FLAGS, DATA_TYPE_UINT16, &flags, NULL) != 0) {
154 be_print_err(gettext("be_installboot: failed to lookup "
155 "BE_ATTR_INSTALL_FLAGS attribute\n"));
156 return (BE_ERR_INVAL);
159 /* Set verbose early, so we get all messages */
160 verbose = flags & BE_INSTALLBOOT_FLAG_VERBOSE;
161 if (verbose == BE_INSTALLBOOT_FLAG_VERBOSE)
162 libbe_print_errors(B_TRUE);
164 ret = nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_NAME,
165 &bt.obe_name);
166 if (ret != 0) {
167 be_print_err(gettext("be_installboot: failed to "
168 "lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
169 return (BE_ERR_INVAL);
172 ret = nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_POOL,
173 &bt.obe_zpool);
174 if (ret != 0) {
175 be_print_err(gettext("be_installboot: failed to "
176 "lookup BE_ATTR_ORIG_BE_POOL attribute\n"));
177 return (BE_ERR_INVAL);
180 ret = nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_ROOT,
181 &bt.obe_root_ds);
182 if (ret != 0) {
183 be_print_err(gettext("be_installboot: failed to "
184 "lookup BE_ATTR_ORIG_BE_ROOT attribute\n"));
185 return (BE_ERR_INVAL);
188 /* Initialize libzfs handle */
189 if (!be_zfs_init())
190 return (BE_ERR_INIT);
192 ret = be_do_installboot(&bt, flags);
194 be_zfs_fini();
196 return (ret);
199 /* ******************************************************************** */
200 /* Semi Private Functions */
201 /* ******************************************************************** */
204 * Function: _be_activate
205 * Description: This does the actual work described in be_activate.
206 * Parameters:
207 * be_name - pointer to the name of BE to activate.
209 * Return:
210 * BE_SUCCESS - Success
211 * be_errnot_t - Failure
212 * Scope:
213 * Public
216 _be_activate(char *be_name)
218 be_transaction_data_t cb = { 0 };
219 zfs_handle_t *zhp = NULL;
220 char root_ds[MAXPATHLEN];
221 char active_ds[MAXPATHLEN];
222 be_node_list_t *be_nodes = NULL;
223 uuid_t uu = {0};
224 int entry, ret = BE_SUCCESS;
225 int zret = 0;
228 * TODO: The BE needs to be validated to make sure that it is actually
229 * a bootable BE.
232 if (be_name == NULL)
233 return (BE_ERR_INVAL);
235 /* Set obe_name to be_name in the cb structure */
236 cb.obe_name = be_name;
238 /* find which zpool the be is in */
239 if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &cb)) == 0) {
240 be_print_err(gettext("be_activate: failed to "
241 "find zpool for BE (%s)\n"), cb.obe_name);
242 return (BE_ERR_BE_NOENT);
243 } else if (zret < 0) {
244 be_print_err(gettext("be_activate: "
245 "zpool_iter failed: %s\n"),
246 libzfs_error_description(g_zfs));
247 ret = zfs_err_to_be_err(g_zfs);
248 return (ret);
251 be_make_root_ds(cb.obe_zpool, cb.obe_name, root_ds, sizeof (root_ds));
252 cb.obe_root_ds = strdup(root_ds);
254 if (getzoneid() == GLOBAL_ZONEID) {
255 ret = be_do_installboot(&cb, BE_INSTALLBOOT_FLAG_NULL);
256 if (ret != BE_SUCCESS)
257 return (ret);
259 if (!be_has_menu_entry(root_ds, cb.obe_zpool, &entry)) {
260 if ((ret = be_append_menu(cb.obe_name, cb.obe_zpool,
261 NULL, NULL, NULL)) != BE_SUCCESS) {
262 be_print_err(gettext("be_activate: Failed to "
263 "add BE (%s) to the menu\n"),
264 cb.obe_name);
265 goto done;
270 if ((ret = _be_list(cb.obe_name, &be_nodes)) != BE_SUCCESS) {
271 return (ret);
274 if ((ret = set_canmount(be_nodes, "noauto")) != BE_SUCCESS) {
275 be_print_err(gettext("be_activate: failed to set "
276 "canmount dataset property\n"));
277 goto done;
280 if (getzoneid() == GLOBAL_ZONEID) {
281 if ((ret = set_bootfs(be_nodes->be_rpool,
282 root_ds)) != BE_SUCCESS) {
283 be_print_err(gettext("be_activate: failed to set "
284 "bootfs pool property for %s\n"), root_ds);
285 goto done;
289 if ((zhp = zfs_open(g_zfs, root_ds, ZFS_TYPE_FILESYSTEM)) != NULL) {
291 * We don't need to close the zfs handle at this
292 * point because The callback funtion
293 * be_promote_ds_callback() will close it for us.
295 if (be_promote_ds_callback(zhp, NULL) != 0) {
296 be_print_err(gettext("be_activate: "
297 "failed to activate the "
298 "datasets for %s: %s\n"),
299 root_ds,
300 libzfs_error_description(g_zfs));
301 ret = BE_ERR_PROMOTE;
302 goto done;
304 } else {
305 be_print_err(gettext("be_activate: failed to open "
306 "dataset (%s): %s\n"), root_ds,
307 libzfs_error_description(g_zfs));
308 ret = zfs_err_to_be_err(g_zfs);
309 goto done;
312 if (getzoneid() == GLOBAL_ZONEID &&
313 be_get_uuid(cb.obe_root_ds, &uu) == BE_SUCCESS &&
314 (ret = be_promote_zone_ds(cb.obe_name, cb.obe_root_ds))
315 != BE_SUCCESS) {
316 be_print_err(gettext("be_activate: failed to promote "
317 "the active zonepath datasets for zones in BE %s\n"),
318 cb.obe_name);
321 if (getzoneid() != GLOBAL_ZONEID) {
322 if (!be_zone_compare_uuids(root_ds)) {
323 be_print_err(gettext("be_activate: activating zone "
324 "root dataset from non-active global BE is not "
325 "supported\n"));
326 ret = BE_ERR_NOTSUP;
327 goto done;
329 if ((zhp = zfs_open(g_zfs, root_ds,
330 ZFS_TYPE_FILESYSTEM)) == NULL) {
331 be_print_err(gettext("be_activate: failed to open "
332 "dataset (%s): %s\n"), root_ds,
333 libzfs_error_description(g_zfs));
334 ret = zfs_err_to_be_err(g_zfs);
335 goto done;
337 /* Find current active zone root dataset */
338 if ((ret = be_find_active_zone_root(zhp, cb.obe_zpool,
339 active_ds, sizeof (active_ds))) != BE_SUCCESS) {
340 be_print_err(gettext("be_activate: failed to find "
341 "active zone root dataset\n"));
342 ZFS_CLOSE(zhp);
343 goto done;
345 /* Do nothing if requested BE is already active */
346 if (strcmp(root_ds, active_ds) == 0) {
347 ret = BE_SUCCESS;
348 ZFS_CLOSE(zhp);
349 goto done;
352 /* Set active property for BE */
353 if (zfs_prop_set(zhp, BE_ZONE_ACTIVE_PROPERTY, "on") != 0) {
354 be_print_err(gettext("be_activate: failed to set "
355 "active property (%s): %s\n"), root_ds,
356 libzfs_error_description(g_zfs));
357 ret = zfs_err_to_be_err(g_zfs);
358 ZFS_CLOSE(zhp);
359 goto done;
361 ZFS_CLOSE(zhp);
363 /* Unset active property for old active root dataset */
364 if ((zhp = zfs_open(g_zfs, active_ds,
365 ZFS_TYPE_FILESYSTEM)) == NULL) {
366 be_print_err(gettext("be_activate: failed to open "
367 "dataset (%s): %s\n"), active_ds,
368 libzfs_error_description(g_zfs));
369 ret = zfs_err_to_be_err(g_zfs);
370 goto done;
372 if (zfs_prop_set(zhp, BE_ZONE_ACTIVE_PROPERTY, "off") != 0) {
373 be_print_err(gettext("be_activate: failed to unset "
374 "active property (%s): %s\n"), active_ds,
375 libzfs_error_description(g_zfs));
376 ret = zfs_err_to_be_err(g_zfs);
377 ZFS_CLOSE(zhp);
378 goto done;
380 ZFS_CLOSE(zhp);
382 done:
383 be_free_list(be_nodes);
384 return (ret);
388 * Function: be_activate_current_be
389 * Description: Set the currently "active" BE to be "active on boot"
390 * Paramters:
391 * none
392 * Returns:
393 * BE_SUCCESS - Success
394 * be_errnot_t - Failure
395 * Scope:
396 * Semi-private (library wide use only)
399 be_activate_current_be(void)
401 int ret = BE_SUCCESS;
402 be_transaction_data_t bt = { 0 };
404 if ((ret = be_find_current_be(&bt)) != BE_SUCCESS) {
405 return (ret);
408 if ((ret = _be_activate(bt.obe_name)) != BE_SUCCESS) {
409 be_print_err(gettext("be_activate_current_be: failed to "
410 "activate %s\n"), bt.obe_name);
411 return (ret);
414 return (BE_SUCCESS);
418 * Function: be_is_active_on_boot
419 * Description: Checks if the BE name passed in has the "active on boot"
420 * property set to B_TRUE.
421 * Paramters:
422 * be_name - the name of the BE to check
423 * Returns:
424 * B_TRUE - if active on boot.
425 * B_FALSE - if not active on boot.
426 * Scope:
427 * Semi-private (library wide use only)
429 boolean_t
430 be_is_active_on_boot(char *be_name)
432 be_node_list_t *be_node = NULL;
434 if (be_name == NULL) {
435 be_print_err(gettext("be_is_active_on_boot: "
436 "be_name must not be NULL\n"));
437 return (B_FALSE);
440 if (_be_list(be_name, &be_node) != BE_SUCCESS) {
441 return (B_FALSE);
444 if (be_node == NULL) {
445 return (B_FALSE);
448 if (be_node->be_active_on_boot) {
449 be_free_list(be_node);
450 return (B_TRUE);
451 } else {
452 be_free_list(be_node);
453 return (B_FALSE);
457 /* ******************************************************************** */
458 /* Private Functions */
459 /* ******************************************************************** */
462 * Function: set_bootfs
463 * Description: Sets the bootfs property on the boot pool to be the
464 * root dataset of the activated BE.
465 * Parameters:
466 * boot_pool - The pool we're setting bootfs in.
467 * be_root_ds - The main dataset for the BE.
468 * Return:
469 * BE_SUCCESS - Success
470 * be_errno_t - Failure
471 * Scope:
472 * Private
474 static int
475 set_bootfs(char *boot_rpool, char *be_root_ds)
477 zpool_handle_t *zhp;
478 int err = BE_SUCCESS;
480 if ((zhp = zpool_open(g_zfs, boot_rpool)) == NULL) {
481 be_print_err(gettext("set_bootfs: failed to open pool "
482 "(%s): %s\n"), boot_rpool, libzfs_error_description(g_zfs));
483 err = zfs_err_to_be_err(g_zfs);
484 return (err);
487 err = zpool_set_prop(zhp, "bootfs", be_root_ds);
488 if (err) {
489 be_print_err(gettext("set_bootfs: failed to set "
490 "bootfs property for pool %s: %s\n"), boot_rpool,
491 libzfs_error_description(g_zfs));
492 err = zfs_err_to_be_err(g_zfs);
493 zpool_close(zhp);
494 return (err);
497 zpool_close(zhp);
498 return (BE_SUCCESS);
502 * Function: set_canmount
503 * Description: Sets the canmount property on the datasets of the
504 * activated BE.
505 * Parameters:
506 * be_nodes - The be_node_t returned from be_list
507 * value - The value of canmount we setting, on|off|noauto.
508 * Return:
509 * BE_SUCCESS - Success
510 * be_errno_t - Failure
511 * Scope:
512 * Private
514 static int
515 set_canmount(be_node_list_t *be_nodes, char *value)
517 char ds_path[MAXPATHLEN];
518 zfs_handle_t *zhp = NULL;
519 be_node_list_t *list = be_nodes;
520 int err = BE_SUCCESS;
522 while (list != NULL) {
523 be_dataset_list_t *datasets = list->be_node_datasets;
525 be_make_root_ds(list->be_rpool, list->be_node_name, ds_path,
526 sizeof (ds_path));
528 if ((zhp = zfs_open(g_zfs, ds_path, ZFS_TYPE_DATASET)) ==
529 NULL) {
530 be_print_err(gettext("set_canmount: failed to open "
531 "dataset (%s): %s\n"), ds_path,
532 libzfs_error_description(g_zfs));
533 err = zfs_err_to_be_err(g_zfs);
534 return (err);
536 if (zfs_prop_get_int(zhp, ZFS_PROP_MOUNTED)) {
538 * it's already mounted so we can't change the
539 * canmount property anyway.
541 err = BE_SUCCESS;
542 } else {
543 err = zfs_prop_set(zhp,
544 zfs_prop_to_name(ZFS_PROP_CANMOUNT), value);
545 if (err) {
546 ZFS_CLOSE(zhp);
547 be_print_err(gettext("set_canmount: failed to "
548 "set dataset property (%s): %s\n"),
549 ds_path, libzfs_error_description(g_zfs));
550 err = zfs_err_to_be_err(g_zfs);
551 return (err);
554 ZFS_CLOSE(zhp);
556 while (datasets != NULL) {
557 be_make_root_ds(list->be_rpool,
558 datasets->be_dataset_name, ds_path,
559 sizeof (ds_path));
561 if ((zhp = zfs_open(g_zfs, ds_path, ZFS_TYPE_DATASET))
562 == NULL) {
563 be_print_err(gettext("set_canmount: failed to "
564 "open dataset %s: %s\n"), ds_path,
565 libzfs_error_description(g_zfs));
566 err = zfs_err_to_be_err(g_zfs);
567 return (err);
569 if (zfs_prop_get_int(zhp, ZFS_PROP_MOUNTED)) {
571 * it's already mounted so we can't change the
572 * canmount property anyway.
574 err = BE_SUCCESS;
575 ZFS_CLOSE(zhp);
576 break;
578 err = zfs_prop_set(zhp,
579 zfs_prop_to_name(ZFS_PROP_CANMOUNT), value);
580 if (err) {
581 ZFS_CLOSE(zhp);
582 be_print_err(gettext("set_canmount: "
583 "Failed to set property value %s "
584 "for dataset %s: %s\n"), value, ds_path,
585 libzfs_error_description(g_zfs));
586 err = zfs_err_to_be_err(g_zfs);
587 return (err);
589 ZFS_CLOSE(zhp);
590 datasets = datasets->be_next_dataset;
592 list = list->be_next_node;
594 return (err);
598 * To be able to boot EFI labeled disks, stage1 needs to be written
599 * into the MBR. We do not do this if we're on disks with a traditional
600 * fdisk partition table only, or if any foreign EFI partitions exist.
601 * In the trivial case of a whole-disk vdev we always write stage1 into
602 * the MBR.
604 static boolean_t
605 be_do_install_mbr(char *diskname, nvlist_t *child)
607 struct uuid allowed_uuids[] = {
608 EFI_UNUSED,
609 EFI_RESV1,
610 EFI_BOOT,
611 EFI_ROOT,
612 EFI_SWAP,
613 EFI_USR,
614 EFI_BACKUP,
615 EFI_RESV2,
616 EFI_VAR,
617 EFI_HOME,
618 EFI_ALTSCTR,
619 EFI_RESERVED,
620 EFI_SYSTEM,
621 EFI_BIOS_BOOT,
622 EFI_SYMC_PUB,
623 EFI_SYMC_CDS
626 uint64_t whole;
627 struct dk_gpt *gpt;
628 struct uuid *u;
629 int fd, npart, i, j;
631 (void) nvlist_lookup_uint64(child, ZPOOL_CONFIG_WHOLE_DISK,
632 &whole);
634 if (whole)
635 return (B_TRUE);
637 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) < 0)
638 return (B_FALSE);
640 if ((npart = efi_alloc_and_read(fd, &gpt)) <= 0)
641 return (B_FALSE);
643 for (i = 0; i != npart; i++) {
644 int match = 0;
646 u = &gpt->efi_parts[i].p_guid;
648 for (j = 0;
649 j != sizeof (allowed_uuids) / sizeof (struct uuid);
650 j++)
651 if (bcmp(u, &allowed_uuids[j],
652 sizeof (struct uuid)) == 0)
653 match++;
655 if (match == 0)
656 return (B_FALSE);
659 return (B_TRUE);
662 static int
663 be_do_installboot_helper(zpool_handle_t *zphp, nvlist_t *child, char *stage1,
664 char *stage2, uint16_t flags)
666 char install_cmd[MAXPATHLEN];
667 char be_run_cmd_errbuf[BUFSIZ];
668 char be_run_cmd_outbuf[BUFSIZ];
669 char diskname[MAXPATHLEN];
670 char *vname;
671 char *path, *type, *dsk_ptr;
672 char *flag = "";
673 int ret;
674 vdev_stat_t *vs;
675 uint_t vsc;
677 if (nvlist_lookup_string(child, ZPOOL_CONFIG_TYPE, &type) != 0) {
678 be_print_err(gettext("%s: failed to get device type\n"),
679 __func__);
680 return (BE_ERR_NODEV);
682 /* Skip indirect devices. */
683 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
684 return (BE_ERR_NOTSUP);
686 if (nvlist_lookup_string(child, ZPOOL_CONFIG_PATH, &path) != 0) {
687 be_print_err(gettext("%s: failed to get device path\n"),
688 __func__);
689 return (BE_ERR_NODEV);
692 if ((nvlist_lookup_uint64_array(child, ZPOOL_CONFIG_VDEV_STATS,
693 (uint64_t **)&vs, &vsc) != 0) ||
694 vs->vs_state < VDEV_STATE_DEGRADED) {
696 * Don't try to run installboot on a vdev that is not ONLINE
697 * or DEGRADED. Try to print a warning for each such vdev.
699 be_print_err(gettext("%s: vdev %s is %s, can't install "
700 "boot loader\n"), __func__, path,
701 zpool_state_to_name(vs->vs_state, vs->vs_aux));
702 return (BE_SUCCESS);
706 * Modify the vdev path to point to the raw disk.
708 path = strdup(path);
709 if (path == NULL)
710 return (BE_ERR_NOMEM);
712 dsk_ptr = strstr(path, "/dsk/");
713 if (dsk_ptr != NULL) {
714 *dsk_ptr = '\0';
715 dsk_ptr++;
716 } else {
717 dsk_ptr = "";
720 (void) snprintf(diskname, sizeof (diskname), "%s/r%s", path, dsk_ptr);
721 free(path);
723 vname = zpool_vdev_name(g_zfs, zphp, child, B_FALSE);
724 if (vname == NULL) {
725 be_print_err(gettext("%s: failed to get device name: %s\n"),
726 __func__, libzfs_error_description(g_zfs));
727 return (zfs_err_to_be_err(g_zfs));
730 if (be_is_isa("i386")) {
731 uint16_t force = flags & BE_INSTALLBOOT_FLAG_FORCE;
732 uint16_t mbr = flags & BE_INSTALLBOOT_FLAG_MBR;
734 if (force == BE_INSTALLBOOT_FLAG_FORCE) {
735 if (mbr == BE_INSTALLBOOT_FLAG_MBR ||
736 be_do_install_mbr(diskname, child))
737 flag = "-F -m -f";
738 else
739 flag = "-F";
740 } else {
741 if (mbr == BE_INSTALLBOOT_FLAG_MBR ||
742 be_do_install_mbr(diskname, child))
743 flag = "-m -f";
746 (void) snprintf(install_cmd, sizeof (install_cmd),
747 "%s %s %s %s %s", BE_INSTALL_BOOT, flag,
748 stage1, stage2, diskname);
749 } else if (be_is_isa("sparc")) {
750 if ((flags & BE_INSTALLBOOT_FLAG_FORCE) ==
751 BE_INSTALLBOOT_FLAG_FORCE)
752 flag = "-f -F zfs";
753 else
754 flag = "-F zfs";
756 (void) snprintf(install_cmd, sizeof (install_cmd),
757 "%s %s %s %s", BE_INSTALL_BOOT, flag, stage2, diskname);
758 } else {
759 be_print_err(gettext("%s: unsupported architecture.\n"),
760 __func__);
761 return (BE_ERR_BOOTFILE_INST);
764 *be_run_cmd_outbuf = '\0';
765 *be_run_cmd_errbuf = '\0';
767 ret = be_run_cmd(install_cmd, be_run_cmd_errbuf, BUFSIZ,
768 be_run_cmd_outbuf, BUFSIZ);
770 if (ret != BE_SUCCESS) {
771 be_print_err(gettext("%s: install failed for device %s.\n"),
772 __func__, vname);
773 ret = BE_ERR_BOOTFILE_INST;
776 be_print_err(gettext(" Command: \"%s\"\n"), install_cmd);
777 if (be_run_cmd_outbuf[0] != 0) {
778 be_print_err(gettext(" Output:\n"));
779 be_print_err("%s", be_run_cmd_outbuf);
782 if (be_run_cmd_errbuf[0] != 0) {
783 be_print_err(gettext(" Errors:\n"));
784 be_print_err("%s", be_run_cmd_errbuf);
786 free(vname);
788 return (ret);
791 static int
792 be_do_installboot_walk(zpool_handle_t *zphp, nvlist_t *nv, char *stage1,
793 char *stage2, uint16_t flags)
795 boolean_t verbose = do_print;
796 nvlist_t **child;
797 uint_t children = 0;
798 int ret = -1;
800 /* It is OK to have no children. */
801 (void) nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
802 &children);
804 for (int c = 0; c < children; c++) {
805 char *vname;
806 int rv;
808 /* ensure update on child status */
809 vname = zpool_vdev_name(g_zfs, zphp, child[c], verbose);
810 if (vname == NULL) {
811 be_print_err(gettext("%s: "
812 "failed to get device name: %s\n"), __func__,
813 libzfs_error_description(g_zfs));
814 return (zfs_err_to_be_err(g_zfs));
815 } else {
816 be_print_err(gettext("%s: child %d of %d device %s\n"),
817 __func__, c, children, vname);
820 rv = be_do_installboot_walk(zphp, child[c], stage1, stage2,
821 flags);
822 switch (rv) {
823 case BE_ERR_NOTSUP:
824 /* ignore unsupported devices */
825 be_print_err(
826 gettext("%s: device %s is not supported\n"),
827 __func__, vname);
828 break;
829 case BE_SUCCESS:
830 /* catch at least one success */
831 ret = rv;
832 break;
833 default:
834 if (ret == -1)
835 ret = rv;
836 break;
838 free(vname);
841 if (children > 0)
842 return (ret == -1? BE_ERR_NOTSUP : ret);
843 return (be_do_installboot_helper(zphp, nv, stage1, stage2, flags));
847 * Function: be_do_installboot
848 * Description: This function runs installboot using the boot
849 * loader files from the BE we're activating and installing
850 * them on the pool the BE lives in.
852 * Parameters:
853 * bt - The transaction data for the BE we're activating.
854 * flags - flags for bootloader install
855 * Return:
856 * BE_SUCCESS - Success
857 * be_errno_t - Failure
859 * Scope:
860 * Private
862 static int
863 be_do_installboot(be_transaction_data_t *bt, uint16_t flags)
865 zpool_handle_t *zphp = NULL;
866 zfs_handle_t *zhp = NULL;
867 nvlist_t *nv, *config;
868 char *tmp_mntpt = NULL;
869 char stage1[MAXPATHLEN];
870 char stage2[MAXPATHLEN];
871 int ret = BE_SUCCESS;
872 boolean_t be_mounted = B_FALSE;
874 if ((zhp = zfs_open(g_zfs, bt->obe_root_ds, ZFS_TYPE_FILESYSTEM)) ==
875 NULL) {
876 be_print_err(gettext("%s: failed to "
877 "open BE root dataset (%s): %s\n"), __func__,
878 bt->obe_root_ds, libzfs_error_description(g_zfs));
879 ret = zfs_err_to_be_err(g_zfs);
880 return (ret);
882 if (!zfs_is_mounted(zhp, &tmp_mntpt)) {
883 if ((ret = _be_mount(bt->obe_name, &tmp_mntpt,
884 BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
885 be_print_err(gettext("%s: failed to "
886 "mount BE (%s)\n"), __func__, bt->obe_name);
887 ZFS_CLOSE(zhp);
888 return (ret);
890 be_mounted = B_TRUE;
892 ZFS_CLOSE(zhp);
894 if (be_is_isa("i386")) {
895 (void) snprintf(stage1, sizeof (stage1), "%s%s",
896 tmp_mntpt, BE_LOADER_STAGE_1);
897 (void) snprintf(stage2, sizeof (stage2), "%s%s",
898 tmp_mntpt, BE_LOADER_STAGE_2);
899 } else if (be_is_isa("sparc")) {
900 char *platform = be_get_platform();
902 if (platform == NULL) {
903 be_print_err(gettext("%s: failed to detect system "
904 "platform name\n"), __func__);
905 if (be_mounted)
906 (void) _be_unmount(bt->obe_name, 0);
907 free(tmp_mntpt);
908 return (BE_ERR_BOOTFILE_INST);
910 stage1[0] = '\0'; /* sparc has no stage1 */
911 (void) snprintf(stage2, sizeof (stage2),
912 "%s/usr/platform/%s%s", tmp_mntpt,
913 platform, BE_SPARC_BOOTBLK);
914 } else {
915 be_print_err(gettext("%s: unsupported architecture.\n"),
916 __func__);
917 return (BE_ERR_BOOTFILE_INST);
920 if ((zphp = zpool_open(g_zfs, bt->obe_zpool)) == NULL) {
921 be_print_err(gettext("%s: failed to open "
922 "pool (%s): %s\n"), __func__, bt->obe_zpool,
923 libzfs_error_description(g_zfs));
924 ret = zfs_err_to_be_err(g_zfs);
925 if (be_mounted)
926 (void) _be_unmount(bt->obe_name, 0);
927 free(tmp_mntpt);
928 return (ret);
931 if ((config = zpool_get_config(zphp, NULL)) == NULL) {
932 be_print_err(gettext("%s: failed to get zpool "
933 "configuration information. %s\n"), __func__,
934 libzfs_error_description(g_zfs));
935 ret = zfs_err_to_be_err(g_zfs);
936 goto done;
940 * Get the vdev tree
942 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) != 0) {
943 be_print_err(gettext("%s: failed to get vdev "
944 "tree: %s\n"), __func__, libzfs_error_description(g_zfs));
945 ret = zfs_err_to_be_err(g_zfs);
946 goto done;
949 ret = be_do_installboot_walk(zphp, nv, stage1, stage2, flags);
951 done:
952 ZFS_CLOSE(zhp);
953 if (be_mounted)
954 (void) _be_unmount(bt->obe_name, 0);
955 zpool_close(zphp);
956 free(tmp_mntpt);
957 return (ret);
961 * Function: be_promote_zone_ds
962 * Description: This function finds the zones for the BE being activated
963 * and the active zonepath dataset for each zone. Then each
964 * active zonepath dataset is promoted.
966 * Parameters:
967 * be_name - the name of the global zone BE that we need to
968 * find the zones for.
969 * be_root_ds - the root dataset for be_name.
970 * Return:
971 * BE_SUCCESS - Success
972 * be_errno_t - Failure
974 * Scope:
975 * Private
977 static int
978 be_promote_zone_ds(char *be_name, char *be_root_ds)
980 char *zone_ds = NULL;
981 char *temp_mntpt = NULL;
982 char origin[MAXPATHLEN];
983 char zoneroot_ds[MAXPATHLEN];
984 zfs_handle_t *zhp = NULL;
985 zfs_handle_t *z_zhp = NULL;
986 boolean_t be_mounted = B_FALSE;
987 int err = BE_SUCCESS;
988 FILE *cookie;
989 struct zoneent *ze;
991 if ((zhp = zfs_open(g_zfs, be_root_ds,
992 ZFS_TYPE_FILESYSTEM)) == NULL) {
993 be_print_err(gettext("be_promote_zone_ds: Failed to open "
994 "dataset (%s): %s\n"), be_root_ds,
995 libzfs_error_description(g_zfs));
996 err = zfs_err_to_be_err(g_zfs);
997 return (err);
1000 if (!zfs_is_mounted(zhp, &temp_mntpt)) {
1001 if ((err = _be_mount(be_name, &temp_mntpt,
1002 BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
1003 be_print_err(gettext("be_promote_zone_ds: failed to "
1004 "mount the BE for zones procesing.\n"));
1005 ZFS_CLOSE(zhp);
1006 return (err);
1008 be_mounted = B_TRUE;
1012 * Set the zone root to the temp mount point for the BE we just mounted.
1014 zonecfg_set_root((const char *)temp_mntpt);
1016 cookie = setzoneent();
1017 while((ze = getzoneent_private(cookie)) != NULL) {
1019 if (strcmp(ze->zone_name, "global") == 0)
1020 continue;
1022 /* Skip zones that aren't at least installed */
1023 if (ze->zone_state < ZONE_STATE_INSTALLED)
1024 continue;
1026 if (((zone_ds = be_get_ds_from_dir(ze->zone_path)) == NULL) ||
1027 !be_zone_supported(zone_ds)) {
1028 free(zone_ds);
1029 free(ze);
1030 continue;
1033 if (be_find_active_zone_root(zhp, zone_ds,
1034 zoneroot_ds, sizeof (zoneroot_ds)) != 0) {
1035 be_print_err(gettext("be_promote_zone_ds: "
1036 "Zone does not have an active root "
1037 "dataset, skipping this zone.\n"));
1038 continue;
1041 if ((z_zhp = zfs_open(g_zfs, zoneroot_ds,
1042 ZFS_TYPE_FILESYSTEM)) == NULL) {
1043 be_print_err(gettext("be_promote_zone_ds: "
1044 "Failed to open dataset "
1045 "(%s): %s\n"), zoneroot_ds,
1046 libzfs_error_description(g_zfs));
1047 err = zfs_err_to_be_err(g_zfs);
1048 goto done;
1051 if (zfs_prop_get(z_zhp, ZFS_PROP_ORIGIN, origin,
1052 sizeof (origin), NULL, NULL, 0, B_FALSE) != 0) {
1053 ZFS_CLOSE(z_zhp);
1054 continue;
1058 * We don't need to close the zfs handle at this
1059 * point because the callback funtion
1060 * be_promote_ds_callback() will close it for us.
1062 if (be_promote_ds_callback(z_zhp, NULL) != 0) {
1063 be_print_err(gettext("be_promote_zone_ds: "
1064 "failed to activate the "
1065 "datasets for %s: %s\n"),
1066 zoneroot_ds,
1067 libzfs_error_description(g_zfs));
1068 err = BE_ERR_PROMOTE;
1069 goto done;
1071 free(ze);
1073 endzoneent(cookie);
1075 done:
1076 if (be_mounted)
1077 (void) _be_unmount(be_name, 0);
1078 ZFS_CLOSE(zhp);
1079 free(temp_mntpt);
1080 return (err);
1084 * Function: be_promote_ds_callback
1085 * Description: This function is used to promote the datasets for the BE
1086 * being activated as well as the datasets for the zones BE
1087 * being activated.
1089 * Parameters:
1090 * zhp - the zfs handle for zone BE being activated.
1091 * data - not used.
1092 * Return:
1093 * 0 - Success
1094 * be_errno_t - Failure
1096 * Scope:
1097 * Private
1099 static int
1100 /* LINTED */
1101 be_promote_ds_callback(zfs_handle_t *zhp, void *data)
1103 char origin[MAXPATHLEN];
1104 char *sub_dataset = NULL;
1105 int ret = 0;
1107 if (zhp != NULL) {
1108 sub_dataset = strdup(zfs_get_name(zhp));
1109 if (sub_dataset == NULL) {
1110 ret = BE_ERR_NOMEM;
1111 goto done;
1113 } else {
1114 be_print_err(gettext("be_promote_ds_callback: "
1115 "Invalid zfs handle passed into function\n"));
1116 ret = BE_ERR_INVAL;
1117 goto done;
1121 * This loop makes sure that we promote the dataset to the
1122 * top of the tree so that it is no longer a decendent of any
1123 * dataset. The ZFS close and then open is used to make sure that
1124 * the promotion is updated before we move on.
1126 while (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
1127 sizeof (origin), NULL, NULL, 0, B_FALSE) == 0) {
1128 if (zfs_promote(zhp) != 0) {
1129 if (libzfs_errno(g_zfs) != EZFS_EXISTS) {
1130 be_print_err(gettext("be_promote_ds_callback: "
1131 "promote of %s failed: %s\n"),
1132 zfs_get_name(zhp),
1133 libzfs_error_description(g_zfs));
1134 ret = zfs_err_to_be_err(g_zfs);
1135 goto done;
1136 } else {
1138 * If the call to zfs_promote returns the
1139 * error EZFS_EXISTS we've hit a snapshot name
1140 * collision. This means we're probably
1141 * attemping to promote a zone dataset above a
1142 * parent dataset that belongs to another zone
1143 * which this zone was cloned from.
1145 * TODO: If this is a zone dataset at some
1146 * point we should skip this if the zone
1147 * paths for the dataset and the snapshot
1148 * don't match.
1150 be_print_err(gettext("be_promote_ds_callback: "
1151 "promote of %s failed due to snapshot "
1152 "name collision: %s\n"), zfs_get_name(zhp),
1153 libzfs_error_description(g_zfs));
1154 ret = zfs_err_to_be_err(g_zfs);
1155 goto done;
1158 ZFS_CLOSE(zhp);
1159 if ((zhp = zfs_open(g_zfs, sub_dataset,
1160 ZFS_TYPE_FILESYSTEM)) == NULL) {
1161 be_print_err(gettext("be_promote_ds_callback: "
1162 "Failed to open dataset (%s): %s\n"), sub_dataset,
1163 libzfs_error_description(g_zfs));
1164 ret = zfs_err_to_be_err(g_zfs);
1165 goto done;
1169 /* Iterate down this dataset's children and promote them */
1170 ret = zfs_iter_filesystems(zhp, be_promote_ds_callback, NULL);
1172 done:
1173 free(sub_dataset);
1174 ZFS_CLOSE(zhp);
1175 return (ret);