Skip to contentSkip to conversation

fdisk: add GPT support

PATCH Applied Davidlohr Bueso 3 participants 5 messages Aug 21, 2012 – Oct 2, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 2/3 fdisk: add GPT support

From: Davidlohr Bueso <[email protected]>

This patch allows fdisk to handle GUID partition tables, based on the latest UEFI specifications version 2.3.1, from June 27th, 2012. The following operations are supported:

- Probing (detects both protective and hybrid MBRs)
- Writing to disk
- Listing used partitions
- Adding partitions
- Deleting partitions
- Data integrity verifications (for both headers and partitions).

A few considerations:
- Currently we do not fix invalid primary headers -- we just abort!
- Header checksums are updated upon every change (ie: add/delete partitions), this allows us to mathematically verify the changes on-the-fly, and not only when writing to disk, like most other related tools do.
- We are extremly picky when writing to disk, any error aborts the opeartion.
- When creating a new partition, the following GUIDs are available:
http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs

For test cases, the gpt.img from libblkid tests, scsi_debug and my own hard drive (/dev/sda) were used. For the image, all operations were tested successfully, and for /dev/sda all except write, which was not tested - hey, I'm not suicidal!

---
Note that when adding libblkid support for probing, some of the local probing functions will go.

 fdisks/Makemodule.am |   12 +-
 fdisks/cfdisk.c      |    8 -
 fdisks/fdisk.c       |   41 +-
 fdisks/fdisk.h       |    3 +
 fdisks/gpt.c         | 1451 +++++++++++++++++++++++++++++++++++++++++++++-----
 fdisks/gpt.h         |    4 +-
 fdisks/sfdisk.c      |   25 -
 fdisks/utils.c       |    1 +
 8 files changed, 1343 insertions(+), 202 deletions(-)
fdisks/Makemodule.am +9 -3
index f586769..97f5d48 100644
@@ -1,8 +1,6 @@
 
 fdisk_common_sources = \
 	fdisks/common.h \
-	fdisks/gpt.c \
-	fdisks/gpt.h \
 	fdisks/i386_sys_types.c
 
 if !ARCH_M68K
@@ -25,16 +23,24 @@ fdisk_SOURCES = \
 	fdisks/fdisksunlabel.h \
 	fdisks/fdiskdoslabel.c \
 	fdisks/fdiskdoslabel.h \
+	fdisks/gpt.c \
+	fdisks/gpt.h \
 	fdisks/partname.c \
 	$(fdisk_common_sources)
 
 fdisk_LDADD = $(LDADD) libcommon.la
+fdisk_CFLAGS = $(AM_CFLAGS)
 
 if BUILD_LIBBLKID
-fdisk_CFLAGS = -I$(ul_libblkid_incdir)
+fdisk_CFLAGS += -I$(ul_libblkid_incdir)
 fdisk_LDADD += libblkid.la
 endif
 
+if BUILD_LIBUUID
+fdisk_CFLAGS += -I$(ul_libuuid_incdir)
+fdisk_LDADD += libuuid.la
+endif
+
 if HAVE_STATIC_FDISK
 sbin_PROGRAMS += fdisk.static
 fdisk_static_SOURCES = $(fdisk_SOURCES)
fdisks/cfdisk.c +0 -8
index 3efa42c..704e0f6 100644
@@ -103,7 +103,6 @@
 #include "blkdev.h"
 #include "strutils.h"
 #include "common.h"
-#include "gpt.h"
 #include "mbsalign.h"
 #include "widechar.h"
 
