[parted-devel] [PATCH v2 1/1] direct handling of partition type-id and -uuid
Arvin Schnell
aschnell at suse.com
Wed Apr 27 13:55:49 BST 2022
Include the partition type-id and type-uuid in the JSON
output. Also add the two commands 'type-id' and 'type-uuid' to
set them.
---
NEWS | 3 +
doc/C/parted.8 | 9 +++
doc/parted.texi | 24 +++++++
include/parted/disk.in.h | 23 ++++++-
libparted/disk.c | 102 +++++++++++++++++++++++++++
libparted/labels/dasd.c | 4 ++
libparted/labels/dos.c | 30 +++++++-
libparted/labels/gpt.c | 40 ++++++++++-
parted/parted.c | 140 +++++++++++++++++++++++++++++++++++++-
tests/Makefile.am | 2 +
tests/t0800-json-gpt.sh | 2 +
tests/t0801-json-msdos.sh | 5 +-
tests/t0900-type-uuid.sh | 69 +++++++++++++++++++
tests/t0901-type-id.sh | 69 +++++++++++++++++++
14 files changed, 515 insertions(+), 7 deletions(-)
create mode 100755 tests/t0900-type-uuid.sh
create mode 100755 tests/t0901-type-id.sh
diff --git a/NEWS b/NEWS
index 2bd161f..00171c9 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,9 @@ GNU parted NEWS -*- outline -*-
* Noteworthy changes in release ?.? (????-??-??) [?]
+** New Features
+
+ Add type-id and type-uuid commands.
* Noteworthy changes in release 3.5 (2022-04-18) [stable]
diff --git a/doc/C/parted.8 b/doc/C/parted.8
index 7895440..7eadc31 100644
--- a/doc/C/parted.8
+++ b/doc/C/parted.8
@@ -134,6 +134,15 @@ human-friendly form for output).
.B toggle \fIpartition\fP \fIflag\fP
Toggle the state of \fIflag\fP on \fIpartition\fP.
.TP
+.B type-id \fIpartition\fP \fIid\fP
+Set the type-id aka. partition id of \fIpartition\fP to \fIid\fP. This
+command works only on MS-DOS disklabels. The \fIid\fP is a value
+between "0x01" and "0xff".
+.TP
+.B type-uuid \fIpartition\fP \fIuuid\fP
+Set the type-uuid of \fIpartition\fP to \fIuuid\fP. This command works
+only on GPT disklabels.
+.TP
.B disk_set \fIflag\fP \fIstate\fP
Change a \fIflag\fP on the disk to \fIstate\fP. A flag can be either "on" or "off".
Some or all of these flags will be available, depending on what disk label you
diff --git a/doc/parted.texi b/doc/parted.texi
index 8a3978a..bcf9ee0 100644
--- a/doc/parted.texi
+++ b/doc/parted.texi
@@ -466,6 +466,8 @@ GNU Parted provides the following commands:
* select::
* set::
* toggle::
+* type-id::
+* type-uuid::
* unit::
@end menu
@@ -1034,6 +1036,28 @@ Toggle the state of @var{flag} on partition @var{number}.
@end deffn
+ at node type-id
+ at subsection type-id
+ at cindex type-id, command description
+ at cindex command description, type-id
+
+ at deffn Command type-ud @var{number} @var{id}
+
+Set the type-id aka partition id to @var{id} on partition @var{number}.
+
+ at end deffn
+
+ at node type-uuid
+ at subsection type-uuid
+ at cindex type-uuid, command description
+ at cindex command description, type-uuid
+
+ at deffn Command type-uuid @var{number} @var{uuid}
+
+Set the type-uuid to @var{uuid} on partition @var{number}.
+
+ at end deffn
+
@node unit
@subsection unit
@cindex unit, command description
diff --git a/include/parted/disk.in.h b/include/parted/disk.in.h
index 38e869d..672c4ee 100644
--- a/include/parted/disk.in.h
+++ b/include/parted/disk.in.h
@@ -32,6 +32,7 @@
*/
#include <stdlib.h>
#include <unistd.h>
+#include <stdint.h>
/**
* Disk flags
@@ -91,11 +92,13 @@ enum _PedPartitionFlag {
#define PED_PARTITION_LAST_FLAG PED_PARTITION_LINUX_HOME
enum _PedDiskTypeFeature {
- PED_DISK_TYPE_EXTENDED=1, /**< supports extended partitions */
- PED_DISK_TYPE_PARTITION_NAME=2 /**< supports partition names */
+ PED_DISK_TYPE_EXTENDED=1, /**< supports extended partitions */
+ PED_DISK_TYPE_PARTITION_NAME=2, /**< supports partition names */
+ PED_DISK_TYPE_PARTITION_TYPE_ID=4, /**< supports partition type-ids */
+ PED_DISK_TYPE_PARTITION_TYPE_UUID=8, /**< supports partition type-uuids */
};
#define PED_DISK_TYPE_FIRST_FEATURE PED_DISK_TYPE_EXTENDED
-#define PED_DISK_TYPE_LAST_FEATURE PED_DISK_TYPE_PARTITION_NAME
+#define PED_DISK_TYPE_LAST_FEATURE PED_DISK_TYPE_PARTITION_TYPE_UUID
struct _PedDisk;
struct _PedPartition;
@@ -247,6 +250,13 @@ struct _PedDiskOps {
PedPartitionFlag flag);
void (*partition_set_name) (PedPartition* part, const char* name);
const char* (*partition_get_name) (const PedPartition* part);
+
+ int (*partition_set_type_id) (PedPartition* part, uint8_t id);
+ uint8_t (*partition_get_type_id) (const PedPartition* part);
+
+ int (*partition_set_type_uuid) (PedPartition* part, const uint8_t* uuid);
+ uint8_t* (*partition_get_type_uuid) (const PedPartition* part);
+
int (*partition_align) (PedPartition* part,
const PedConstraint* constraint);
int (*partition_enumerate) (PedPartition* part);
@@ -347,6 +357,13 @@ extern int ped_partition_set_system (PedPartition* part,
const PedFileSystemType* fs_type);
extern int ped_partition_set_name (PedPartition* part, const char* name);
extern const char* ped_partition_get_name (const PedPartition* part);
+
+extern int ped_partition_set_type_id (PedPartition* part, uint8_t id);
+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_is_busy (const PedPartition* part);
extern char* ped_partition_get_path (const PedPartition* part);
diff --git a/libparted/disk.c b/libparted/disk.c
index 3bf7634..22dff36 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -1458,6 +1458,36 @@ _assert_partition_name_feature (const PedDiskType* disk_type)
return 1;
}
+static int
+_assert_partition_type_id_feature (const PedDiskType* disk_type)
+{
+ if (!ped_disk_type_check_feature (
+ disk_type, PED_DISK_TYPE_PARTITION_TYPE_ID)) {
+ ped_exception_throw (
+ PED_EXCEPTION_ERROR,
+ PED_EXCEPTION_CANCEL,
+ "%s disk labels do not support partition type-ids.",
+ disk_type->name);
+ return 0;
+ }
+ return 1;
+}
+
+static int
+_assert_partition_type_uuid_feature (const PedDiskType* disk_type)
+{
+ if (!ped_disk_type_check_feature (
+ disk_type, PED_DISK_TYPE_PARTITION_TYPE_UUID)) {
+ ped_exception_throw (
+ PED_EXCEPTION_ERROR,
+ PED_EXCEPTION_CANCEL,
+ "%s disk labels do not support partition type-uuids.",
+ disk_type->name);
+ return 0;
+ }
+ return 1;
+}
+
/**
* Sets the name of a partition.
*
@@ -1510,6 +1540,78 @@ ped_partition_get_name (const PedPartition* part)
return part->disk->type->ops->partition_get_name (part);
}
+/**
+ * Set the type-id of the partition \p part. This will only work if the disk label
+ * supports it.
+ */
+int
+ped_partition_set_type_id (PedPartition *part, uint8_t id)
+{
+ PED_ASSERT (part != NULL);
+ PED_ASSERT (part->disk != NULL);
+ PED_ASSERT (ped_partition_is_active (part));
+
+ if (!_assert_partition_type_id_feature (part->disk->type))
+ return 0;
+
+ PED_ASSERT (part->disk->type->ops->partition_set_type_id != NULL);
+ return part->disk->type->ops->partition_set_type_id (part, id);
+}
+
+/**
+ * Get the type-id of the partition \p part. This will only work if the disk label
+ * supports it.
+ */
+uint8_t
+ped_partition_get_type_id (const PedPartition *part)
+{
+ PED_ASSERT (part != NULL);
+ PED_ASSERT (part->disk != NULL);
+ PED_ASSERT (ped_partition_is_active (part));
+
+ if (!_assert_partition_type_id_feature (part->disk->type))
+ return 0;
+
+ PED_ASSERT (part->disk->type->ops->partition_set_type_id != NULL);
+ return part->disk->type->ops->partition_get_type_id (part);
+}
+
+/**
+ * Set the type-uuid of the partition \p part. This will only work if the disk label
+ * supports it.
+ */
+int
+ped_partition_set_type_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_type_uuid_feature (part->disk->type))
+ return 0;
+
+ PED_ASSERT (part->disk->type->ops->partition_set_type_uuid != NULL);
+ return part->disk->type->ops->partition_set_type_uuid (part, uuid);
+}
+
+/**
+ * Get the type-uuid of the partition \p part. This will only work if the disk label
+ * supports it.
+ */
+uint8_t*
+ped_partition_get_type_uuid (const PedPartition *part)
+{
+ PED_ASSERT (part != NULL);
+ PED_ASSERT (part->disk != NULL);
+ PED_ASSERT (ped_partition_is_active (part));
+
+ if (!_assert_partition_type_uuid_feature (part->disk->type))
+ return NULL;
+
+ PED_ASSERT (part->disk->type->ops->partition_set_type_uuid != NULL);
+ return part->disk->type->ops->partition_get_type_uuid (part);
+}
+
/** @} */
/**
diff --git a/libparted/labels/dasd.c b/libparted/labels/dasd.c
index 38f2b00..0c00c4f 100644
--- a/libparted/labels/dasd.c
+++ b/libparted/labels/dasd.c
@@ -117,6 +117,10 @@ static PedDiskOps dasd_disk_ops = {
partition_set_name: NULL,
partition_get_name: NULL,
+ partition_set_type_id: NULL,
+ partition_get_type_id: NULL,
+ partition_set_type_uuid: NULL,
+ partition_get_type_uuid: NULL,
get_partition_alignment: dasd_get_partition_alignment,
diff --git a/libparted/labels/dos.c b/libparted/labels/dos.c
index 26d8804..bf8da48 100644
--- a/libparted/labels/dos.c
+++ b/libparted/labels/dos.c
@@ -1754,6 +1754,30 @@ msdos_partition_is_flag_available (const PedPartition* part,
}
}
+
+int
+msdos_partition_set_type_id (PedPartition* part, uint8_t id)
+{
+ DosPartitionData* dos_part_data = part->disk_specific;
+
+ dos_part_data->system = id;
+
+ // TODO set the correct flag but there is no function for that. better cleanup flags
+ // and use system directly (like in gpt.c)
+
+ return 1;
+}
+
+
+uint8_t _GL_ATTRIBUTE_PURE
+msdos_partition_get_type_id (const PedPartition* part)
+{
+ const DosPartitionData* dos_part_data = part->disk_specific;
+
+ return dos_part_data->system;
+}
+
+
static PedGeometry*
_try_constraint (const PedPartition* part, const PedConstraint* external,
PedConstraint* internal)
@@ -2590,6 +2614,10 @@ static PedDiskOps msdos_disk_ops = {
partition_set_name: NULL,
partition_get_name: NULL,
+ partition_set_type_id: msdos_partition_set_type_id,
+ partition_get_type_id: msdos_partition_get_type_id,
+ partition_set_type_uuid: NULL,
+ partition_get_type_uuid: NULL,
PT_op_function_initializers (msdos)
};
@@ -2598,7 +2626,7 @@ static PedDiskType msdos_disk_type = {
next: NULL,
name: "msdos",
ops: &msdos_disk_ops,
- features: PED_DISK_TYPE_EXTENDED
+ features: PED_DISK_TYPE_EXTENDED | PED_DISK_TYPE_PARTITION_TYPE_ID
};
void
diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 037d021..f51b146 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -1686,6 +1686,40 @@ gpt_partition_get_name (const PedPartition *part)
return gpt_part_data->translated_name;
}
+
+static int
+gpt_partition_set_type_uuid (PedPartition *part, const uint8_t *uuid)
+{
+ GPTPartitionData *gpt_part_data = part->disk_specific;
+
+ efi_guid_t* type_uuid = &gpt_part_data->type;
+ memcpy(type_uuid, uuid, sizeof (efi_guid_t));
+
+ type_uuid->time_low = PED_SWAP32(type_uuid->time_low);
+ type_uuid->time_mid = PED_SWAP16(type_uuid->time_mid);
+ type_uuid->time_hi_and_version = PED_SWAP16(type_uuid->time_hi_and_version);
+
+ return 1;
+}
+
+
+static uint8_t*
+gpt_partition_get_type_uuid (const PedPartition *part)
+{
+ const GPTPartitionData *gpt_part_data = part->disk_specific;
+
+ efi_guid_t type_uuid = gpt_part_data->type;
+
+ type_uuid.time_low = PED_SWAP32(type_uuid.time_low);
+ type_uuid.time_mid = PED_SWAP16(type_uuid.time_mid);
+ type_uuid.time_hi_and_version = PED_SWAP16(type_uuid.time_hi_and_version);
+
+ uint8_t *buf = ped_malloc(sizeof (uuid_t));
+ memcpy(buf, &type_uuid, sizeof (uuid_t));
+ return buf;
+}
+
+
static int
gpt_get_max_primary_partition_count (const PedDisk *disk)
{
@@ -1781,6 +1815,10 @@ static PedDiskOps gpt_disk_ops =
partition_set_name: gpt_partition_set_name,
partition_get_name: gpt_partition_get_name,
+ partition_set_type_id: NULL,
+ partition_get_type_id: NULL,
+ partition_set_type_uuid: gpt_partition_set_type_uuid,
+ partition_get_type_uuid: gpt_partition_get_type_uuid,
disk_set_flag: gpt_disk_set_flag,
disk_get_flag: gpt_disk_get_flag,
disk_is_flag_available: gpt_disk_is_flag_available,
@@ -1793,7 +1831,7 @@ static PedDiskType gpt_disk_type =
next: NULL,
name: "gpt",
ops: &gpt_disk_ops,
- features: PED_DISK_TYPE_PARTITION_NAME
+ features: PED_DISK_TYPE_PARTITION_NAME | PED_DISK_TYPE_PARTITION_TYPE_UUID
};
void
diff --git a/parted/parted.c b/parted/parted.c
index 5c7c270..4dc3305 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -19,6 +19,7 @@
#include <config.h>
#include <stdbool.h>
+#include <uuid/uuid.h>
#include "argmatch.h"
#include "closeout.h"
@@ -174,6 +175,8 @@ static const char* end_msg = N_("END is disk location, such as "
static const char* state_msg = N_("STATE is one of: on, off\n");
static const char* device_msg = N_("DEVICE is usually /dev/hda or /dev/sda\n");
static const char* name_msg = N_("NAME is any word you want\n");
+static const char* type_id_msg = N_("TYPE_ID is a value between 0x01 and 0xff\n");
+static const char* type_uuid_msg = N_("TYPE_UUID is a non-null uuid\n");
static const char* copyright_msg = N_(
"Copyright (C) 1998 - 2006 Free Software Foundation, Inc.\n"
@@ -917,6 +920,106 @@ error:
return 0;
}
+static int
+do_type_id (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_TYPE_ID)) {
+ ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+ _("%s disk labels do not support partition type-id."),
+ (*diskp)->type->name);
+ goto error;
+ }
+
+ PedPartition* part = NULL;
+ if (!command_line_get_partition (_("Partition number?"), *diskp, &part))
+ goto error;
+
+ uint8_t type_id = ped_partition_get_type_id (part);
+
+ static char buf[8];
+ snprintf(buf, 8, "0x%02x", type_id);
+
+ char* input = command_line_get_word (_("Partition type-id?"), buf, NULL, 0);
+ if (!input)
+ goto error;
+
+ unsigned int tmp = strtol (input, (char**) NULL, 0);
+ if (tmp == 0 || tmp > 0xff) {
+ ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+ _("Invalid id."));
+ goto error_free_input;
+ }
+
+ if (!ped_partition_set_type_id (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 int
+do_type_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_TYPE_UUID)) {
+ ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+ _("%s disk labels do not support partition type-uuid."),
+ (*diskp)->type->name);
+ goto error;
+ }
+
+ PedPartition* part = NULL;
+ if (!command_line_get_partition (_("Partition number?"), *diskp, &part))
+ goto error;
+
+ uint8_t* type_uuid = ped_partition_get_type_uuid (part);
+ static char buf[UUID_STR_LEN];
+ uuid_unparse_lower (type_uuid, buf);
+ free (type_uuid);
+
+ char* input = command_line_get_word (_("Partition type-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_type_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)
{
@@ -1270,6 +1373,10 @@ do_print (PedDevice** dev, PedDisk** diskp)
PED_DISK_TYPE_EXTENDED);
has_name = ped_disk_type_check_feature ((*diskp)->type,
PED_DISK_TYPE_PARTITION_NAME);
+ bool has_type_id = ped_disk_type_check_feature ((*diskp)->type,
+ PED_DISK_TYPE_PARTITION_TYPE_ID);
+ bool has_type_uuid = ped_disk_type_check_feature ((*diskp)->type,
+ PED_DISK_TYPE_PARTITION_TYPE_UUID);
PedPartition* part;
if (opt_output_mode == HUMAN) {
@@ -1407,10 +1514,25 @@ do_print (PedDevice** dev, PedDisk** diskp)
if (!(part->type & PED_PARTITION_FREESPACE)) {
+ if (has_type_id) {
+ uint8_t type_id = ped_partition_get_type_id (part);
+ static char buf[8];
+ snprintf(buf, 8, "0x%02x", type_id);
+ ul_jsonwrt_value_s (&json, "type-id", buf);
+ }
+
+ if (has_type_uuid) {
+ uint8_t* type_uuid = ped_partition_get_type_uuid (part);
+ static char buf[UUID_STR_LEN];
+ uuid_unparse_lower (type_uuid, buf);
+ ul_jsonwrt_value_s (&json, "type-uuid", buf);
+ free (type_uuid);
+ }
+
if (has_name) {
name = ped_partition_get_name (part);
if (strcmp (name, "") != 0)
- ul_jsonwrt_value_s (&json, "name", ped_partition_get_name (part));
+ ul_jsonwrt_value_s (&json, "name", name);
}
if (part->fs_type)
@@ -2316,6 +2438,22 @@ _("toggle [NUMBER [FLAG]] toggle the state of FLAG on "
NULL),
str_list_create (_(number_msg), flag_msg, NULL), 1));
+command_register (commands, command_create (
+ str_list_create_unique ("type-id", _("type-id"), NULL),
+ do_type_id,
+ str_list_create (
+_("type-id NUMBER TYPE-ID type-id set TYPE-ID of partition NUMBER"),
+NULL),
+ str_list_create (_(number_msg), _(type_id_msg), NULL), 1));
+
+command_register (commands, command_create (
+ str_list_create_unique ("type-uuid", _("type-uuid"), NULL),
+ do_type_uuid,
+ str_list_create (
+_("type-uuid NUMBER TYPE-UUID type-uuid set TYPE-UUID of partition NUMBER"),
+NULL),
+ str_list_create (_(number_msg), _(type_uuid_msg), NULL), 1));
+
command_register (commands, command_create (
str_list_create_unique ("unit", _("unit"), NULL),
do_unit,
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5cb7aa3..b52e791 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -43,6 +43,8 @@ TESTS = \
t0501-duplicate.sh \
t0800-json-gpt.sh \
t0801-json-msdos.sh \
+ t0900-type-uuid.sh \
+ t0901-type-id.sh \
t1100-busy-label.sh \
t1101-busy-partition.sh \
t1102-loop-label.sh \
diff --git a/tests/t0800-json-gpt.sh b/tests/t0800-json-gpt.sh
index 8dd1862..354c0bd 100755
--- a/tests/t0800-json-gpt.sh
+++ b/tests/t0800-json-gpt.sh
@@ -62,6 +62,7 @@ cat <<EOF > exp || fail=1
"end": "20479s",
"size": "10240s",
"type": "primary",
+ "type-uuid": "0fc63daf-8483-4772-8e79-3d69d8477de4",
"name": "test1"
},{
"number": 2,
@@ -69,6 +70,7 @@ cat <<EOF > exp || fail=1
"end": "61439s",
"size": "40960s",
"type": "primary",
+ "type-uuid": "a19d880f-05fc-4d3b-a006-743f0f84911e",
"name": "test2",
"flags": [
"raid"
diff --git a/tests/t0801-json-msdos.sh b/tests/t0801-json-msdos.sh
index a14a5af..c5446d8 100755
--- a/tests/t0801-json-msdos.sh
+++ b/tests/t0801-json-msdos.sh
@@ -52,13 +52,15 @@ cat <<EOF > exp || fail=1
"start": "5.00MiB",
"end": "10.0MiB",
"size": "5.00MiB",
- "type": "primary"
+ "type": "primary",
+ "type-id": "0x83"
},{
"number": 2,
"start": "10.0MiB",
"end": "30.0MiB",
"size": "20.0MiB",
"type": "extended",
+ "type-id": "0x0f",
"flags": [
"lba"
]
@@ -68,6 +70,7 @@ cat <<EOF > exp || fail=1
"end": "20.0MiB",
"size": "10.0MiB",
"type": "logical",
+ "type-id": "0x8e",
"flags": [
"lvm"
]
diff --git a/tests/t0900-type-uuid.sh b/tests/t0900-type-uuid.sh
new file mode 100755
index 0000000..27c99b1
--- /dev/null
+++ b/tests/t0900-type-uuid.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+# Test type-uuid command with GPT label
+
+# Copyright (C) 2022 SUSE LLC
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../parted
+require_512_byte_sector_size_
+
+dev=loop-file
+
+# create device
+truncate --size 50MiB "$dev" || fail=1
+
+# create gpt label and one partition
+parted --script "$dev" mklabel gpt > out 2>&1 || fail=1
+parted --script "$dev" mkpart "''" "linux-swap" 10% 20% > out 2>&1 || fail=1
+
+# set type-uuid
+parted --script "$dev" type-uuid 1 "deadfd6d-a4ab-43c4-84e5-0933c84b4f4f" || fail=1
+
+# print with json format
+parted --script --json "$dev" unit s print > out 2>&1 || fail=1
+
+cat <<EOF > exp || fail=1
+{
+ "disk": {
+ "path": "loop-file",
+ "size": "102400s",
+ "model": "",
+ "transport": "file",
+ "logical-sector-size": 512,
+ "physical-sector-size": 512,
+ "label": "gpt",
+ "max-partitions": 128,
+ "partitions": [
+ {
+ "number": 1,
+ "start": "10240s",
+ "end": "20479s",
+ "size": "10240s",
+ "type": "primary",
+ "type-uuid": "deadfd6d-a4ab-43c4-84e5-0933c84b4f4f"
+ }
+ ]
+ }
+}
+EOF
+
+# remove full path of device from actual output
+mv out o2 && sed "s,\"/.*/$dev\",\"$dev\"," o2 > out || fail=1
+
+# check for expected output
+compare exp out || fail=1
+
+Exit $fail
diff --git a/tests/t0901-type-id.sh b/tests/t0901-type-id.sh
new file mode 100755
index 0000000..37aef3d
--- /dev/null
+++ b/tests/t0901-type-id.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+# Test type-id command with MS-DOS label
+
+# Copyright (C) 2022 SUSE LLC
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../parted
+require_512_byte_sector_size_
+
+dev=loop-file
+
+# create device
+truncate --size 50MiB "$dev" || fail=1
+
+# create msdos label and one partition
+parted --script "$dev" mklabel msdos > out 2>&1 || fail=1
+parted --script "$dev" mkpart primary "linux-swap" 10% 20% > out 2>&1 || fail=1
+
+# set type-id
+parted --script "$dev" type-id 1 "0x83" || fail=1
+
+# print with json format
+parted --script --json "$dev" unit s print > out 2>&1 || fail=1
+
+cat <<EOF > exp || fail=1
+{
+ "disk": {
+ "path": "loop-file",
+ "size": "102400s",
+ "model": "",
+ "transport": "file",
+ "logical-sector-size": 512,
+ "physical-sector-size": 512,
+ "label": "msdos",
+ "max-partitions": 4,
+ "partitions": [
+ {
+ "number": 1,
+ "start": "10240s",
+ "end": "20479s",
+ "size": "10240s",
+ "type": "primary",
+ "type-id": "0x83"
+ }
+ ]
+ }
+}
+EOF
+
+# remove full path of device from actual output
+mv out o2 && sed "s,\"/.*/$dev\",\"$dev\"," o2 > out || fail=1
+
+# check for expected output
+compare exp out || fail=1
+
+Exit $fail
--
2.36.0
More information about the parted-devel
mailing list