From: Karel Zak Date: Wed, 4 Dec 2019 11:38:57 +0000 (+0100) Subject: lsblk: add PARTTYPENAME column X-Git-Tag: v2.35-rc1~29 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=107e95594642b96cde430229095edcacb81dfa2f;p=thirdparty%2Futil-linux.git lsblk: add PARTTYPENAME column Print also partition type in human-readable way. Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1777261 Signed-off-by: Karel Zak --- diff --git a/misc-utils/lsblk-properties.c b/misc-utils/lsblk-properties.c index a1aa695312..a1c73bc7d0 100644 --- a/misc-utils/lsblk-properties.c +++ b/misc-utils/lsblk-properties.c @@ -9,6 +9,7 @@ #include "xalloc.h" #include "mangle.h" #include "path.h" +#include "nls.h" #include "lsblk.h" @@ -311,3 +312,66 @@ void lsblk_properties_deinit(void) udev_unref(udev); #endif } + + + +/* + * Partition types + */ +struct lsblk_parttype { + unsigned int code; /* type as number or zero */ + char *name; /* description */ + char *typestr; /* type as string or NULL */ +}; + +static const struct lsblk_parttype mbr_types[] = +{ + #include "pt-mbr-partnames.h" +}; + +#define DEF_GUID(_u, _n) \ + { \ + .typestr = (_u), \ + .name = (_n), \ + } +static const struct lsblk_parttype gpt_types[] = +{ + #include "pt-gpt-partnames.h" +}; + +const char *lsblk_parttype_code_to_string(const char *code, const char *pttype) +{ + size_t i; + + if (!code || !pttype) + return NULL; + + if (strcmp(pttype, "dos") == 0 || strcmp(pttype, "mbr") == 0) { + char *end = NULL; + unsigned int xcode; + + errno = 0; + xcode = strtol(code, &end, 16); + + if (errno || *end != '\0') + return NULL; + + for (i = 0; i < ARRAY_SIZE(mbr_types); i++) { + const struct lsblk_parttype *t = &mbr_types[i]; + + if (t->name && t->code == xcode) + return t->name; + } + + } else if (strcmp(pttype, "gpt") == 0) { + for (i = 0; i < ARRAY_SIZE(gpt_types); i++) { + const struct lsblk_parttype *t = &gpt_types[i]; + + if (t->name && t->typestr && + strcasecmp(code, t->typestr) == 0) + return t->name; + } + } + + return NULL; +} diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index cd9f1f823d..441655e249 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -79,6 +79,7 @@ enum { COL_PTUUID, COL_PTTYPE, COL_PARTTYPE, + COL_PARTTYPENAME, COL_PARTLABEL, COL_PARTUUID, COL_PARTFLAGS, @@ -168,7 +169,8 @@ static struct colinfo infos[] = { [COL_PTUUID] = { "PTUUID", 36, 0, N_("partition table identifier (usually UUID)") }, [COL_PTTYPE] = { "PTTYPE", 0.1, 0, N_("partition table type") }, - [COL_PARTTYPE] = { "PARTTYPE", 36, 0, N_("partition type UUID") }, + [COL_PARTTYPE] = { "PARTTYPE", 36, 0, N_("partition type code or UUID") }, + [COL_PARTTYPENAME] = { "PARTTYPENAME", 0.1, 0, N_("partition type name") }, [COL_PARTLABEL] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") }, [COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") }, [COL_PARTFLAGS] = { "PARTFLAGS", 36, 0, N_("partition flags") }, @@ -826,6 +828,15 @@ static char *device_get_data( if (prop && prop->parttype) str = xstrdup(prop->parttype); break; + case COL_PARTTYPENAME: + prop = lsblk_device_get_properties(dev); + if (prop && prop->parttype && prop->pttype) { + const char *x = lsblk_parttype_code_to_string( + prop->parttype, prop->pttype); + if (x) + str = xstrdup(x); + } + break; case COL_PARTLABEL: prop = lsblk_device_get_properties(dev); if (prop && prop->partlabel) diff --git a/misc-utils/lsblk.h b/misc-utils/lsblk.h index f922dc8bf9..87221271ed 100644 --- a/misc-utils/lsblk.h +++ b/misc-utils/lsblk.h @@ -199,6 +199,8 @@ extern void lsblk_device_free_properties(struct lsblk_devprop *p); extern struct lsblk_devprop *lsblk_device_get_properties(struct lsblk_device *dev); extern void lsblk_properties_deinit(void); +extern const char *lsblk_parttype_code_to_string(const char *code, const char *pttype); + /* lsblk-devtree.c */ void lsblk_reset_iter(struct lsblk_iter *itr, int direction); struct lsblk_device *lsblk_new_device(void);