@@ -1501,13 +1500,6 @@ fill_p_info(void) {
 	 opentype = O_RDWR;
     opened = TRUE;
 
-    if (gpt_probe_signature_fd(fd)) {
-	 print_warning(_("Warning!!  Unsupported GPT (GUID Partition Table) detected. Use GNU Parted."));
-	 refresh();
-	 getch();
-	 clear_warning();
-    }
-
 #ifdef BLKFLSBUF
     /* Blocks are visible in more than one way:
        e.g. as block on /dev/hda and as block on /dev/hda3
fdisks/fdisk.c +18 -23

120 lines not on this page · raw message

fdisks/fdisk.h +3 -0
index 05dc8a8..b17d492 100644
@@ -160,6 +160,7 @@ extern const struct fdisk_label bsd_label;
 extern const struct fdisk_label mac_label;
 extern const struct fdisk_label sun_label;
 extern const struct fdisk_label sgi_label;
+extern const struct fdisk_label gpt_label;
 
 extern struct fdisk_context *fdisk_new_context_from_filename(const char *fname, int readonly);
 extern int fdisk_dev_has_topology(struct fdisk_context *cxt);
@@ -217,6 +218,7 @@ extern unsigned int read_int_with_suffix(struct fdisk_context *cxt,
 extern sector_t align_lba(struct fdisk_context *cxt, sector_t lba, int direction);
 extern int get_partition_dflt(struct fdisk_context *cxt, int warn, int max, int dflt);
 extern void update_sector_offset(struct fdisk_context *cxt);
+extern struct systypes *get_sys_types(void);
 
 #define PLURAL	0
 #define SINGULAR 1
@@ -234,6 +236,7 @@ enum fdisk_labeltype {
 	AIX_LABEL = 8,
 	OSF_LABEL = 16,
 	MAC_LABEL = 32,
+	GPT_LABEL = 64,
 	ANY_LABEL = -1
 };
 
fdisks/gpt.c +1310 -141

1,511 lines not on this page · raw message

fdisks/gpt.h +2 -2
index 2ac21c4..28326c4 100644
@@ -1,7 +1,7 @@
 #ifndef FDISK_GPT_H
 #define FDISK_GPT_H
 
-extern int gpt_probe_signature_fd(int fd);
-extern int gpt_probe_signature_devname(char *devname);
+extern struct systypes gpt_sys_types[];
+extern void gpt_list_table(struct fdisk_context *cxt, int xtra);
 
 #endif /* FDISK_GPT_H */
fdisks/sfdisk.c +0 -25
index 15c6d33..19bbb6f 100644
@@ -50,7 +50,6 @@
 #include "linux_version.h"
 #include "common.h"
 #include "wholedisk.h"
-#include "gpt.h"
 #include "pathnames.h"
 #include "canonicalize.h"
 #include "rpmatch.h"
@@ -2526,25 +2525,6 @@ nextproc(FILE * procf) {
     return NULL;
 }
 
-static void
-gpt_warning(char *dev, int warn_only) {
-    if (force)
-	warn_only = 1;
-
-    if (dev && gpt_probe_signature_devname(dev)) {
-	fflush(stdout);
-	fprintf(stderr,
-		_("\nWARNING: GPT (GUID Partition Table) detected on '%s'! "
-		  "The util sfdisk doesn't support GPT. Use GNU Parted.\n\n"),
-		dev);
-	if (!warn_only) {
-	    fprintf(stderr,
-		    _("Use the --force flag to overrule this check.\n"));
-	    exit(1);
-	}
-    }
-}
-
 static void do_list(char *dev, int silent);
 static void do_size(char *dev, int silent);
 static void do_geom(char *dev, int silent);
@@ -2733,7 +2713,6 @@ main(int argc, char **argv) {
 	else {
 	    while ((dev = nextproc(procf)) != NULL) {
 		if (!is_ide_cdrom_or_tape(dev)) {
-		    gpt_warning(dev, 1);
 		    if (opt_out_geom)
 			do_geom(dev, 1);
 		    if (opt_out_pt_geom)
@@ -2765,7 +2744,6 @@ main(int argc, char **argv) {
 
     if (opt_list || opt_out_geom || opt_out_pt_geom || opt_size || verify) {
 	while (optind < argc) {
-	    gpt_warning(argv[optind], 1);
 	    if (opt_out_geom)
 		do_geom(argv[optind], 0);
 	    if (opt_out_pt_geom)
@@ -2779,9 +2757,6 @@ main(int argc, char **argv) {
 	exit(exit_status);
     }
 
-    if (optind != argc - 1)
-	gpt_warning(argv[optind], 0);
-
     if (activate) {
 	do_activate(argv + optind, argc - optind, activatearg);
 	exit(exit_status);
fdisks/utils.c +1 -0
index 84cdd4d..1d8e1af 100644
@@ -39,6 +39,7 @@ int fdisk_debug_mask;
  */
 static const struct fdisk_label *labels[] =
 {
+	&gpt_label,
 	&dos_label,
 	&sun_label,
 	&sgi_label,
Signature · 1 line
1.7.4.1
maintainer Applied
#1 lore raw
Quoted 2 lines · Davidlohr Bueso

This patch allows fdisk to handle GUID partition tables, based on the latest UEFI specifications version 2.3.1, from June 27th, 2012.

Applied, with some changes (add string based list of partition types).

TODO:

- differentiate between number of allocated and used partitions in GPT, for example "Partition number (1-128):" dialog is strange if you have only one partition. [I'll fix it tomorrow]

- add 'Device' column to GPT list_table output [I'll fix it tomorrow]

- add 'g' to main menu to create a new empty GPT disklabel

- LBA alignment, it necessary to use the same things we use for MBR; partitions has to be aligned according to I/O limits (sector size). [Important!]

- move list_table() functions to fdisk_label drive struct

- remove 'disklabel' from fdisk.c at all and keep fdisk.c independent on MBR

- remove global label-specific variables (e.g. gpt ents[])

- test GPT (specially 't' command) on big-endian machine (EFI UUIDs has been designed during LSD trip...)

less important things:

- add '<something>' to fdisk menu to print details about selected partition (uuid, type uuid, type name, name, etc...)

- add '<something>' to menu to print details about the partition table (header, backup header, locations, number of allocated entries, used entries, offset of entry table and offset and size of data area, etc.)

All should be implemented by drive specific functions, without exceptions, fdisk.c has to be simple and readable :-)

Note I did not applied the man page yet. I think we can do that later when the code will be stable.

Quoted 1 line

Note that when adding libblkid support for probing, some of the local probing functions will go.

Yes, but this is not so important for now. We have to clean up before we start to implement new things.

Thanks!

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#2 lore raw
Quoted 6 lines · Karel Zak
Quoted 2 lines · Davidlohr Bueso

This patch allows fdisk to handle GUID partition tables, based on the latest UEFI specifications version 2.3.1, from June 27th, 2012.

Applied, with some changes (add string based list of partition types).

Thanks!!

Quoted 10 lines

TODO:

- differentiate between number of allocated and used partitions in GPT, for example "Partition number (1-128):" dialog is strange if you have only one partition. [I'll fix it tomorrow]

- add 'Device' column to GPT list_table output [I'll fix it tomorrow]

- add 'g' to main menu to create a new empty GPT disklabel

Yes, this is a big TODO, but fortunately it is quite straightforward with what we have now. I'll get on it as soon as I can.

Quoted 9 lines

- LBA alignment, it necessary to use the same things we use for MBR; partitions has to be aligned according to I/O limits (sector size). [Important!]

- move list_table() functions to fdisk_label drive struct

- remove 'disklabel' from fdisk.c at all and keep fdisk.c independent on MBR

Yeah, this is easy yet invasive (like most initial fdisk-patches).

Quoted 2 lines

- remove global label-specific variables (e.g. gpt ents[])

Do you mean this?
static struct gpt_header *pheader = NULL;
static struct gpt_header *bheader = NULL;
static struct gpt_entry *ents = NULL;

If so, the reason for it being global is that it's accessed by the fdisk_label struct members, which, as you know, callers/users cannot know of label-specific stuff (only fdisk_context).

Quoted 3 lines

- test GPT (specially 't' command) on big-endian machine (EFI UUIDs has been designed during LSD trip...)

LOL. I lack the box, perhaps some good folks at Oracle/Sun can help us with an older SPARC machine.

Quoted 11 lines

less important things:

- add '<something>' to fdisk menu to print details about selected partition (uuid, type uuid, type name, name, etc...)

- add '<something>' to menu to print details about the partition table (header, backup header, locations, number of allocated entries, used entries, offset of entry table and offset and size of data area, etc.)

Perhaps in verify?

Quoted 3 lines

All should be implemented by drive specific functions, without exceptions, fdisk.c has to be simple and readable :-)

Yep, working towards that!

Quoted 3 lines

Note I did not applied the man page yet. I think we can do that later when the code will be stable.

Ok, fine by me. The manpage should be updated before the next release though.

On another note, I am a bit concerned about dealing with writing changes on disks with hybrid MBRs and not transforming it the standard protective. We need to be able to do so. Any thoughts are appreciated.

- Davidlohr

maintainer
#3 lore raw
Quoted 6 lines · Davidlohr Bueso
Quoted 1 line

- remove global label-specific variables (e.g. gpt ents[])

Do you mean this?
static struct gpt_header *pheader = NULL;
static struct gpt_header *bheader = NULL;
static struct gpt_entry *ents = NULL;

yes, and also many others in fdisk{dos,bsd,...}.c files

Quoted 3 lines

If so, the reason for it being global is that it's accessed by the fdisk_label struct members, which, as you know, callers/users cannot know of label-specific stuff (only fdisk_context).

Think about it as about a shared library. Maybe one day we will want to support work with more contexts of more labels, nested partition tables (bsd within dos) etc. And it's also about code readability.

We have everywhere fdisk_context, so fix this problem should not be a problem.

Quoted 9 lines
Quoted 7 lines

- add '<something>' to fdisk menu to print details about selected partition (uuid, type uuid, type name, name, etc...)

- add '<something>' to menu to print details about the partition table (header, backup header, locations, number of allocated entries, used entries, offset of entry table and offset and size of data area, etc.)

Perhaps in verify?

Maybe. It would be nice to have a way how to get all possible information about PT.

Quoted 3 lines

On another note, I am a bit concerned about dealing with writing changes on disks with hybrid MBRs and not transforming it the standard protective. We need to be able to do so. Any thoughts are appreciated.

From my point of view hybrid MBRs is nonsense and it's also against UEFI standard (there should be protective MBR and GPT, nothing other). I don't see a problem to ignore hybrid MBR at all.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#4 lore raw
Quoted 8 lines · Karel Zak
Quoted 3 lines · Davidlohr Bueso

On another note, I am a bit concerned about dealing with writing changes on disks with hybrid MBRs and not transforming it the standard protective. We need to be able to do so. Any thoughts are appreciated.

From my point of view hybrid MBRs is nonsense and it's also against UEFI standard (there should be protective MBR and GPT, nothing other). I don't see a problem to ignore hybrid MBR at all.

+1. While there might be (unfortunately) use cases for hybrid MBRs, I believe fdisk should _by default_ just ignore such crap and always write proper protective MBR according to the standard (vanilla parted does the same).

Some sort of support for hybrid MBRs could be eventually implemented later.

My 2c,

Petr

Signature · 2 lines
Petr Uzel
IRC: ptr_uzl @ freenode