diff options
| author | sumuel <samuel@yakubos.org> | 2026-08-17 20:44:55 +0000 |
|---|---|---|
| committer | sumuel <samuel@yakubos.org> | 2026-08-17 20:44:55 +0000 |
| commit | 76424950e373d3b04ac3dd13019151bfba3e8423 (patch) | |
| tree | 4c3cfdbda039e592b9186be3e28f8d7cfd439e8a /src/detection/zpool | |
Add the files
Diffstat (limited to 'src/detection/zpool')
| -rw-r--r-- | src/detection/zpool/libzfs_simplified.h | 48 | ||||
| -rw-r--r-- | src/detection/zpool/zpool.c | 130 | ||||
| -rw-r--r-- | src/detection/zpool/zpool.h | 17 |
3 files changed, 195 insertions, 0 deletions
diff --git a/src/detection/zpool/libzfs_simplified.h b/src/detection/zpool/libzfs_simplified.h new file mode 100644 index 0000000..5faf4ec --- /dev/null +++ b/src/detection/zpool/libzfs_simplified.h @@ -0,0 +1,48 @@ +#pragma once + +#include "fastfetch.h" + +// From https://github.com/openzfs/zfs/blob/master/include/libzfs.h + +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or https://opensource.org/licenses/CDDL-1.0. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +// zpool_prop_t and zprop_source_t were previously enums in upstream OpenZFS. +// However, the enum values for these types vary greatly between different platforms, +// making it unsafe to use the enum values directly. To ensure portability, +// we define them as simple int typedefs and use zpool_name_to_prop to look up +// the correct value for a property at runtime. +typedef int zpool_prop_t; +typedef int zprop_source_t; +typedef int boolean_t; + +typedef struct libzfs_handle libzfs_handle_t; +typedef struct zpool_handle zpool_handle_t; +typedef int (*zpool_iter_f)(zpool_handle_t*, void*); + +extern libzfs_handle_t* libzfs_init(void); +extern void libzfs_fini(libzfs_handle_t*); +extern int zpool_iter(libzfs_handle_t*, zpool_iter_f, void*); +extern zpool_prop_t zpool_name_to_prop(const char*); +// https://github.com/openzfs/zfs/blob/06c73cffabc30b61a695988ec8e290f43cb3768d/lib/libzfs/libzfs_pool.c#L300 +extern uint64_t zpool_get_prop_int(zpool_handle_t* zhp, zpool_prop_t prop, zprop_source_t* srctype); +extern int zpool_get_prop(zpool_handle_t* zhp, zpool_prop_t prop, char* buf, size_t len, zprop_source_t* srctype, boolean_t literal); +extern void zpool_close(zpool_handle_t*); diff --git a/src/detection/zpool/zpool.c b/src/detection/zpool/zpool.c new file mode 100644 index 0000000..ce7abb3 --- /dev/null +++ b/src/detection/zpool/zpool.c @@ -0,0 +1,130 @@ +#include "zpool.h" + +#if FF_HAVE_LIBZFS + + #include "common/kmod.h" + + #ifdef __sun + #include <libzfs.h> + #ifndef __illumos__ + // On Solaris 11, zpool_get_prop has only 5 arguments. #2173 + #define ffzpool_get_prop(zhp, prop, buf, len, srctype, literal) \ + ffzpool_get_prop(zhp, prop, buf, len, srctype) + #endif + #else + #include "libzfs_simplified.h" + #endif + + #include "common/library.h" + +typedef struct FFZfsData { + FF_LIBRARY_SYMBOL(libzfs_fini) + FF_LIBRARY_SYMBOL(zpool_get_prop_int) + FF_LIBRARY_SYMBOL(zpool_get_prop) + FF_LIBRARY_SYMBOL(zpool_close) + + // The fields in this struct store property IDs returned by `zpool_name_to_prop`, + // not the property values themselves. + struct { + int name; + int health; + int guid; + int size; + int free; + int allocated; + int fragmentation; + int readonly; + } props; + + libzfs_handle_t* handle; + FFlist* result; +} FFZfsData; + +static inline void cleanLibzfs(FFZfsData* data) { + if (data->fflibzfs_fini && data->handle) { + data->fflibzfs_fini(data->handle); + data->handle = NULL; + } +} + +static int enumZpoolCallback(zpool_handle_t* zpool, void* param) { + FFZfsData* data = (FFZfsData*) param; + zprop_source_t source; + FFZpoolResult* item = FF_LIST_ADD(FFZpoolResult, *data->result); + char buf[1024]; + if (data->ffzpool_get_prop(zpool, data->props.name, buf, ARRAY_SIZE(buf), &source, false) == 0) { + ffStrbufInitS(&item->name, buf); + } else { + ffStrbufInitStatic(&item->name, "unknown"); + } + if (data->ffzpool_get_prop(zpool, data->props.health, buf, ARRAY_SIZE(buf), &source, false) == 0) { + ffStrbufInitS(&item->state, buf); + } else { + ffStrbufInitStatic(&item->state, "unknown"); + } + item->guid = data->ffzpool_get_prop_int(zpool, data->props.guid, &source); + item->total = data->ffzpool_get_prop_int(zpool, data->props.size, &source); + item->used = item->total - data->ffzpool_get_prop_int(zpool, data->props.free, &source); + item->allocated = data->ffzpool_get_prop_int(zpool, data->props.allocated, &source); + uint64_t fragmentation = data->ffzpool_get_prop_int(zpool, data->props.fragmentation, &source); + item->fragmentation = fragmentation == UINT64_MAX ? -DBL_MAX : (double) fragmentation; + item->readOnly = (bool) data->ffzpool_get_prop_int(zpool, data->props.readonly, &source); + data->ffzpool_close(zpool); + return 0; +} + +const char* ffDetectZpool(FFlist* result /* list of FFZpoolResult */) { + FF_LIBRARY_LOAD_MESSAGE(libzfs, "libzfs" FF_LIBRARY_EXTENSION, 6); + FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libzfs, libzfs_init); + + libzfs_handle_t* handle = fflibzfs_init(); + if (!handle) { + if (!ffKmodLoaded("zfs")) { + return "`zfs` kernel module is not loaded"; + } + return "libzfs_init() failed"; + } + + FF_A_CLEANUP(cleanLibzfs) FFZfsData data = { + .handle = handle, + .result = result, + }; + + FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libzfs, zpool_name_to_prop); + + #define FF_QUERY_ZPOOL_PROP_FROM_NAME(prop_name) \ + do { \ + data.props.prop_name = ffzpool_name_to_prop(#prop_name); \ + if (data.props.prop_name < 0) \ + return "Failed to query prop: " #prop_name; \ + } while (false) + FF_QUERY_ZPOOL_PROP_FROM_NAME(name); + FF_QUERY_ZPOOL_PROP_FROM_NAME(health); + FF_QUERY_ZPOOL_PROP_FROM_NAME(guid); + FF_QUERY_ZPOOL_PROP_FROM_NAME(size); + FF_QUERY_ZPOOL_PROP_FROM_NAME(free); + FF_QUERY_ZPOOL_PROP_FROM_NAME(allocated); + FF_QUERY_ZPOOL_PROP_FROM_NAME(fragmentation); + FF_QUERY_ZPOOL_PROP_FROM_NAME(readonly); + #undef FF_QUERY_ZPOOL_PROP_FROM_NAME + + FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libzfs, zpool_iter); + FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, libzfs_fini); + FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, zpool_get_prop_int); + FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, zpool_get_prop); + FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, zpool_close); + + if (ffzpool_iter(handle, enumZpoolCallback, &data) < 0) { + return "zpool_iter() failed"; + } + + return NULL; +} + +#else + +const char* ffDetectZpool(FF_A_UNUSED FFlist* result) { + return "fastfetch was compiled without libzfs support"; +} + +#endif diff --git a/src/detection/zpool/zpool.h b/src/detection/zpool/zpool.h new file mode 100644 index 0000000..2e6523f --- /dev/null +++ b/src/detection/zpool/zpool.h @@ -0,0 +1,17 @@ +#pragma once + +#include "fastfetch.h" +#include "modules/zpool/option.h" + +typedef struct FFZpoolResult { + FFstrbuf name; + FFstrbuf state; + uint64_t guid; + uint64_t used; + uint64_t total; + uint64_t allocated; + double fragmentation; + bool readOnly; +} FFZpoolResult; + +const char* ffDetectZpool(FFlist* result /* list of FFZpoolResult */); |