]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add generic function to set disklabel ID
authorKarel Zak <kzak@redhat.com>
Tue, 25 Jun 2013 13:31:21 +0000 (15:31 +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-menu.c
fdisks/fdisk.c
fdisks/fdiskdoslabel.c
fdisks/fdiskdoslabel.h
libfdisk/src/fdiskP.h
libfdisk/src/gpt.c
libfdisk/src/label.c
libfdisk/src/libfdisk.h

index fe65d46224212ff9c3c70aae31d9011c5e785e30..e854f0bc77da682568dbba64cd873cd61cb5c2fc 100644 (file)
@@ -134,8 +134,10 @@ struct menu menu_gpt = {
        .label = FDISK_DISKLABEL_GPT,
        .entries = {
                MENU_XSEP(N_("GPT")),
-               MENU_XENT('u', N_("change partition UUID")),
+               MENU_XENT('i', N_("change disk GUID")),
                MENU_XENT('n', N_("change partition name")),
+               MENU_XENT('u', N_("change partition UUID")),
+
                { 0, NULL }
        }
 };
@@ -389,6 +391,9 @@ static int gpt_menu_cb(struct fdisk_context *cxt,
 
        DBG(FRONTEND, dbgprint("enter GPT menu"));
 
+       if (ent->key == 'i')
+               return fdisk_set_disklabel_id(cxt);
+
        rc = fdisk_ask_partnum(cxt, &n, FALSE);
        if (rc)
                return rc;
index 803eeeed9d1c47844a64744aa38ea27df3c72ac1..ff581ce74c795af7212f9e870d711bc551db4ebe 100644 (file)
@@ -425,8 +425,7 @@ expert_command_prompt(struct fdisk_context *cxt)
                        fdisk_create_disklabel(cxt, "sgi");
                        break;
                case 'i':
-                       if (fdisk_is_disklabel(cxt, DOS))
-                               dos_set_mbr_id(cxt);
+                       fdisk_set_disklabel_id(cxt);
                        break;
                case 'p':
                        list_table(cxt, 1);
index 023fbbdb1796e75c52f2e3a76c84861e242b13cc..84c4a34311e39e62eb075f43092f636c4c768343 100644 (file)
@@ -480,13 +480,18 @@ static int dos_create_disklabel(struct fdisk_context *cxt)
        return 0;
 }
 
-int dos_set_mbr_id(struct fdisk_context *cxt)
+static int dos_set_disklabel_id(struct fdisk_context *cxt)
 {
        char *end = NULL, *str = NULL;
        unsigned int id, old;
-       struct fdisk_dos_label *l = self_label(cxt);
+       struct fdisk_dos_label *l;
        int rc;
 
+       assert(cxt);
+       assert(cxt->label);
+       assert(fdisk_is_disklabel(cxt, DOS));
+
+       l = self_label(cxt);
        old = mbr_get_id(cxt->firstsector);
        rc = fdisk_ask_string(cxt,
                        _("Enter of the new disk identifier"), &str);
@@ -1808,6 +1813,7 @@ static const struct fdisk_label_operations dos_operations =
        .create         = dos_create_disklabel,
        .list           = dos_list_disklabel,
        .get_id         = dos_get_disklabel_id,
+       .set_id         = dos_set_disklabel_id,
 
        .part_add       = dos_add_partition,
        .part_delete    = dos_delete_partition,
index 3ba71453d0b18a1ac7b054cc33834c6ba03e2fab..a0878b2258652c4f38da0c276fc18d2d977a9c5c 100644 (file)
@@ -7,9 +7,6 @@ extern struct dos_partition *fdisk_dos_get_partition(
                                struct fdisk_context *cxt,
                                size_t i);
 
-extern void dos_print_mbr_id(struct fdisk_context *cxt);
-extern int dos_set_mbr_id(struct fdisk_context *cxt);
-
 extern void dos_fix_partition_table_order(struct fdisk_context *cxt);
 extern void dos_move_begin(struct fdisk_context *cxt, int i);
 extern void dos_toggle_active(struct fdisk_context *cxt, int i);
index a24f2e6eff4845581840c6f3e7515cf6ad3682dc..3832c012379ff91b38002c830bc3c0d73e73b61f 100644 (file)
@@ -150,6 +150,8 @@ struct fdisk_label_operations {
        int (*list)(struct fdisk_context *cxt);
        /* get disk label ID */
        int (*get_id)(struct fdisk_context *cxt, char **id);
+       /* set disk label ID */
+       int (*set_id)(struct fdisk_context *cxt);
 
        /* new partition */
        int (*part_add)(struct fdisk_context *cxt,
index efe78a50a57b2a08c307187d42399f776bf7594f..242c83e58e529087ede61c0a496e89e345824df7 100644 (file)
@@ -1791,6 +1791,49 @@ static int gpt_get_disklabel_id(struct fdisk_context *cxt, char **id)
        return 0;
 }
 
+static int gpt_set_disklabel_id(struct fdisk_context *cxt)
+{
+       struct fdisk_gpt_label *gpt;
+       struct gpt_guid uuid;
+       char *str, *old, *new;
+       int rc;
+
+       assert(cxt);
+       assert(cxt->label);
+       assert(fdisk_is_disklabel(cxt, GPT));
+
+       gpt = self_label(cxt);
+       if (fdisk_ask_string(cxt,
+                       _("Enter new disk UUID (in 8-4-4-4-12 format)"), &str))
+               return -EINVAL;
+
+       rc = string_to_guid(str, &uuid);
+       free(str);
+
+       if (rc) {
+               fdisk_warnx(cxt, _("Failed to parse your UUID."));
+               return rc;
+       }
+
+       gpt_get_disklabel_id(cxt, &old);
+
+       gpt->pheader->disk_guid = uuid;
+       gpt->bheader->disk_guid = uuid;
+
+       gpt_recompute_crc(gpt->pheader, gpt->ents);
+       gpt_recompute_crc(gpt->bheader, gpt->ents);
+
+       gpt_get_disklabel_id(cxt, &new);
+
+       fdisk_info(cxt, _("Changing disk identifier from %s to %s."), old, new);
+
+       free(old);
+       free(new);
+       fdisk_label_set_changed(cxt->label, 1);
+       return 0;
+}
+
+
 static struct fdisk_parttype *gpt_get_partition_type(
                struct fdisk_context *cxt,
                size_t i)
@@ -1894,8 +1937,10 @@ int fdisk_gpt_partition_set_uuid(struct fdisk_context *cxt, size_t i)
        rc = string_to_guid(str, &uuid);
        free(str);
 
-       if (rc)
+       if (rc) {
+               fdisk_warnx(cxt, _("Failed to parse your UUID."));
                return rc;
+       }
 
        e = &gpt->ents[i];
 
@@ -1985,6 +2030,7 @@ static const struct fdisk_label_operations gpt_operations =
        .create         = gpt_create_disklabel,
        .list           = gpt_list_disklabel,
        .get_id         = gpt_get_disklabel_id,
+       .set_id         = gpt_set_disklabel_id,
 
        .part_add       = gpt_add_partition,
        .part_delete    = gpt_delete_partition,
index e39a543c8fc755abc9fc13e1ca482a0e293c4941..f7631811dc12a8f9398075dc1221a2f8b8169bb9 100644 (file)
@@ -249,10 +249,27 @@ int fdisk_get_disklabel_id(struct fdisk_context *cxt, char **id)
        if (!cxt->label->op->get_id)
                return -ENOSYS;
 
-       DBG(LABEL, dbgprint("asking for %s ID", cxt->label->name));
+       DBG(LABEL, dbgprint("asking for disk %s ID", cxt->label->name));
        return cxt->label->op->get_id(cxt, id);
 }
 
+/**
+ * fdisk_get_disklabel_id:
+ * @cxt: fdisk context
+ *
+ * Returns 0 on success, otherwise, a corresponding error.
+ */
+int fdisk_set_disklabel_id(struct fdisk_context *cxt)
+{
+       if (!cxt || !cxt->label)
+               return -EINVAL;
+       if (!cxt->label->op->set_id)
+               return -ENOSYS;
+
+       DBG(LABEL, dbgprint("setting %s disk ID", cxt->label->name));
+       return cxt->label->op->set_id(cxt);
+}
+
 /**
  * fdisk_get_partition_type:
  * @cxt: fdisk context
index b88756fd272786b81cbdd8afc7eb60f6b142e75a..1d824177e9a1d6d9aef3b186a2c47767fad56b26 100644 (file)
@@ -125,6 +125,7 @@ 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_set_disklabel_id(struct fdisk_context *cxt);
 
 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);