1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifndef _LIBBE_H 32 #define _LIBBE_H 33 34 #include <libnvpair.h> 35 #include <stdbool.h> 36 37 #define BE_MAXPATHLEN 512 38 39 typedef struct libbe_handle libbe_handle_t; 40 41 typedef enum be_error { 42 BE_ERR_SUCCESS = 0, /* No error */ 43 BE_ERR_INVALIDNAME, /* invalid boot env name */ 44 BE_ERR_EXISTS, /* boot env name already taken */ 45 BE_ERR_NOENT, /* boot env doesn't exist */ 46 BE_ERR_PERMS, /* insufficient permissions */ 47 BE_ERR_DESTROYACT, /* cannot destroy active boot env */ 48 BE_ERR_DESTROYMNT, /* destroying a mounted be requires force */ 49 BE_ERR_BADPATH, /* path not suitable for operation */ 50 BE_ERR_PATHBUSY, /* requested path is busy */ 51 BE_ERR_PATHLEN, /* provided name exceeds maximum length limit */ 52 BE_ERR_INVORIGIN, /* snapshot origin's mountpoint is not '/' */ 53 BE_ERR_NOORIGIN, /* could not open snapshot's origin */ 54 BE_ERR_MOUNTED, /* boot environment is already mounted */ 55 BE_ERR_NOMOUNT, /* boot environment is not mounted */ 56 BE_ERR_ZFSOPEN, /* calling zfs_open() failed */ 57 BE_ERR_ZFSCLONE, /* error when calling zfs_clone to create be */ 58 BE_ERR_IO, /* error when doing some I/O operation */ 59 BE_ERR_UNKNOWN, /* unknown error */ 60 } be_error_t; 61 62 63 /* Library handling functions: be.c */ 64 libbe_handle_t *libbe_init(void); 65 void libbe_close(libbe_handle_t *); 66 67 /* Bootenv information functions: be_info.c */ 68 const char *be_active_name(libbe_handle_t *); 69 const char *be_active_path(libbe_handle_t *); 70 const char *be_nextboot_name(libbe_handle_t *); 71 const char *be_nextboot_path(libbe_handle_t *); 72 const char *be_root_path(libbe_handle_t *); 73 74 int be_get_bootenv_props(libbe_handle_t *, nvlist_t *); 75 int be_get_dataset_props(libbe_handle_t *, const char *, nvlist_t *); 76 int be_get_dataset_snapshots(libbe_handle_t *, const char *, nvlist_t *); 77 int be_prop_list_alloc(nvlist_t **be_list); 78 void be_prop_list_free(nvlist_t *be_list); 79 80 int be_activate(libbe_handle_t *, char *, bool); 81 82 /* Bootenv creation functions */ 83 int be_create(libbe_handle_t *, char *); 84 int be_create_from_existing(libbe_handle_t *, const char *, const char *); 85 int be_create_from_existing_snap(libbe_handle_t *, const char *, const char *); 86 int be_snapshot(libbe_handle_t *, const char *, const char *, bool, char *); 87 88 /* Bootenv manipulation functions */ 89 int be_rename(libbe_handle_t *, char *, char *); 90 91 /* Bootenv removal functions */ 92 93 typedef enum { 94 BE_DESTROY_FORCE = 1 << 0, 95 } be_destroy_opt_t; 96 97 int be_destroy(libbe_handle_t *, char *, int); 98 99 /* Bootenv mounting functions: be_access.c */ 100 101 typedef enum { 102 BE_MNT_FORCE = 1 << 0, 103 BE_MNT_DEEP = 1 << 1, 104 } be_mount_opt_t; 105 106 int be_mount(libbe_handle_t *, char *, char *, int, char *); 107 int be_unmount(libbe_handle_t *, char *, int); 108 int be_mounted_at(libbe_handle_t *, const char *path, nvlist_t *); 109 110 /* Error related functions: be_error.c */ 111 int libbe_errno(libbe_handle_t *); 112 const char *libbe_error_description(libbe_handle_t *); 113 void libbe_print_on_error(libbe_handle_t *, bool); 114 115 /* Utility Functions */ 116 int be_root_concat(libbe_handle_t *, const char *, char *); 117 int be_validate_name(libbe_handle_t * __unused, const char *); 118 int be_validate_snap(libbe_handle_t *, const char *); 119 bool be_exists(libbe_handle_t *, char *); 120 121 int be_export(libbe_handle_t *, char *, int fd); 122 int be_import(libbe_handle_t *, char *, int fd); 123 124 int be_add_child(libbe_handle_t *, char *, bool); 125 void be_nicenum(uint64_t num, char *buf, size_t buflen); 126 127 #endif /* _LIBBE_H */ 128