Blame SOURCES/0006-libparted-Fix-handling-of-gpt-partition-types.patch

ceb987
From 22ec6553b00b8cc21bf8e78529e1cd0f775ff36f Mon Sep 17 00:00:00 2001
ceb987
From: "Brian C. Lane" <bcl@redhat.com>
ceb987
Date: Mon, 8 Aug 2022 12:04:32 -0700
ceb987
Subject: [PATCH 6/9] libparted: Fix handling of gpt partition types
ceb987
ceb987
This restores the previous behavior by testing the GUID against the list
ceb987
of known types and skipping the filesystem GUID reset. Now the sequence
ceb987
of:
ceb987
ceb987
ped_partition_new(...)
ceb987
ped_partition_set_flag(part, PED_PARTITION_BIOS_GRUB, 1);
ceb987
ped_partition_set_system(part, ped_file_system_type_get("ext4"));
ceb987
ceb987
Will keep the GUID set to PED_PARTITION_BIOS_GRUB, which is how it used
ceb987
to behave.
ceb987
---
ceb987
 libparted/labels/gpt.c | 45 ++++++++++++++++++++++++++++++++++++++++--
ceb987
 1 file changed, 43 insertions(+), 2 deletions(-)
ceb987
ceb987
diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
ceb987
index 0e9e060..8e6a37d 100644
ceb987
--- a/libparted/labels/gpt.c
ceb987
+++ b/libparted/labels/gpt.c
ceb987
@@ -196,6 +196,24 @@ static const struct flag_uuid_mapping_t flag_uuid_mapping[] =
ceb987
     { PED_PARTITION_SWAP,               PARTITION_SWAP_GUID },
ceb987
 };
ceb987
 
ceb987
+static const efi_guid_t skip_set_system_guids[] =
ceb987
+{
ceb987
+    PARTITION_LVM_GUID,
ceb987
+    PARTITION_SWAP_GUID,
ceb987
+    PARTITION_RAID_GUID,
ceb987
+    PARTITION_PREP_GUID,
ceb987
+    PARTITION_SYSTEM_GUID,
ceb987
+    PARTITION_BIOS_GRUB_GUID,
ceb987
+    PARTITION_HPSERVICE_GUID,
ceb987
+    PARTITION_MSFT_RESERVED_GUID,
ceb987
+    PARTITION_BASIC_DATA_GUID,
ceb987
+    PARTITION_MSFT_RECOVERY,
ceb987
+    PARTITION_APPLE_TV_RECOVERY_GUID,
ceb987
+    PARTITION_IRST_GUID,
ceb987
+    PARTITION_CHROMEOS_KERNEL_GUID,
ceb987
+    PARTITION_BLS_BOOT_GUID,
ceb987
+};
ceb987
+
ceb987
 static const struct flag_uuid_mapping_t* _GL_ATTRIBUTE_CONST
ceb987
 gpt_find_flag_uuid_mapping (PedPartitionFlag flag)
ceb987
 {
ceb987
@@ -1421,6 +1439,21 @@ gpt_partition_destroy (PedPartition *part)
ceb987
   _ped_partition_free (part);
ceb987
 }
ceb987
 
ceb987
+/* is_skip_guid checks the guid against the list of guids that should not be
ceb987
+ * overridden by set_system. It returns a 1 if it is in the list.
ceb987
+*/
ceb987
+static bool
ceb987
+is_skip_guid(efi_guid_t guid) {
ceb987
+    int n = sizeof(skip_set_system_guids) / sizeof(skip_set_system_guids[0]);
ceb987
+    for (int i = 0; i < n; ++i) {
ceb987
+        if (guid_cmp(guid, skip_set_system_guids[i]) == 0) {
ceb987
+            return true;
ceb987
+        }
ceb987
+    }
ceb987
+
ceb987
+    return false;
ceb987
+}
ceb987
+
ceb987
 static int
ceb987
 gpt_partition_set_system (PedPartition *part,
ceb987
                           const PedFileSystemType *fs_type)
ceb987
@@ -1431,6 +1464,11 @@ gpt_partition_set_system (PedPartition *part,
ceb987
 
ceb987
   part->fs_type = fs_type;
ceb987
 
ceb987
+  // Is this a GUID that should skip fs_type checking?
ceb987
+  if (is_skip_guid(gpt_part_data->type)) {
ceb987
+      return 1;
ceb987
+  }
ceb987
+
ceb987
   if (fs_type)
ceb987
     {
ceb987
       if (strncmp (fs_type->name, "fat", 3) == 0
ceb987
@@ -1563,10 +1601,13 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
ceb987
   const struct flag_uuid_mapping_t* p = gpt_find_flag_uuid_mapping (flag);
ceb987
   if (p)
ceb987
   {
ceb987
-    if (state)
ceb987
+    if (state) {
ceb987
       gpt_part_data->type = p->type_uuid;
ceb987
-    else if (guid_cmp (gpt_part_data->type, p->type_uuid) == 0)
ceb987
+    } else if (guid_cmp (gpt_part_data->type, p->type_uuid) == 0) {
ceb987
+      // Clear the GUID so that fs_type will be used to return it to the default
ceb987
+      gpt_part_data->type = PARTITION_LINUX_DATA_GUID;
ceb987
       return gpt_partition_set_system (part, part->fs_type);
ceb987
+    }
ceb987
     return 1;
ceb987
   }
ceb987
 
ceb987
-- 
ceb987
2.37.1
ceb987