diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index b4dd57e5f4..17a7aea2bb 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -1051,6 +1051,7 @@ nodeDeviceParseMdevctlChildDevice(const char *parent, virJSONValue *props; virJSONValue *attrs; g_autoptr(virNodeDeviceDef) child = g_new0(virNodeDeviceDef, 1); + g_autofree char *parent_sysfs_path = NULL; /* the child object should have a single key equal to its uuid. * The value is an object describing the properties of the mdev */ @@ -1060,7 +1061,25 @@ nodeDeviceParseMdevctlChildDevice(const char *parent, uuid = virJSONValueObjectGetKey(json, 0); props = virJSONValueObjectGetValue(json, 0); - child->parent = g_strdup(parent); + /* Look up id of parent device. mdevctl supports defining mdevs for parent + * devices that are not present on the system (to support starting mdevs on + * hotplug, etc) so the parent may not actually exist. */ + parent_sysfs_path = g_strdup_printf("/sys/class/mdev_bus/%s", parent); + if (virFileExists(parent_sysfs_path)) { + g_autofree char *canon_syspath = virFileCanonicalizePath(parent_sysfs_path); + virNodeDeviceObj *parentobj = NULL; + + if ((parentobj = virNodeDeviceObjListFindBySysfsPath(driver->devs, + canon_syspath))) { + virNodeDeviceDef *parentdef = virNodeDeviceObjGetDef(parentobj); + child->parent = g_strdup(parentdef->name); + virNodeDeviceObjEndAPI(&parentobj); + + child->parent_sysfs_path = g_steal_pointer(&canon_syspath); + } + } + if (!child->parent) + child->parent = g_strdup("computer"); child->caps = g_new0(virNodeDevCapsDef, 1); child->caps->data.type = VIR_NODE_DEV_CAP_MDEV; diff --git a/tests/nodedevmdevctldata/mdevctl-list-multiple.out.xml b/tests/nodedevmdevctldata/mdevctl-list-multiple.out.xml index cf7e966256..eead6f2a40 100644 --- a/tests/nodedevmdevctldata/mdevctl-list-multiple.out.xml +++ b/tests/nodedevmdevctldata/mdevctl-list-multiple.out.xml @@ -1,6 +1,6 @@ mdev_200f228a_c80a_4d50_bfb7_f5a0e4e34045 - 0000:00:02.0 + computer 200f228a-c80a-4d50-bfb7-f5a0e4e34045 @@ -9,7 +9,7 @@ mdev_de807ffc_1923_4d5f_b6c9_b20ecebc6d4b - 0000:00:02.0 + computer de807ffc-1923-4d5f-b6c9-b20ecebc6d4b @@ -18,7 +18,7 @@ mdev_435722ea_5f43_468a_874f_da34f1217f13 - 0000:00:02.0 + computer 435722ea-5f43-468a-874f-da34f1217f13 @@ -28,7 +28,7 @@ mdev_783e6dbb_ea0e_411f_94e2_717eaad438bf - matrix + computer 783e6dbb-ea0e-411f-94e2-717eaad438bf