]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add generic function to get disklabel ID
authorKarel Zak <kzak@redhat.com>
Tue, 25 Jun 2013 12:40:34 +0000 (14:40 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 16 Sep 2013 14:47:03 +0000 (16:47 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk.c
fdisks/fdiskdoslabel.c
libfdisk/src/fdiskP.h
libfdisk/src/gpt.c
libfdisk/src/label.c
libfdisk/src/libfdisk.h

index dae7986fac2021e2ebf1d84c11f3d8f54fb70af2..803eeeed9d1c47844a64744aa38ea27df3c72ac1 100644 (file)
@@ -229,8 +229,9 @@ static void change_partition_type(struct fdisk_context *cxt)
        fdisk_free_parttype(org_t);
 }
 
-static void
-list_disk_geometry(struct fdisk_context *cxt) {
+static void list_disk_geometry(struct fdisk_context *cxt)
+{
+       char *id = NULL;
        unsigned long long bytes = cxt->total_sectors * cxt->sector_size;
        long megabytes = bytes/1000000;
 
@@ -262,8 +263,9 @@ list_disk_geometry(struct fdisk_context *cxt) {
                printf(_("Alignment offset: %lu bytes\n"), cxt->alignment_offset);
        if (fdisk_dev_has_disklabel(cxt))
                printf(_("Disk label type: %s\n"), cxt->label->name);
-       if (fdisk_is_disklabel(cxt, DOS))
-               dos_print_mbr_id(cxt);
+
+       if (fdisk_get_disklabel_id(cxt, &id) == 0 && id)
+               printf(_("Disk identifier: %s\n"), id);
        printf("\n");
 }
 
index 40656c4dc17cabe28f67a5a7f1688bb06f978cc3..023fbbdb1796e75c52f2e3a76c84861e242b13cc 100644 (file)
@@ -438,10 +438,20 @@ static void read_extended(struct fdisk_context *cxt, int ext)
        }
 }
 
-void dos_print_mbr_id(struct fdisk_context *cxt)
+static int dos_get_disklabel_id(struct fdisk_context *cxt, char **id)
 {
-       fdisk_info(cxt, _("Disk identifier: 0x%08x"),
-                       mbr_get_id(cxt->firstsector));
+       unsigned int num;
+
+       assert(cxt);
+       assert(id);
+       assert(cxt->label);
+       assert(fdisk_is_disklabel(cxt, DOS));
+
+       num = mbr_get_id(cxt->firstsector);
+       if (asprintf(id, "0x%08x", num) > 0)
+               return 0;
+
+       return -ENOMEM;
 }
 
 static int dos_create_disklabel(struct fdisk_context *cxt)
@@ -1797,6 +1807,7 @@ static const struct fdisk_label_operations dos_operations =
        .verify         = dos_verify_disklabel,
        .create         = dos_create_disklabel,
        .list           = dos_list_disklabel,
+       .get_id         = dos_get_disklabel_id,
 
        .part_add       = dos_add_partition,
        .part_delete    = dos_delete_partition,
index d20154b802f4222de952b294fb5ab53a82533ef8..a24f2e6eff4845581840c6f3e7515cf6ad3682dc 100644 (file)
@@ -148,6 +148,8 @@ struct fdisk_label_operations {
        int (*create)(struct fdisk_context *cxt);
        /* list partition table */
        int (*list)(struct fdisk_context *cxt);
+       /* get disk label ID */
+       int (*get_id)(struct fdisk_context *cxt, char **id);
 
        /* new partition */
        int (*part_add)(struct fdisk_context *cxt,
index 9270e2ef94fa56922bd3d91f6e565491ad5e14db..efe78a50a57b2a08c307187d42399f776bf7594f 100644 (file)
@@ -1709,7 +1709,7 @@ static int gpt_create_disklabel(struct fdisk_context *cxt)
 {
        int rc = 0;
        ssize_t esz = 0;
-       struct gpt_guid *uid;
+       char str[37];
        struct fdisk_gpt_label *gpt;
 
        assert(cxt);
@@ -1765,21 +1765,32 @@ static int gpt_create_disklabel(struct fdisk_context *cxt)
        cxt->label->nparts_max = le32_to_cpu(gpt->pheader->npartition_entries);
        cxt->label->nparts_cur = 0;
 
-       uid = &gpt->pheader->disk_guid;
-       fdisk_info(cxt, _("Building a new GPT disklabel "
-                           "(GUID: %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X)\n"),
-                           uid->time_low, uid->time_mid,
-                           uid->time_hi_and_version,
-                           uid->clock_seq_hi,
-                           uid->clock_seq_low,
-                           uid->node[0], uid->node[1],
-                           uid->node[2], uid->node[3],
-                           uid->node[4], uid->node[5]);
+       guid_to_string(&gpt->pheader->disk_guid, str);
+       fdisk_info(cxt, _("Building a new GPT disklabel (GUID: %s)"), str);
        fdisk_label_set_changed(cxt->label, 1);
 done:
        return rc;
 }
 
+static int gpt_get_disklabel_id(struct fdisk_context *cxt, char **id)
+{
+       struct fdisk_gpt_label *gpt;
+       char str[37];
+
+       assert(cxt);
+       assert(id);
+       assert(cxt->label);
+       assert(fdisk_is_disklabel(cxt, GPT));
+
+       gpt = self_label(cxt);
+       guid_to_string(&gpt->pheader->disk_guid, str);
+
+       *id = strdup(str);
+       if (!*id)
+               return -ENOMEM;
+       return 0;
+}
+
 static struct fdisk_parttype *gpt_get_partition_type(
                struct fdisk_context *cxt,
                size_t i)
@@ -1973,6 +1984,8 @@ static const struct fdisk_label_operations gpt_operations =
        .verify         = gpt_verify_disklabel,
        .create         = gpt_create_disklabel,
        .list           = gpt_list_disklabel,
+       .get_id         = gpt_get_disklabel_id,
+
        .part_add       = gpt_add_partition,
        .part_delete    = gpt_delete_partition,
        .part_get_type  = gpt_get_partition_type,
index 32af51f88e8ec508f7edc2a773189d34066a0ff4..e39a543c8fc755abc9fc13e1ca482a0e293c4941 100644 (file)
@@ -235,6 +235,24 @@ int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name)
        return cxt->label->op->create(cxt);
 }
 
+/**
+ * fdisk_get_disklabel_id:
+ * @cxt: fdisk context
+ * @id: returns pointer to allocated string
+ *
+ * Returns 0 on success, otherwise, a corresponding error.
+ */
+int fdisk_get_disklabel_id(struct fdisk_context *cxt, char **id)
+{
+       if (!cxt || !cxt->label)
+               return -EINVAL;
+       if (!cxt->label->op->get_id)
+               return -ENOSYS;
+
+       DBG(LABEL, dbgprint("asking for %s ID", cxt->label->name));
+       return cxt->label->op->get_id(cxt, id);
+}
+
 /**
  * fdisk_get_partition_type:
  * @cxt: fdisk context
index aec9ecacdd41ccdd3ef792e898cae5d243ede3b6..b88756fd272786b81cbdd8afc7eb60f6b142e75a 100644 (file)
@@ -124,6 +124,8 @@ extern int fdisk_verify_disklabel(struct fdisk_context *cxt);
 extern int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name);
 extern int fdisk_list_disklabel(struct fdisk_context *cxt);
 
+extern int fdisk_get_disklabel_id(struct fdisk_context *cxt, char **id);
+
 extern int fdisk_add_partition(struct fdisk_context *cxt, struct fdisk_parttype *t);
 extern int fdisk_delete_partition(struct fdisk_context *cxt, size_t partnum);