libfdisk: add generic function to set disklabel ID
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
21fe3dde84
commit
35b1f0a4c4
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue