]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: return disk ID/UUID by PT binary interface
authorKarel Zak <kzak@redhat.com>
Tue, 19 Feb 2013 12:09:48 +0000 (13:09 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 19 Feb 2013 12:09:48 +0000 (13:09 +0100)
 Add blkid_parttable_get_id() function.

Signed-off-by: Karel Zak <kzak@redhat.com>
libblkid/docs/libblkid-sections.txt
libblkid/samples/partitions.c
libblkid/src/blkid.h.in
libblkid/src/blkid.sym
libblkid/src/partitions/dos.c
libblkid/src/partitions/dos.h
libblkid/src/partitions/gpt.c
libblkid/src/partitions/partitions.c
libblkid/src/partitions/partitions.h

index 511fc30c4f1eaa4ca181287a16c243f15085c13a..265bfcaa140f72f2812e0a1ab5bd2e7185916063 100644 (file)
@@ -98,6 +98,7 @@ blkid_partlist_numof_partitions
 blkid_partlist_devno_to_partition
 blkid_partlist_get_table
 <SUBSECTION>
+blkid_parttable_get_id
 blkid_parttable_get_offset
 blkid_parttable_get_parent
 blkid_parttable_get_type
index 3b5273649409650b39009d886eeaba5d7f82e50e..fe0ad4827d444b544d7a2acec1e3d72f5c84caa4 100644 (file)
@@ -48,11 +48,12 @@ int main(int argc, char *argv[])
                errx(EXIT_FAILURE, "%s: does not contains any "
                                 "known partition table\n", devname);
 
-       printf("size: %jd, sector size: %u, PT: %s, offset: %jd\n---\n",
+       printf("size: %jd, sector size: %u, PT: %s, offset: %jd, id=%s\n---\n",
                blkid_probe_get_size(pr),
                blkid_probe_get_sectorsize(pr),
                blkid_parttable_get_type(root_tab),
-               blkid_parttable_get_offset(root_tab));
+               blkid_parttable_get_offset(root_tab),
+               blkid_parttable_get_id(root_tab));
 
        /*
         * List partitions
index 44f2aa7b6e89bc620e5241050a044fbc74af3a51..68a82c3568f452c066374f69dbfe04f774d15c7d 100644 (file)
@@ -391,6 +391,9 @@ extern int blkid_partition_is_primary(blkid_partition par)
 extern const char *blkid_parttable_get_type(blkid_parttable tab)
                        __ul_attribute__((warn_unused_result));
 
+extern const char *blkid_parttable_get_id(blkid_parttable tab)
+                       __ul_attribute__((warn_unused_result));
+
 extern blkid_loff_t blkid_parttable_get_offset(blkid_parttable tab)
                        __ul_attribute__((warn_unused_result));
 extern blkid_partition blkid_parttable_get_parent(blkid_parttable tab)
index cf2a16f9444964ff430b54d8d1517a603b0c0f8e..012fe26c17f172c63646e6a57be38aef78e24b0f 100644 (file)
@@ -152,4 +152,5 @@ global:
 BLKID_2.23 {
 global:
        blkid_probe_step_back;
+       blkid_parttable_get_id;
 } BLKID_2.21;
index 22d2625dfadb85891972c52f74896c0268af305a..58877691d5eb7a0672411ee30aca8b9ca9bce787 100644 (file)
@@ -137,7 +137,7 @@ static int probe_dos_pt(blkid_probe pr,
        blkid_partlist ls;
        struct dos_partition *p0, *p;
        unsigned char *data;
-       uint32_t start, size;
+       uint32_t start, size, id;
 
        data = blkid_probe_get_sector(pr, 0);
        if (!data)
@@ -200,6 +200,15 @@ static int probe_dos_pt(blkid_probe pr,
        if (!tab)
                goto err;
 
+       id = dos_parttable_id(data);
+       if (id) {
+               char buf[37];
+
+               snprintf(buf, sizeof(buf), "0x%08x", id);
+               blkid_parttable_set_id(tab, (unsigned char *) buf);
+       }
+
+
        /* Parse primary partitions */
        for (p = p0, i = 0; i < 4; i++, p++) {
                blkid_partition par;
index 130aa0115d7b4abae7c2dee6875f5fc914c60218..d7588a85643f4b26a1acfabcd2cea9a3059d33b5 100644 (file)
@@ -13,7 +13,7 @@ struct dos_partition {
 #define BLKID_MSDOS_PT_OFFSET          0x1be
 
 /* assemble badly aligned little endian integer */
-static inline unsigned int assemble4le(unsigned char *p)
+static inline unsigned int assemble4le(const unsigned char *p)
 {
        return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
 }
@@ -33,4 +33,9 @@ static inline int is_valid_mbr_signature(const unsigned char *mbr)
        return mbr[510] == 0x55 && mbr[511] == 0xaa ? 1 : 0;
 }
 
+static inline unsigned int dos_parttable_id(const unsigned char *mbr)
+{
+       return assemble4le(&mbr[440]);
+}
+
 #endif /* BLKID_PARTITIONS_DOS_H */
index 98e82bfd2f9ac0e60e312fbe52a39de40de76156..7288d683e6fafb8885449945eecdcacd359b85b0 100644 (file)
@@ -334,6 +334,10 @@ static int probe_gpt_pt(blkid_probe pr,
        if (!tab)
                goto err;
 
+       guid = h->disk_guid;
+       swap_efi_guid(&guid);
+       blkid_parttable_set_id(tab, (const unsigned char *) &guid);
+
        ssf = blkid_probe_get_sectorsize(pr) / 512;
 
        fu = le64_to_cpu(h->first_usable_lba);
index d51d4074417967d879c932bda28ac05523a9375d..93ec4d224e23b4a053c816043d510082d34c54e2 100644 (file)
@@ -163,6 +163,7 @@ struct blkid_struct_parttable {
        blkid_loff_t    offset;         /* begin of the partition table */
        int             nparts;         /* number of partitions */
        blkid_partition parent;         /* parent of nested partition table */
+       char            id[37];         /* PT identifier (e.g. UUID for GPT) */
 
        struct list_head t_tabs;        /* all tables */
 };
@@ -1002,6 +1003,33 @@ blkid_partition blkid_partlist_devno_to_partition(blkid_partlist ls, dev_t devno
        return NULL;
 }
 
+int blkid_parttable_set_id(blkid_parttable tab, const unsigned char *id)
+{
+       if (!tab)
+               return -1;
+
+       if (strcmp(tab->type, "gpt") == 0)
+               blkid_unparse_uuid(id, tab->id, sizeof(tab->id));
+       else if (strcmp(tab->type, "dos") == 0)
+               strncpy(tab->id, (const char *) id, sizeof(tab->id));
+
+       return 0;
+}
+
+/**
+ * blkid_parttable_get_id:
+ * @tab: partition table
+ *
+ * The ID is GPT disk UUID or DOS disk ID (in hex format).
+ *
+ * Returns: partition table ID (for example GPT disk UUID) or NULL
+ */
+const char *blkid_parttable_get_id(blkid_parttable tab)
+{
+       return tab && tab->id && *tab->id ? tab->id : NULL;
+}
+
+
 int blkid_partition_set_type(blkid_partition par, int type)
 {
        if (!par)
index c4ccd3b6554471c5044fd3ae915ee5532ea35a60..496bd4a1ece55f121ad57e5c3e02d9247d00afa6 100644 (file)
@@ -9,6 +9,8 @@ extern int blkid_partitions_get_flags(blkid_probe pr);
 extern blkid_parttable blkid_partlist_new_parttable(blkid_partlist ls,
                                const char *type, blkid_loff_t offset);
 
+extern int blkid_parttable_set_id(blkid_parttable tab, const unsigned char *id);
+
 extern blkid_partition blkid_partlist_add_partition(blkid_partlist ls,
                                blkid_parttable tab,
                                blkid_loff_t start, blkid_loff_t size);