From 93737ad3518b98c3b42b4373cdd5d50376560f61 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 19 Mar 2021 14:54:34 +0100 Subject: [PATCH] libfdisk: (gpt) returns location of the backup header too Reported-by: Lennart Poettering Signed-off-by: Karel Zak --- libfdisk/src/gpt.c | 12 ++++++++++++ libfdisk/src/label.c | 16 +++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index 7a71ab9da..6a01b1d61 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -1247,6 +1247,18 @@ static int gpt_locate_disklabel(struct fdisk_context *cxt, int n, *offset = (uint64_t) le64_to_cpu(gpt->pheader->partition_entry_lba) * cxt->sector_size; return gpt_sizeof_entries(gpt->pheader, size); + case 3: + *name = _("GPT Backup Entries"); + gpt = self_label(cxt); + *offset = (uint64_t) le64_to_cpu(gpt->bheader->partition_entry_lba) * + cxt->sector_size; + return gpt_sizeof_entries(gpt->bheader, size); + case 4: + *name = _("GPT Backup Header"); + gpt = self_label(cxt); + *offset = (uint64_t) le64_to_cpu(gpt->pheader->alternative_lba) * cxt->sector_size; + *size = sizeof(struct gpt_header); + break; default: return 1; /* no more chunks */ } diff --git a/libfdisk/src/label.c b/libfdisk/src/label.c index 98ab475bf..524a2bfb4 100644 --- a/libfdisk/src/label.c +++ b/libfdisk/src/label.c @@ -393,9 +393,19 @@ int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name) * @offset: return offset where is item * @size: of the item * - * Locate disklabel and returns info about @n item of the label. For example - * GPT is composed from two items, PMBR and GPT, n=0 return offset to PMBR and n=1 - * return offset to GPT. For more details see 'D' expert fdisk command. + * Locate disklabel and returns info about @n item of the label. + * + * For example GPT is composed from three items, PMBR and GPT, n=0 return + * offset to PMBR and n=1 return offset to GPT Header and n=2 returns offset to + * GPT array of partitions, n=3 and n=4 returns location of the backup GPT + * label at the end of the disk. + * + * The function returns the current in-memory situation. It's possible that a + * header location is modified by write operation, for example when enabled + * minimization (see fdisk_gpt_enable_minimize()). In this case it's better to + * call this function after fdisk_write_disklabel(). + * + * For more details see 'D' expert fdisk command. * * Returns: 0 on success, <0 on error, 1 no more items. */