]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
sfdisk: add --no-device-names option
authorLeefancy <lijian01@kylinos.cn>
Tue, 5 May 2026 10:01:36 +0000 (12:01 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 6 May 2026 09:25:04 +0000 (11:25 +0200)
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 <lijian01@kylinos.cn>
Signed-off-by: Karel Zak <kzak@redhat.com>
bash-completion/sfdisk
disk-utils/sfdisk.8.adoc
disk-utils/sfdisk.c
libfdisk/docs/libfdisk-sections.txt
libfdisk/src/libfdisk.h.in
libfdisk/src/libfdisk.sym
libfdisk/src/script.c

index 290d234fdfbb99a86de810af44d9c6b0d1e631ae..f4fc9fc1b91fc673a238635a7315c93cdc7c2fc9 100644 (file)
@@ -80,6 +80,7 @@ _sfdisk_module()
                                --lock
                                --partno
                                --no-act
+                               --no-device-names
                                --no-reread
                                --no-tell-kernel
                                --backup-file
index 379dd46deeb1f6d3db653ef7969824bdc02503a2..53731682e45062248247bf3c2a22e76b7864d851 100644 (file)
@@ -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.
 
index f97029768f3f90aebc3cbc5002f80e4498442ffc..0a66bc5fcf3bfcc3f70b9da928bd3e555e4da303 100644 (file)
@@ -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[=<mode>]       use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
        fputs(_(" -N, --partno <num>        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 <path>  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;
index fd11df7939b209ee38c849b04b9614b6214a8f8a..8445d17e02db2e9acd306065f11258d08203cb15 100644 (file)
@@ -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
 </SECTION>
 
 <SECTION>
index bc9ff603601aef3989d2d35ef87a2fe52a0a1a38..fe76784454901006f67cdbfa9513034cb7631eb2 100644 (file)
@@ -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);
 
index 9e82cfd40a8dbc77a2b07c283573ddc9f5e58906..badb085c991bad96014cd70c8e1520cd4b21bdd2 100644 (file)
@@ -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;
index 9af965fb01ed85de4547b0c21894ce197fa28dc9..a01e98c25a89d48edced86de0e1f428f771372a8 100644 (file)
@@ -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: <name> specified */
+                               force_label : 1,        /* label: <name> 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);