/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
#include <assert.h>
#include <boot_utils.h>
#include <libintl.h>
#include <libnvpair.h>
#include <libzfs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/systeminfo.h>
#include <unistd.h>
#include <libbe.h>
#include <libbe_priv.h>
/*
* Private function prototypes
*/
static int set_canmount(be_node_list_t *, char *);
static int be_promote_zone_ds(char *, char *);
static int be_promote_ds_callback(zfs_handle_t *, void *);
/* ******************************************************************** */
/* Public Functions */
/* ******************************************************************** */
/*
* Function: be_activate
* Description: Calls _be_activate which activates the BE named in the
* attributes passed in through be_attrs. The process of
* activation sets the bootfs property of the root pool, and resets
* the canmount property to noauto. Any boot menu handling must be
* performed by the caller.
* Parameters:
* be_attrs - pointer to nvlist_t of attributes being passed in.
* The follow attribute values are used by this function:
*
* BE_ATTR_ORIG_BE_NAME *required
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
* Scope:
* Public
*/
int
{
if (getzoneid() != GLOBAL_ZONEID) {
/*
* Check to see if we have write access to the root filesystem
*/
ret = be_check_rozr();
if (ret != BE_SUCCESS)
return (ret);
}
/* Initialize libzfs handle */
if (!be_zfs_init())
return (BE_ERR_INIT);
/* Get the BE name to activate */
!= 0) {
"lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
be_zfs_fini();
return (BE_ERR_INVAL);
}
/* Validate BE name */
if (!be_valid_be_name(be_name)) {
be_name);
be_zfs_fini();
return (BE_ERR_INVAL);
}
be_zfs_fini();
return (ret);
}
/* ******************************************************************** */
/* Semi Private Functions */
/* ******************************************************************** */
/*
* Function: _be_activate
* Description: This does the actual work described in be_activate.
* Parameters:
* be_name - pointer to the name of BE to activate.
*
* Return:
* BE_SUCCESS - Success
* be_errnot_t - Failure
* Scope:
* Public
*/
int
{
int zret = 0;
/*
* TODO: The BE needs to be validated to make sure that it is actually
* a bootable BE.
*/
return (BE_ERR_INVAL);
/* Set obe_name to be_name in the cb structure */
/* find which zpool the be is in */
return (BE_ERR_BE_NOENT);
} else if (zret < 0) {
"zpool_iter failed: %s\n"),
return (ret);
}
/*
* Check to see if this is an attempt to activate a zone BE
* that is not associated with the currently active global
* zone (ie. an unbootable BE).
*/
if (getzoneid() != GLOBAL_ZONEID) {
"supported on unbootable BE\n"));
return (ret);
}
}
}
int
{
int ret;
int zfs_init = 0;
/*
* Check to see if we're operating inside a Solaris Container
* or the Global Zone.
*/
if (getzoneid() != GLOBAL_ZONEID)
if (!be_zfs_init())
return (BE_ERR_INIT);
zfs_init = 1;
}
return (ret);
}
"canmount dataset property\n"));
goto done;
}
if (!in_ngz) {
!= BE_SUCCESS) {
"bootfs pool property for %s\n"), root_ds);
goto done;
}
}
/*
* We don't need to close the zfs handle at this
* point because The callback funtion
* be_promote_ds_callback() will close it for us.
*/
"failed to activate the "
"datasets for %s: %s\n"),
goto done;
}
} else {
"dataset (%s): %s\n"), root_ds,
goto done;
}
!= BE_SUCCESS) {
"the active zonepath datasets for zones in BE %s\n"),
be_name);
}
if (in_ngz) {
sizeof (cur_root_ds))) != BE_SUCCESS) {
"active zone root\n"));
goto done;
}
/*
* If we are trying to activate the currently
* active BE, we just fall through and return
* BE_SUCCESS.
*/
/* Set the active property on the 'new' BE */
BE_SUCCESS) {
"to activate BE %s\n"), be_name);
goto done;
}
/* Unset the active property on the 'old' BE */
BE_SUCCESS) {
/*
* Find the BE name for the currently active
* zone root dataset
*/
"failed to de-activate dataset "
"%s\n"), cur_root_ds);
} else {
"failed to de-activate BE "
"%s\n"), ds);
}
/* Try to un-activate the 'new' BE */
if ((ret =
!= BE_SUCCESS) {
"failed to un-activate BE "
"%s\n"), be_name);
}
goto done;
}
}
ret = BE_SUCCESS;
}
done:
if (zfs_init)
be_zfs_fini();
return (ret);
}
/*
* Function: be_activate_current_be
* Description: Set the currently "active" BE to be "active on boot"
* Paramters:
* none
* Returns:
* BE_SUCCESS - Success
* be_errnot_t - Failure
* Scope:
* Semi-private (library wide use only)
*/
int
be_activate_current_be(void)
{
return (ret);
}
return (ret);
}
return (BE_SUCCESS);
}
/*
* Function: be_is_active_on_boot
* Description: Checks if the BE name passed in has the "active on boot"
* property set to B_TRUE.
* Paramters:
* be_name - the name of the BE to check
* Returns:
* B_TRUE - if active on boot.
* B_FALSE - if not active on boot.
* Scope:
* Semi-private (library wide use only)
*/
{
"be_name must not be NULL\n"));
return (B_FALSE);
}
return (B_FALSE);
}
return (B_FALSE);
}
if (be_node->be_active_on_boot) {
return (B_TRUE);
} else {
return (B_FALSE);
}
}
/* ******************************************************************** */
/* Private Functions */
/* ******************************************************************** */
/*
* Function: set_bootfs
* Description: Sets the bootfs property on the boot pool to be the
* root dataset of the activated BE.
* Parameters:
* boot_pool - The pool we're setting bootfs in.
* be_root_ds - The main dataset for the BE.
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
* Scope:
* Private
*/
static int
{
return (err);
}
if (err) {
"bootfs property for pool %s: %s\n"), boot_rpool,
return (err);
}
return (BE_SUCCESS);
}
/*
* Function: set_canmount
* Description: Sets the canmount property on the datasets of the
* activated BE.
* Parameters:
* be_nodes - The be_node_t returned from be_list
* value - The value of canmount we setting, on|off|noauto.
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
* Scope:
* Private
*/
static int
{
sizeof (ds_path));
NULL) {
"dataset (%s): %s\n"), ds_path,
return (err);
}
/*
* it's already mounted so we can't change the
* canmount property anyway.
*/
err = BE_SUCCESS;
} else {
if (err) {
"set dataset property (%s): %s\n"),
return (err);
}
}
sizeof (ds_path));
== NULL) {
"open dataset %s: %s\n"), ds_path,
return (err);
}
/*
* it's already mounted so we can't change the
* canmount property anyway.
*/
err = BE_SUCCESS;
break;
}
if (err) {
"Failed to set property value %s "
return (err);
}
}
}
return (err);
}
/*
* Function: _be_get_boot_device_list
* Description: This function gathers the list of devices where bootblocks
* should be installed, based on the name of a zpool.
* Parameters:
* zpool - the name of the ZFS pool for which we want to know the
* boot devices
* boot_devices - a pointer to a list of strings which on return
* from the function will be populated with the list of
* boot devices. The memory for the list and each element
* will be allocated here and must be freed by the caller.
* num_boot_devices - a pointer to an integer which on return from
* the function will point to the number of devices in the
* boot_devices list.
*/
int
int *num_boot_devices)
{
uint_t c;
int zfs_init = 0;
char *path;
if (boot_devices) {
if ((*boot_devices =
"allocation failed"));
return (BE_ERR_NOMEM);
}
}
if (!be_zfs_init())
return (BE_ERR_INIT);
zfs_init = 1;
}
"pool (%s): %s\n"), zpool,
return (zfs_err_to_be_err(g_zfs));
}
"zpool configuration information. %s\n"),
return (zfs_err_to_be_err(g_zfs));
}
/*
* Get the vdev tree
*/
return (zfs_err_to_be_err(g_zfs));
}
&children) != 0) {
"traverse the vdev tree: %s\n"),
return (zfs_err_to_be_err(g_zfs));
}
for (c = 0; c < children; c++) {
char *type;
!= 0) {
"_be_get_boot_device_list: "
"failed to get device type for top-level vdev in "
"root pool %s\n"), zpool);
return (BE_ERR_NO_RPOOLS);
}
if (nvlist_lookup_nvlist_array(child[c],
gettext("_be_get_boot_device_list: "
"failed to traverse the vdev tree: %s\n"),
return (zfs_err_to_be_err(g_zfs));
}
for (i = 0; i < nchildren; i++) {
/* get full path directly for slice inclusion */
if (nvlist_lookup_string(nvchild[i],
ZPOOL_CONFIG_PATH, &path) != 0) {
/* ignore */
continue;
}
path += 9;
if (((*boot_devices)[num_devices++] =
"_be_get_boot_device_list: memory "
"allocation failed"));
return (BE_ERR_NOMEM);
}
max_devices *= 2;
if ((*boot_devices = realloc(
*boot_devices, max_devices)) ==
NULL) {
"_be_get_boot_device_list: "
"memory allocation "
"failed"));
return (BE_ERR_NOMEM);
}
}
}
/* get full path directly for slice inclusion */
if (nvlist_lookup_string(child[c],
ZPOOL_CONFIG_PATH, &path) != 0) {
/* ignore */
continue;
}
path += 9;
NULL) {
" memory allocation failed"));
return (BE_ERR_NOMEM);
}
max_devices *= 2;
max_devices)) == NULL) {
"_be_get_boot_device_list: memory "
"allocation failed"));
return (BE_ERR_NOMEM);
}
}
}
}
done:
if (zfs_init)
be_zfs_fini();
return (ret);
}
/*
* Function: be_promote_zone_ds
* Description: This function finds the zones for the BE being activated
* and the active zonepath dataset for each zone. Then each
* active zonepath dataset is promoted.
*
* Parameters:
* be_name - the name of the global zone BE that we need to
* find the zones for.
* be_root_ds - the root dataset for be_name.
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
*
* Scope:
* Private
*/
static int
{
int zone_index = 0;
/*
* Get the supported zone brands so we can pass that
* to z_get_nonglobal_zone_list_by_brand. Currently
* only the solaris and labeled brand zones are supported
*
*/
"brands\n"));
return (BE_SUCCESS);
}
ZFS_TYPE_FILESYSTEM)) == NULL) {
"dataset (%s): %s\n"), be_root_ds,
return (err);
}
BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
"mount the BE for zones procesing.\n"));
return (err);
}
be_mounted = B_TRUE;
}
/*
* Set the zone root to the temp mount point for the BE we just mounted.
*/
/*
* Get all the zones based on the brands we're looking for. If no zones
* are found that we're interested in unmount the BE and move on.
*/
if (be_mounted)
return (BE_SUCCESS);
}
!= NULL; zone_index++) {
/* Skip zones that aren't at least installed */
continue;
if (((zone_path =
continue;
zoneroot_ds, sizeof (zoneroot_ds)) != 0) {
"Zone does not have an active root "
"dataset, skipping this zone.\n"));
continue;
}
ZFS_TYPE_FILESYSTEM)) == NULL) {
"Failed to open dataset "
"(%s): %s\n"), zoneroot_ds,
goto done;
}
continue;
}
/*
* We don't need to close the zfs handle at this
* point because the callback funtion
* be_promote_ds_callback() will close it for us.
*/
"failed to activate the "
"datasets for %s: %s\n"),
goto done;
}
}
done:
if (be_mounted)
return (err);
}
/*
* Function: be_promote_ds_callback
* Description: This function is used to promote the datasets for the BE
* being activated as well as the datasets for the zones BE
* being activated.
*
* Parameters:
* zhp - the zfs handle for zone BE being activated.
* data - not used.
* Return:
* 0 - Success
* be_errno_t - Failure
*
* Scope:
* Private
*/
static int
/* LINTED */
{
int ret = 0;
if (sub_dataset == NULL) {
ret = BE_ERR_NOMEM;
goto done;
}
} else {
"Invalid zfs handle passed into function\n"));
ret = BE_ERR_INVAL;
goto done;
}
/*
* This loop makes sure that we promote the dataset to the
* top of the tree so that it is no longer a decendent of any
* dataset. The ZFS close and then open is used to make sure that
* the promotion is updated before we move on.
*/
/*
* If we're inside a zone, we need to guard against
* attempting to promote origins that live outside of
* the zone's 'zpool analog'.
*/
if (getzoneid() != GLOBAL_ZONEID) {
!= 0) {
/*
* The origin's zpool analog does not match the
* zpool analog that the dataset lives in which
* indicates perhaps it was generated via a
* zoneadm clone of another zone. So, we can't
* promote it since it's not part of this zone's
* BE namespace and so we don't.
*/
goto done;
}
}
if (zfs_promote(zhp) != 0) {
"promote of %s failed: %s\n"),
goto done;
} else {
/*
* If the call to zfs_promote returns the
* error EZFS_EXISTS we've hit a snapshot name
* collision. This means we're probably
* attemping to promote a zone dataset above a
* parent dataset that belongs to another zone
* which this zone was cloned from.
*
* TODO: If this is a zone dataset at some
* point we should skip this if the zone
* paths for the dataset and the snapshot
* don't match.
*/
"promote of %s failed due to snapshot "
goto done;
}
}
ZFS_TYPE_FILESYSTEM)) == NULL) {
"Failed to open dataset (%s): %s\n"), sub_dataset,
goto done;
}
}
/* Iterate down this dataset's children and promote them */
done:
return (ret);
}