Skip to content

Commit

Permalink
Merge pull request #1045 from vojtechtrefny/master_part-type-string
Browse files Browse the repository at this point in the history
part: Add human readable partition type to BDPartSpec
  • Loading branch information
vojtechtrefny authored Jul 19, 2024
2 parents 0a441b5 + e4022cb commit e648ae9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/lib/plugin_apis/part.api
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ typedef struct BDPartSpec {
guint64 size;
gboolean bootable;
guint64 attrs;
gchar *type_name;
} BDPartSpec;

BDPartSpec* bd_part_spec_copy (BDPartSpec *data) {
Expand All @@ -85,6 +86,7 @@ BDPartSpec* bd_part_spec_copy (BDPartSpec *data) {
ret->uuid = g_strdup (data->uuid);
ret->id = g_strdup (data->id);
ret->type_guid = g_strdup (data->type_guid);
ret->type_name = g_strdup (data->type_name);
ret->type = data->type;
ret->start = data->start;
ret->size = data->size;
Expand All @@ -103,6 +105,7 @@ void bd_part_spec_free (BDPartSpec *data) {
g_free (data->uuid);
g_free (data->id);
g_free (data->type_guid);
g_free (data->type_name);
g_free (data);
}

Expand Down
20 changes: 18 additions & 2 deletions src/plugins/part.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ BDPartSpec* bd_part_spec_copy (BDPartSpec *data) {
ret->id = g_strdup (data->id);
ret->uuid = g_strdup (data->uuid);
ret->type_guid = g_strdup (data->type_guid);
ret->type_name = g_strdup (data->type_name);
ret->type = data->type;
ret->start = data->start;
ret->size = data->size;
Expand All @@ -79,6 +80,7 @@ void bd_part_spec_free (BDPartSpec *data) {
g_free (data->uuid);
g_free (data->id);
g_free (data->type_guid);
g_free (data->type_name);
g_free (data);
}

Expand Down Expand Up @@ -395,7 +397,7 @@ gboolean bd_part_create_table (const gchar *disk, BDPartTableType type, gboolean
return TRUE;
}

static gchar* get_part_type_guid_and_gpt_flags (const gchar *device, int part_num, guint64 *attrs, GError **error) {
static gchar* get_part_type_guid_and_gpt_flags (const gchar *device, int part_num, guint64 *attrs, char **type_name, GError **error) {
struct fdisk_context *cxt = NULL;
struct fdisk_label *lb = NULL;
struct fdisk_partition *pa = NULL;
Expand Down Expand Up @@ -457,6 +459,19 @@ static gchar* get_part_type_guid_and_gpt_flags (const gchar *device, int part_nu
return NULL;
}

/* part type name -- human readable string, e.g. "Microsoft Reserved Partition" */
ptype_string = fdisk_parttype_get_name (ptype);
if (!ptype_string) {
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL,
"Failed to get partition type string for partition %d on device '%s'", part_num, device);
fdisk_unref_partition (pa);
close_context (cxt);
return NULL;
}

*type_name = g_strdup (ptype_string);

/* part type string -- GUID as a string */
ptype_string = fdisk_parttype_get_string (ptype);
if (!ptype_string) {
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL,
Expand Down Expand Up @@ -528,7 +543,8 @@ static BDPartSpec* get_part_spec_fdisk (struct fdisk_context *cxt, struct fdisk_
if (g_strcmp0 (fdisk_label_get_name (lb), "gpt") == 0) {
if (ret->type == BD_PART_TYPE_NORMAL) {
/* only 'normal' partitions have GUIDs */
ret->type_guid = get_part_type_guid_and_gpt_flags (devname, fdisk_partition_get_partno (pa) + 1, &(ret->attrs), &l_error);
ret->type_guid = get_part_type_guid_and_gpt_flags (devname, fdisk_partition_get_partno (pa) + 1,
&(ret->attrs), &(ret->type_name), &l_error);
if (!ret->type_guid && l_error) {
g_propagate_error (error, l_error);
bd_part_spec_free (ret);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/part.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ typedef struct BDPartSpec {
guint64 size;
gboolean bootable;
guint64 attrs;
gchar *type_name;
} BDPartSpec;

BDPartSpec* bd_part_spec_copy (BDPartSpec *data);
Expand Down
3 changes: 3 additions & 0 deletions tests/part_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,11 +1227,13 @@ def test_set_part_type(self):
self.assertTrue(succ)
ps = BlockDev.part_get_part_spec (self.loop_dev, ps.path)
self.assertEqual(ps.type_guid, "E6D6D379-F507-44C2-A23C-238F2A3DF928")
self.assertEqual(ps.type_name, "Linux LVM")

succ = BlockDev.part_set_part_type (self.loop_dev, ps.path, "0FC63DAF-8483-4772-8E79-3D69D8477DE4")
self.assertTrue(succ)
ps = BlockDev.part_get_part_spec (self.loop_dev, ps.path)
self.assertEqual(ps.type_guid, "0FC63DAF-8483-4772-8E79-3D69D8477DE4")
self.assertEqual(ps.type_name, "Linux filesystem")

# let's now test an MSDOS partition table (doesn't support type GUIDs)
succ = BlockDev.part_create_table (self.loop_dev, BlockDev.PartTableType.MSDOS, True)
Expand Down Expand Up @@ -1322,6 +1324,7 @@ def test_set_part_type(self):
self.assertTrue(succ)
ps = BlockDev.part_get_part_spec (self.loop_dev, ps.path)
self.assertEqual(ps.type_guid, esp_guid)
self.assertEqual(ps.type_name, "EFI System")


class PartSetGptAttrsCase(PartTestCase):
Expand Down

0 comments on commit e648ae9

Please sign in to comment.