4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
32 #include <libnvpair.h>
39 #include <sys/mnttab.h>
40 #include <sys/types.h>
44 #include <sys/efi_partition.h>
47 #include <libbe_priv.h>
49 char *mnttab
= MNTTAB
;
52 * Private function prototypes
54 static int set_bootfs(char *boot_rpool
, char *be_root_ds
);
55 static int set_canmount(be_node_list_t
*, char *);
56 static boolean_t
be_do_installgrub_mbr(char *, nvlist_t
*);
57 static int be_do_installgrub_helper(zpool_handle_t
*, nvlist_t
*, char *,
59 static int be_do_installgrub(be_transaction_data_t
*);
60 static int be_get_grub_vers(be_transaction_data_t
*, char **, char **);
61 static int get_ver_from_capfile(char *, char **);
62 static int be_promote_zone_ds(char *, char *);
63 static int be_promote_ds_callback(zfs_handle_t
*, void *);
65 /* ******************************************************************** */
66 /* Public Functions */
67 /* ******************************************************************** */
70 * Function: be_activate
71 * Description: Calls _be_activate which activates the BE named in the
72 * attributes passed in through be_attrs. The process of
73 * activation sets the bootfs property of the root pool, resets
74 * the canmount property to noauto, and sets the default in the
75 * grub menu to the entry corresponding to the entry for the named
78 * be_attrs - pointer to nvlist_t of attributes being passed in.
79 * The follow attribute values are used by this function:
81 * BE_ATTR_ORIG_BE_NAME *required
83 * BE_SUCCESS - Success
84 * be_errno_t - Failure
89 be_activate(nvlist_t
*be_attrs
)
94 /* Initialize libzfs handle */
98 /* Get the BE name to activate */
99 if (nvlist_lookup_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, &be_name
)
101 be_print_err(gettext("be_activate: failed to "
102 "lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
104 return (BE_ERR_INVAL
);
107 /* Validate BE name */
108 if (!be_valid_be_name(be_name
)) {
109 be_print_err(gettext("be_activate: invalid BE name %s\n"),
112 return (BE_ERR_INVAL
);
115 ret
= _be_activate(be_name
);
122 /* ******************************************************************** */
123 /* Semi Private Functions */
124 /* ******************************************************************** */
127 * Function: _be_activate
128 * Description: This does the actual work described in be_activate.
130 * be_name - pointer to the name of BE to activate.
133 * BE_SUCCESS - Success
134 * be_errnot_t - Failure
139 _be_activate(char *be_name
)
141 be_transaction_data_t cb
= { 0 };
142 zfs_handle_t
*zhp
= NULL
;
143 char root_ds
[MAXPATHLEN
];
144 char active_ds
[MAXPATHLEN
];
145 char *cur_vers
= NULL
, *new_vers
= NULL
;
146 be_node_list_t
*be_nodes
= NULL
;
148 int entry
, ret
= BE_SUCCESS
;
152 * TODO: The BE needs to be validated to make sure that it is actually
157 return (BE_ERR_INVAL
);
159 /* Set obe_name to be_name in the cb structure */
160 cb
.obe_name
= be_name
;
162 /* find which zpool the be is in */
163 if ((zret
= zpool_iter(g_zfs
, be_find_zpool_callback
, &cb
)) == 0) {
164 be_print_err(gettext("be_activate: failed to "
165 "find zpool for BE (%s)\n"), cb
.obe_name
);
166 return (BE_ERR_BE_NOENT
);
167 } else if (zret
< 0) {
168 be_print_err(gettext("be_activate: "
169 "zpool_iter failed: %s\n"),
170 libzfs_error_description(g_zfs
));
171 ret
= zfs_err_to_be_err(g_zfs
);
175 be_make_root_ds(cb
.obe_zpool
, cb
.obe_name
, root_ds
, sizeof (root_ds
));
176 cb
.obe_root_ds
= strdup(root_ds
);
178 if (getzoneid() == GLOBAL_ZONEID
) {
179 if (be_has_grub() && (ret
= be_get_grub_vers(&cb
, &cur_vers
,
180 &new_vers
)) != BE_SUCCESS
) {
181 be_print_err(gettext("be_activate: failed to get grub "
182 "versions from capability files.\n"));
185 if (cur_vers
!= NULL
) {
187 * We need to check to see if the version number from
188 * the BE being activated is greater than the current
191 if (new_vers
!= NULL
&&
192 atof(cur_vers
) < atof(new_vers
)) {
193 if ((ret
= be_do_installgrub(&cb
))
202 } else if (new_vers
!= NULL
) {
203 if ((ret
= be_do_installgrub(&cb
)) != BE_SUCCESS
) {
209 if (!be_has_menu_entry(root_ds
, cb
.obe_zpool
, &entry
)) {
210 if ((ret
= be_append_menu(cb
.obe_name
, cb
.obe_zpool
,
211 NULL
, NULL
, NULL
)) != BE_SUCCESS
) {
212 be_print_err(gettext("be_activate: Failed to "
213 "add BE (%s) to the GRUB menu\n"),
219 if ((ret
= be_change_grub_default(cb
.obe_name
,
220 cb
.obe_zpool
)) != BE_SUCCESS
) {
221 be_print_err(gettext("be_activate: failed to "
222 "change the default entry in menu.lst\n"));
228 if ((ret
= _be_list(cb
.obe_name
, &be_nodes
)) != BE_SUCCESS
) {
232 if ((ret
= set_canmount(be_nodes
, "noauto")) != BE_SUCCESS
) {
233 be_print_err(gettext("be_activate: failed to set "
234 "canmount dataset property\n"));
238 if (getzoneid() == GLOBAL_ZONEID
) {
239 if ((ret
= set_bootfs(be_nodes
->be_rpool
,
240 root_ds
)) != BE_SUCCESS
) {
241 be_print_err(gettext("be_activate: failed to set "
242 "bootfs pool property for %s\n"), root_ds
);
247 if ((zhp
= zfs_open(g_zfs
, root_ds
, ZFS_TYPE_FILESYSTEM
)) != NULL
) {
249 * We don't need to close the zfs handle at this
250 * point because The callback funtion
251 * be_promote_ds_callback() will close it for us.
253 if (be_promote_ds_callback(zhp
, NULL
) != 0) {
254 be_print_err(gettext("be_activate: "
255 "failed to activate the "
256 "datasets for %s: %s\n"),
258 libzfs_error_description(g_zfs
));
259 ret
= BE_ERR_PROMOTE
;
263 be_print_err(gettext("be_activate: failed to open "
264 "dataset (%s): %s\n"), root_ds
,
265 libzfs_error_description(g_zfs
));
266 ret
= zfs_err_to_be_err(g_zfs
);
270 if (getzoneid() == GLOBAL_ZONEID
&&
271 be_get_uuid(cb
.obe_root_ds
, &uu
) == BE_SUCCESS
&&
272 (ret
= be_promote_zone_ds(cb
.obe_name
, cb
.obe_root_ds
))
274 be_print_err(gettext("be_activate: failed to promote "
275 "the active zonepath datasets for zones in BE %s\n"),
279 if (getzoneid() != GLOBAL_ZONEID
) {
280 if (!be_zone_compare_uuids(root_ds
)) {
281 be_print_err(gettext("be_activate: activating zone "
282 "root dataset from non-active global BE is not "
287 if ((zhp
= zfs_open(g_zfs
, root_ds
,
288 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
289 be_print_err(gettext("be_activate: failed to open "
290 "dataset (%s): %s\n"), root_ds
,
291 libzfs_error_description(g_zfs
));
292 ret
= zfs_err_to_be_err(g_zfs
);
295 /* Find current active zone root dataset */
296 if ((ret
= be_find_active_zone_root(zhp
, cb
.obe_zpool
,
297 active_ds
, sizeof (active_ds
))) != BE_SUCCESS
) {
298 be_print_err(gettext("be_activate: failed to find "
299 "active zone root dataset\n"));
303 /* Do nothing if requested BE is already active */
304 if (strcmp(root_ds
, active_ds
) == 0) {
310 /* Set active property for BE */
311 if (zfs_prop_set(zhp
, BE_ZONE_ACTIVE_PROPERTY
, "on") != 0) {
312 be_print_err(gettext("be_activate: failed to set "
313 "active property (%s): %s\n"), root_ds
,
314 libzfs_error_description(g_zfs
));
315 ret
= zfs_err_to_be_err(g_zfs
);
321 /* Unset active property for old active root dataset */
322 if ((zhp
= zfs_open(g_zfs
, active_ds
,
323 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
324 be_print_err(gettext("be_activate: failed to open "
325 "dataset (%s): %s\n"), active_ds
,
326 libzfs_error_description(g_zfs
));
327 ret
= zfs_err_to_be_err(g_zfs
);
330 if (zfs_prop_set(zhp
, BE_ZONE_ACTIVE_PROPERTY
, "off") != 0) {
331 be_print_err(gettext("be_activate: failed to unset "
332 "active property (%s): %s\n"), active_ds
,
333 libzfs_error_description(g_zfs
));
334 ret
= zfs_err_to_be_err(g_zfs
);
341 be_free_list(be_nodes
);
346 * Function: be_activate_current_be
347 * Description: Set the currently "active" BE to be "active on boot"
351 * BE_SUCCESS - Success
352 * be_errnot_t - Failure
354 * Semi-private (library wide use only)
357 be_activate_current_be(void)
359 int ret
= BE_SUCCESS
;
360 be_transaction_data_t bt
= { 0 };
362 if ((ret
= be_find_current_be(&bt
)) != BE_SUCCESS
) {
366 if ((ret
= _be_activate(bt
.obe_name
)) != BE_SUCCESS
) {
367 be_print_err(gettext("be_activate_current_be: failed to "
368 "activate %s\n"), bt
.obe_name
);
376 * Function: be_is_active_on_boot
377 * Description: Checks if the BE name passed in has the "active on boot"
378 * property set to B_TRUE.
380 * be_name - the name of the BE to check
382 * B_TRUE - if active on boot.
383 * B_FALSE - if not active on boot.
385 * Semi-private (library wide use only)
388 be_is_active_on_boot(char *be_name
)
390 be_node_list_t
*be_node
= NULL
;
392 if (be_name
== NULL
) {
393 be_print_err(gettext("be_is_active_on_boot: "
394 "be_name must not be NULL\n"));
398 if (_be_list(be_name
, &be_node
) != BE_SUCCESS
) {
402 if (be_node
== NULL
) {
406 if (be_node
->be_active_on_boot
) {
407 be_free_list(be_node
);
410 be_free_list(be_node
);
415 /* ******************************************************************** */
416 /* Private Functions */
417 /* ******************************************************************** */
420 * Function: set_bootfs
421 * Description: Sets the bootfs property on the boot pool to be the
422 * root dataset of the activated BE.
424 * boot_pool - The pool we're setting bootfs in.
425 * be_root_ds - The main dataset for the BE.
427 * BE_SUCCESS - Success
428 * be_errno_t - Failure
433 set_bootfs(char *boot_rpool
, char *be_root_ds
)
436 int err
= BE_SUCCESS
;
438 if ((zhp
= zpool_open(g_zfs
, boot_rpool
)) == NULL
) {
439 be_print_err(gettext("set_bootfs: failed to open pool "
440 "(%s): %s\n"), boot_rpool
, libzfs_error_description(g_zfs
));
441 err
= zfs_err_to_be_err(g_zfs
);
445 err
= zpool_set_prop(zhp
, "bootfs", be_root_ds
);
447 be_print_err(gettext("set_bootfs: failed to set "
448 "bootfs property for pool %s: %s\n"), boot_rpool
,
449 libzfs_error_description(g_zfs
));
450 err
= zfs_err_to_be_err(g_zfs
);
460 * Function: set_canmount
461 * Description: Sets the canmount property on the datasets of the
464 * be_nodes - The be_node_t returned from be_list
465 * value - The value of canmount we setting, on|off|noauto.
467 * BE_SUCCESS - Success
468 * be_errno_t - Failure
473 set_canmount(be_node_list_t
*be_nodes
, char *value
)
475 char ds_path
[MAXPATHLEN
];
476 zfs_handle_t
*zhp
= NULL
;
477 be_node_list_t
*list
= be_nodes
;
478 int err
= BE_SUCCESS
;
480 while (list
!= NULL
) {
481 be_dataset_list_t
*datasets
= list
->be_node_datasets
;
483 be_make_root_ds(list
->be_rpool
, list
->be_node_name
, ds_path
,
486 if ((zhp
= zfs_open(g_zfs
, ds_path
, ZFS_TYPE_DATASET
)) ==
488 be_print_err(gettext("set_canmount: failed to open "
489 "dataset (%s): %s\n"), ds_path
,
490 libzfs_error_description(g_zfs
));
491 err
= zfs_err_to_be_err(g_zfs
);
494 if (zfs_prop_get_int(zhp
, ZFS_PROP_MOUNTED
)) {
496 * it's already mounted so we can't change the
497 * canmount property anyway.
501 err
= zfs_prop_set(zhp
,
502 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), value
);
505 be_print_err(gettext("set_canmount: failed to "
506 "set dataset property (%s): %s\n"),
507 ds_path
, libzfs_error_description(g_zfs
));
508 err
= zfs_err_to_be_err(g_zfs
);
514 while (datasets
!= NULL
) {
515 be_make_root_ds(list
->be_rpool
,
516 datasets
->be_dataset_name
, ds_path
,
519 if ((zhp
= zfs_open(g_zfs
, ds_path
, ZFS_TYPE_DATASET
))
521 be_print_err(gettext("set_canmount: failed to "
522 "open dataset %s: %s\n"), ds_path
,
523 libzfs_error_description(g_zfs
));
524 err
= zfs_err_to_be_err(g_zfs
);
527 if (zfs_prop_get_int(zhp
, ZFS_PROP_MOUNTED
)) {
529 * it's already mounted so we can't change the
530 * canmount property anyway.
536 err
= zfs_prop_set(zhp
,
537 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), value
);
540 be_print_err(gettext("set_canmount: "
541 "Failed to set property value %s "
542 "for dataset %s: %s\n"), value
, ds_path
,
543 libzfs_error_description(g_zfs
));
544 err
= zfs_err_to_be_err(g_zfs
);
548 datasets
= datasets
->be_next_dataset
;
550 list
= list
->be_next_node
;
556 * Function: be_get_grub_vers
557 * Description: Gets the grub version number from /boot/grub/capability. If
558 * capability file doesn't exist NULL is returned.
560 * bt - The transaction data for the BE we're getting the grub
562 * cur_vers - used to return the current version of grub from
564 * new_vers - used to return the grub version of the BE we're
567 * BE_SUCCESS - Success
568 * be_errno_t - Failed to find version
573 be_get_grub_vers(be_transaction_data_t
*bt
, char **cur_vers
, char **new_vers
)
575 zfs_handle_t
*zhp
= NULL
;
576 zfs_handle_t
*pool_zhp
= NULL
;
577 int ret
= BE_SUCCESS
;
578 char cap_file
[MAXPATHLEN
];
579 char *temp_mntpnt
= NULL
;
580 char *zpool_mntpt
= NULL
;
581 char *ptmp_mntpnt
= NULL
;
582 char *orig_mntpnt
= NULL
;
583 boolean_t be_mounted
= B_FALSE
;
584 boolean_t pool_mounted
= B_FALSE
;
586 if (!be_has_grub()) {
587 be_print_err(gettext("be_get_grub_vers: Not supported on "
588 "this architecture\n"));
589 return (BE_ERR_NOTSUP
);
592 if (bt
== NULL
|| bt
->obe_name
== NULL
|| bt
->obe_zpool
== NULL
||
593 bt
->obe_root_ds
== NULL
) {
594 be_print_err(gettext("be_get_grub_vers: Invalid BE\n"));
595 return (BE_ERR_INVAL
);
598 if ((pool_zhp
= zfs_open(g_zfs
, bt
->obe_zpool
, ZFS_TYPE_FILESYSTEM
)) ==
600 be_print_err(gettext("be_get_grub_vers: zfs_open failed: %s\n"),
601 libzfs_error_description(g_zfs
));
602 return (zfs_err_to_be_err(g_zfs
));
606 * Check to see if the pool's dataset is mounted. If it isn't we'll
607 * attempt to mount it.
609 if ((ret
= be_mount_pool(pool_zhp
, &ptmp_mntpnt
,
610 &orig_mntpnt
, &pool_mounted
)) != BE_SUCCESS
) {
611 be_print_err(gettext("be_get_grub_vers: pool dataset "
612 "(%s) could not be mounted\n"), bt
->obe_zpool
);
618 * Get the mountpoint for the root pool dataset.
620 if (!zfs_is_mounted(pool_zhp
, &zpool_mntpt
)) {
621 be_print_err(gettext("be_get_grub_vers: pool "
622 "dataset (%s) is not mounted. Can't set the "
623 "default BE in the grub menu.\n"), bt
->obe_zpool
);
624 ret
= BE_ERR_NO_MENU
;
629 * get the version of the most recent grub update.
631 (void) snprintf(cap_file
, sizeof (cap_file
), "%s%s",
632 zpool_mntpt
, BE_CAP_FILE
);
636 if ((ret
= get_ver_from_capfile(cap_file
, cur_vers
)) != BE_SUCCESS
)
639 if ((zhp
= zfs_open(g_zfs
, bt
->obe_root_ds
, ZFS_TYPE_FILESYSTEM
)) ==
641 be_print_err(gettext("be_get_grub_vers: failed to "
642 "open BE root dataset (%s): %s\n"), bt
->obe_root_ds
,
643 libzfs_error_description(g_zfs
));
645 ret
= zfs_err_to_be_err(g_zfs
);
648 if (!zfs_is_mounted(zhp
, &temp_mntpnt
)) {
649 if ((ret
= _be_mount(bt
->obe_name
, &temp_mntpnt
,
650 BE_MOUNT_FLAG_NO_ZONES
)) != BE_SUCCESS
) {
651 be_print_err(gettext("be_get_grub_vers: failed to "
652 "mount BE (%s)\n"), bt
->obe_name
);
663 * Now get the grub version for the BE being activated.
665 (void) snprintf(cap_file
, sizeof (cap_file
), "%s%s", temp_mntpnt
,
667 ret
= get_ver_from_capfile(cap_file
, new_vers
);
668 if (ret
!= BE_SUCCESS
) {
673 (void) _be_unmount(bt
->obe_name
, 0);
677 int iret
= BE_SUCCESS
;
678 iret
= be_unmount_pool(pool_zhp
, ptmp_mntpnt
, orig_mntpnt
);
679 if (ret
== BE_SUCCESS
)
691 * Function: get_ver_from_capfile
692 * Description: Parses the capability file passed in looking for the VERSION
693 * line. If found the version is returned in vers, if not then
694 * NULL is returned in vers.
697 * file - the path to the capability file we want to parse.
698 * vers - the version string that will be passed back.
700 * BE_SUCCESS - Success
701 * be_errno_t - Failed to find version
706 get_ver_from_capfile(char *file
, char **vers
)
711 int err
= BE_SUCCESS
;
714 if (!be_has_grub()) {
715 be_print_err(gettext("get_ver_from_capfile: Not supported "
716 "on this architecture\n"));
717 return (BE_ERR_NOTSUP
);
721 * Set version string to NULL; the only case this shouldn't be set
722 * to be NULL is when we've actually found a version in the capability
723 * file, which is set below.
728 * If the capability file doesn't exist, we're returning success
729 * because on older releases, the capability file did not exist
730 * so this is a valid scenario.
732 if (access(file
, F_OK
) == 0) {
733 if ((fp
= fopen(file
, "r")) == NULL
) {
735 be_print_err(gettext("get_ver_from_capfile: failed to "
736 "open file %s with error %s\n"), file
,
738 err
= errno_to_be_err(err
);
742 while (fgets(line
, BUFSIZ
, fp
)) {
743 char *tok
= strtok_r(line
, "=", &last
);
745 if (tok
== NULL
|| tok
[0] == '#') {
747 } else if (strcmp(tok
, "VERSION") == 0) {
748 *vers
= strdup(last
);
759 * To be able to boot EFI labeled disks, GRUB stage1 needs to be written
760 * into the MBR. We do not do this if we're on disks with a traditional
761 * fdisk partition table only, or if any foreign EFI partitions exist.
762 * In the trivial case of a whole-disk vdev we always write stage1 into
766 be_do_installgrub_mbr(char *diskname
, nvlist_t
*child
)
768 struct uuid allowed_uuids
[] = {
792 (void) nvlist_lookup_uint64(child
, ZPOOL_CONFIG_WHOLE_DISK
,
798 if ((fd
= open(diskname
, O_RDONLY
|O_NDELAY
)) < 0)
801 if ((npart
= efi_alloc_and_read(fd
, &gpt
)) <= 0)
804 for (i
= 0; i
!= npart
; i
++) {
807 u
= &gpt
->efi_parts
[i
].p_guid
;
810 j
!= sizeof (allowed_uuids
) / sizeof (struct uuid
);
812 if (bcmp(u
, &allowed_uuids
[j
],
813 sizeof (struct uuid
)) == 0)
824 be_do_installgrub_helper(zpool_handle_t
*zphp
, nvlist_t
*child
, char *stage1
,
827 char installgrub_cmd
[MAXPATHLEN
];
828 char be_run_cmd_errbuf
[BUFSIZ
];
829 char diskname
[MAXPATHLEN
];
831 char *path
, *dsk_ptr
;
834 if (nvlist_lookup_string(child
, ZPOOL_CONFIG_PATH
, &path
) != 0) {
835 be_print_err(gettext("be_do_installgrub: "
836 "failed to get device path\n"));
837 return (BE_ERR_NODEV
);
841 * Modify the vdev path to point to the raw disk.
845 return (BE_ERR_NOMEM
);
847 dsk_ptr
= strstr(path
, "/dsk/");
848 if (dsk_ptr
!= NULL
) {
855 (void) snprintf(diskname
, sizeof (diskname
), "%s/r%s", path
, dsk_ptr
);
858 if (be_do_installgrub_mbr(diskname
, child
))
861 vname
= zpool_vdev_name(g_zfs
, zphp
, child
, B_FALSE
);
863 be_print_err(gettext("be_do_installgrub: "
864 "failed to get device name: %s\n"),
865 libzfs_error_description(g_zfs
));
866 return (zfs_err_to_be_err(g_zfs
));
869 (void) snprintf(installgrub_cmd
, sizeof (installgrub_cmd
),
870 "%s %s %s %s %s", BE_INSTALL_GRUB
, m_flag
, stage1
, stage2
,
872 if (be_run_cmd(installgrub_cmd
, be_run_cmd_errbuf
, BUFSIZ
, NULL
, 0)
874 be_print_err(gettext("be_do_installgrub: installgrub "
875 "failed for device %s.\n"), vname
);
876 /* Assume localized cmd err output. */
877 be_print_err(gettext(" Command: \"%s\"\n"),
879 be_print_err("%s", be_run_cmd_errbuf
);
881 return (BE_ERR_BOOTFILE_INST
);
889 * Function: be_do_installgrub
890 * Description: This function runs installgrub using the grub loader files
891 * from the BE we're activating and installing them on the
892 * pool the BE lives in.
895 * bt - The transaction data for the BE we're activating.
897 * BE_SUCCESS - Success
898 * be_errno_t - Failure
904 be_do_installgrub(be_transaction_data_t
*bt
)
906 zpool_handle_t
*zphp
= NULL
;
907 zfs_handle_t
*zhp
= NULL
;
908 nvlist_t
**child
, *nv
, *config
;
909 uint_t c
, children
= 0;
910 char *tmp_mntpt
= NULL
;
911 char *pool_mntpnt
= NULL
;
912 char *ptmp_mntpnt
= NULL
;
913 char *orig_mntpnt
= NULL
;
915 FILE *zpool_cap_fp
= NULL
;
917 char cap_file
[MAXPATHLEN
];
918 char zpool_cap_file
[MAXPATHLEN
];
919 char stage1
[MAXPATHLEN
];
920 char stage2
[MAXPATHLEN
];
922 int ret
= BE_SUCCESS
;
924 boolean_t be_mounted
= B_FALSE
;
925 boolean_t pool_mounted
= B_FALSE
;
927 if (!be_has_grub()) {
928 be_print_err(gettext("be_do_installgrub: Not supported "
929 "on this architecture\n"));
930 return (BE_ERR_NOTSUP
);
933 if ((zhp
= zfs_open(g_zfs
, bt
->obe_root_ds
, ZFS_TYPE_FILESYSTEM
)) ==
935 be_print_err(gettext("be_do_installgrub: failed to "
936 "open BE root dataset (%s): %s\n"), bt
->obe_root_ds
,
937 libzfs_error_description(g_zfs
));
938 ret
= zfs_err_to_be_err(g_zfs
);
941 if (!zfs_is_mounted(zhp
, &tmp_mntpt
)) {
942 if ((ret
= _be_mount(bt
->obe_name
, &tmp_mntpt
,
943 BE_MOUNT_FLAG_NO_ZONES
)) != BE_SUCCESS
) {
944 be_print_err(gettext("be_do_installgrub: failed to "
945 "mount BE (%s)\n"), bt
->obe_name
);
953 (void) snprintf(stage1
, sizeof (stage1
), "%s%s", tmp_mntpt
, BE_STAGE_1
);
954 (void) snprintf(stage2
, sizeof (stage2
), "%s%s", tmp_mntpt
, BE_STAGE_2
);
956 if ((zphp
= zpool_open(g_zfs
, bt
->obe_zpool
)) == NULL
) {
957 be_print_err(gettext("be_do_installgrub: failed to open "
958 "pool (%s): %s\n"), bt
->obe_zpool
,
959 libzfs_error_description(g_zfs
));
960 ret
= zfs_err_to_be_err(g_zfs
);
962 (void) _be_unmount(bt
->obe_name
, 0);
967 if ((config
= zpool_get_config(zphp
, NULL
)) == NULL
) {
968 be_print_err(gettext("be_do_installgrub: failed to get zpool "
969 "configuration information. %s\n"),
970 libzfs_error_description(g_zfs
));
971 ret
= zfs_err_to_be_err(g_zfs
);
978 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) != 0) {
979 be_print_err(gettext("be_do_installgrub: failed to get vdev "
980 "tree: %s\n"), libzfs_error_description(g_zfs
));
981 ret
= zfs_err_to_be_err(g_zfs
);
985 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
987 be_print_err(gettext("be_do_installgrub: failed to traverse "
988 "the vdev tree: %s\n"), libzfs_error_description(g_zfs
));
989 ret
= zfs_err_to_be_err(g_zfs
);
992 for (c
= 0; c
< children
; c
++) {
993 uint_t i
, nchildren
= 0;
995 vname
= zpool_vdev_name(g_zfs
, zphp
, child
[c
], B_FALSE
);
997 be_print_err(gettext(
998 "be_do_installgrub: "
999 "failed to get device name: %s\n"),
1000 libzfs_error_description(g_zfs
));
1001 ret
= zfs_err_to_be_err(g_zfs
);
1004 if (strcmp(vname
, "mirror") == 0 || vname
[0] != 'c') {
1007 if (nvlist_lookup_nvlist_array(child
[c
],
1008 ZPOOL_CONFIG_CHILDREN
, &nvchild
, &nchildren
) != 0) {
1009 be_print_err(gettext("be_do_installgrub: "
1010 "failed to traverse the vdev tree: %s\n"),
1011 libzfs_error_description(g_zfs
));
1012 ret
= zfs_err_to_be_err(g_zfs
);
1016 for (i
= 0; i
< nchildren
; i
++) {
1017 ret
= be_do_installgrub_helper(zphp
, nvchild
[i
],
1019 if (ret
!= BE_SUCCESS
)
1025 ret
= be_do_installgrub_helper(zphp
, child
[c
], stage1
,
1027 if (ret
!= BE_SUCCESS
)
1033 * Copy the grub capability file from the BE we're activating into
1036 (void) snprintf(cap_file
, sizeof (cap_file
), "%s%s", tmp_mntpt
,
1039 if ((zhp
= zfs_open(g_zfs
, bt
->obe_zpool
, ZFS_TYPE_FILESYSTEM
)) ==
1041 be_print_err(gettext("be_do_installgrub: zfs_open "
1042 "failed: %s\n"), libzfs_error_description(g_zfs
));
1044 return (zfs_err_to_be_err(g_zfs
));
1048 * Check to see if the pool's dataset is mounted. If it isn't we'll
1049 * attempt to mount it.
1051 if ((ret
= be_mount_pool(zhp
, &ptmp_mntpnt
,
1052 &orig_mntpnt
, &pool_mounted
)) != BE_SUCCESS
) {
1053 be_print_err(gettext("be_do_installgrub: pool dataset "
1054 "(%s) could not be mounted\n"), bt
->obe_zpool
);
1061 * Get the mountpoint for the root pool dataset.
1063 if (!zfs_is_mounted(zhp
, &pool_mntpnt
)) {
1064 be_print_err(gettext("be_do_installgrub: pool "
1065 "dataset (%s) is not mounted. Can't check the grub "
1066 "version from the grub capability file.\n"), bt
->obe_zpool
);
1067 ret
= BE_ERR_NO_MENU
;
1071 (void) snprintf(zpool_cap_file
, sizeof (zpool_cap_file
), "%s%s",
1072 pool_mntpnt
, BE_CAP_FILE
);
1077 if ((cap_fp
= fopen(cap_file
, "r")) == NULL
) {
1079 be_print_err(gettext("be_do_installgrub: failed to open grub "
1080 "capability file\n"));
1081 ret
= errno_to_be_err(err
);
1084 if ((zpool_cap_fp
= fopen(zpool_cap_file
, "w")) == NULL
) {
1086 be_print_err(gettext("be_do_installgrub: failed to open new "
1087 "grub capability file\n"));
1088 ret
= errno_to_be_err(err
);
1089 (void) fclose(cap_fp
);
1093 while (fgets(line
, BUFSIZ
, cap_fp
)) {
1094 (void) fputs(line
, zpool_cap_fp
);
1097 (void) fclose(zpool_cap_fp
);
1098 (void) fclose(cap_fp
);
1103 iret
= be_unmount_pool(zhp
, ptmp_mntpnt
, orig_mntpnt
);
1104 if (ret
== BE_SUCCESS
)
1111 (void) _be_unmount(bt
->obe_name
, 0);
1118 * Function: be_promote_zone_ds
1119 * Description: This function finds the zones for the BE being activated
1120 * and the active zonepath dataset for each zone. Then each
1121 * active zonepath dataset is promoted.
1124 * be_name - the name of the global zone BE that we need to
1125 * find the zones for.
1126 * be_root_ds - the root dataset for be_name.
1128 * BE_SUCCESS - Success
1129 * be_errno_t - Failure
1135 be_promote_zone_ds(char *be_name
, char *be_root_ds
)
1137 char *zone_ds
= NULL
;
1138 char *temp_mntpt
= NULL
;
1139 char origin
[MAXPATHLEN
];
1140 char zoneroot_ds
[MAXPATHLEN
];
1141 zfs_handle_t
*zhp
= NULL
;
1142 zfs_handle_t
*z_zhp
= NULL
;
1143 zoneList_t zone_list
= NULL
;
1144 zoneBrandList_t
*brands
= NULL
;
1145 boolean_t be_mounted
= B_FALSE
;
1147 int err
= BE_SUCCESS
;
1150 * Get the supported zone brands so we can pass that
1151 * to z_get_nonglobal_zone_list_by_brand. Currently
1152 * only the ipkg and labeled brand zones are supported
1155 if ((brands
= be_get_supported_brandlist()) == NULL
) {
1156 be_print_err(gettext("be_promote_zone_ds: no supported "
1158 return (BE_SUCCESS
);
1161 if ((zhp
= zfs_open(g_zfs
, be_root_ds
,
1162 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
1163 be_print_err(gettext("be_promote_zone_ds: Failed to open "
1164 "dataset (%s): %s\n"), be_root_ds
,
1165 libzfs_error_description(g_zfs
));
1166 err
= zfs_err_to_be_err(g_zfs
);
1167 z_free_brand_list(brands
);
1171 if (!zfs_is_mounted(zhp
, &temp_mntpt
)) {
1172 if ((err
= _be_mount(be_name
, &temp_mntpt
,
1173 BE_MOUNT_FLAG_NO_ZONES
)) != BE_SUCCESS
) {
1174 be_print_err(gettext("be_promote_zone_ds: failed to "
1175 "mount the BE for zones procesing.\n"));
1177 z_free_brand_list(brands
);
1180 be_mounted
= B_TRUE
;
1184 * Set the zone root to the temp mount point for the BE we just mounted.
1186 z_set_zone_root(temp_mntpt
);
1189 * Get all the zones based on the brands we're looking for. If no zones
1190 * are found that we're interested in unmount the BE and move on.
1192 if ((zone_list
= z_get_nonglobal_zone_list_by_brand(brands
)) == NULL
) {
1194 (void) _be_unmount(be_name
, 0);
1196 z_free_brand_list(brands
);
1198 return (BE_SUCCESS
);
1200 for (zone_index
= 0; z_zlist_get_zonename(zone_list
, zone_index
)
1201 != NULL
; zone_index
++) {
1202 char *zone_path
= NULL
;
1204 /* Skip zones that aren't at least installed */
1205 if (z_zlist_get_current_state(zone_list
, zone_index
) <
1206 ZONE_STATE_INSTALLED
)
1210 z_zlist_get_zonepath(zone_list
, zone_index
)) == NULL
) ||
1211 ((zone_ds
= be_get_ds_from_dir(zone_path
)) == NULL
) ||
1212 !be_zone_supported(zone_ds
))
1215 if (be_find_active_zone_root(zhp
, zone_ds
,
1216 zoneroot_ds
, sizeof (zoneroot_ds
)) != 0) {
1217 be_print_err(gettext("be_promote_zone_ds: "
1218 "Zone does not have an active root "
1219 "dataset, skipping this zone.\n"));
1223 if ((z_zhp
= zfs_open(g_zfs
, zoneroot_ds
,
1224 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
1225 be_print_err(gettext("be_promote_zone_ds: "
1226 "Failed to open dataset "
1227 "(%s): %s\n"), zoneroot_ds
,
1228 libzfs_error_description(g_zfs
));
1229 err
= zfs_err_to_be_err(g_zfs
);
1233 if (zfs_prop_get(z_zhp
, ZFS_PROP_ORIGIN
, origin
,
1234 sizeof (origin
), NULL
, NULL
, 0, B_FALSE
) != 0) {
1240 * We don't need to close the zfs handle at this
1241 * point because the callback funtion
1242 * be_promote_ds_callback() will close it for us.
1244 if (be_promote_ds_callback(z_zhp
, NULL
) != 0) {
1245 be_print_err(gettext("be_promote_zone_ds: "
1246 "failed to activate the "
1247 "datasets for %s: %s\n"),
1249 libzfs_error_description(g_zfs
));
1250 err
= BE_ERR_PROMOTE
;
1256 (void) _be_unmount(be_name
, 0);
1259 z_free_brand_list(brands
);
1260 z_free_zone_list(zone_list
);
1265 * Function: be_promote_ds_callback
1266 * Description: This function is used to promote the datasets for the BE
1267 * being activated as well as the datasets for the zones BE
1271 * zhp - the zfs handle for zone BE being activated.
1275 * be_errno_t - Failure
1282 be_promote_ds_callback(zfs_handle_t
*zhp
, void *data
)
1284 char origin
[MAXPATHLEN
];
1285 char *sub_dataset
= NULL
;
1289 sub_dataset
= strdup(zfs_get_name(zhp
));
1290 if (sub_dataset
== NULL
) {
1295 be_print_err(gettext("be_promote_ds_callback: "
1296 "Invalid zfs handle passed into function\n"));
1302 * This loop makes sure that we promote the dataset to the
1303 * top of the tree so that it is no longer a decendent of any
1304 * dataset. The ZFS close and then open is used to make sure that
1305 * the promotion is updated before we move on.
1307 while (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, origin
,
1308 sizeof (origin
), NULL
, NULL
, 0, B_FALSE
) == 0) {
1309 if (zfs_promote(zhp
) != 0) {
1310 if (libzfs_errno(g_zfs
) != EZFS_EXISTS
) {
1311 be_print_err(gettext("be_promote_ds_callback: "
1312 "promote of %s failed: %s\n"),
1314 libzfs_error_description(g_zfs
));
1315 ret
= zfs_err_to_be_err(g_zfs
);
1319 * If the call to zfs_promote returns the
1320 * error EZFS_EXISTS we've hit a snapshot name
1321 * collision. This means we're probably
1322 * attemping to promote a zone dataset above a
1323 * parent dataset that belongs to another zone
1324 * which this zone was cloned from.
1326 * TODO: If this is a zone dataset at some
1327 * point we should skip this if the zone
1328 * paths for the dataset and the snapshot
1331 be_print_err(gettext("be_promote_ds_callback: "
1332 "promote of %s failed due to snapshot "
1333 "name collision: %s\n"), zfs_get_name(zhp
),
1334 libzfs_error_description(g_zfs
));
1335 ret
= zfs_err_to_be_err(g_zfs
);
1340 if ((zhp
= zfs_open(g_zfs
, sub_dataset
,
1341 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
1342 be_print_err(gettext("be_promote_ds_callback: "
1343 "Failed to open dataset (%s): %s\n"), sub_dataset
,
1344 libzfs_error_description(g_zfs
));
1345 ret
= zfs_err_to_be_err(g_zfs
);
1350 /* Iterate down this dataset's children and promote them */
1351 ret
= zfs_iter_filesystems(zhp
, be_promote_ds_callback
, NULL
);