Permalink
Browse files

Add support for storagepool

  • Loading branch information...
1 parent ea29cdf commit c67329b0f1a29d1bf3564d8399a5cd4392951bac @msantos committed Aug 29, 2012
Showing with 443 additions and 31 deletions.
  1. +132 −0 bin/vert
  2. +1 −0 c_src/vert.c
  3. +32 −3 c_src/vert.h
  4. +30 −0 c_src/vert_connect.c
  5. +25 −0 c_src/vert_func.h
  6. +77 −0 c_src/vert_storagepool.c
  7. +3 −3 c_src/vert_util.c
  8. +8 −0 include/vert.hrl
  9. +135 −25 src/vert.erl
View
@@ -310,6 +310,117 @@ call(["nwfilter-undefine", Name | Arg]) ->
{ok, Filter} = nwfilter(Connect, Name),
rp(vert:virNWFilterUndefine(Filter));
+call(["find-storage-pool-sources", Type, File | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ Res = case file:read_file(File) of
+ {ok, Spec} ->
+ vert:virConnectFindStoragePoolSources(Connect, Type, Spec);
+ Error ->
+ Error
+ end,
+ rp(Res);
+
+call(["pool-autostart", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Pool} = storagepool(Connect, Name),
+
+ Autostart = case proplists:get_value("disable", Opt) of
+ undefined -> 1;
+ _ -> 0
+ end,
+
+ rp(vert:virStoragePoolSetAutostart(Pool, Autostart));
+
+call(["pool-build", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Pool} = storagepool(Connect, Name),
+ rp(vert:virStoragePoolBuild(Pool));
+
+call(["pool-create", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Pool} = case file:read_file(Name) of
+ {ok, XML} ->
+ vert:virStoragePoolDefineXML(Connect, XML);
+ {error, enoent} ->
+ storagepool(Connect, Name)
+ end,
+ rp(vert:virStoragePoolCreate(Pool));
+
+call(["pool-define", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ Res = case file:read_file(Name) of
+ {ok, XML} ->
+ vert:virStoragePoolDefineXML(Connect, XML);
+ Error ->
+ Error
+ end,
+ rp(Res);
+
+call(["pool-delete", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Pool} = storagepool(Connect, Name),
+ rp(vert:virStoragePoolDelete(Pool));
+
+call(["pool-destroy", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Pool} = storagepool(Connect, Name),
+ rp(vert:virStoragePoolDestroy(Pool));
+
+call(["pool-dumpxml", Name | Arg]) ->
+ Opt = getopt(Arg),
+ Flags = proplists:get_value("flags", Opt, 0),
+ {ok, Connect} = connect(Opt),
+ {ok, Pool} = storagepool(Connect, Name),
+ rp(vert:virStoragePoolGetXMLDesc(Pool, Flags));
+
+call(["pool-info", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Pool} = storagepool(Connect, Name),
+ rp(vert:virStoragePoolGetInfo(Pool));
+
+call(["pool-list" | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Pools} = vert:virConnectListStoragePools(Connect),
+ rp({ok, [ begin
+ {ok, Pool} = storagepool(Connect, Name),
+ {ok, Active} = vert:virStoragePoolIsActive(Pool),
+ Autostart = vert:virStoragePoolGetAutostart(Pool),
+ {Name, [{state, bool(Active)}, {autostart, Autostart}]}
+ end || Name <- Pools ]});
+
+call(["pool-name", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Pool} = storagepool(Connect, Name),
+ rp(vert:virStoragePoolGetName(Pool));
+
+call(["pool-refresh", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Pool} = storagepool(Connect, Name),
+ rp(vert:virStoragePoolRefresh(Pool));
+
+call(["pool-undefine", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Pool} = storagepool(Connect, Name),
+ rp(vert:virStoragePoolUndefine(Pool));
+
+call(["pool-uuid", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Pool} = storagepool(Connect, Name),
+ rp(vert:virStoragePoolGetUUIDString(Pool));
+
call(_) ->
Help = [
"autostart",
@@ -347,6 +458,22 @@ call(_) ->
"nwfilter-dumpxml",
"nwfilter-list",
"nwfilter-undefine"
+
+ "find-storage-pool-sources",
+ "pool-autostart",
+ "pool-build",
+ "pool-create",
+ "pool-define",
+ "pool-delete",
+ "pool-destroy",
+ "pool-dumpxml",
+ "pool-info",
+ "pool-list",
+ "pool-name",
+ "pool-refresh",
+ "pool-undefine",
+ "pool-uuid"
+
],
rp(Help),
halt(1).
@@ -394,6 +521,11 @@ nwfilter(Connect, Name) ->
fun() -> vert:virNWFilterLookupByName(Connect, Name) end ],
lookup(Fun).
+storagepool(Connect, Name) ->
+ Fun = [ fun() -> vert:virStoragePoolLookupByUUIDString(Connect, Name) end,
+ fun() -> vert:virStoragePoolLookupByName(Connect, Name) end ],
+ lookup(Fun).
+
lookup(Fun) ->
lookup(Fun, []).
lookup([], [{error, Error}|_]) ->
View
@@ -86,6 +86,7 @@ load(ErlNifEnv *env, void **priv_data, ERL_NIF_TERM load_info)
atom_interface = enif_make_atom(env, "interface");
atom_network = enif_make_atom(env, "network");
atom_nwfilter = enif_make_atom(env, "nwfilter");
+ atom_storagepool = enif_make_atom(env, "storagepool");
atom_stream = enif_make_atom(env, "stream");
atom_true = enif_make_atom(env, "true");
atom_false = enif_make_atom(env, "false");
View
@@ -51,6 +51,7 @@ ERL_NIF_TERM atom_domain;
ERL_NIF_TERM atom_interface;
ERL_NIF_TERM atom_network;
ERL_NIF_TERM atom_nwfilter;
+ERL_NIF_TERM atom_storagepool;
ERL_NIF_TERM atom_stream;
ERL_NIF_TERM atom_true;
ERL_NIF_TERM atom_false;
@@ -86,13 +87,13 @@ typedef struct _vert_resource {
enum {
VERT_RES_CONNECT = 0,
VERT_RES_DOMAIN,
+ VERT_RES_FILTER,
VERT_RES_INTERFACE,
VERT_RES_NETWORK,
VERT_RES_NWFILTER,
- VERT_RES_STORAGEPOOL,
- VERT_RES_FILTER,
VERT_RES_SECRET,
- VERT_RES_STREAM,
+ VERT_RES_STORAGEPOOL,
+ VERT_RES_STREAM
};
#define VERT_RES_ALLOC(var,vtype,initial) do { \
@@ -267,6 +268,34 @@ vert_##fun(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) \
return vert_make_resource(env, rp, tag); \
}
+#define VERT_FUN_CREATEXML(fun, type, tag) \
+ ERL_NIF_TERM \
+vert_##fun(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) \
+{ \
+ VERT_RESOURCE *vp = NULL; \
+ ErlNifBinary xml = {0}; \
+ u_int32_t flags = 0; \
+ \
+ VERT_RESOURCE *rp = NULL; \
+ \
+ VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT); \
+ VERT_GET_IOLIST(1, xml); \
+ VERT_GET_UINT(2, flags); \
+ \
+ VERT_BIN_APPEND_NULL(xml); \
+ \
+ VERT_RES_ALLOC(rp, type, vp->res); \
+ \
+ rp->res = fun(vp->res, (const char *)xml.data, flags); \
+ \
+ if (rp->res == NULL) { \
+ enif_release_resource(rp); \
+ return verterr(env); \
+ } \
+ \
+ return vert_make_resource(env, rp, tag); \
+}
+
#define VERT_FUN_GETNAME(fun, type) \
ERL_NIF_TERM \
vert_##fun(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) \
View
@@ -226,6 +226,36 @@ vert_virConnectListDomains(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
list);
}
+ ERL_NIF_TERM
+vert_virConnectFindStoragePoolSources(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
+{
+ VERT_RESOURCE *vp = NULL;
+ ErlNifBinary type = {0};
+ ErlNifBinary srcSpec = {0};
+ u_int32_t flags = 0;
+
+ char *buf = NULL;
+
+
+ VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
+ VERT_GET_IOLIST(1, type);
+ VERT_GET_IOLIST(2, srcSpec);
+
+ VERT_BIN_APPEND_NULL(type);
+ VERT_BIN_APPEND_NULL(srcSpec);
+
+ buf = virConnectFindStoragePoolSources(vp->res,
+ type.size == 0 ? NULL : (char *)type.data,
+ srcSpec.size == 0 ? NULL : (char *)srcSpec.data,
+ flags);
+
+ VERTERR(buf == NULL);
+
+ return enif_make_tuple2(env,
+ atom_ok,
+ enif_make_string(env, buf, ERL_NIF_LATIN1));
+}
+
/*
* Internal functions
View
@@ -32,6 +32,7 @@
ErlNifFunc vert_funcs[] = {
/* connect */
+ {"virConnectFindStoragePoolSources", 3, vert_virConnectFindStoragePoolSources},
{"virConnectGetCapabilities", 1, vert_virConnectGetCapabilities},
{"virConnectGetHostname", 1, vert_virConnectGetHostname},
{"virConnectGetLibVersion", 1, vert_virConnectGetLibVersion},
@@ -143,6 +144,30 @@ ErlNifFunc vert_funcs[] = {
{"virNWFilterLookupByUUIDString", 2, vert_virNWFilterLookupByUUIDString},
{"virNWFilterUndefine", 1, vert_virNWFilterUndefine},
+ /* storagepool */
+ {"virStoragePoolBuild", 2, vert_virStoragePoolBuild},
+ {"virStoragePoolCreate", 2, vert_virStoragePoolCreate},
+ {"virStoragePoolCreateXML", 3, vert_virStoragePoolCreateXML},
+ {"virStoragePoolDefineXML", 3, vert_virStoragePoolDefineXML},
+ {"virStoragePoolDelete", 2, vert_virStoragePoolDelete},
+ {"virStoragePoolDestroy", 1, vert_virStoragePoolDestroy},
+ {"virStoragePoolGetAutostart", 1, vert_virStoragePoolGetAutostart},
+ {"virStoragePoolGetInfo", 1, vert_virStoragePoolGetInfo},
+ {"virStoragePoolGetName", 1, vert_virStoragePoolGetName},
+ {"virStoragePoolGetUUID", 1, vert_virStoragePoolGetUUID},
+ {"virStoragePoolGetUUIDString", 1, vert_virStoragePoolGetUUIDString},
+ {"virStoragePoolGetXMLDesc", 2, vert_virStoragePoolGetXMLDesc},
+ {"virStoragePoolIsActive", 1, vert_virStoragePoolIsActive},
+ {"virStoragePoolIsPersistent", 1, vert_virStoragePoolIsPersistent},
+ {"virStoragePoolListVolumes", 2, vert_virStoragePoolListVolumes},
+ {"virStoragePoolLookupByName", 2, vert_virStoragePoolLookupByName},
+ {"virStoragePoolLookupByUUID", 2, vert_virStoragePoolLookupByUUID},
+ {"virStoragePoolLookupByUUIDString", 2, vert_virStoragePoolLookupByUUIDString},
+ {"virStoragePoolNumOfVolumes", 1, vert_virStoragePoolNumOfVolumes},
+ {"virStoragePoolRefresh", 2, vert_virStoragePoolRefresh},
+ {"virStoragePoolSetAutostart", 2, vert_virStoragePoolSetAutostart},
+ {"virStoragePoolUndefine", 1, vert_virStoragePoolUndefine},
+
/* stream */
{"virStreamAbort", 1, vert_virStreamAbort},
{"virStreamFinish", 1, vert_virStreamFinish},
@@ -0,0 +1,77 @@
+/* Copyright (c) 2012, Michael Santos <michael.santos@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:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 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.
+ *
+ * Neither the name of the author nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDER 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 "vert.h"
+#include "vert_util.h"
+
+VERT_FUN_SETFLAG(virStoragePoolBuild, VERT_RES_STORAGEPOOL)
+VERT_FUN_SETFLAG(virStoragePoolCreate, VERT_RES_STORAGEPOOL)
+VERT_FUN_SETFLAG(virStoragePoolDelete, VERT_RES_STORAGEPOOL)
+
+VERT_FUN_CREATEXML(virStoragePoolCreateXML, VERT_RES_STORAGEPOOL, atom_storagepool)
+VERT_FUN_CREATEXML(virStoragePoolDefineXML, VERT_RES_STORAGEPOOL, atom_storagepool)
+
+VERT_FUN_INT_RES(virStoragePoolDestroy, VERT_RES_STORAGEPOOL)
+VERT_FUN_GETAUTOSTART(virStoragePoolGetAutostart, VERT_RES_STORAGEPOOL)
+VERT_FUN_GETNAME(virStoragePoolGetName, VERT_RES_STORAGEPOOL)
+VERT_FUN_GETUUID(virStoragePoolGetUUID, VERT_RES_STORAGEPOOL)
+VERT_FUN_GETUUIDSTRING(virStoragePoolGetUUIDString, VERT_RES_STORAGEPOOL)
+VERT_FUN_GETXMLDESC(virStoragePoolGetXMLDesc, VERT_RES_STORAGEPOOL)
+VERT_FUN_INT_RES(virStoragePoolIsActive, VERT_RES_STORAGEPOOL)
+VERT_FUN_INT_RES(virStoragePoolIsPersistent, VERT_RES_STORAGEPOOL)
+
+VERT_FUN_INT_RES_CHARPP_INT(virStoragePoolListVolumes, VERT_RES_STORAGEPOOL)
+VERT_FUN_INT_RES(virStoragePoolNumOfVolumes, VERT_RES_STORAGEPOOL)
+VERT_FUN_LOOKUPBYNAME(virStoragePoolLookupByName, VERT_RES_STORAGEPOOL, atom_storagepool)
+VERT_FUN_LOOKUPBYNAME(virStoragePoolLookupByUUIDString, VERT_RES_STORAGEPOOL, atom_storagepool)
+VERT_FUN_LOOKUPBYUUID(virStoragePoolLookupByUUID, VERT_RES_STORAGEPOOL, atom_storagepool)
+VERT_FUN_SETFLAG(virStoragePoolRefresh, VERT_RES_STORAGEPOOL)
+VERT_FUN_SETFLAG(virStoragePoolSetAutostart, VERT_RES_STORAGEPOOL)
+VERT_FUN_INT_RES(virStoragePoolUndefine, VERT_RES_STORAGEPOOL)
+
+ ERL_NIF_TERM
+vert_virStoragePoolGetInfo(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
+{
+ VERT_RESOURCE *sp = NULL;
+ ErlNifBinary info = {0};
+
+
+ VERT_GET_RESOURCE(0, sp, VERT_RES_STORAGEPOOL);
+
+ if (!enif_alloc_binary(sizeof(virStoragePoolInfo), &info))
+ return error_tuple(env, atom_enomem);
+
+ VERTERR(virStoragePoolGetInfo(sp->res, (virStoragePoolInfoPtr)info.data) < 0);
+
+ return enif_make_tuple2(env,
+ atom_ok,
+ enif_make_binary(env, &info));
+}
View
@@ -133,9 +133,6 @@ vert_cleanup(ErlNifEnv *env, void *obj)
case VERT_RES_NETWORK:
(void)virNetworkFree(vp->res);
break;
- case VERT_RES_STORAGEPOOL:
- (void)virStoragePoolFree(vp->res);
- break;
#if HAVE_NWFILTER
case VERT_RES_NWFILTER:
(void)virNWFilterFree(vp->res);
@@ -144,6 +141,9 @@ vert_cleanup(ErlNifEnv *env, void *obj)
case VERT_RES_SECRET:
(void)virSecretFree(vp->res);
break;
+ case VERT_RES_STORAGEPOOL:
+ (void)virStoragePoolFree(vp->res);
+ break;
case VERT_RES_STREAM:
(void)virStreamFree(vp->res);
break;
Oops, something went wrong.

0 comments on commit c67329b

Please sign in to comment.