LIBGEOM(3)
π LIBGEOM(3) β the userland API library for the kernel GEOM subsystem
okay bestie, welcome to libgeom β this is the official and publicized userland API for talking to the GEOM subsystem in the kernel. It's in section 3 of the manual (that's the "library functions" one, iykyk), and this page is genuinely a lot, so grab a snack π.
π LIBRARY
Userland API Library for kernel GEOM subsystem (that's libgeom, link it with -lgeom) π«Ά
β¨ SYNOPSIS β¨
Heads up first: stdio.h is only required for gctl_dump π
/* stdio.h is only required for `gctl_dump` */
#include <stdio.h>
#include <libgeom.h>
π Statistics Functions
void
geom_stats_close(void);
int
geom_stats_open(void);
void
geom_stats_resync(void);
void *
geom_stats_snapshot_get(void);
void
geom_stats_snapshot_free(void *arg);
void
geom_stats_snapshot_timestamp(void *arg, struct timespec *tp);
void
geom_stats_snapshot_reset(void *arg);
struct devstat *
geom_stats_snapshot_next(void *arg);
πΉοΈ Control Functions
struct gctl_req *
gctl_get_handle(void);
void
gctl_ro_param(struct gctl_req *req, const char *name, int len,
const void *value);
void
gctl_rw_param(struct gctl_req *req, const char *name, int len,
void *value);
const char *
gctl_issue(struct gctl_req *req);
void
gctl_free(struct gctl_req *req);
void
gctl_dump(struct gctl_req *req, FILE *f);
π§° Utility Functions
char *
geom_getxml(void);
int
geom_xml2tree(struct gmesh *gmp, char *p);
int
geom_gettree(struct gmesh *gmp);
void
geom_deletetree(struct gmesh *gmp);
int
g_open(const char *name, int dowrite);
int
g_close(int fd);
off_t
g_mediasize(int fd);
ssize_t
g_sectorsize(int fd);
ssize_t
g_stripeoffset(int fd);
ssize_t
g_stripesize(int fd);
int
g_flush(int fd);
int
g_delete(int fd, off_t offset, off_t length);
char *
g_device_path(const char *devpath);
int
g_get_ident(int fd, char *ident, size_t size);
int
g_get_name(const char *ident, char *name, size_t size);
int
g_open_by_ident(const char *ident, int dowrite, char *name,
size_t size);
char *
g_providername(int fd);
πΈ DESCRIPTION πΈ
So the geom library is the official and publicized API for interacting with the GEOM subsystem in the kernel. That's the whole vibe. Now let's go function by function π
π Statistics Functions
GEOM collects statistics data for all consumers and providers β but it does not do any normalization or presentation on the raw data. That part is left as an exercise for the user-land presentation utilities, bestie. Not our problem, fr fr.
geom_stats_open()/geom_stats_close()β these open and close the necessary pathways to access the raw statistics information in the kernel. Heads up: these functions are likely to open one or more files and cache the file descriptors locally π.geom_stats_open()returns zero on success, and setserrnoif not.geom_stats_resync()β checks whether more statistics collection points have been added in the kernel sincegeom_stats_open()or the previous call togeom_stats_resync().geom_stats_snapshot_get()β acquires a snapshot of the raw data from the kernel. And listen, a reasonable effort is made to make this snapshot as atomic and consistent as possible, but no guarantee is given that it will actually be so π. The snapshot must be freed again usinggeom_stats_snapshot_free(). It returnsNULLon failure.geom_stats_snapshot_timestamp()β gives you access to the timestamp acquired in the snapshot. cute.geom_stats_snapshot_reset()/geom_stats_snapshot_next()β these two are an iterator over the statistics slots in the snapshot π.geom_stats_snapshot_reset()forces the internal pointer in the snapshot back to before the first item.geom_stats_snapshot_next()returns the next item, andNULLif there are no more items in the snapshot.
πΉοΈ Control Functions
The gctl_*() functions are how you send requests to GEOM classes. But for a GEOM class to actually be able to receive these requests, it must have defined a "ctlreq" method. No method, no request, that's the rule β¨
A struct gctl_req *, which you get with gctl_get_handle(), can hold any number of parameters β and you add them with gctl_ro_param() (read-only parameters) or gctl_rw_param() (read/write parameters).
Both gctl_ro_param() and gctl_rw_param() take a string name, which identifies the parameter, and a value. For read-only, value holds the data to be passed to the GEOM class. For read/write, value is a pointer to preallocated memory that the GEOM class should fill with the desired data π«Ά. If len is negative, it's assumed that value is an ASCII string and the actual length is taken from the string length of value; otherwise len must hold the size of value.
mandatory alert π¨ a parameter with a name containing the string "class" is mandatory for each request, and the corresponding value must hold the name of the GEOM class where the request should be sent.
Also mandatory for each request: a parameter with a name called "verb", and the corresponding value needs to hold the command string that the GEOM class should react upon.
Once all the desired parameters are filled in, the request must be sent to the GEOM subsystem with gctl_issue() β which returns NULL on success, or a string containing the error message on failure π
After the request is finished, release the allocated memory with gctl_free().
gctl_dump()β formats the contents of req to the open file handle pointed to by f, for debugging purposes π- Error handling for the control functions is postponed until the call to
gctl_issue(), which returnsNULLon success, or an error message corresponding to the first error which happened.
π§° Utility Functions
geom_getxml()β a wrapper aroundsysctl(3)that fetches thekern.geom.confxmlOID, and returns its value. Release the allocated memory withfree(3)after use πgeom_xml2tree()β parses the XML representation of a GEOM topology passed as p, allocates the needed data structures to access this information, and fills in the passed gmp data structure. Release memory allocated during this transformation usinggeom_deletetree()after use.geom_gettree()β a wrapper around thegeom_getxml()andgeom_xml2tree()functions. Release memory allocated during this operation usinggeom_deletetree()after use.geom_deletetree()β releases memory allocated for storing the data-structures referenced by gmp.
Now the g_*() functions, which are used to communicate with GEOM providers π
g_open()β opens the given provider and returns the file descriptor number, which you can use with other functions. The dowrite argument indicates if operations that modify the provider (likeg_flush()org_delete()) are going to be called.g_close()β closes the provider.g_mediasize()β returns the size of the given provider.g_sectorsize()β returns the sector size of the given provider.g_stripeoffset()β returns the stripe offset of the given provider.g_stripesize()β returns the stripe size of the given provider.g_flush()β sends aBIO_FLUSHrequest to flush the write cache of the provider.g_delete()β tells the provider that the given data range is no longer used.g_device_path()β returns the full path to a provider given a partial or full path to the device node.NULLis returned if the device cannot be found or is not a valid geom provider.g_get_ident()β returns the provider's fixed and unique identifier. The ident argument should be at leastDISK_IDENT_SIZEbig πg_get_name()β returns the name of the provider whose identifier is equal to the ident string.g_open_by_ident()β opens a provider using its identification, unlikeg_open()which uses the provider's name. It will store the provider's name in the name parameter if it is notNULL.g_providername()β returns the provider name of an open file descriptor.NULLis returned if the file descriptor does not point to a valid geom provider.
rule of thumb β all functions exceptg_providername()andg_device_path()return a value greater than or equal to0on success, or-1on failure.
π EXAMPLES π
Create a request that is to be sent to the CCD class, and tell it to destroy a specific geom. iconic:
H = gctl_get_handle();
gctl_ro_param(H, "verb", -1, "destroy geom");
gctl_ro_param(H, "class", -1, "CCD");
sprintf(buf, "ccd%d", ccd);
gctl_ro_param(H, "geom", -1, buf);
errstr = gctl_issue(H);
if (errstr != NULL)
err(1, "could not destroy ccd: %s", errstr);
gctl_free(H);
π¦ HISTORY π¦
The geom library appeared in FreeBSD 5.1. vintage π
π AUTHORS π
- Poul-Henning Kamp
<phk@FreeBSD.org> - Lukas Ertl
<le@FreeBSD.org> - Pawel Jakub Dawidek
<pjd@FreeBSD.org>
and that's the whole page bestie, go build something π§π
sign-off: this one comes from the Debian edition of the manual, dated October 16, 2017 β LIBGEOM(3), no cap πΈ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2003 Poul-Henning Kamp
- Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms