gpart: add libxo support for "show" subcommand + man page updates

Added libxo support to `gpart show`, also updated the man
pages for geom and gpart to show where you can expect
libxo formatted output.

PR:                     290629
MFC after:              1 week
Sponsored by:           ConnectWise
Reviewed by:            asomers, mckusick, phil
Approved by:            asomers (mentor)
Differential Revision:  https://reviews.freebsd.org/D53950
This commit is contained in:
Johan Söllvander
2025-12-18 16:06:09 +01:00
parent e3a0571ad7
commit 4f809ffec6
4 changed files with 82 additions and 35 deletions
+1 -1
View File
@@ -2,6 +2,6 @@ PACKAGE=geom
GEOM_CLASS= part GEOM_CLASS= part
LIBADD= util LIBADD= util xo
.include <bsd.lib.mk> .include <bsd.lib.mk>
+60 -32
View File
@@ -46,6 +46,7 @@
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include <unistd.h> #include <unistd.h>
#include <libxo/xo.h>
#include "core/geom.h" #include "core/geom.h"
#include "misc/subr.h" #include "misc/subr.h"
@@ -77,7 +78,6 @@ static int geom_is_withered(struct ggeom *);
static const char *find_geomcfg(struct ggeom *, const char *); static const char *find_geomcfg(struct ggeom *, const char *);
static const char *find_provcfg(struct gprovider *, const char *); static const char *find_provcfg(struct gprovider *, const char *);
static struct gprovider *find_provider(struct ggeom *, off_t); static struct gprovider *find_provider(struct ggeom *, off_t);
static const char *fmtsize(int64_t);
static int gpart_autofill(struct gctl_req *); static int gpart_autofill(struct gctl_req *);
static int gpart_autofill_resize(struct gctl_req *); static int gpart_autofill_resize(struct gctl_req *);
static void gpart_bootcode(struct gctl_req *, unsigned int); static void gpart_bootcode(struct gctl_req *, unsigned int);
@@ -283,16 +283,6 @@ find_provider(struct ggeom *gp, off_t minsector)
return (bestpp); return (bestpp);
} }
static const char *
fmtsize(int64_t rawsz)
{
static char buf[5];
humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
HN_B | HN_NOSPACE | HN_DECIMAL);
return (buf);
}
static const char * static const char *
fmtattrib(struct gprovider *pp) fmtattrib(struct gprovider *pp)
{ {
@@ -622,6 +612,7 @@ static void
gpart_show_geom(struct ggeom *gp, const char *element, int show_providers) gpart_show_geom(struct ggeom *gp, const char *element, int show_providers)
{ {
struct gprovider *pp; struct gprovider *pp;
struct gconfig *gc;
const char *s, *scheme; const char *s, *scheme;
off_t first, last, sector, end; off_t first, last, sector, end;
off_t length, secsz; off_t length, secsz;
@@ -659,12 +650,14 @@ gpart_show_geom(struct ggeom *gp, const char *element, int show_providers)
wname = wmax; wname = wmax;
pp = LIST_FIRST(&gp->lg_consumer)->lg_provider; pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
secsz = pp->lg_sectorsize; secsz = pp->lg_sectorsize;
printf("=>%*jd %*jd %*s %s (%s)%s\n", xo_open_instance("part");
wblocks, (intmax_t)first, wblocks, (intmax_t)(last - first + 1), xo_emit("=>{t:start/%*jd} {t:sectors/%*jd} {t:name/%*s} {:scheme} ({h:size/%ld}){t:state}\n",
wname, gp->lg_name, wblocks, (intmax_t)first, wblocks, (intmax_t)(last - first + 1),
scheme, fmtsize(pp->lg_mediasize), wname, gp->lg_name,
s ? " [CORRUPT]": ""); scheme, pp->lg_mediasize,
s ? " [CORRUPT]": "");
xo_open_list("partitions");
while ((pp = find_provider(gp, first)) != NULL) { while ((pp = find_provider(gp, first)) != NULL) {
s = find_provcfg(pp, "start"); s = find_provcfg(pp, "start");
sector = (off_t)strtoimax(s, NULL, 0); sector = (off_t)strtoimax(s, NULL, 0);
@@ -676,33 +669,66 @@ gpart_show_geom(struct ggeom *gp, const char *element, int show_providers)
s = find_provcfg(pp, "index"); s = find_provcfg(pp, "index");
idx = atoi(s); idx = atoi(s);
if (first < sector) { if (first < sector) {
printf(" %*jd %*jd %*s - free - (%s)\n", xo_open_instance(s);
xo_emit(" {t:start/%*jd} {t:sectors/%*jd} {P:/%*s} {ne:free}- free - ({h:size/%ld})\n",
wblocks, (intmax_t)first, wblocks, wblocks, (intmax_t)first, wblocks,
(intmax_t)(sector - first), wname, "", (intmax_t)(sector - first), wname, "",
fmtsize((sector - first) * secsz)); "true", (sector - first) * secsz);
xo_close_instance(s);
} }
xo_open_instance(s);
xo_emit(" {t:start/%*jd} {t:sectors/%*jd}",
wblocks, (intmax_t)sector, wblocks, (intmax_t)length);
if (show_providers) { if (show_providers) {
printf(" %*jd %*jd %*s %s %s (%s)\n", xo_emit(" {t:name/%*s}{e:index/%d}", wname, pp->lg_name, idx);
wblocks, (intmax_t)sector, wblocks,
(intmax_t)length, wname, pp->lg_name,
find_provcfg(pp, element), fmtattrib(pp),
fmtsize(pp->lg_mediasize));
} else } else
printf(" %*jd %*jd %*d %s %s (%s)\n", xo_emit(" {t:index/%*d}{e:name}", wname, idx, pp->lg_name);
wblocks, (intmax_t)sector, wblocks,
(intmax_t)length, wname, idx, if (strcmp(element, "label") == 0)
find_provcfg(pp, element), fmtattrib(pp), xo_emit(" {:label}{e:type}{e:rawtype}",
fmtsize(pp->lg_mediasize)); find_provcfg(pp, element),
find_provcfg(pp, "type"),
find_provcfg(pp, "rawtype"));
else if (strcmp(element, "type") == 0)
xo_emit(" {:type}{e:label}{e:rawtype}",
find_provcfg(pp, element),
find_provcfg(pp, "label"),
find_provcfg(pp, "rawtype"));
else
xo_emit(" {:rawtype}{e:type}{e:label}",
find_provcfg(pp, element),
find_provcfg(pp, "type"),
find_provcfg(pp, "label"));
idx = 0;
LIST_FOREACH(gc, &pp->lg_config, lg_config) {
if (strcmp(gc->lg_name, "attrib") != 0)
continue;
idx++;
if (idx == 1)
xo_emit(" [");
else
xo_emit(",");
xo_emit("{l:attribute}", gc->lg_val);
}
if (idx)
xo_emit("]");
xo_emit(" ({h:size/%ld})\n", pp->lg_mediasize);
xo_close_instance(s);
first = end + 1; first = end + 1;
} }
if (first <= last) { if (first <= last) {
xo_open_instance("unallocated");
length = last - first + 1; length = last - first + 1;
printf(" %*jd %*jd %*s - free - (%s)\n", xo_emit(" {t:start/%*jd} {t:sectors/%*jd} {P:/%*s} {ne:free}- free - ({h:size/%ld})\n",
wblocks, (intmax_t)first, wblocks, (intmax_t)length, wblocks, (intmax_t)first, wblocks, (intmax_t)length,
wname, "", wname, "", "true", length * secsz);
fmtsize(length * secsz)); xo_close_instance("unallocated");
} }
printf("\n"); xo_close_list("partitions");
xo_close_instance("part");
xo_emit("\n");
} }
static int static int
@@ -752,6 +778,7 @@ gpart_show(struct gctl_req *req, unsigned int fl __unused)
errx(EXIT_FAILURE, "Class %s not found.", name); errx(EXIT_FAILURE, "Class %s not found.", name);
} }
show_providers = gctl_get_int(req, "show_providers"); show_providers = gctl_get_int(req, "show_providers");
xo_open_list(name);
if (nargs > 0) { if (nargs > 0) {
for (i = 0; i < nargs; i++) { for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i); name = gctl_get_ascii(req, "arg%d", i);
@@ -766,6 +793,7 @@ gpart_show(struct gctl_req *req, unsigned int fl __unused)
gpart_show_geom(gp, element, show_providers); gpart_show_geom(gp, element, show_providers);
} }
} }
xo_close_list(name);
geom_deletetree(&mesh); geom_deletetree(&mesh);
} }
+10 -1
View File
@@ -22,7 +22,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.Dd October 24, 2025 .Dd December 4, 2025
.Dt GPART 8 .Dt GPART 8
.Os .Os
.Sh NAME .Sh NAME
@@ -112,6 +112,7 @@
.Ar geom .Ar geom
.\" ==== SHOW ==== .\" ==== SHOW ====
.Nm .Nm
.Op Fl -libxo
.Cm show .Cm show
.Op Fl l | r .Op Fl l | r
.Op Fl p .Op Fl p
@@ -537,6 +538,13 @@ See
.It Cm unload .It Cm unload
See See
.Xr geom 8 . .Xr geom 8 .
.It Fl -libxo
Generate output via
.Xr libxo 3
in a selection of different human and machine readable formats.
See
.Xr xo_options 7
for details on command line arguments.
.El .El
.Sh PARTITIONING SCHEMES .Sh PARTITIONING SCHEMES
Several partitioning schemes are supported by the Several partitioning schemes are supported by the
@@ -1519,6 +1527,7 @@ for details
.El .El
.Sh SEE ALSO .Sh SEE ALSO
.Xr geom 4 , .Xr geom 4 ,
.Xr xo_options 7 ,
.Xr boot0cfg 8 , .Xr boot0cfg 8 ,
.Xr geom 8 , .Xr geom 8 ,
.Xr glabel 8 , .Xr glabel 8 ,
+11 -1
View File
@@ -25,7 +25,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.Dd May 9, 2023 .Dd December 4, 2025
.Dt GEOM 8 .Dt GEOM 8
.Os .Os
.Sh NAME .Sh NAME
@@ -36,11 +36,13 @@
.Ar class .Ar class
.Cm help .Cm help
.Nm .Nm
.Op Fl -libxo
.Ar class .Ar class
.Cm list .Cm list
.Op Fl a .Op Fl a
.Op Ar name ... .Op Ar name ...
.Nm .Nm
.Op Fl -libxo
.Ar class .Ar class
.Cm status .Cm status
.Op Fl ags .Op Fl ags
@@ -118,6 +120,13 @@ Print detailed information about the geom which provides
.Ar provider-name . .Ar provider-name .
.It Fl t .It Fl t
Display geoms hierarchy as a tree. Display geoms hierarchy as a tree.
.It Fl -libxo
Generate output via
.Xr libxo 3
in a selection of different human and machine readable formats.
See
.Xr xo_options 7
for details on command line arguments.
.El .El
.Pp .Pp
Class-specific commands are implemented as shared libraries which Class-specific commands are implemented as shared libraries which
@@ -205,6 +214,7 @@ geom md unload
.Sh SEE ALSO .Sh SEE ALSO
.Xr libgeom 3 , .Xr libgeom 3 ,
.Xr geom 4 , .Xr geom 4 ,
.Xr xo_options 7 ,
.Xr gcache 8 , .Xr gcache 8 ,
.Xr gconcat 8 , .Xr gconcat 8 ,
.Xr geli 8 , .Xr geli 8 ,