2018-08-07 15:46:06 +02:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
2018-07-24 15:17:40 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
|
2018-08-07 15:46:06 +02:00
|
|
|
* Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
|
2018-07-24 15:17:40 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-08-07 16:02:41 +02:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2018-07-24 15:17:40 +02:00
|
|
|
#include "be.h"
|
|
|
|
#include "be_impl.h"
|
|
|
|
|
2018-08-07 04:40:00 +02:00
|
|
|
static int snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data);
|
2018-08-05 21:38:56 +02:00
|
|
|
|
2018-07-24 15:17:40 +02:00
|
|
|
/*
|
|
|
|
* Returns the name of the active boot environment
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
be_active_name(libbe_handle_t *lbh)
|
|
|
|
{
|
2018-07-25 05:50:01 +02:00
|
|
|
|
2018-11-19 03:12:08 +01:00
|
|
|
if (*lbh->rootfs != '\0')
|
|
|
|
return (strrchr(lbh->rootfs, '/') + sizeof(char));
|
|
|
|
else
|
|
|
|
return (lbh->rootfs);
|
2018-07-24 15:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns full path of the active boot environment
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
be_active_path(libbe_handle_t *lbh)
|
|
|
|
{
|
2018-07-25 05:50:01 +02:00
|
|
|
|
2018-07-25 05:08:11 +02:00
|
|
|
return (lbh->rootfs);
|
2018-07-24 15:17:40 +02:00
|
|
|
}
|
|
|
|
|
2018-07-25 16:45:00 +02:00
|
|
|
/*
|
|
|
|
* Returns the name of the next active boot environment
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
be_nextboot_name(libbe_handle_t *lbh)
|
|
|
|
{
|
|
|
|
|
2018-11-19 03:12:08 +01:00
|
|
|
if (*lbh->bootfs != '\0')
|
|
|
|
return (strrchr(lbh->bootfs, '/') + sizeof(char));
|
|
|
|
else
|
|
|
|
return (lbh->bootfs);
|
2018-07-25 16:45:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns full path of the active boot environment
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
be_nextboot_path(libbe_handle_t *lbh)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (lbh->bootfs);
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:17:40 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the path of the boot environment root dataset
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
be_root_path(libbe_handle_t *lbh)
|
|
|
|
{
|
2018-07-25 05:50:01 +02:00
|
|
|
|
2018-07-24 15:17:40 +02:00
|
|
|
return (lbh->root);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2018-07-25 16:30:47 +02:00
|
|
|
* Populates dsnvl with one nvlist per bootenv dataset describing the properties
|
|
|
|
* of that dataset that we've declared ourselves to care about.
|
2018-07-24 15:17:40 +02:00
|
|
|
*/
|
2018-07-25 16:30:47 +02:00
|
|
|
int
|
|
|
|
be_get_bootenv_props(libbe_handle_t *lbh, nvlist_t *dsnvl)
|
2018-07-24 15:17:40 +02:00
|
|
|
{
|
|
|
|
prop_data_t data;
|
|
|
|
|
|
|
|
data.lbh = lbh;
|
2018-07-25 16:30:47 +02:00
|
|
|
data.list = dsnvl;
|
2018-08-03 04:04:57 +02:00
|
|
|
data.single_object = false;
|
2018-08-07 04:40:00 +02:00
|
|
|
return (be_proplist_update(&data));
|
2018-07-24 15:17:40 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 04:04:57 +02:00
|
|
|
int
|
2018-08-05 06:40:13 +02:00
|
|
|
be_get_dataset_props(libbe_handle_t *lbh, const char *name, nvlist_t *props)
|
2018-08-03 04:04:57 +02:00
|
|
|
{
|
|
|
|
zfs_handle_t *snap_hdl;
|
|
|
|
prop_data_t data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
data.lbh = lbh;
|
|
|
|
data.list = props;
|
|
|
|
data.single_object = true;
|
|
|
|
if ((snap_hdl = zfs_open(lbh->lzh, name,
|
2018-08-05 06:40:13 +02:00
|
|
|
ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL)
|
2018-08-03 04:04:57 +02:00
|
|
|
return (BE_ERR_ZFSOPEN);
|
|
|
|
|
|
|
|
ret = prop_list_builder_cb(snap_hdl, &data);
|
|
|
|
zfs_close(snap_hdl);
|
|
|
|
return (ret);
|
|
|
|
}
|
2018-07-24 15:17:40 +02:00
|
|
|
|
2018-08-05 21:38:56 +02:00
|
|
|
int
|
|
|
|
be_get_dataset_snapshots(libbe_handle_t *lbh, const char *name, nvlist_t *props)
|
|
|
|
{
|
|
|
|
zfs_handle_t *ds_hdl;
|
|
|
|
prop_data_t data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
data.lbh = lbh;
|
|
|
|
data.list = props;
|
|
|
|
data.single_object = false;
|
|
|
|
if ((ds_hdl = zfs_open(lbh->lzh, name,
|
|
|
|
ZFS_TYPE_FILESYSTEM)) == NULL)
|
|
|
|
return (BE_ERR_ZFSOPEN);
|
|
|
|
|
2018-08-07 04:40:00 +02:00
|
|
|
ret = snapshot_proplist_update(ds_hdl, &data);
|
2018-08-05 21:38:56 +02:00
|
|
|
zfs_close(ds_hdl);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:17:40 +02:00
|
|
|
/*
|
|
|
|
* Internal callback function used by zfs_iter_filesystems. For each dataset in
|
|
|
|
* the bootenv root, populate an nvlist_t of its relevant properties.
|
|
|
|
*/
|
2018-07-26 05:13:07 +02:00
|
|
|
int
|
2018-07-24 15:17:40 +02:00
|
|
|
prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data_p)
|
|
|
|
{
|
2018-07-25 17:45:42 +02:00
|
|
|
char buf[512], *mountpoint;
|
2018-07-24 15:17:40 +02:00
|
|
|
prop_data_t *data;
|
2018-07-25 05:50:01 +02:00
|
|
|
libbe_handle_t *lbh;
|
|
|
|
nvlist_t *props;
|
|
|
|
const char *dataset, *name;
|
2018-07-25 17:14:35 +02:00
|
|
|
boolean_t mounted;
|
2018-07-24 15:17:40 +02:00
|
|
|
|
2018-07-25 05:50:01 +02:00
|
|
|
/*
|
|
|
|
* XXX TODO:
|
|
|
|
* some system for defining constants for the nvlist keys
|
|
|
|
* error checking
|
|
|
|
*/
|
2018-07-24 15:17:40 +02:00
|
|
|
data = (prop_data_t *)data_p;
|
2018-07-25 05:50:01 +02:00
|
|
|
lbh = data->lbh;
|
2018-07-24 15:17:40 +02:00
|
|
|
|
2018-08-03 04:04:57 +02:00
|
|
|
if (data->single_object)
|
|
|
|
props = data->list;
|
|
|
|
else
|
|
|
|
nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
|
2018-07-24 15:17:40 +02:00
|
|
|
|
2018-07-25 05:50:01 +02:00
|
|
|
dataset = zfs_get_name(zfs_hdl);
|
2018-07-24 15:17:40 +02:00
|
|
|
nvlist_add_string(props, "dataset", dataset);
|
|
|
|
|
2018-07-25 05:50:01 +02:00
|
|
|
name = strrchr(dataset, '/') + 1;
|
2018-07-24 15:17:40 +02:00
|
|
|
nvlist_add_string(props, "name", name);
|
|
|
|
|
2018-07-25 17:45:42 +02:00
|
|
|
mounted = zfs_is_mounted(zfs_hdl, &mountpoint);
|
2018-07-24 15:17:40 +02:00
|
|
|
|
2018-07-25 17:45:42 +02:00
|
|
|
if (mounted)
|
2018-08-07 05:07:54 +02:00
|
|
|
nvlist_add_string(props, "mounted", mountpoint);
|
|
|
|
|
|
|
|
if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, 512,
|
|
|
|
NULL, NULL, 0, 1) == 0)
|
|
|
|
nvlist_add_string(props, "mountpoint", buf);
|
2018-07-24 15:17:40 +02:00
|
|
|
|
|
|
|
if (zfs_prop_get(zfs_hdl, ZFS_PROP_ORIGIN, buf, 512,
|
2018-08-03 03:43:51 +02:00
|
|
|
NULL, NULL, 0, 1) == 0)
|
2018-07-24 15:17:40 +02:00
|
|
|
nvlist_add_string(props, "origin", buf);
|
|
|
|
|
|
|
|
if (zfs_prop_get(zfs_hdl, ZFS_PROP_CREATION, buf, 512,
|
2018-08-03 03:43:51 +02:00
|
|
|
NULL, NULL, 0, 1) == 0)
|
2018-07-24 15:17:40 +02:00
|
|
|
nvlist_add_string(props, "creation", buf);
|
|
|
|
|
|
|
|
nvlist_add_boolean_value(props, "active",
|
|
|
|
(strcmp(be_active_path(lbh), dataset) == 0));
|
|
|
|
|
|
|
|
if (zfs_prop_get(zfs_hdl, ZFS_PROP_USED, buf, 512,
|
2018-08-03 03:43:51 +02:00
|
|
|
NULL, NULL, 0, 1) == 0)
|
2018-07-24 15:17:40 +02:00
|
|
|
nvlist_add_string(props, "used", buf);
|
|
|
|
|
|
|
|
if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDDS, buf, 512,
|
2018-08-03 03:43:51 +02:00
|
|
|
NULL, NULL, 0, 1) == 0)
|
2018-07-24 15:17:40 +02:00
|
|
|
nvlist_add_string(props, "usedds", buf);
|
|
|
|
|
|
|
|
if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDSNAP, buf, 512,
|
2018-08-03 03:43:51 +02:00
|
|
|
NULL, NULL, 0, 1) == 0)
|
2018-07-24 15:17:40 +02:00
|
|
|
nvlist_add_string(props, "usedsnap", buf);
|
|
|
|
|
|
|
|
if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDREFRESERV, buf, 512,
|
2018-08-03 03:43:51 +02:00
|
|
|
NULL, NULL, 0, 1) == 0)
|
2018-07-24 15:17:40 +02:00
|
|
|
nvlist_add_string(props, "usedrefreserv", buf);
|
|
|
|
|
|
|
|
if (zfs_prop_get(zfs_hdl, ZFS_PROP_REFERENCED, buf, 512,
|
2018-08-03 03:43:51 +02:00
|
|
|
NULL, NULL, 0, 1) == 0)
|
2018-07-24 15:17:40 +02:00
|
|
|
nvlist_add_string(props, "referenced", buf);
|
|
|
|
|
2018-07-25 16:45:00 +02:00
|
|
|
nvlist_add_boolean_value(props, "nextboot",
|
|
|
|
(strcmp(be_nextboot_path(lbh), dataset) == 0));
|
2018-07-24 15:17:40 +02:00
|
|
|
|
2018-08-03 04:04:57 +02:00
|
|
|
if (!data->single_object)
|
|
|
|
nvlist_add_nvlist(data->list, name, props);
|
2018-07-24 15:17:40 +02:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Updates the properties of each bootenv in the libbe handle
|
2018-07-25 05:50:01 +02:00
|
|
|
* XXX TODO: ensure that this is always consistent (run after adds, deletes,
|
2018-07-24 15:17:40 +02:00
|
|
|
* renames,etc
|
|
|
|
*/
|
2018-07-26 05:13:07 +02:00
|
|
|
int
|
2018-08-07 04:40:00 +02:00
|
|
|
be_proplist_update(prop_data_t *data)
|
2018-07-24 15:17:40 +02:00
|
|
|
{
|
|
|
|
zfs_handle_t *root_hdl;
|
|
|
|
|
2018-07-25 05:50:01 +02:00
|
|
|
if ((root_hdl = zfs_open(data->lbh->lzh, data->lbh->root,
|
|
|
|
ZFS_TYPE_FILESYSTEM)) == NULL)
|
2018-07-24 15:17:40 +02:00
|
|
|
return (BE_ERR_ZFSOPEN);
|
|
|
|
|
2018-07-25 05:50:01 +02:00
|
|
|
/* XXX TODO: some error checking here */
|
2018-07-24 15:17:40 +02:00
|
|
|
zfs_iter_filesystems(root_hdl, prop_list_builder_cb, data);
|
|
|
|
|
|
|
|
zfs_close(root_hdl);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2018-08-05 21:38:56 +02:00
|
|
|
static int
|
2018-08-07 04:40:00 +02:00
|
|
|
snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data)
|
2018-08-05 21:38:56 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
return (zfs_iter_snapshots_sorted(hdl, prop_list_builder_cb, data));
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:17:40 +02:00
|
|
|
|
2018-07-25 18:00:48 +02:00
|
|
|
int
|
|
|
|
be_prop_list_alloc(nvlist_t **be_list)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (nvlist_alloc(be_list, NV_UNIQUE_NAME, KM_SLEEP));
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:17:40 +02:00
|
|
|
/*
|
|
|
|
* frees property list and its children
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
be_prop_list_free(nvlist_t *be_list)
|
|
|
|
{
|
|
|
|
nvlist_t *prop_list;
|
2018-07-25 05:50:01 +02:00
|
|
|
nvpair_t *be_pair;
|
2018-07-24 15:17:40 +02:00
|
|
|
|
2018-07-25 05:50:01 +02:00
|
|
|
be_pair = nvlist_next_nvpair(be_list, NULL);
|
|
|
|
if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
|
2018-07-24 15:17:40 +02:00
|
|
|
nvlist_free(prop_list);
|
|
|
|
|
|
|
|
while ((be_pair = nvlist_next_nvpair(be_list, be_pair)) != NULL) {
|
2018-07-25 05:50:01 +02:00
|
|
|
if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
|
2018-07-24 15:17:40 +02:00
|
|
|
nvlist_free(prop_list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Usage
|
|
|
|
*/
|
2018-09-01 04:22:26 +02:00
|
|
|
int
|
2018-07-24 15:17:40 +02:00
|
|
|
be_exists(libbe_handle_t *lbh, char *be)
|
|
|
|
{
|
|
|
|
char buf[BE_MAXPATHLEN];
|
|
|
|
|
|
|
|
be_root_concat(lbh, be, buf);
|
|
|
|
|
2018-08-07 05:07:54 +02:00
|
|
|
if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_DATASET))
|
2018-09-01 04:22:26 +02:00
|
|
|
return (BE_ERR_NOENT);
|
2018-08-07 05:07:54 +02:00
|
|
|
|
libbe(3): rewrite init to support chroot usage
libbe(3) currently uses zfs_be_root and locates which of its children is
currently mounted at "/". This is reasonable, but not correct in the case of
a chroot, for two reasons:
- chroot root may be of a different zpool than zfs_be_root
- chroot root will not show up as mounted at "/"
Fix both of these by rewriting libbe_init to work from the rootfs down.
zfs_path_to_zhandle on / will resolve to the dataset mounted at the new
root, rather than the real root. From there, we can derive the BE root/pool
and grab the bootfs off of the new pool. This does no harm in the average
case, and opens up bectl to operating on different pools for scenarios where
one may be, for instance, updating a pool that generally gets re-rooted into
from a separate UFS root or zfs bootpool.
While here, I've also:
- Eliminated the check for /boot and / to be on the same partition. This
leaves one open to a setup where /boot (and consequently, kernel/modules)
are not included in the boot environment. This may very well be an
intentional setup done by someone that knows what they're doing, we should
not kill BE usage because of it.
- Eliminated the validation bits of BEs and snapshots that enforced
'mountpoint' to be "/" -- this broke when trying to operate on an imported
pool with an altroot, but we need not be this picky.
Reported by: philip
Reviewed by: philip, allanjude (previous version)
Tested by: philip
MFC after: 3 days
Differential Revision: https://reviews.freebsd.org/D18012
2018-11-17 20:15:29 +01:00
|
|
|
return (BE_ERR_SUCCESS);
|
2018-07-24 15:17:40 +02:00
|
|
|
}
|