Skip to content

Commit 7e8fb0f

Browse files
committed
Zpool: simplifies impl; exposes allocated/guid/readOnly
1 parent 9d70469 commit 7e8fb0f

File tree

5 files changed

+64
-64
lines changed

5 files changed

+64
-64
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ cmake_dependent_option(ENABLE_DDCUTIL "Enable ddcutil" ON "LINUX" OFF)
9292
cmake_dependent_option(ENABLE_DIRECTX_HEADERS "Enable DirectX headers for WSL" ON "LINUX" OFF)
9393
cmake_dependent_option(ENABLE_ELF "Enable libelf" ON "LINUX OR ANDROID OR DragonFly OR Haiku OR GNU" OFF)
9494
cmake_dependent_option(ENABLE_THREADS "Enable multithreading" ON "Threads_FOUND" OFF)
95-
cmake_dependent_option(ENABLE_LIBZFS "Enable libzfs" ON "LINUX OR FreeBSD OR SunOS" OFF)
95+
cmake_dependent_option(ENABLE_LIBZFS "Enable libzfs" ON "LINUX OR FreeBSD OR SunOS OR NetBSD" OFF)
9696
cmake_dependent_option(ENABLE_PCIACCESS "Enable libpciaccess" ON "GNU" OFF)
9797

9898
option(ENABLE_ZLIB "Enable zlib" ON)

src/detection/zpool/libzfs_simplified.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ extern int zpool_iter(libzfs_handle_t *, zpool_iter_f, void *);
8484

8585
extern libzfs_handle_t *libzfs_init(void);
8686
extern void libzfs_fini(libzfs_handle_t *);
87-
extern uint64_t zpool_get_prop_int(zpool_handle_t *, zpool_prop_t, zprop_source_t *);
88-
extern const char *zpool_get_name(zpool_handle_t *);
89-
extern const char *zpool_get_state_str(zpool_handle_t *);
87+
// https://github.com/openzfs/zfs/blob/06c73cffabc30b61a695988ec8e290f43cb3768d/lib/libzfs/libzfs_pool.c#L300
88+
extern uint64_t zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *srctype);
89+
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);
90+
extern void zpool_close(zpool_handle_t *);

src/detection/zpool/zpool.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ typedef struct FFZpoolResult
77
{
88
FFstrbuf name;
99
FFstrbuf state;
10+
uint64_t guid;
1011
uint64_t used;
1112
uint64_t total;
12-
uint64_t version;
13+
uint64_t allocated;
1314
double fragmentation;
15+
bool readOnly;
1416
} FFZpoolResult;
1517

1618
const char* ffDetectZpool(FFlist* result /* list of FFZpoolResult */);

src/detection/zpool/zpool_linux.c

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,10 @@
11
#include "zpool.h"
22

3-
#ifdef FF_HAVE_LIBZFS
43
#include "util/kmod.h"
54

