[git commit] fdisk: implement -t PARTTYPE

Denys Vlasenko vda.linux at googlemail.com
Sun Feb 15 14:15:30 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=9e8f8a196838b63acdbd2c9b48a2a333bc885e8b
branch: https://git.busybox.net/busybox/log/?h=master

This allows to operate on MBR even if disk also has GPT.

function                                             old     new   delta
.rodata                                           107019  107074     +55
packed_usage                                       36009   36034     +25
get_boot                                            1513    1532     +19
fdisk_main                                          4618    4622      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/0 up/down: 103/0)             Total: 103 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 util-linux/fdisk.c     | 17 ++++++++++++-----
 util-linux/fdisk_gpt.c | 16 ++++++++++------
 2 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index 6611d3954..5ab350c74 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -93,7 +93,7 @@
 
 //usage:#define fdisk_trivial_usage
 //usage:       "[-ul" IF_FEATURE_FDISK_BLKSIZE("s") "] "
-//usage:       "[-C CYLINDERS] [-H HEADS] [-S SECTORS] [-b SSZ] DISK"
+//usage:       "[-C CYLINDERS] [-H HEADS] [-S SECTORS] [-b SSZ] [-t PARTTYPE] DISK"
 //usage:#define fdisk_full_usage "\n\n"
 //usage:       "Change partition table\n"
 //usage:     "\n	-u		Start and End are in sectors (instead of cylinders)"
@@ -104,6 +104,7 @@
 //but in fact, util-linux 2.41.1 shows the size in KILOBYTES!
 //usage:	)
 //usage:     "\n	-b 2048		(for certain MO disks) use 2048-byte sectors"
+//usage:     "\n	-T PARTTYPE	Force 'dos' partition if 'gpt' also present"
 //usage:     "\n	-C CYLINDERS	Set number of cylinders/heads/sectors"
 //usage:     "\n	-H HEADS	Typically 255"
 //usage:     "\n	-S SECTORS	Typically 63"
@@ -186,7 +187,8 @@ enum {
 	OPT_l = 1 << 3,
 	OPT_S = 1 << 4,
 	OPT_u = 1 << 5,
-	OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
+	OPT_t = 1 << 6,
+	OPT_s = (1 << 7) * ENABLE_FEATURE_FDISK_BLKSIZE,
 };
 #define USER_SET_SECTOR_SIZE (option_mask32 & OPT_b)
 #define NOWARN_OPT_ls        (!ENABLE_FEATURE_FDISK_WRITABLE || (option_mask32 & (OPT_l|OPT_s)))
@@ -464,6 +466,7 @@ struct globals {
 	sector_t extended_offset;       /* offset of link pointers */
 	sector_t total_number_of_sectors;
 
+	const char *opt_t;
 #if ENABLE_FEATURE_GPT_LABEL
 	struct gpt_header *gpt_hdr;
 	char *gpt_part_array;
@@ -1209,7 +1212,7 @@ static void
 warn_cylinders(void)
 {
 	if (LABEL_IS_DOS && g_cylinders > 1024 && !NOWARN_OPT_ls)
-		printf("\n"
+		printf(
 "The number of cylinders for this disk is set to %u.\n"
 "There is nothing wrong with that, but this is larger than 1024,\n"
 "and could in certain setups cause problems with:\n"
@@ -3064,10 +3067,14 @@ int fdisk_main(int argc UNUSED_PARAM, char **argv)
 
 	close_dev_fd(); /* needed: fd 3 must not stay closed */
 
-	opt = getopt32(argv, "^" "b:+C:+H:+lS:+u"IF_FEATURE_FDISK_BLKSIZE("s")"\0"
+	//G.opt_t = NULL;
+	opt = getopt32(argv, "^" "b:+C:+H:+lS:+ut:"IF_FEATURE_FDISK_BLKSIZE("s")"\0"
 		/* among -s and -l, the last one takes precedence */
 		IF_FEATURE_FDISK_BLKSIZE("s-l:l-s"),
-		&sector_size, &user_cylinders, &user_heads, &user_sectors);
+		&sector_size, // -b
+		&user_cylinders, &user_heads, &user_sectors, //-CHS
+		&G.opt_t
+	);
 	argv += optind;
 
 #if ENABLE_FEATURE_FDISK_BLKSIZE
diff --git a/util-linux/fdisk_gpt.c b/util-linux/fdisk_gpt.c
index c085af79c..696b4937c 100644
--- a/util-linux/fdisk_gpt.c
+++ b/util-linux/fdisk_gpt.c
@@ -150,12 +150,13 @@ check_gpt_label(void)
 	struct pte pe;
 	uint32_t crc;
 
+	current_label_type = LABEL_DOS;
+
 	/* LBA 0 contains the legacy MBR */
 
 	if (!valid_part_table_flag(MBRbuffer)
 	 || first->sys_ind != LEGACY_GPT_TYPE
 	) {
-		current_label_type = LABEL_DOS;
 		return 0;
 	}
 
@@ -165,7 +166,6 @@ check_gpt_label(void)
 	G.gpt_hdr = (void *)pe.sectorbuffer;
 
 	if (G.gpt_hdr->magic != SWAP_LE64(GPT_MAGIC)) {
-		current_label_type = LABEL_DOS;
 		return 0;
 	}
 
@@ -188,7 +188,6 @@ check_gpt_label(void)
 	 || SWAP_LE32(G.gpt_hdr->hdr_size) > sector_size
 	) {
 		puts("\nwarning: can't parse GPT disklabel");
-		current_label_type = LABEL_DOS;
 		return 0;
 	}
 
@@ -204,10 +203,15 @@ check_gpt_label(void)
 		puts("\nwarning: GPT array CRC is invalid");
 	}
 
-	puts("Found valid GPT with protective MBR; using GPT");
+	fputs_stdout("Found valid GPT with protective MBR; ");
 
-	current_label_type = LABEL_GPT;
-	return 1;
+	if (!G.opt_t || strcasecmp(G.opt_t, "gpt") == 0) {
+		puts("using GPT (-t dos to override)");
+		current_label_type = LABEL_GPT;
+		return 1;
+	}
+	puts("NOT using it (-t specified)");
+	return 0;
 }
 
 #endif /* GPT_LABEL */


More information about the busybox-cvs mailing list