New API method is introduced - ldm_partition_dm_get_device(...) which
returns device mapper device after it have been created or NULL
otherwise.
ldmtool "show partition" command displays "device" field if
corresponding device mapper device have been created for the specified
partition. Field is omitted otherwise.
e.g. /dev/mapper/ldm_part_Red-nzv8x6obywgDg0_Disk6-01
---
src/ldm.c | 114 ++++++++++++++++++++++++++++---------------------
src/ldm.h | 14 ++++++
src/ldmtool.c | 13 ++++++
test/ldmread.c | 30 +++++++------
4 files changed, 111 insertions(+), 60 deletions(-)
diff --git a/src/ldm.c b/src/ldm.c
index 75f6d5b..20ec1b0 100644
--- a/src/ldm.c
+++ b/src/ldm.c
@@ -2569,6 +2569,60 @@ _dm_find_tree_node_by_uuid(const gchar * const uuid,
return r;
}
+gchar *
+_dm_get_device(const gchar * const uuid, GError ** const err)
+{
+ GString *r = NULL;
+
+ struct dm_tree_node *node = NULL;
+ struct dm_tree *tree = NULL;
+ struct dm_task *task = NULL;
+
+ if (_dm_find_tree_node_by_uuid(uuid, &node, &tree, err)) {
+ const struct dm_info *info = dm_tree_node_get_info(node);
+
+ task = dm_task_create(DM_DEVICE_INFO);
+ if (!task) {
+ g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
+ "dm_task_create: %s", _dm_err_last_msg);
+ goto error;
+ }
+
+ if (!dm_task_set_major(task, info->major)) {
+ g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
+ "DM_DEVICE_INFO: dm_task_set_major(%d) failed: %s",
+ info->major, _dm_err_last_msg);
+ goto error;
+ }
+
+ if (!dm_task_set_minor(task, info->minor)) {
+ g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
+ "DM_DEVICE_INFO: dm_task_set_major(%d) failed: %s",
+ info->minor, _dm_err_last_msg);
+ goto error;
+ }
+
+ if (!dm_task_run(task)) {
+ g_set_error_literal(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
+ _dm_err_last_msg);
+ goto error;
+ }
+
+ const char *dir = dm_dir();
+ char *mangled_name = dm_task_get_name_mangled(task);
+ r = g_string_new("");
+ g_string_printf(r, "%s/%s", dir, mangled_name);
+ dm_free(mangled_name);
+ }
+
+error:
+ if (tree) dm_tree_free(tree);
+ if (task) dm_task_destroy(task);
+
+ /* Really FALSE here - don't free but return the character data. */
+ return r ? g_string_free(r, FALSE) : NULL;
+}
+
gboolean
_dm_create(const gchar * const name, const gchar * const uuid,
uint32_t udev_cookie, const guint n_targets,
@@ -3044,59 +3098,23 @@ ldm_volume_dm_get_name(const LDMVolume * const o)
}
gchar *
-ldm_volume_dm_get_device(const LDMVolume * const o, GError ** const err)
+ldm_partition_dm_get_device(const LDMPartition * const o, GError ** const err)
{
- GString *r = NULL;
-
- GString *uuid = _dm_vol_uuid(o->priv);
- struct dm_tree_node *node = NULL;
- struct dm_tree *tree = NULL;
- struct dm_task *task = NULL;
-
- if (_dm_find_tree_node_by_uuid(uuid->str, &node, &tree, err)) {
- const struct dm_info *info = dm_tree_node_get_info(node);
-
- task = dm_task_create(DM_DEVICE_INFO);
- if (!task) {
- g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
- "dm_task_create: %s", _dm_err_last_msg);
- goto error;
- }
-
- if (!dm_task_set_major(task, info->major)) {
- g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
- "DM_DEVICE_INFO: dm_task_set_major(%d) failed: %s",
- info->major, _dm_err_last_msg);
- goto error;
- }
-
- if (!dm_task_set_minor(task, info->minor)) {
- g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
- "DM_DEVICE_INFO: dm_task_set_major(%d) failed: %s",
- info->minor, _dm_err_last_msg);
- goto error;
- }
-
- if (!dm_task_run(task)) {
- g_set_error_literal(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
- _dm_err_last_msg);
- goto error;
- }
+ GString *uuid = _dm_part_uuid(o->priv);
+ gchar* r = _dm_get_device(uuid->str, err);
+ g_string_free(uuid, TRUE);
- const char *dir = dm_dir();
- char *mangled_name = dm_task_get_name_mangled(task);
- r = g_string_new("");
- g_string_printf(r, "%s/%s", dir, mangled_name);
- dm_free(mangled_name);
- }
+ return r;
+}
-error:
- if (tree) dm_tree_free(tree);
- if (task) dm_task_destroy(task);
+gchar *
+ldm_volume_dm_get_device(const LDMVolume * const o, GError ** const err)
+{
+ GString *uuid = _dm_vol_uuid(o->priv);
+ gchar* r = _dm_get_device(uuid->str, err);
g_string_free(uuid, TRUE);
- /* Really FALSE here - don't free but return the character data. */
- return r ? g_string_free(r, FALSE) : NULL;
+ return r;
}
gboolean
diff --git a/src/ldm.h b/src/ldm.h
index de1552a..fd615b4 100644
--- a/src/ldm.h
+++ b/src/ldm.h
@@ -538,6 +538,20 @@ guint64 ldm_partition_get_start(const LDMPartition *o);
*/
guint64 ldm_partition_get_size(const LDMPartition *o);
+/**
+ * ldm_partition_dm_get_device:
+ * @o: An #LDMPartition
+ * @err: A #GError to receive any generated errors
+ *
+ * Get the host device mapper device which was created for this partition
+ * (e.g. /dev/mapper/ldm_part_Red-nzv8x6obywgDg0_Disk1-01). It is dynamic
+ * runtime property and it will be NULL if device mapper device is absent.
+ *
+ * Returns: (transfer full): The host device mapper device if present,
+ * or NULL otherwise
+ */
+gchar *ldm_partition_dm_get_device(const LDMPartition * const o, GError **err);
+
/**
* ldm_disk_get_name
* @o: An #LDMDisk
diff --git a/src/ldmtool.c b/src/ldmtool.c
index bc58601..6957c1a 100644
--- a/src/ldmtool.c
+++ b/src/ldmtool.c
@@ -376,6 +376,14 @@ show_partition(LDM *const ldm, const gint argc, gchar ** const argv,
gchar *diskname = ldm_disk_get_name(disk);
g_object_unref(disk);
+ GError *err = NULL;
+ gchar *device = ldm_partition_dm_get_device(part, &err);
+ if (err) {
+ g_warning("Unable to get device for partition %s on disk %s:
%s",
+ name, diskname, err->message);
+ g_error_free(err);
+ }
+
json_builder_begin_object(jb);
json_builder_set_member_name(jb, "name");
@@ -386,10 +394,15 @@ show_partition(LDM *const ldm, const gint argc, gchar ** const
argv,
json_builder_add_int_value(jb, size);
json_builder_set_member_name(jb, "disk");
json_builder_add_string_value(jb, diskname);
+ if (device != NULL) {
+ json_builder_set_member_name(jb, "device");
+ json_builder_add_string_value(jb, device);
+ }
json_builder_end_object(jb);
g_free(diskname);
+ g_free(device);
}
g_free(name);
diff --git a/test/ldmread.c b/test/ldmread.c
index fd40670..a01ac67 100644
--- a/test/ldmread.c
+++ b/test/ldmread.c
@@ -61,7 +61,7 @@ int main(int argc, const char *argv[])
g_object_get(dg, "guid", &guid, "name", &name,
NULL);
printf("Disk Group: %s\n", name);
- printf(" GUID: %s\n", guid);
+ printf(" GUID: %s\n", guid);
g_free(guid);
g_free(name);
@@ -93,13 +93,13 @@ int main(int argc, const char *argv[])
gchar *device = ldm_volume_dm_get_device(vol, &err);
printf(" Volume: %s\n", name);
- printf(" GUID: %s\n", guid);
+ printf(" GUID: %s\n", guid);
printf(" Type: %s\n", type_v->value_nick);
printf(" Size: %lu\n", size);
printf(" Part Type: %hhu\n", part_type);
printf(" Hint: %s\n", hint);
printf(" Chunk Size: %lu\n", chunk_size);
- printf(" Device: %s\n", device);
+ printf(" Device: %s\n", device);
g_free(name);
g_free(guid);
@@ -121,11 +121,17 @@ int main(int argc, const char *argv[])
g_object_get(part, "name", &name, "start",
&start,
"size", &size, NULL);
+ GError *err = NULL;
+ gchar *device = ldm_partition_dm_get_device(part, &err);
+
printf(" Partition: %s\n", name);
- printf(" Start: %lu\n", start);
- printf(" Size: %lu\n", size);
+ printf(" Start: %lu\n", start);
+ printf(" Size: %lu\n", size);
+ printf(" Device: %s\n", device);
g_free(name);
+ g_free(device);
+ if (err) g_error_free(err);
}
LDMDisk * const disk = ldm_partition_get_disk(part);
@@ -147,13 +153,13 @@ int main(int argc, const char *argv[])
"metadata-size", &metadata_size,
NULL);
- printf(" Disk: %s\n", name);
- printf(" GUID: %s\n", guid);
- printf(" Device: %s\n", device);
- printf(" Data Start: %lu\n", data_start);
- printf(" Data Size: %lu\n", data_size);
- printf(" Metadata Start: %lu\n", metadata_start);
- printf(" Metadata Size: %lu\n", metadata_size);
+ printf(" Disk: %s\n", name);
+ printf(" GUID: %s\n", guid);
+ printf(" Device: %s\n", device);
+ printf(" Data Start: %lu\n", data_start);
+ printf(" Data Size: %lu\n", data_size);
+ printf(" Metadata Start: %lu\n", metadata_start);
+ printf(" Metadata Size: %lu\n", metadata_size);
g_free(name);
g_free(guid);
--
2.17.0