65
#ifdef __sun
76
#define FF_DISABLE_DLOPEN
87
#include <libzfs.h>
9-
10-
const char* zpool_get_state_str(zpool_handle_t* zpool)
11-
{
12-
if (zpool_get_state(zpool) == POOL_STATE_UNAVAIL)
13-
return "FAULTED";
14-
else
15-
{
16-
const char *str;
17-
zpool_errata_t errata;
18-
zpool_status_t status = zpool_get_status(zpool, (char**) &str, &errata);
19-
if (status == ZPOOL_STATUS_IO_FAILURE_WAIT ||
20-
status == ZPOOL_STATUS_IO_FAILURE_CONTINUE ||
21-
status == ZPOOL_STATUS_IO_FAILURE_MMP)
22-
return "SUSPENDED";
23-
else
24-
{
25-
nvlist_t *nvroot = fnvlist_lookup_nvlist(zpool_get_config(zpool, NULL), ZPOOL_CONFIG_VDEV_TREE);
26-
uint_t vsc;
27-
vdev_stat_t *vs;
28-
#ifdef __x86_64__
29-
if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, (uint64_t**) &vs, &vsc) != 0)
30-
#else
31-
if (nvlist_lookup_uint32_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, (uint32_t**) &vs, &vsc) != 0)
32-
#endif
33-
return "UNKNOWN";
34-
else
35-
return zpool_state_to_name(vs->vs_state, vs->vs_aux);
36-
}
37-
}
38-
}
398
#else
409
#include "libzfs_simplified.h"
4110
#endif
@@ -45,10 +14,9 @@ const char* zpool_get_state_str(zpool_handle_t* zpool)
4514
typedef struct FFZfsData
4615
{
4716
FF_LIBRARY_SYMBOL(libzfs_fini)
48-
FF_LIBRARY_SYMBOL(zpool_iter)
4917
FF_LIBRARY_SYMBOL(zpool_get_prop_int)
50-
FF_LIBRARY_SYMBOL(zpool_get_name)
51-
FF_LIBRARY_SYMBOL(zpool_get_state_str)
18+
FF_LIBRARY_SYMBOL(zpool_get_prop)
19+
FF_LIBRARY_SYMBOL(zpool_close)
5220

5321
libzfs_handle_t* handle;
5422
FFlist* result;
@@ -68,13 +36,23 @@ static int enumZpoolCallback(zpool_handle_t* zpool, void* param)
6836
FFZfsData* data = (FFZfsData*) param;
6937
zprop_source_t source;
7038
FFZpoolResult* item = ffListAdd(data->result);
71-
ffStrbufInitS(&item->name, data->ffzpool_get_name(zpool));
72-
ffStrbufInitS(&item->state, data->ffzpool_get_state_str(zpool));
73-
item->version = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_VERSION, &source);
39+
char buf[1024];
40+
if (data->ffzpool_get_prop(zpool, ZPOOL_PROP_NAME, buf, ARRAY_SIZE(buf), &source, false) == 0)
41+
ffStrbufInitS(&item->name, buf);
42+
else
43+
ffStrbufInitStatic(&item->name, "unknown");
44+
if (data->ffzpool_get_prop(zpool, ZPOOL_PROP_HEALTH, buf, ARRAY_SIZE(buf), &source, false) == 0)
45+
ffStrbufInitS(&item->state, buf);
46+
else
47+
ffStrbufInitStatic(&item->state, "unknown");
48+
item->guid = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_GUID, &source);
7449
item->total = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_SIZE, &source);
7550
item->used = item->total - data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_FREE, &source);
51+
item->allocated = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_ALLOCATED, &source);
7652
uint64_t fragmentation = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_FRAGMENTATION, &source);
7753
item->fragmentation = fragmentation == UINT64_MAX ? -DBL_MAX : (double) fragmentation;
54+
item->readOnly = (bool) data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_READONLY, &source);
55+
data->ffzpool_close(zpool);
7856
return 0;
7957
}
8058

@@ -98,20 +76,11 @@ const char* ffDetectZpool(FFlist* result /* list of FFZpoolResult */)
9876
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libzfs, zpool_iter);
9977
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, libzfs_fini);
10078
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, zpool_get_prop_int);
101-
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, zpool_get_name);
102-
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, zpool_get_state_str);
79+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, zpool_get_prop);
80+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, zpool_close);
10381

10482
if (ffzpool_iter(handle, enumZpoolCallback, &data) < 0)
10583
return "zpool_iter() failed";
10684

10785
return NULL;
10886
}
109-
110-
#else
111-
112-
const char* ffDetectZpool(FF_MAYBE_UNUSED FFlist* result)
113-
{
114-
return "Fastfetch was compiled without libzfs support";
115-
}
116-
117-
#endif

src/modules/zpool/zpool.c

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "common/size.h"
55
#include "detection/zpool/zpool.h"
66
#include "modules/zpool/zpool.h"
7+
#include "util/FFstrbuf.h"
78
#include "util/stringUtils.h"
89

