1b179da01SKyle Evans /*- 2b179da01SKyle Evans * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 328f16a0fSKyle Evans * 428f16a0fSKyle Evans * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in> 528f16a0fSKyle Evans * All rights reserved. 628f16a0fSKyle Evans * 728f16a0fSKyle Evans * Redistribution and use in source and binary forms, with or without 828f16a0fSKyle Evans * modification, are permitted provided that the following conditions 928f16a0fSKyle Evans * are met: 1028f16a0fSKyle Evans * 1. Redistributions of source code must retain the above copyright 1128f16a0fSKyle Evans * notice, this list of conditions and the following disclaimer. 1228f16a0fSKyle Evans * 2. Redistributions in binary form must reproduce the above copyright 1328f16a0fSKyle Evans * notice, this list of conditions and the following disclaimer in the 1428f16a0fSKyle Evans * documentation and/or other materials provided with the distribution. 1528f16a0fSKyle Evans * 1628f16a0fSKyle Evans * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1728f16a0fSKyle Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1828f16a0fSKyle Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1928f16a0fSKyle Evans * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2028f16a0fSKyle Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2128f16a0fSKyle Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2228f16a0fSKyle Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2328f16a0fSKyle Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2428f16a0fSKyle Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2528f16a0fSKyle Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2628f16a0fSKyle Evans * SUCH DAMAGE. 27b6e7c421SKyle Evans * 28b6e7c421SKyle Evans * $FreeBSD$ 2928f16a0fSKyle Evans */ 3028f16a0fSKyle Evans 3128f16a0fSKyle Evans #ifndef _LIBBE_H 3228f16a0fSKyle Evans #define _LIBBE_H 3328f16a0fSKyle Evans 343682d5e9SKyle Evans #include <libnvpair.h> 3528f16a0fSKyle Evans #include <stdbool.h> 3628f16a0fSKyle Evans 3728f16a0fSKyle Evans #define BE_MAXPATHLEN 512 3828f16a0fSKyle Evans 3928f16a0fSKyle Evans typedef struct libbe_handle libbe_handle_t; 4028f16a0fSKyle Evans 4128f16a0fSKyle Evans typedef enum be_error { 4228f16a0fSKyle Evans BE_ERR_SUCCESS = 0, /* No error */ 4328f16a0fSKyle Evans BE_ERR_INVALIDNAME, /* invalid boot env name */ 4428f16a0fSKyle Evans BE_ERR_EXISTS, /* boot env name already taken */ 4528f16a0fSKyle Evans BE_ERR_NOENT, /* boot env doesn't exist */ 4628f16a0fSKyle Evans BE_ERR_PERMS, /* insufficient permissions */ 4728f16a0fSKyle Evans BE_ERR_DESTROYACT, /* cannot destroy active boot env */ 4828f16a0fSKyle Evans BE_ERR_DESTROYMNT, /* destroying a mounted be requires force */ 49f1ca70d3SKyle Evans BE_ERR_BADPATH, /* path not suitable for operation */ 50f1ca70d3SKyle Evans BE_ERR_PATHBUSY, /* requested path is busy */ 5128f16a0fSKyle Evans BE_ERR_PATHLEN, /* provided name exceeds maximum length limit */ 5228f16a0fSKyle Evans BE_ERR_INVORIGIN, /* snapshot origin's mountpoint is not '/' */ 5328f16a0fSKyle Evans BE_ERR_NOORIGIN, /* could not open snapshot's origin */ 5428f16a0fSKyle Evans BE_ERR_MOUNTED, /* boot environment is already mounted */ 5528f16a0fSKyle Evans BE_ERR_NOMOUNT, /* boot environment is not mounted */ 5628f16a0fSKyle Evans BE_ERR_ZFSOPEN, /* calling zfs_open() failed */ 5728f16a0fSKyle Evans BE_ERR_ZFSCLONE, /* error when calling zfs_clone to create be */ 58f1ca70d3SKyle Evans BE_ERR_IO, /* error when doing some I/O operation */ 592989df09SKyle Evans BE_ERR_NOPOOL, /* operation not supported on this pool */ 6028f16a0fSKyle Evans BE_ERR_UNKNOWN, /* unknown error */ 6128f16a0fSKyle Evans } be_error_t; 6228f16a0fSKyle Evans 6328f16a0fSKyle Evans 6428f16a0fSKyle Evans /* Library handling functions: be.c */ 6528f16a0fSKyle Evans libbe_handle_t *libbe_init(void); 6628f16a0fSKyle Evans void libbe_close(libbe_handle_t *); 6728f16a0fSKyle Evans 6828f16a0fSKyle Evans /* Bootenv information functions: be_info.c */ 6928f16a0fSKyle Evans const char *be_active_name(libbe_handle_t *); 7028f16a0fSKyle Evans const char *be_active_path(libbe_handle_t *); 71ff8676ccSKyle Evans const char *be_nextboot_name(libbe_handle_t *); 72ff8676ccSKyle Evans const char *be_nextboot_path(libbe_handle_t *); 7328f16a0fSKyle Evans const char *be_root_path(libbe_handle_t *); 7428f16a0fSKyle Evans 753682d5e9SKyle Evans int be_get_bootenv_props(libbe_handle_t *, nvlist_t *); 769b1662e6SKyle Evans int be_get_dataset_props(libbe_handle_t *, const char *, nvlist_t *); 7796c5db58SKyle Evans int be_get_dataset_snapshots(libbe_handle_t *, const char *, nvlist_t *); 78734e362fSKyle Evans int be_prop_list_alloc(nvlist_t **be_list); 79b29bf2f8SKyle Evans void be_prop_list_free(nvlist_t *be_list); 8028f16a0fSKyle Evans 81*73c3d608SKyle Evans int be_activate(libbe_handle_t *, const char *, bool); 8228f16a0fSKyle Evans 8328f16a0fSKyle Evans /* Bootenv creation functions */ 84*73c3d608SKyle Evans int be_create(libbe_handle_t *, const char *); 85b29bf2f8SKyle Evans int be_create_from_existing(libbe_handle_t *, const char *, const char *); 86b29bf2f8SKyle Evans int be_create_from_existing_snap(libbe_handle_t *, const char *, const char *); 87b29bf2f8SKyle Evans int be_snapshot(libbe_handle_t *, const char *, const char *, bool, char *); 8828f16a0fSKyle Evans 8928f16a0fSKyle Evans /* Bootenv manipulation functions */ 90*73c3d608SKyle Evans int be_rename(libbe_handle_t *, const char *, const char *); 9128f16a0fSKyle Evans 9228f16a0fSKyle Evans /* Bootenv removal functions */ 9328f16a0fSKyle Evans 9428f16a0fSKyle Evans typedef enum { 9528f16a0fSKyle Evans BE_DESTROY_FORCE = 1 << 0, 9628f16a0fSKyle Evans } be_destroy_opt_t; 9728f16a0fSKyle Evans 98*73c3d608SKyle Evans int be_destroy(libbe_handle_t *, const char *, int); 9928f16a0fSKyle Evans 10028f16a0fSKyle Evans /* Bootenv mounting functions: be_access.c */ 10128f16a0fSKyle Evans 10228f16a0fSKyle Evans typedef enum { 10328f16a0fSKyle Evans BE_MNT_FORCE = 1 << 0, 10428f16a0fSKyle Evans BE_MNT_DEEP = 1 << 1, 10528f16a0fSKyle Evans } be_mount_opt_t; 10628f16a0fSKyle Evans 10728f16a0fSKyle Evans int be_mount(libbe_handle_t *, char *, char *, int, char *); 10828f16a0fSKyle Evans int be_unmount(libbe_handle_t *, char *, int); 109843e39ceSKyle Evans int be_mounted_at(libbe_handle_t *, const char *path, nvlist_t *); 11028f16a0fSKyle Evans 11128f16a0fSKyle Evans /* Error related functions: be_error.c */ 11228f16a0fSKyle Evans int libbe_errno(libbe_handle_t *); 11328f16a0fSKyle Evans const char *libbe_error_description(libbe_handle_t *); 11428f16a0fSKyle Evans void libbe_print_on_error(libbe_handle_t *, bool); 11528f16a0fSKyle Evans 11628f16a0fSKyle Evans /* Utility Functions */ 117b29bf2f8SKyle Evans int be_root_concat(libbe_handle_t *, const char *, char *); 118b29bf2f8SKyle Evans int be_validate_name(libbe_handle_t * __unused, const char *); 119b29bf2f8SKyle Evans int be_validate_snap(libbe_handle_t *, const char *); 12028f16a0fSKyle Evans bool be_exists(libbe_handle_t *, char *); 12128f16a0fSKyle Evans 122*73c3d608SKyle Evans int be_export(libbe_handle_t *, const char *, int fd); 123*73c3d608SKyle Evans int be_import(libbe_handle_t *, const char *, int fd); 12428f16a0fSKyle Evans 125*73c3d608SKyle Evans int be_add_child(libbe_handle_t *, const char *, bool); 1269b1662e6SKyle Evans void be_nicenum(uint64_t num, char *buf, size_t buflen); 12728f16a0fSKyle Evans 12828f16a0fSKyle Evans #endif /* _LIBBE_H */ 129