Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Convert zfssnap to new plugin interface
Baptiste Daroussin committed 13 years ago
commit 92c2100d4066209cd3180fe99bfd91474ae6d26d
parent 25ee889
10 files changed +245 -288
modified plugins/Makefile
@@ -1,4 +1,5 @@
SUBDIR+=	command-mystats
SUBDIR+=	stats
+
SUBDIR+=	zfssnap

.include <bsd.subdir.mk>
deleted plugins/pkg-plugin-zfssnap/Makefile
@@ -1,18 +0,0 @@
-
.include <bsd.own.mk>
-

-
LIB=		pkg-plugin-zfssnap
-
INCS=		zfssnap.h
-
#WARNS=		6
-
PREFIX?=	/usr/local
-
LIBDIR=		${PREFIX}/lib
-
INCLUDEDIR=	${PREFIX}/include
-
SHLIB_MAJOR=	0
-

-
SRCS=		zfssnap.c
-

-
CFLAGS+=	-std=c99 -fPIC -shared
-
CFLAGS+=	-I${INCLUDEDIR}
-

-
DEBUG_FLAGS+=  -g -O0
-

-
.include <bsd.lib.mk>
deleted plugins/pkg-plugin-zfssnap/README.md
@@ -1,45 +0,0 @@
-
## General Information
-

-
The *pkg-plugin-zfssnap* plugin is a plugin meant to be used for creating snapshots
-
on a system with ZFS prior any install/deinstall actions are taken.
-

-
*pkg-plugin-zfssnap* is useful in a way that if something breaks in case your installation or
-
deinstallation of package(s) fails you will be able to rollback to a previously known and working
-
state of your system.
-

-
## How to build the plugin?
-

-
In order to build the plugin enter into the plugin's directory and run make(1), e.g.:
-

-
	$ cd /path/to/pkg-plugins-zfssnap
-
	$ make
-
	
-
Once the plugin is built you can install it using the following command:
-

-
	$ make install 
-
	
-
The plugin will be installed as a shared library in ${PREFIX}/lib/libpkg-plugin-zfssnap.so
-

-
## Configuring the plugin
-

-
In order to configure the plugin simply copy the *zfssnap.conf* file to the pkgng plugins directory,
-
which by default is set to */usr/local/etc/pkg/plugins*, unless you've specified it elsewhere by 
-
using the *PKG\_PLUGINS\_DIR* option.
-

-
	$ cp /path/to/pkg-plugins-zfssnap/zfssnap.conf /usr/local/etc/pkg/plugins/
-
	
-
Next, open */usr/local/etc/pkg/plugins/zfssnap.conf* and configure any ZFS related options.
-
	
-
## Testing the plugin
-

-
To test the plugin, first check that it is recognized and
-
loaded by pkgng by executing the `pkg plugins` command:
-

-
	$ pkg plugins
-
	NAME       DESC                                VERSION    LOADED    
-
	zfssnap    ZFS snapshot plugin for pkgng       1.0        YES       
-

-
If the plugin shows up correctly then you are good to go! :)
-

-
Once you start installing/deinstall package(s) zfssnap will create a snapshot for you! 
-

