xref: /freebsd/lib/libbe/be.h (revision 9b1662e67b54e3a4ae04050beacc38645a003a8c)
128f16a0fSKyle Evans /*
228f16a0fSKyle Evans  * be.h
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.
2728f16a0fSKyle Evans  */
2828f16a0fSKyle Evans 
2928f16a0fSKyle Evans #ifndef _LIBBE_H
3028f16a0fSKyle Evans #define _LIBBE_H
3128f16a0fSKyle Evans 
323682d5e9SKyle Evans #include <libnvpair.h>
3328f16a0fSKyle Evans #include <stdbool.h>
3428f16a0fSKyle Evans 
3528f16a0fSKyle Evans #define BE_MAXPATHLEN    512
3628f16a0fSKyle Evans 
3728f16a0fSKyle Evans typedef struct libbe_handle libbe_handle_t;
3828f16a0fSKyle Evans 
3928f16a0fSKyle Evans typedef enum be_error {
4028f16a0fSKyle Evans 	BE_ERR_SUCCESS = 0,     /* No error */
4128f16a0fSKyle Evans 	BE_ERR_INVALIDNAME,     /* invalid boot env name */
4228f16a0fSKyle Evans 	BE_ERR_EXISTS,          /* boot env name already taken */
4328f16a0fSKyle Evans 	BE_ERR_NOENT,           /* boot env doesn't exist */
4428f16a0fSKyle Evans 	BE_ERR_PERMS,           /* insufficient permissions */
4528f16a0fSKyle Evans 	BE_ERR_DESTROYACT,      /* cannot destroy active boot env */
4628f16a0fSKyle Evans 	BE_ERR_DESTROYMNT,      /* destroying a mounted be requires force */
4728f16a0fSKyle Evans 	BE_ERR_PATHLEN,         /* provided name exceeds maximum length limit */
4828f16a0fSKyle Evans 	BE_ERR_INVORIGIN,       /* snapshot origin's mountpoint is not '/' */
4928f16a0fSKyle Evans 	BE_ERR_NOORIGIN,        /* could not open snapshot's origin */
5028f16a0fSKyle Evans 	BE_ERR_MOUNTED,         /* boot environment is already mounted */
5128f16a0fSKyle Evans 	BE_ERR_NOMOUNT,         /* boot environment is not mounted */
5228f16a0fSKyle Evans 	BE_ERR_ZFSOPEN,         /* calling zfs_open() failed */
5328f16a0fSKyle Evans 	BE_ERR_ZFSCLONE,        /* error when calling zfs_clone to create be */
5428f16a0fSKyle Evans 	BE_ERR_UNKNOWN,         /* unknown error */
5528f16a0fSKyle Evans } be_error_t;
5628f16a0fSKyle Evans 
5728f16a0fSKyle Evans 
5828f16a0fSKyle Evans /* Library handling functions: be.c */
5928f16a0fSKyle Evans libbe_handle_t *libbe_init(void);
6028f16a0fSKyle Evans void libbe_close(libbe_handle_t *);
6128f16a0fSKyle Evans 
6228f16a0fSKyle Evans /* Bootenv information functions: be_info.c */
6328f16a0fSKyle Evans const char *be_active_name(libbe_handle_t *);
6428f16a0fSKyle Evans const char *be_active_path(libbe_handle_t *);
65ff8676ccSKyle Evans const char *be_nextboot_name(libbe_handle_t *);
66ff8676ccSKyle Evans const char *be_nextboot_path(libbe_handle_t *);
6728f16a0fSKyle Evans const char *be_root_path(libbe_handle_t *);
6828f16a0fSKyle Evans 
693682d5e9SKyle Evans int be_get_bootenv_props(libbe_handle_t *, nvlist_t *);
70*9b1662e6SKyle Evans int be_get_dataset_props(libbe_handle_t *, const char *, nvlist_t *);
71734e362fSKyle Evans int be_prop_list_alloc(nvlist_t **be_list);
72b29bf2f8SKyle Evans void be_prop_list_free(nvlist_t *be_list);
7328f16a0fSKyle Evans 
7428f16a0fSKyle Evans int be_activate(libbe_handle_t *, char *, bool);
7528f16a0fSKyle Evans 
7628f16a0fSKyle Evans /* Bootenv creation functions */
7728f16a0fSKyle Evans int be_create(libbe_handle_t *, char *);
78b29bf2f8SKyle Evans int be_create_from_existing(libbe_handle_t *, const char *, const char *);
79b29bf2f8SKyle Evans int be_create_from_existing_snap(libbe_handle_t *, const char *, const char *);
80b29bf2f8SKyle Evans int be_snapshot(libbe_handle_t *, const char *, const char *, bool, char *);
8128f16a0fSKyle Evans 
8228f16a0fSKyle Evans /* Bootenv manipulation functions */
8328f16a0fSKyle Evans int be_rename(libbe_handle_t *, char *, char *);
8428f16a0fSKyle Evans 
8528f16a0fSKyle Evans /* Bootenv removal functions */
8628f16a0fSKyle Evans 
8728f16a0fSKyle Evans typedef enum {
8828f16a0fSKyle Evans 	BE_DESTROY_FORCE = 1 << 0,
8928f16a0fSKyle Evans } be_destroy_opt_t;
9028f16a0fSKyle Evans 
9128f16a0fSKyle Evans int be_destroy(libbe_handle_t *, char *, int);
9228f16a0fSKyle Evans 
9328f16a0fSKyle Evans /* Bootenv mounting functions: be_access.c */
9428f16a0fSKyle Evans 
9528f16a0fSKyle Evans typedef enum {
9628f16a0fSKyle Evans 	BE_MNT_FORCE		= 1 << 0,
9728f16a0fSKyle Evans 		BE_MNT_DEEP	= 1 << 1,
9828f16a0fSKyle Evans } be_mount_opt_t;
9928f16a0fSKyle Evans 
10028f16a0fSKyle Evans int be_mount(libbe_handle_t *, char *, char *, int, char *);
10128f16a0fSKyle Evans int be_unmount(libbe_handle_t *, char *, int);
102843e39ceSKyle Evans int be_mounted_at(libbe_handle_t *, const char *path, nvlist_t *);
10328f16a0fSKyle Evans 
10428f16a0fSKyle Evans /* Error related functions: be_error.c */
10528f16a0fSKyle Evans int libbe_errno(libbe_handle_t *);
10628f16a0fSKyle Evans const char *libbe_error_description(libbe_handle_t *);
10728f16a0fSKyle Evans void libbe_print_on_error(libbe_handle_t *, bool);
10828f16a0fSKyle Evans 
10928f16a0fSKyle Evans /* Utility Functions */
110b29bf2f8SKyle Evans int be_root_concat(libbe_handle_t *, const char *, char *);
111b29bf2f8SKyle Evans int be_validate_name(libbe_handle_t * __unused, const char *);
112b29bf2f8SKyle Evans int be_validate_snap(libbe_handle_t *, const char *);
11328f16a0fSKyle Evans bool be_exists(libbe_handle_t *, char *);
11428f16a0fSKyle Evans 
11528f16a0fSKyle Evans int be_export(libbe_handle_t *, char *, int fd);
11628f16a0fSKyle Evans int be_import(libbe_handle_t *, char *, int fd);
11728f16a0fSKyle Evans 
11828f16a0fSKyle Evans int be_add_child(libbe_handle_t *, char *, bool);
119*9b1662e6SKyle Evans void be_nicenum(uint64_t num, char *buf, size_t buflen);
12028f16a0fSKyle Evans 
12128f16a0fSKyle Evans #endif  /* _LIBBE_H */
122