]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: recommend GPT on interactive terminals on EFI systems for an empty device
authorHenrique Surian Stobbe <henriquesstobbe@gmail.com>
Wed, 15 Jul 2026 18:12:12 +0000 (15:12 -0300)
committerHenrique Surian Stobbe <henriquesstobbe@gmail.com>
Sun, 2 Aug 2026 12:45:13 +0000 (09:45 -0300)
When running fdisk or sfdisk on a device without any partition label, on a terminal
(stdin) and on an EFI system, the function fdisk_get_recommended_labelname() suggests
the GPT disklabel; otherwise, it returns DOS, or SUN on SPARC.

Addresses: https://github.com/util-linux/util-linux/issues/4478

Signed-off-by: Henrique Surian Stobbe <henriquesstobbe@gmail.com>
disk-utils/fdisk.8.adoc
disk-utils/fdisk.c
disk-utils/sfdisk.8.adoc
disk-utils/sfdisk.c
include/pathnames.h
libfdisk/docs/libfdisk-sections.txt
libfdisk/src/label.c
libfdisk/src/libfdisk.h.in
libfdisk/src/libfdisk.sym

index 95fe6c510c8d64c928cb48a3ed3fecda41fe1120..11ee6ae866d583f03c1618a84bac60a4ae3312a7 100644 (file)
@@ -163,6 +163,9 @@ An IRIX/SGI disklabel can describe 16 partitions, the eleventh of which should b
 +
 A *sync*(2) and an ioctl(BLKRRPART) (rereading the partition table from disk) are performed before exiting when the partition table has been updated.
 
+*Default label on empty devices*::
+When *fdisk* is given a device with no recognized partition table, it creates one automatically (unless --noauto-pt is used). Since v2.43, on an interactive terminal and on an EFI system, the chosen disklabel is GPT; otherwise, it defaults to DOS/MBR, or BSD/SUN on SPARC.
+
 == DOS mode and DOS 6.x WARNING
 
 *Note that all this is deprecated. You don't have to care about things like* *geometry and cylinders on modern operating systems. If you really want* *DOS-compatible partitioning then you have to enable DOS mode and cylinder* *units by using the '-c=dos -u=cylinders' fdisk command-line options.*
index bd90d14d9ce577042e0854268b09105cbb7d7f2c..556271c7de3fab39af5a1484b60905fb545cfd29 100644 (file)
@@ -1404,7 +1404,11 @@ int main(int argc, char **argv)
                if (!fdisk_has_label(cxt)) {
                        fdisk_info(cxt, _("Device does not contain a recognized partition table."));
                        if (!noauto_pt) {
-                               rc = fdisk_create_disklabel(cxt, wanted_label);
+                               if (is_interactive && !wanted_label)
+                                       rc = fdisk_create_disklabel(cxt, fdisk_get_recommended_labelname());
+                               else
+                                       rc = fdisk_create_disklabel(cxt, wanted_label);
+
                                if (rc)
                                        fdisk_warn(cxt, _("Failed to create a disklabel."));
                        }
index aaaf6e8cbba0ad280d24e12d8a12300ed1fbd071..d66944412c8781a9aa045439ef1aa29ad9e406f3 100644 (file)
@@ -458,6 +458,8 @@ Since version 2.26 *sfdisk* no longer provides the *-R* or *--re-read* option to
 
 Since version 2.26 *sfdisk* does not provide the *--DOS*, *--IBM*, *--DOS-extended*, *--unhide*, *--show-extended*, *--cylinders*, *--heads*, *--sectors*, *--inside-outer*, *--not-inside-outer* options.
 
+Since version 2.43, on an interactive terminal and on an EFI system, the chosen disklabel is GPT; otherwise, *sfdisk* defaults to DOS/MBR, or BSD/SUN on SPARC.
+
 == EXAMPLES
 
 *sfdisk --list --label-nested=mbr /dev/sda*::
index 431b87aed03e63b4a4f4370427899990a3963b91..b94a6e838d8c06bb5bd5c82b30f9c53d319008f8 100644 (file)
@@ -1952,6 +1952,8 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
                label = sf->label;
        else if (fdisk_has_label(sf->cxt))
                label = fdisk_label_get_name(fdisk_get_label(sf->cxt, NULL));
+       else if (sf->interactive)
+               label = fdisk_get_recommended_labelname();
        else
                label = "dos";  /* just for backward compatibility */
 
index 6dcfced222de85c676c20f5932db996e3ace4a37..fbda4e5a8894dd8652363274c4581fb12cf238eb 100644 (file)
 #define _PATH_SYS_SCSI         "/sys/bus/scsi"
 #define _PATH_SYS_CPU_BYTEORDER        "/sys/kernel/cpu_byteorder"
 #define _PATH_SYS_ADDRESS_BITS "/sys/kernel/address_bits"
+#define _PATH_SYS_EFI          "/sys/firmware/efi"
 
 #define _PATH_SYS_SELINUX      "/sys/fs/selinux"
 #define _PATH_SYS_APPARMOR     "/sys/kernel/security/apparmor"
index 8445d17e02db2e9acd306065f11258d08203cb15..53fa1a179d6701617766383c2859f1956e05f02f 100644 (file)
@@ -77,6 +77,7 @@ fdisk_save_user_sector_size
 
 <SECTION>
 <FILE>label</FILE>
+fdisk_get_recommended_labelname
 fdisk_create_disklabel
 fdisk_list_disklabel
 fdisk_locate_disklabel
index cbf6c7956afb8bcd93eabe046b4b1e672213c2ec..b09bdc42275e34f86e3b857b61e0729038c9dcea 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "fdiskP.h"
 #include "cctype.h"
+#include "pathnames.h"
 
 
 /**
@@ -332,6 +333,27 @@ int fdisk_list_disklabel(struct fdisk_context *cxt)
        return rc < 0 ? rc : 0;
 }
 
+/**
+ * fdisk_get_recommended_labelname:
+ *
+ * Suggests a disklabel type based on the current system: "sun" on SPARC,
+ * "gpt" when booted via EFI, otherwise "dos".
+ *
+ * Returns: label name string
+ * Since: 2.43
+ */
+const char *fdisk_get_recommended_labelname(void)
+{
+#ifdef __sparc__
+       return "sun";
+#else
+       if (access(_PATH_SYS_EFI, F_OK) == 0)
+               return "gpt";
+
+       return "dos";
+#endif
+}
+
 /**
  * fdisk_create_disklabel:
  * @cxt: fdisk context
index fe76784454901006f67cdbfa9513034cb7631eb2..c23ad475c26839ffabc16aaec360b82c5a4c0945 100644 (file)
@@ -397,6 +397,7 @@ int fdisk_label_require_geometry(const struct fdisk_label *lb);
 
 extern int fdisk_write_disklabel(struct fdisk_context *cxt);
 extern int fdisk_verify_disklabel(struct fdisk_context *cxt);
+extern const char *fdisk_get_recommended_labelname(void);
 extern int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name);
 extern int fdisk_list_disklabel(struct fdisk_context *cxt);
 extern int fdisk_locate_disklabel(struct fdisk_context *cxt, int n,
index badb085c991bad96014cd70c8e1520cd4b21bdd2..eaeea09880a28b7d87aa86bf534646268fd5f74d 100644 (file)
@@ -336,4 +336,5 @@ FDISK_2_42 {
 FDISK_2_43 {
        fdisk_script_disable_devnames;
        fdisk_script_has_devnames;
+       fdisk_get_recommended_labelname;
 } FDISK_2_42;