deleted plugins/pkg-plugin-zfssnap/zfssnap.c
@@ -1,165 +0,0 @@
-
/*
-
 * Copyright (c) 2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
-
 * 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
-
 *    in this position and unchanged.
-
 * 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 AUTHOR(S) ``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 AUTHOR(S) 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 <sys/types.h>
-
#include <sys/param.h>
-

-
#include <assert.h>
-
#include <fcntl.h>
-
#include <time.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <stdbool.h>
-
#include <string.h>
-
#include <libutil.h>
-
#include <unistd.h>
-

-
#include <pkg.h>
-

-
#include "zfssnap.h"
-

-
#define PLUGIN_NAME "zfssnap"
-
#define PLUGIN_CONF "/usr/local/etc/pkg/plugins/zfssnap.conf"
-

-
static struct _zfssnap_config {
-
        const char *key;
-
        const char *val;
-
} c[] = {
-
        { "zfs_fs", NULL },
-
	{ "zfs_prefix", NULL },
-
        { "zfs_args", NULL },
-
        { NULL, NULL }
-
};
-

-
static int plugins_zfssnap_load_conf(const char *file);
-
static const char *plugins_zfssnap_get_conf(const char *key);
-
static int plugins_zfssnap_fd = -1;
-
static properties plugins_zfssnap_p = NULL;
-

-
int
-
pkg_plugins_init_zfssnap(void)
-
{
-
	if (plugins_zfssnap_load_conf(PLUGIN_CONF) != EPKG_OK) {
-
		fprintf(stderr, ">>> Cannot parse configuration file %s\n", PLUGIN_CONF);
-
		return (EPKG_FATAL);
-
	}
-
	
-
	if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGINS_HOOK_PRE_INSTALL, &plugins_zfssnap_callback) != EPKG_OK) {
-
		fprintf(stderr, ">>> Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
-
		return (EPKG_FATAL);
-
	}
-
	
-
	if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGINS_HOOK_PRE_DEINSTALL, &plugins_zfssnap_callback) != EPKG_OK) {
-
		fprintf(stderr, ">>> Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
-
		return (EPKG_FATAL);
-
	}
-
	
-
	return (EPKG_OK);
-
}
-

-
int
-
pkg_plugins_shutdown_zfssnap(void)
-
{
-
	properties_free(plugins_zfssnap_p);
-
	close(plugins_zfssnap_fd);
-
	
-
	return (EPKG_OK);
-
}
-

-
static int
-
plugins_zfssnap_load_conf(const char *file)
-
{
-
        int i;
-
	bool wrong_conf = false;
-

-
	assert(file != NULL);
-

-
	if ((plugins_zfssnap_fd = open(file, O_RDONLY)) < 0) {
-
                fprintf(stderr, ">>> Cannot open configuration file %s", file);
-
                return (EPKG_FATAL);
-
        }
-

-
	plugins_zfssnap_p = properties_read(plugins_zfssnap_fd);
-
	
-
        for (i = 0; c[i].key != NULL; i++)
-
		c[i].val = property_find(plugins_zfssnap_p, c[i].key);
-

-
	return (EPKG_OK);
-
}
-

-
static const char *
-
plugins_zfssnap_get_conf(const char *key)
-
{
-
	unsigned int i;
-
	
-
	assert (key != NULL);
-

-
        for (i = 0; c[i].key != NULL; i++)
-
		if (strcmp(c[i].key, key) == 0)
-
			return (c[i].val);
-

-
	return (NULL);
-
}
-

-
int
-
plugins_zfssnap_callback(void *data, struct pkgdb *db)
-
{
-
	char cmd_buf[MAXPATHLEN + 1];
-
	struct tm *tm = NULL;
-
	const char *zfs_fs = NULL;
-
	const char *zfs_args = NULL;
-
	const char *zfs_prefix = NULL;
-
	time_t t = 0;
-

-
	t = time(NULL);
-
	tm = localtime(&t);
-
	
-
	/* we don't care about data and db, so nothing to assert() here */
-
	/* assert(db != NULL); */ 
-
	/* assert(data != NULL); */
-

-
	zfs_fs = plugins_zfssnap_get_conf("zfs_fs");
-
	zfs_args = plugins_zfssnap_get_conf("zfs_args");
-
	zfs_prefix = plugins_zfssnap_get_conf("zfs_prefix");
-

-
	if ((zfs_fs == NULL) || (zfs_prefix == NULL)) {
-
		fprintf(stderr, ">>> Configuration options missing, plugin '%s' will not be loaded\n",
-
			       PLUGIN_NAME);
-
		return (EPKG_FATAL);
-
	}
-

-
	snprintf(cmd_buf, sizeof(cmd_buf), "%s %s %s@%s-%d-%d-%d_%d.%d.%d",
-
		 "/sbin/zfs snapshot", zfs_args,
-
		 zfs_fs, zfs_prefix,
-
		 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
-
		 tm->tm_hour, tm->tm_min, tm->tm_sec);
-

-
	printf(">>> Creating ZFS snapshot\n");
-
	system(cmd_buf);
-
	
-
	return (EPKG_OK);
-
}
-

deleted plugins/pkg-plugin-zfssnap/zfssnap.conf
@@ -1,23 +0,0 @@
-
# Configuration file for pkg-plugin-template
-

-
enabled=YES
-
name=zfssnap
-
description=ZFS snapshot plugin for pkgng
-
version=1.0
-
plugin=/usr/local/lib/libpkg-plugin-zfssnap.so
-

-
#
-
# zfssnap specific options follow below
-
#
-

-
# ZFS file system
-
zfs_fs=zroot
-

-
# ZFS args, '-r' for recursive snapshots creation
-
zfs_args=-r
-

-
# ZFS snapshot prefix name
-
zfs_prefix=zfssnap
-

-

-

deleted plugins/pkg-plugin-zfssnap/zfssnap.h
@@ -1,37 +0,0 @@
-
/*
-
 * Copyright (c) 2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
-
 * 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
-
 *    in this position and unchanged.
-
 * 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 AUTHOR(S) ``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 AUTHOR(S) 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.
-
 */
