diff mbox series

[v2,1/1] diskpart_handler: use existing uuid if not explicitely set

Message ID 20211201094326.31072-1-james.hilliard1@gmail.com
State Accepted
Headers show
Series [v2,1/1] diskpart_handler: use existing uuid if not explicitely set | expand

Commit Message

James Hilliard Dec. 1, 2021, 9:43 a.m. UTC
When the ability to specifiy a partition UUID was added it also
starting enforcing the uuid matches in is_diskpart_different,
however in cases where the uuid is not set a new one will be
randomly generated. To ensure this doesn't result in a comparison
of the partition table being marked as changed due to a different
random uuid getting generated, set the UUID based on the existing
partition if it exists in cases where one is not specified in the
sw-description.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 handlers/diskpart_handler.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

Comments

Michael Adler Dec. 1, 2021, 9:54 a.m. UTC | #1
LGTM.

Reviewed-by: Michael Adler <michael.adler@siemens.com>
diff mbox series

Patch

diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
index a48f4d2..9a712bb 100644
--- a/handlers/diskpart_handler.c
+++ b/handlers/diskpart_handler.c
@@ -287,7 +287,8 @@  static int diskpart_get_partitions(struct fdisk_context *cxt, struct diskpart_ta
 static int diskpart_set_partition(struct fdisk_partition *pa,
 				  struct partition_data *part,
 				  unsigned long sector_size,
-				  struct fdisk_parttype *parttype)
+				  struct fdisk_parttype *parttype,
+				  struct fdisk_table *oldtb)
 {
 	int ret;
 
@@ -312,8 +313,21 @@  static int diskpart_set_partition(struct fdisk_partition *pa,
 	if (parttype)
 		ret |= fdisk_partition_set_type(pa, parttype);
 
-	if (strlen(part->partuuid))
-	      ret |= fdisk_partition_set_uuid(pa, part->partuuid);
+	if (strlen(part->partuuid)) {
+		ret |= fdisk_partition_set_uuid(pa, part->partuuid);
+	} else {
+		/*
+		 * If the uuid is not set a random one will be generated, retrieve the
+		 * existing uuid from the on-disk partition if one exists so that we
+		 * don't mark the partition as changed due to a different random uuid.
+		 */
+		struct fdisk_partition *oldpart = fdisk_table_get_partition_by_partno(oldtb, part->partno);
+		if (oldpart) {
+			const char *uuid = fdisk_partition_get_uuid(oldpart);
+			if (uuid)
+				ret |= fdisk_partition_set_uuid(pa, uuid);
+		}
+	}
 
 	return ret;
 }
@@ -496,7 +510,7 @@  static int diskpart_reload_table(struct fdisk_context *cxt, struct fdisk_table *
 }
 
 static int diskpart_fill_table(struct fdisk_context *cxt, struct diskpart_table *tb,
-		struct partition_data *part, struct hnd_priv priv)
+		struct diskpart_table *oldtb, struct partition_data *part, struct hnd_priv priv)
 {
 	struct fdisk_parttype *parttype;
 	struct fdisk_label *lb;
@@ -527,7 +541,7 @@  static int diskpart_fill_table(struct fdisk_context *cxt, struct diskpart_table
 		} else {
 			parttype = fdisk_label_get_parttype_from_code(lb, ustrtoull(part->type, 16));
 		}
-		ret = diskpart_set_partition(newpa, part, sector_size, parttype);
+		ret = diskpart_set_partition(newpa, part, sector_size, parttype, oldtb->parent);
 		if (ret) {
 			WARN("I cannot set all partition's parameters");
 		}
@@ -949,7 +963,7 @@  static int diskpart(struct img_type *img,
 	/*
 	 * Fill the new in-memory partition table from the partition list.
 	 */
-	ret = diskpart_fill_table(cxt, tb, part, priv);
+	ret = diskpart_fill_table(cxt, tb, oldtb, part, priv);
 	if (ret)
 		goto handler_exit;