Date: Mon, 26 Jun 2017 09:09:51 GMT From: kneitinger@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r323964 - in soc2017/kneitinger/libbe-head: lib/libbe sbin/be Message-ID: <201706260909.v5Q99p2c034421@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: kneitinger Date: Mon Jun 26 09:09:50 2017 New Revision: 323964 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=323964 Log: Initial libbe commit Added: soc2017/kneitinger/libbe-head/lib/libbe/be_error.c Modified: soc2017/kneitinger/libbe-head/lib/libbe/Makefile soc2017/kneitinger/libbe-head/lib/libbe/be.c soc2017/kneitinger/libbe-head/lib/libbe/be.h soc2017/kneitinger/libbe-head/sbin/be/Makefile soc2017/kneitinger/libbe-head/sbin/be/be.c Modified: soc2017/kneitinger/libbe-head/lib/libbe/Makefile ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/Makefile Mon Jun 26 05:56:49 2017 (r323963) +++ soc2017/kneitinger/libbe-head/lib/libbe/Makefile Mon Jun 26 09:09:50 2017 (r323964) @@ -28,7 +28,7 @@ LIB= be SHLIB_MAJOR= 1 SHLIB_MINOR= 0 -SRCS= be.c +SRCS= be.c be_error.c INCS= be.h MAN= libbe.3 @@ -46,6 +46,7 @@ CFLAGS+= -I${SRCTOP}/sys/cddl/contrib/opensolaris/uts/common/fs/zfs CFLAGS+= -I${SRCTOP}/sys/cddl/contrib/opensolaris/uts/common CFLAGS+= -I${SRCTOP}/cddl/contrib/opensolaris/head +LDFLAGS+= -v CFLAGS+= -DNEED_SOLARIS_BOOLEAN Modified: soc2017/kneitinger/libbe-head/lib/libbe/be.c ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/be.c Mon Jun 26 05:56:49 2017 (r323963) +++ soc2017/kneitinger/libbe-head/lib/libbe/be.c Mon Jun 26 09:09:50 2017 (r323964) @@ -26,4 +26,105 @@ * SUCH DAMAGE. */ +#include <errno.h> +#include <kenv.h> +#include <libgen.h> +#include <stdio.h> +#include <sys/types.h> + #include "be.h" + +/* + * Test iterator function to verify datasets were mounted correctly. Will be + * deleted shortly + */ +static int +zfs_printer(zfs_handle_t *zfs_hdl, void *data) +{ + fprintf(stdout, "Name: %s\n", zfs_get_name(zfs_hdl)); + return (0); +} + + +/* + * Initializes the libbe context to operate in the root boot environment + * dataset, for example, zroot/ROOT. + */ +libbe_handle_t * +libbe_init(void) +{ + libbe_handle_t *lbh; + char buf[MAX_PATHLEN]; + + + if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL) { + return (NULL); + } + + + if ((lbh->lzh = libzfs_init()) == NULL) { + free(lbh); + return (NULL); + } + + libzfs_print_on_error(lbh->lzh, 1); + + + /* Obtain path to active boot environment */ + if ((kenv(KENV_GET, "zfs_be_active", buf, MAX_PATHLEN)) == -1) { + libzfs_fini(lbh->lzh); + free(lbh); + return (NULL); + } + + /* Remove leading 'zfs:' if present, otherwise use value as-is */ + char *path = strrchr(buf, ':'); + path = path ? path + 1 : buf; + + if ((lbh->be_active = zfs_open(lbh->lzh, path, + ZFS_TYPE_DATASET)) == NULL) { + libzfs_fini(lbh->lzh); + free(lbh); + return (NULL); + } + + + /* Obtain path to boot environment root */ + if ((kenv(KENV_GET, "zfs_be_root", buf, MAX_PATHLEN)) == -1) { + zfs_close(lbh->be_active); + libzfs_fini(lbh->lzh); + free(lbh); + return (NULL); + } + + if ((lbh->be_root = + zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL) { + zfs_close(lbh->be_active); + libzfs_fini(lbh->lzh); + free(lbh); + return (NULL); + } + + + /* TODO: verify that /boot is mounted on the active be */ + + /* Quick test to print child datasets and therefore verify that be_root + * was opened correctly. Will soon be deleted. + */ + zfs_iter_filesystems(lbh->be_root, zfs_printer, NULL); + + return (lbh); +} + + +/* + * Free memory allocated by libbe_init() + */ +void +libbe_close(libbe_handle_t *lbh) +{ + zfs_close(lbh->be_active); + zfs_close(lbh->be_root); + libzfs_fini(lbh->lzh); + free(lbh); +} Modified: soc2017/kneitinger/libbe-head/lib/libbe/be.h ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/be.h Mon Jun 26 05:56:49 2017 (r323963) +++ soc2017/kneitinger/libbe-head/lib/libbe/be.h Mon Jun 26 09:09:50 2017 (r323964) @@ -26,4 +26,39 @@ * SUCH DAMAGE. */ +#ifndef _LIBBE_H +#define _LIBBE_H + #include <libzfs.h> + +#define MAX_PATHLEN 512 + +typedef enum be_error { + BE_ERR_SUCCESS = 0, /* No error */ + BE_ERR_INVALIDNAME, /* invalid boot env name */ + BE_ERR_EXISTS, /* boot env name already taken */ + BE_ERR_PERMS, /* insufficient permissions */ + BE_ERR_UNKNOWN, /* unknown error */ +} be_error_t; + +/* + * TODO: this should be moved to a different file so as to hide the internals + * from the interface + */ +typedef struct libbe_handle { + libzfs_handle_t *lzh; + zfs_handle_t *be_root; + zfs_handle_t *be_active; + be_error_t error; +} libbe_handle_t; + +/* Library handling functions */ +libbe_handle_t *libbe_init(void); +void libbe_close(libbe_handle_t *); + + +/* Error related functions */ +int libbe_errno(libbe_handle_t *); +const char *libbe_error_description(libbe_handle_t *); + +#endif /* _LIBBE_H */ Added: soc2017/kneitinger/libbe-head/lib/libbe/be_error.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2017/kneitinger/libbe-head/lib/libbe/be_error.c Mon Jun 26 09:09:50 2017 (r323964) @@ -0,0 +1,62 @@ +/* + * be_error.c + * + * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in> + * 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. + */ + +#include "be.h" + + +/* + * Usage + */ +int +libbe_errno(libbe_handle_t *lbh) +{ + return (lbh->error); +} + + +const char * +libbe_error_description(libbe_handle_t *lbh) +{ + switch (lbh->error) { + case BE_ERR_INVALIDNAME: + return ("invalid boot env name"); + + case BE_ERR_EXISTS: + return ("boot env name already taken"); + + case BE_ERR_PERMS: + return ("insufficient permissions"); + + case BE_ERR_UNKNOWN: + return ("unknown error"); + + default: + assert(lbh->error == BE_ERR_SUCCESS); + return ("no error"); + } +} Modified: soc2017/kneitinger/libbe-head/sbin/be/Makefile ============================================================================== --- soc2017/kneitinger/libbe-head/sbin/be/Makefile Mon Jun 26 05:56:49 2017 (r323963) +++ soc2017/kneitinger/libbe-head/sbin/be/Makefile Mon Jun 26 09:09:50 2017 (r323964) @@ -6,6 +6,8 @@ LIBADD+= be +CFLAGS+= -I${SRCTOP}/lib/libbe + CFLAGS+= -I${SRCTOP}/cddl/compat/opensolaris/include CFLAGS+= -I${SRCTOP}/cddl/compat/opensolaris/lib/libumem CFLAGS+= -I${SRCTOP}/cddl/contrib/opensolaris/lib/libzfs/common Modified: soc2017/kneitinger/libbe-head/sbin/be/be.c ============================================================================== --- soc2017/kneitinger/libbe-head/sbin/be/be.c Mon Jun 26 05:56:49 2017 (r323963) +++ soc2017/kneitinger/libbe-head/sbin/be/be.c Mon Jun 26 09:09:50 2017 (r323964) @@ -45,6 +45,7 @@ static int be_unjail(int argc, char *argv[]); static int be_unmount(int argc, char *argv[]); +libbe_handle_t *libbe; static int usage(bool explicit) @@ -369,6 +370,7 @@ { int opt; char *cmd, *target; + bool force; /* Store alias used */ cmd = argv[0]; @@ -472,5 +474,14 @@ exit(usage(false)); } - return (command_map[command_index].fn(argc-1, argv+1)); + libbe = libbe_init(); + + int rc = command_map[command_index].fn(argc-1, argv+1); + + fprintf(stdout, "libbe error description: %s\n", + libbe_error_description(libbe)); + + libbe_close(libbe); + + return (rc); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201706260909.v5Q99p2c034421>