910
static void printZpool(FFZpoolOptions* options, FFZpoolResult* result, uint8_t index)
@@ -22,17 +23,22 @@ static void printZpool(FFZpoolOptions* options, FFZpoolResult* result, uint8_t i
2223
FF_PARSE_FORMAT_STRING_CHECKED(&buffer, &options->moduleArgs.key, ((FFformatarg[]) {
2324
FF_FORMAT_ARG(index, "index"),
2425
FF_FORMAT_ARG(result->name, "name"),
26+
FF_FORMAT_ARG(result->guid, "guid"),
2527
FF_FORMAT_ARG(options->moduleArgs.keyIcon, "icon"),
2628
}));
2729
}
2830

2931
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
3032
ffSizeAppendNum(result->used, &usedPretty);
3133

34+
FF_STRBUF_AUTO_DESTROY allocatedPretty = ffStrbufCreate();
35+
ffSizeAppendNum(result->allocated, &allocatedPretty);
36+
3237
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
3338
ffSizeAppendNum(result->total, &totalPretty);
3439

35-
double bytesPercentage = result->total > 0 ? (double) result->used / (double) result->total * 100.0 : 0;
40+
double usedPercentage = result->total > 0 ? (double) result->used / (double) result->total * 100.0 : 0;
41+
double allocatedPercentage = result->total > 0 ? (double) result->allocated / (double) result->total * 100.0 : 0;
3642
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
3743

