mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-22 04:25:18 +00:00
virsh: Factor out function to find node device
Several functions accept providing a node device by name or by wwnn,wwpn pair. Extract the logic to do this into a function that can be used by both callers. Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com> Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
parent
bb311cede7
commit
732a5eecbc
@ -111,23 +111,18 @@ static const vshCmdOptDef opts_node_device_destroy[] = {
|
||||
{.name = NULL}
|
||||
};
|
||||
|
||||
static bool
|
||||
cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
|
||||
static virNodeDevice*
|
||||
vshFindNodeDevice(vshControl *ctl, const char *value)
|
||||
{
|
||||
virNodeDevicePtr dev = NULL;
|
||||
bool ret = false;
|
||||
const char *device_value = NULL;
|
||||
char **arr = NULL;
|
||||
int narr;
|
||||
virshControlPtr priv = ctl->privData;
|
||||
|
||||
if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
|
||||
return false;
|
||||
|
||||
if (strchr(device_value, ',')) {
|
||||
narr = vshStringToArray(device_value, &arr);
|
||||
if (strchr(value, ',')) {
|
||||
narr = vshStringToArray(value, &arr);
|
||||
if (narr != 2) {
|
||||
vshError(ctl, _("Malformed device value '%s'"), device_value);
|
||||
vshError(ctl, _("Malformed device value '%s'"), value);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@ -136,14 +131,33 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
|
||||
|
||||
dev = virNodeDeviceLookupSCSIHostByWWN(priv->conn, arr[0], arr[1], 0);
|
||||
} else {
|
||||
dev = virNodeDeviceLookupByName(priv->conn, device_value);
|
||||
dev = virNodeDeviceLookupByName(priv->conn, value);
|
||||
}
|
||||
|
||||
if (!dev) {
|
||||
vshError(ctl, "%s '%s'", _("Could not find matching device"), device_value);
|
||||
vshError(ctl, "%s '%s'", _("Could not find matching device"), value);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
cleanup:
|
||||
g_strfreev(arr);
|
||||
return dev;
|
||||
}
|
||||
|
||||
static bool
|
||||
cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
|
||||
{
|
||||
virNodeDevice *dev = NULL;
|
||||
bool ret = false;
|
||||
const char *device_value = NULL;
|
||||
|
||||
if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
|
||||
return false;
|
||||
|
||||
dev = vshFindNodeDevice(ctl, device_value);
|
||||
if (!dev)
|
||||
goto cleanup;
|
||||
|
||||
if (virNodeDeviceDestroy(dev) == 0) {
|
||||
vshPrintExtra(ctl, _("Destroyed node device '%s'\n"), device_value);
|
||||
} else {
|
||||
@ -153,7 +167,6 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
|
||||
|
||||
ret = true;
|
||||
cleanup:
|
||||
g_strfreev(arr);
|
||||
if (dev)
|
||||
virNodeDeviceFree(dev);
|
||||
return ret;
|
||||
@ -578,33 +591,15 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
|
||||
virNodeDevicePtr device = NULL;
|
||||
char *xml = NULL;
|
||||
const char *device_value = NULL;
|
||||
char **arr = NULL;
|
||||
int narr;
|
||||
bool ret = false;
|
||||
virshControlPtr priv = ctl->privData;
|
||||
|
||||
if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
|
||||
return false;
|
||||
|
||||
if (strchr(device_value, ',')) {
|
||||
narr = vshStringToArray(device_value, &arr);
|
||||
if (narr != 2) {
|
||||
vshError(ctl, _("Malformed device value '%s'"), device_value);
|
||||
goto cleanup;
|
||||
}
|
||||
device = vshFindNodeDevice(ctl, device_value);
|
||||
|
||||
if (!virValidateWWN(arr[0]) || !virValidateWWN(arr[1]))
|
||||
goto cleanup;
|
||||
|
||||
device = virNodeDeviceLookupSCSIHostByWWN(priv->conn, arr[0], arr[1], 0);
|
||||
} else {
|
||||
device = virNodeDeviceLookupByName(priv->conn, device_value);
|
||||
}
|
||||
|
||||
if (!device) {
|
||||
vshError(ctl, "%s '%s'", _("Could not find matching device"), device_value);
|
||||
if (!device)
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (!(xml = virNodeDeviceGetXMLDesc(device, 0)))
|
||||
goto cleanup;
|
||||
@ -613,7 +608,6 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
|
||||
|
||||
ret = true;
|
||||
cleanup:
|
||||
g_strfreev(arr);
|
||||
VIR_FREE(xml);
|
||||
if (device)
|
||||
virNodeDeviceFree(device);
|
||||
|
Loading…
x
Reference in New Issue
Block a user