From: Karel Zak Date: Tue, 25 Jun 2013 13:31:21 +0000 (+0200) Subject: libfdisk: add generic function to set disklabel ID X-Git-Tag: v2.24-rc1~156 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=35b1f0a4c4fc151723e2b2880ffb0203b1bdfd84;p=thirdparty%2Futil-linux.git libfdisk: add generic function to set disklabel ID Signed-off-by: Karel Zak --- diff --git a/fdisks/fdisk-menu.c b/fdisks/fdisk-menu.c index fe65d46224..e854f0bc77 100644 --- a/fdisks/fdisk-menu.c +++ b/fdisks/fdisk-menu.c @@ -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; diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index 803eeeed9d..ff581ce74c 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -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); diff --git a/fdisks/fdiskdoslabel.c b/fdisks/fdiskdoslabel.c index 023fbbdb17..84c4a34311 100644 --- a/fdisks/fdiskdoslabel.c +++ b/fdisks/fdiskdoslabel.c @@ -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, diff --git a/fdisks/fdiskdoslabel.h b/fdisks/fdiskdoslabel.h index 3ba71453d0..a0878b2258 100644 --- a/fdisks/fdiskdoslabel.h +++ b/fdisks/fdiskdoslabel.h @@ -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); diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index a24f2e6eff..3832c01237 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -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, diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index efe78a50a5..242c83e58e 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -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, diff --git a/libfdisk/src/label.c b/libfdisk/src/label.c index e39a543c8f..f7631811dc 100644 --- a/libfdisk/src/label.c +++ b/libfdisk/src/label.c @@ -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 diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index b88756fd27..1d824177e9 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -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);