-

-
#ifndef _PKG_PLUGINS_ZFSSNAP_H
-
#define _PKG_PLUGINS_ZFSSNAP_H
-

-
/* callback functions */
-
int plugins_zfssnap_callback(void *data, struct pkgdb *db);
-

-
/* plugin init and shutdown functions */
-
int pkg_plugins_init_zfssnap(void);
-
int pkg_plugins_shutdown_zfssnap(void);
-

-
#endif /* !_PKG_PLUGINS_ZFSSNAP_H */
added plugins/zfssnap/Makefile
@@ -0,0 +1,16 @@
+
.include <bsd.own.mk>
+

+
PREFIX?=	/usr/local
+
LIBDIR=		${PREFIX}/lib/pkg/
+
SHLIB_DIR?=	${LIBDIR}/
+
SHLIB_NAME?=	${PLUGIN_NAME}.so
+

+
PLUGIN_NAME=	zfssnap
+
SRCS=		zfssnap.c
+

+
CFLAGS+=	-I${.CURDIR}/../../libpkg
+

+
beforeinstall:
+
	${INSTALL} -d ${LIBDIR}
+

+
.include <bsd.lib.mk>
added plugins/zfssnap/README.md
@@ -0,0 +1,45 @@
+
## General Information
+

+
The *pkg-plugin-zfssnap* plugin is a plugin meant to be used for creating snapshots
+
on a system with ZFS prior any install/deinstall actions are taken.
+

+
*pkg-plugin-zfssnap* is useful in a way that if something breaks in case your installation or
+
deinstallation of package(s) fails you will be able to rollback to a previously known and working
+
state of your system.
+

+
## How to build the plugin?
+

+
In order to build the plugin enter into the plugin's directory and run make(1), e.g.:
+

+
	$ cd /path/to/pkg-plugins-zfssnap
+
	$ make
+
	
+
Once the plugin is built you can install it using the following command:
+

+
	$ make install 
+
	
+
The plugin will be installed as a shared library in ${PREFIX}/lib/libpkg-plugin-zfssnap.so
+

+
## Configuring the plugin
+

+
In order to configure the plugin simply copy the *zfssnap.conf* file to the pkgng plugins directory,
+
which by default is set to */usr/local/etc/pkg/plugins*, unless you've specified it elsewhere by 
+
using the *PKG\_PLUGINS\_DIR* option.
+

+
	$ cp /path/to/pkg-plugins-zfssnap/zfssnap.conf /usr/local/etc/pkg/plugins/
+
	
+
Next, open */usr/local/etc/pkg/plugins/zfssnap.conf* and configure any ZFS related options.
+
	
+
## Testing the plugin
+

+
To test the plugin, first check that it is recognized and
+
loaded by pkgng by executing the `pkg plugins` command:
+

+
	$ pkg plugins
+
	NAME       DESC                                VERSION    LOADED    
+
	zfssnap    ZFS snapshot plugin for pkgng       1.0        YES       
+

+
If the plugin shows up correctly then you are good to go! :)
+

+
Once you start installing/deinstall package(s) zfssnap will create a snapshot for you! 
+

