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>
+
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.*
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."));
}
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*::
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 */
#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"
<SECTION>
<FILE>label</FILE>
+fdisk_get_recommended_labelname
fdisk_create_disklabel
fdisk_list_disklabel
fdisk_locate_disklabel
#include "fdiskP.h"
#include "cctype.h"
+#include "pathnames.h"
/**
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
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,
FDISK_2_43 {
fdisk_script_disable_devnames;
fdisk_script_has_devnames;
+ fdisk_get_recommended_labelname;
} FDISK_2_42;