[parted-devel] RFC: Set Partition UUID on GPT
Arvin Schnell
aschnell at suse.de
Mon Nov 9 15:15:50 GMT 2026
Hi all,
appended below is a patch that introduces a command to set the
partition UUID on GPT partition tables (e.g., parted <device> uuid
<number> <uuid>).
Motivation:
- The Discoverable Partitions Specification (DPS): It requires a
calculated UUID for the /var partition [1].
- Reproducible Image Build: Setting explicit partition (and disk)
UUIDs is needed for bit-for-bit reproducibility. If needed I can add
a command "disk_uuid" that as well.
So far another tool like sfdisk is needed for the task.
This is an early RFC to get feedback before writing full test cases
and documentation updates. The implementation is similar to setting
the type uuid.
Kind Regards,
Arvin Schnell
[1] https://uapi-group.org/specifications/specs/discoverable_partitions_specification/
diff --git a/include/parted/disk.in.h b/include/parted/disk.in.h
index e2ee7ca..25854d6 100644
--- a/include/parted/disk.in.h
+++ b/include/parted/disk.in.h
@@ -264,6 +264,7 @@ struct _PedDiskOps {
int (*partition_set_type_uuid) (PedPartition* part, const uint8_t* uuid);
uint8_t* (*partition_get_type_uuid) (const PedPartition* part);
+ int (*partition_set_uuid) (const PedPartition* part, const uint8_t* uuid);
uint8_t* (*partition_get_uuid) (const PedPartition* part);
int (*partition_align) (PedPartition* part,
@@ -375,6 +376,7 @@ extern uint8_t ped_partition_get_type_id (const PedPartition* part);
extern int ped_partition_set_type_uuid (PedPartition* part, const uint8_t* uuid);
extern uint8_t* ped_partition_get_type_uuid (const PedPartition* part);
+extern int ped_partition_set_uuid (PedPartition* part, const uint8_t* uuid);
extern uint8_t* ped_partition_get_uuid (const PedPartition* part);
extern int ped_partition_is_busy (const PedPartition* part);
diff --git a/libparted/disk.c b/libparted/disk.c
index f1da6c4..4a3dcff 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -1663,6 +1663,24 @@ ped_partition_get_type_uuid (const PedPartition *part)
return part->disk->type->ops->partition_get_type_uuid (part);
}
+/**
+ * Set the part-uuid of the partition \p part. This will only work if the disk label
+ * supports it.
+ */
+int
+ped_partition_set_uuid (PedPartition *part, const uint8_t* uuid)
+{
+ PED_ASSERT (part != NULL);
+ PED_ASSERT (part->disk != NULL);
+ PED_ASSERT (ped_partition_is_active (part));
+
+ if (!_assert_partition_uuid_feature (part->disk->type))
+ return 0;
+
+ PED_ASSERT (part->disk->type->ops->partition_set_uuid != NULL);
+ return part->disk->type->ops->partition_set_uuid (part, uuid);
+}
+
/**
* Get the uuid of the partition \p part. This will only work if the disk label
* supports it.
diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 5a48b9b..6f14aee 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -1790,6 +1790,25 @@ gpt_partition_get_type_uuid (const PedPartition *part)
return buf;
}
+
+static int
+gpt_partition_set_uuid (const PedPartition *part, const uint8_t *uuid)
+{
+ GPTPartitionData *gpt_part_data = part->disk_specific;
+
+ efi_guid_t* part_uuid = &gpt_part_data->uuid;
+ memcpy(part_uuid, uuid, sizeof (efi_guid_t));
+
+ /* part_uuid is always LE, while uint8_t is always kind of BE */
+
+ part_uuid->time_low = PED_SWAP32(part_uuid->time_low);
+ part_uuid->time_mid = PED_SWAP16(part_uuid->time_mid);
+ part_uuid->time_hi_and_version = PED_SWAP16(part_uuid->time_hi_and_version);
+
+ return 1;
+}
+
+
static uint8_t*
gpt_partition_get_uuid (const PedPartition *part)
{
@@ -1907,6 +1926,7 @@ static PedDiskOps gpt_disk_ops =
partition_get_type_id: NULL,
partition_set_type_uuid: gpt_partition_set_type_uuid,
partition_get_type_uuid: gpt_partition_get_type_uuid,
+ partition_set_uuid: gpt_partition_set_uuid,
partition_get_uuid: gpt_partition_get_uuid,
disk_set_flag: gpt_disk_set_flag,
disk_get_flag: gpt_disk_get_flag,
diff --git a/parted/parted.c b/parted/parted.c
index f86619f..f6b9a0a 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -177,6 +177,7 @@ static const char* device_msg = N_("DEVICE is usually /dev/hda or /dev/sda
static const char* name_msg = N_("NAME is any word you want\n");
static const char* type_msg = N_("TYPE_ID is a value between 0x01 and 0xff, "
"TYPE_UUID is a UUID\n");
+static const char* uuid_msg = N_("UUID is a UUID\n");
static const char* copyright_msg = N_(
"Copyright (C) 1998 - 2006 Free Software Foundation, Inc.\n"
@@ -1013,6 +1014,57 @@ error:
return 0;
}
+static int
+do_uuid (PedDevice** dev, PedDisk** diskp)
+{
+ if (!*diskp)
+ *diskp = ped_disk_new (*dev);
+ if (!*diskp)
+ goto error;
+
+ if (!ped_disk_type_check_feature((*diskp)->type, PED_DISK_TYPE_PARTITION_UUID)) {
+ ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+ _("%s disk labels do not support partition uuid."),
+ (*diskp)->type->name);
+ goto error;
+ }
+
+ PedPartition* part = NULL;
+ if (!command_line_get_partition (_("Partition number?"), *diskp, &part))
+ goto error;
+
+ uint8_t* part_uuid = ped_partition_get_uuid (part);
+ static char buf[UUID_STR_LEN];
+ uuid_unparse_lower (part_uuid, buf);
+ free (part_uuid);
+
+ char* input = command_line_get_word (_("Partition uuid?"), buf, NULL, 0);
+ if (!input)
+ goto error;
+
+ uuid_t tmp;
+ if (uuid_parse (input, tmp) != 0 || uuid_is_null (tmp)) {
+ ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+ _("Invalid uuid."));
+ goto error_free_input;
+ }
+
+ if (!ped_partition_set_uuid (part, tmp))
+ goto error_free_input;
+
+ free (input);
+
+ if (!ped_disk_commit (*diskp))
+ goto error;
+ return 1;
+
+error_free_input:
+ free (input);
+error:
+ return 0;
+}
+
+
static char*
partition_print_flags (PedPartition const *part)
{
@@ -2452,10 +2504,18 @@ command_register (commands, command_create (
str_list_create_unique ("type", _("type"), NULL),
do_type,
str_list_create (
-_("type NUMBER TYPE-ID or TYPE-UUID type set TYPE-ID or TYPE-UUID of partition NUMBER"),
+_("type NUMBER TYPE-ID or TYPE-UUID set TYPE-ID or TYPE-UUID of partition NUMBER"),
NULL),
str_list_create (_(number_msg), _(type_msg), NULL), 1));
+command_register (commands, command_create (
+ str_list_create_unique ("uuid", _("uuid"), NULL),
+ do_uuid,
+ str_list_create (
+_("uuid NUMBER UUID set UUID of partition NUMBER"),
+NULL),
+ str_list_create (_(number_msg), _(uuid_msg), NULL), 1));
+
command_register (commands, command_create (
str_list_create_unique ("unit", _("unit"), NULL),
do_unit,
--
Arvin Schnell, <aschnell at suse.com>
Senior Software Engineer, Research & Development
SUSE Software Solutions Germany GmbH
Frankenstr. 146
90461 Nürnberg
Germany
Geschäftsführer: Stefan Gaiser, Jochen Jaser, Abhinav Puri
(HRB 36809, AG Nürnberg)
More information about the parted-devel
mailing list