added plugins/zfssnap/zfssnap.c
@@ -0,0 +1,168 @@
+
/*
+
 * Copyright (c) 2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
+
 * 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
+
 *    in this position and unchanged.
+
 * 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 AUTHOR(S) ``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 AUTHOR(S) 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 <sys/types.h>
+
#include <sys/param.h>
+

+
#include <assert.h>
+
#include <fcntl.h>
+
#include <time.h>
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <stdbool.h>
+
#include <string.h>
+
#include <libutil.h>
+
#include <unistd.h>
+

+
#include <pkg.h>
+

+
#include "zfssnap.h"
+

+
#define PLUGIN_NAME "zfssnap"
+
#define PLUGIN_CONF "/usr/local/etc/pkg/plugins/zfssnap.conf"
+

+
static struct _zfssnap_config {
+
        const char *key;
+
        const char *val;
+
} c[] = {
+
        { "zfs_fs", NULL },
+
	{ "zfs_prefix", NULL },
+
        { "zfs_args", NULL },
+
        { NULL, NULL }
+
};
+

+
static int plugins_zfssnap_load_conf(const char *file);
+
static const char *plugins_zfssnap_get_conf(const char *key);
+
static int plugins_zfssnap_fd = -1;
+
static properties plugins_zfssnap_p = NULL;
+

+
int
+
init(struct pkg_plugin *p)
+
{
+
	pkg_plugin_set(p, PKG_PLUGIN_NAME, PLUGIN_NAME);
+
	pkg_plugin_set(p, PKG_PLUGIN_DESC, "ZFS snapshot plugin");
+
	pkg_plugin_set(p, PKG_PLUGIN_VERSION, "1.0.0");
+
	if (plugins_zfssnap_load_conf(PLUGIN_CONF) != EPKG_OK) {
+
		fprintf(stderr, ">>> Cannot parse configuration file %s\n", PLUGIN_CONF);
+
		return (EPKG_FATAL);
+
	}
+
	
+
	if (pkg_plugin_hook_register(p, PKG_PLUGIN_HOOK_PRE_INSTALL, &plugins_zfssnap_callback) != EPKG_OK) {
+
		fprintf(stderr, ">>> Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
+
		return (EPKG_FATAL);
+
	}
+
	
+
	if (pkg_plugin_hook_register(p, PKG_PLUGIN_HOOK_PRE_DEINSTALL, &plugins_zfssnap_callback) != EPKG_OK) {
+
		fprintf(stderr, ">>> Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
+
		return (EPKG_FATAL);
+
	}
+
	
+
	return (EPKG_OK);
+
}
+

+
int
+
shutdown(struct pkg_plugin *p __unused)
+
{
+
	properties_free(plugins_zfssnap_p);
+
	close(plugins_zfssnap_fd);
+
	
+
	return (EPKG_OK);
+
}
+

+
static int
+
plugins_zfssnap_load_conf(const char *file)
+
{
+
        int i;
+
	bool wrong_conf = false;
+

+
	assert(file != NULL);
+

+
	if ((plugins_zfssnap_fd = open(file, O_RDONLY)) < 0) {
+
                fprintf(stderr, ">>> Cannot open configuration file %s", file);
+
                return (EPKG_FATAL);
+
        }
+

+
	plugins_zfssnap_p = properties_read(plugins_zfssnap_fd);
+
	
+
        for (i = 0; c[i].key != NULL; i++)
+
		c[i].val = property_find(plugins_zfssnap_p, c[i].key);
+

+
	return (EPKG_OK);
+
}
+

+
static const char *
+
plugins_zfssnap_get_conf(const char *key)
+
{
+
	unsigned int i;
+
	
+
	assert (key != NULL);
+

+
        for (i = 0; c[i].key != NULL; i++)
+
		if (strcmp(c[i].key, key) == 0)
+
			return (c[i].val);
+

+
	return (NULL);
+
}
+

+
int
+
plugins_zfssnap_callback(void *data, struct pkgdb *db)
+
{
+
	char cmd_buf[MAXPATHLEN + 1];
+
	struct tm *tm = NULL;
+
	const char *zfs_fs = NULL;
+
	const char *zfs_args = NULL;
+
	const char *zfs_prefix = NULL;
+
	time_t t = 0;
+

+
	t = time(NULL);
+
	tm = localtime(&t);
+
	
+
	/* we don't care about data and db, so nothing to assert() here */
+
	/* assert(db != NULL); */ 
+
	/* assert(data != NULL); */
+

+
	zfs_fs = plugins_zfssnap_get_conf("zfs_fs");
+
	zfs_args = plugins_zfssnap_get_conf("zfs_args");
+
	zfs_prefix = plugins_zfssnap_get_conf("zfs_prefix");
+

+
	if ((zfs_fs == NULL) || (zfs_prefix == NULL)) {
+
		fprintf(stderr, ">>> Configuration options missing, plugin '%s' will not be loaded\n",
+
			       PLUGIN_NAME);
+
		return (EPKG_FATAL);
+
	}
+

+
	snprintf(cmd_buf, sizeof(cmd_buf), "%s %s %s@%s-%d-%d-%d_%d.%d.%d",
+
		 "/sbin/zfs snapshot", zfs_args,
+
		 zfs_fs, zfs_prefix,
+
		 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
+
		 tm->tm_hour, tm->tm_min, tm->tm_sec);
+

+
	printf(">>> Creating ZFS snapshot\n");
+
	system(cmd_buf);
+
	
+
	return (EPKG_OK);
+
}
+

added plugins/zfssnap/zfssnap.conf
@@ -0,0 +1,15 @@
+
#
+
# zfssnap specific options follow below
+
#
+

+
# ZFS file system
+
zfs_fs=zroot
+

+
# ZFS args, '-r' for recursive snapshots creation
+
zfs_args=-r
+

+
# ZFS snapshot prefix name
+
zfs_prefix=zfssnap
+

+

+