From: Leefancy Date: Tue, 5 May 2026 10:01:36 +0000 (+0200) Subject: sfdisk: add --no-device-names option X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=bd8e7e96ade5f9c00d171dffbb7a23bb43b692d2;p=thirdparty%2Futil-linux.git sfdisk: add --no-device-names option Add --no-device-names option to suppress device names in --dump output, using partition numbers instead (e.g., "1 : ..." rather than "/dev/sda1 : ..."). The new libfdisk API: - fdisk_script_disable_devnames() - fdisk_script_has_devnames() Addresses: https://github.com/util-linux/util-linux/issues/3737 [kzak@redhat.com: - fix gtk-doc comments format - move new symbols to FDISK_2_43 - simplify error handling in command_dump - fix man page to mention --dump - move --no-device-names in usage() with --no-* group] Signed-off-by: Leefancy Signed-off-by: Karel Zak --- diff --git a/bash-completion/sfdisk b/bash-completion/sfdisk index 290d234fd..f4fc9fc1b 100644 --- a/bash-completion/sfdisk +++ b/bash-completion/sfdisk @@ -80,6 +80,7 @@ _sfdisk_module() --lock --partno --no-act + --no-device-names --no-reread --no-tell-kernel --backup-file diff --git a/disk-utils/sfdisk.8.adoc b/disk-utils/sfdisk.8.adoc index 379dd46de..53731682e 100644 --- a/disk-utils/sfdisk.8.adoc +++ b/disk-utils/sfdisk.8.adoc @@ -181,6 +181,9 @@ to avoid collisions with *systemd-udevd*(8) or other tools. *-n*, *--no-act*:: Do everything except writing to the device. +*--no-device-names*:: +Do not print device names in *--dump* output. + *--no-reread*:: Do not check through the re-read-partition-table ioctl whether the device is in use. diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index f97029768..0a66bc5fc 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -120,7 +120,8 @@ struct sfdisk { movedata: 1, /* move data after resize */ movefsync: 1, /* use fsync() after each write() */ notell : 1, /* don't tell kernel about new PT */ - noact : 1; /* do not write to device */ + noact : 1, /* do not write to device */ + no_device_names : 1; /* do not display device names in partition rows */ }; #define SFDISK_PROMPT ">>> " @@ -1068,6 +1069,9 @@ static int command_dump(struct sfdisk *sf, int argc, char **argv) if (!dp) err(EXIT_FAILURE, _("failed to allocate dump struct")); + if (sf->no_device_names) + fdisk_script_disable_devnames(dp, 1); + rc = fdisk_script_read_context(dp, NULL); if (rc) errx(EXIT_FAILURE, _("%s: failed to dump partition table"), devname); @@ -2185,6 +2189,7 @@ static void __attribute__((__noreturn__)) usage(void) _(" --lock[=] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock"); fputs(_(" -N, --partno specify partition number\n"), out); fputs(_(" -n, --no-act do everything except write to device\n"), out); + fputs(_(" --no-device-names do not print device names in dump output\n"), out); fputs(_(" --no-reread do not check whether the device is in use\n"), out); fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out); fputs(_(" -O, --backup-file override default backup file name\n"), out); @@ -2244,7 +2249,8 @@ int main(int argc, char *argv[]) OPT_NOTELL, OPT_RELOCATE, OPT_LOCK, - OPT_SECTORSIZE + OPT_SECTORSIZE, + OPT_NO_DEVICE_NAMES }; static const struct option longopts[] = { @@ -2267,6 +2273,7 @@ int main(int argc, char *argv[]) { "list-free", no_argument, NULL, 'F' }, { "list-types", no_argument, NULL, 'T' }, { "no-act", no_argument, NULL, 'n' }, + { "no-device-names", no_argument, NULL, OPT_NO_DEVICE_NAMES }, { "no-reread", no_argument, NULL, OPT_NOREREAD }, { "no-tell-kernel", no_argument, NULL, OPT_NOTELL }, { "move-data", optional_argument, NULL, OPT_MOVEDATA }, @@ -2464,6 +2471,9 @@ int main(int argc, char *argv[]) case OPT_DELETE: sf->act = ACT_DELETE; break; + case OPT_NO_DEVICE_NAMES: + sf->no_device_names = 1; + break; case OPT_NOTELL: sf->notell = 1; break; diff --git a/libfdisk/docs/libfdisk-sections.txt b/libfdisk/docs/libfdisk-sections.txt index fd11df793..8445d17e0 100644 --- a/libfdisk/docs/libfdisk-sections.txt +++ b/libfdisk/docs/libfdisk-sections.txt @@ -149,6 +149,8 @@ fdisk_script_write_file fdisk_script_set_userdata fdisk_script_get_userdata fdisk_unref_script +fdisk_script_disable_devnames +fdisk_script_has_devnames
diff --git a/libfdisk/src/libfdisk.h.in b/libfdisk/src/libfdisk.h.in index bc9ff6036..fe7678445 100644 --- a/libfdisk/src/libfdisk.h.in +++ b/libfdisk/src/libfdisk.h.in @@ -824,6 +824,9 @@ int fdisk_script_write_file(struct fdisk_script *dp, FILE *f); int fdisk_script_read_file(struct fdisk_script *dp, FILE *f); int fdisk_script_read_line(struct fdisk_script *dp, FILE *f, char *buf, size_t bufsz); +int fdisk_script_disable_devnames(struct fdisk_script *dp, int disable); +int fdisk_script_has_devnames(struct fdisk_script *dp); + int fdisk_set_script(struct fdisk_context *cxt, struct fdisk_script *dp); struct fdisk_script *fdisk_get_script(struct fdisk_context *cxt); diff --git a/libfdisk/src/libfdisk.sym b/libfdisk/src/libfdisk.sym index 9e82cfd40..badb085c9 100644 --- a/libfdisk/src/libfdisk.sym +++ b/libfdisk/src/libfdisk.sym @@ -332,3 +332,8 @@ FDISK_2_41 { FDISK_2_42 { fdisk_is_collision_area; } FDISK_2_41; + +FDISK_2_43 { + fdisk_script_disable_devnames; + fdisk_script_has_devnames; +} FDISK_2_42; diff --git a/libfdisk/src/script.c b/libfdisk/src/script.c index 9af965fb0..a01e98c25 100644 --- a/libfdisk/src/script.c +++ b/libfdisk/src/script.c @@ -67,7 +67,8 @@ struct fdisk_script { unsigned long sector_size; /* as defined by script */ unsigned int json : 1, /* JSON output */ - force_label : 1; /* label: specified */ + force_label : 1, /* label: specified */ + no_device_names : 1; /* 1=suppress device name, 0=display device name (default) */ }; static void fdisk_script_free_header(struct fdisk_scriptheader *fi) @@ -82,6 +83,44 @@ static void fdisk_script_free_header(struct fdisk_scriptheader *fi) free(fi); } +/** + * fdisk_script_disable_devnames: + * @dp: script instance + * @disable: 1 to suppress device names, 0 to show them (default) + * + * After setting this flag, the script output will use partition numbers + * instead of device names (e.g., "1 : ..." rather than "/dev/sda1 : ..."). + * This does not affect the "device" header. + * + * Returns: 0 on success, negative number in case of error. + * + * Since: 2.43 + */ +int fdisk_script_disable_devnames(struct fdisk_script *dp, int disable) +{ + if (!dp) + return -EINVAL; + + dp->no_device_names = disable ? 1 : 0; + return 0; +} + +/** + * fdisk_script_has_devnames: + * @dp: script instance + * + * Returns: 1 if device names are shown, 0 if suppressed, negative number in case of error. + * + * Since: 2.43 + */ +int fdisk_script_has_devnames(struct fdisk_script *dp) +{ + if (!dp) + return -EINVAL; + + return dp->no_device_names ? 0 : 1; +} + /** * fdisk_new_script: * @cxt: context @@ -590,7 +629,7 @@ static int write_file_json(struct fdisk_script *dp, FILE *f) else ul_jsonwrt_value_s(&json, name, fi->data); - if (strcmp(name, "device") == 0) + if (fdisk_script_has_devnames(dp) && strcmp(name, "device") == 0) devname = fi->data; } @@ -611,6 +650,9 @@ static int write_file_json(struct fdisk_script *dp, FILE *f) ul_jsonwrt_object_open(&json, NULL); if (devname) p = fdisk_partname(devname, pa->partno + 1); + else if (asprintf(&p, "%zu", pa->partno + 1) < 0) + return -ENOMEM; + if (p) { DBG_OBJ(SCRIPT, dp, ul_debug("write %s entry", p)); ul_jsonwrt_value_s(&json, "node", p); @@ -675,7 +717,7 @@ static int write_file_sfdisk(struct fdisk_script *dp, FILE *f) list_for_each(h, &dp->headers) { struct fdisk_scriptheader *fi = list_entry(h, struct fdisk_scriptheader, headers); fprintf(f, "%s: %s\n", fi->name, fi->data); - if (strcmp(fi->name, "device") == 0) + if (fdisk_script_has_devnames(dp) && strcmp(fi->name, "device") == 0) devname = fi->data; } @@ -695,6 +737,9 @@ static int write_file_sfdisk(struct fdisk_script *dp, FILE *f) if (devname) p = fdisk_partname(devname, pa->partno + 1); + else if (asprintf(&p, "%zu", pa->partno + 1) < 0) + return -ENOMEM; + if (p) { DBG_OBJ(SCRIPT, dp, ul_debug("write %s entry", p)); fprintf(f, "%s : ", p);