nodedev: expose internal helper for naming devices

Expose a helper function that can be used by udev and mdevctl to
generate device names for node devices.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Jonathon Jongsma 2020-11-06 12:02:50 -06:00
parent e3107a1862
commit 8fed1d9636
3 changed files with 35 additions and 16 deletions

View File

@ -980,3 +980,29 @@ nodedevRegister(void)
return udevNodeRegister(); return udevNodeRegister();
#endif #endif
} }
void
nodeDeviceGenerateName(virNodeDeviceDef *def,
const char *subsystem,
const char *sysname,
const char *s)
{
size_t i;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
virBufferAsprintf(&buf, "%s_%s",
subsystem,
sysname);
if (s != NULL)
virBufferAsprintf(&buf, "_%s", s);
g_free(def->name);
def->name = virBufferContentAndReset(&buf);
for (i = 0; i < strlen(def->name); i++) {
if (!(g_ascii_isalnum(*(def->name + i))))
*(def->name + i) = '_';
}
}

View File

@ -120,3 +120,9 @@ nodeDeviceGetMdevctlStartCommand(virNodeDeviceDefPtr def,
virCommandPtr virCommandPtr
nodeDeviceGetMdevctlStopCommand(const char *uuid, nodeDeviceGetMdevctlStopCommand(const char *uuid,
char **errmsg); char **errmsg);
void
nodeDeviceGenerateName(virNodeDeviceDef *def,
const char *subsystem,
const char *sysname,
const char *s);

View File

@ -311,22 +311,9 @@ udevGenerateDeviceName(struct udev_device *device,
virNodeDeviceDefPtr def, virNodeDeviceDefPtr def,
const char *s) const char *s)
{ {
size_t i; nodeDeviceGenerateName(def,
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
virBufferAsprintf(&buf, "%s_%s",
udev_device_get_subsystem(device), udev_device_get_subsystem(device),
udev_device_get_sysname(device)); udev_device_get_sysname(device), s);
if (s != NULL)
virBufferAsprintf(&buf, "_%s", s);
def->name = virBufferContentAndReset(&buf);
for (i = 0; i < strlen(def->name); i++) {
if (!(g_ascii_isalnum(*(def->name + i))))
*(def->name + i) = '_';
}
return 0; return 0;
} }