3844
if(options->moduleArgs.outputFormat.length == 0)
@@ -41,20 +47,31 @@ static void printZpool(FFZpoolOptions* options, FFZpoolResult* result, uint8_t i
4147

4248
ffStrbufClear(&buffer);
4349
ffStrbufSetF(&buffer, "%s / %s (", usedPretty.chars, totalPretty.chars);
44-
ffPercentAppendNum(&buffer, bytesPercentage, options->percent, false, &options->moduleArgs);
50+
ffPercentAppendNum(&buffer, usedPercentage, options->percent, false, &options->moduleArgs);
4551
ffStrbufAppendS(&buffer, ", ");
52+
ffPercentAppendNum(&buffer, allocatedPercentage, options->percent, false, &options->moduleArgs);
53+
ffStrbufAppendS(&buffer, " allocated, ");
4654
ffPercentAppendNum(&buffer, result->fragmentation, options->percent, false, &options->moduleArgs);
4755
ffStrbufAppendF(&buffer, " frag) - %s", result->state.chars);
56+
if (result->readOnly)
57+
ffStrbufAppendS(&buffer, " [Read-only]");
4858
ffStrbufPutTo(&buffer, stdout);
4959
}
5060
else
5161
{
52-
FF_STRBUF_AUTO_DESTROY bytesPercentageNum = ffStrbufCreate();
62+
FF_STRBUF_AUTO_DESTROY usedPercentageNum = ffStrbufCreate();
63+
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
64+
ffPercentAppendNum(&usedPercentageNum, usedPercentage, options->percent, false, &options->moduleArgs);
65+
FF_STRBUF_AUTO_DESTROY usedPercentageBar = ffStrbufCreate();
66+
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
67+
ffPercentAppendBar(&usedPercentageBar, usedPercentage, options->percent, &options->moduleArgs);
68+
69+
FF_STRBUF_AUTO_DESTROY allocatedPercentageNum = ffStrbufCreate();
5370
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
54-
ffPercentAppendNum(&bytesPercentageNum, bytesPercentage, options->percent, false, &options->moduleArgs);
55-
FF_STRBUF_AUTO_DESTROY bytesPercentageBar = ffStrbufCreate();
71+
ffPercentAppendNum(&allocatedPercentageNum, allocatedPercentage, options->percent, false, &options->moduleArgs);
72+
FF_STRBUF_AUTO_DESTROY allocatedPercentageBar = ffStrbufCreate();
5673
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
57-
ffPercentAppendBar(&bytesPercentageBar, bytesPercentage, options->percent, &options->moduleArgs);
74+
ffPercentAppendBar(&allocatedPercentageBar, allocatedPercentage, options->percent, &options->moduleArgs);
5875

5976
FF_STRBUF_AUTO_DESTROY fragPercentageNum = ffStrbufCreate();
6077
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
@@ -65,13 +82,18 @@ static void printZpool(FFZpoolOptions* options, FFZpoolResult* result, uint8_t i
6582

6683
FF_PRINT_FORMAT_CHECKED(buffer.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
6784
FF_FORMAT_ARG(result->name, "name"),
85+
FF_FORMAT_ARG(result->guid, "guid"),
6886
FF_FORMAT_ARG(result->state, "state"),
6987
FF_FORMAT_ARG(usedPretty, "size-used"),
88+
FF_FORMAT_ARG(allocatedPretty, "size-allocated"),
7089
FF_FORMAT_ARG(totalPretty, "size-total"),
71-
FF_FORMAT_ARG(bytesPercentageNum, "size-percentage"),
90+
FF_FORMAT_ARG(usedPercentageNum, "used-percentage"),
91+
FF_FORMAT_ARG(allocatedPercentageNum, "allocated-percentage"),
7292
FF_FORMAT_ARG(fragPercentageNum, "frag-percentage"),
73-
FF_FORMAT_ARG(bytesPercentageBar, "size-percentage-bar"),
93+
FF_FORMAT_ARG(usedPercentageBar, "used-percentage-bar"),
94+
FF_FORMAT_ARG(allocatedPercentageBar, "allocated-percentage-bar"),
7495
FF_FORMAT_ARG(fragPercentageBar, "frag-percentage-bar"),
96+
FF_FORMAT_ARG(result->readOnly, "is-readonly"),
7597
}));
7698
}
7799
}
@@ -149,13 +171,15 @@ bool ffGenerateZpoolJsonResult(FF_MAYBE_UNUSED FFZpoolOptions* options, yyjson_m
149171
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
150172
yyjson_mut_obj_add_strbuf(doc, obj, "name", &zpool->name);
151173
yyjson_mut_obj_add_strbuf(doc, obj, "state", &zpool->state);
174+
yyjson_mut_obj_add_uint(doc, obj, "guid", zpool->guid);
152175
yyjson_mut_obj_add_uint(doc, obj, "used", zpool->used);
176+
yyjson_mut_obj_add_uint(doc, obj, "allocated", zpool->allocated);
153177
yyjson_mut_obj_add_uint(doc, obj, "total", zpool->total);
154-
yyjson_mut_obj_add_uint(doc, obj, "version", zpool->version);
155178
if (zpool->fragmentation != -DBL_MAX)
156179
yyjson_mut_obj_add_real(doc, obj, "fragmentation", zpool->fragmentation);
157180
else
158181
yyjson_mut_obj_add_null(doc, obj, "fragmentation");
182+
yyjson_mut_obj_add_bool(doc, obj, "readOnly", zpool->readOnly);
159183
}
160184

161185
FF_LIST_FOR_EACH(FFZpoolResult, zpool, results)
@@ -188,12 +212,16 @@ FFModuleBaseInfo ffZpoolModuleInfo = {
188212
.generateJsonConfig = (void*) ffGenerateZpoolJsonConfig,
189213
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
190214
{"Zpool name", "name"},
215+
{"Zpool guid", "guid"},
191216
{"Zpool state", "state"},
192217
{"Size used", "used"},
218+
{"Size allocated", "allocated"},
193219
{"Size total", "total"},
194-
{"Size percentage num", "used-percentage"},
220+
{"Size used percentage num", "used-percentage"},
221+
{"Size allocated percentage num", "allocated-percentage"},
195222
{"Fragmentation percentage num", "fragmentation-percentage"},
196-
{"Size percentage bar", "used-percentage-bar"},
223+
{"Size used percentage bar", "used-percentage-bar"},
224+
{"Size allocated percentage bar", "allocated-percentage-bar"},
197225
{"Fragmentation percentage bar", "fragmentation-percentage-bar"},
198226
}))
199227
};

0 commit comments

Comments
 (0)