1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

virsh: Use virNodeDeviceLookupSCSIHostByWWN

Only nodedev-destroy and nodedev-dumpxml can benifit from the
new API, other commands like nodedev-detach only works for
PCI devices, WWN makes no sense for them.
This commit is contained in:
Osier Yang 2013-02-04 22:16:44 +08:00
parent fb2e465362
commit 9be2018469
2 changed files with 78 additions and 23 deletions

View File

@ -101,9 +101,14 @@ static const vshCmdInfo info_node_device_destroy[] = {
static const vshCmdOptDef opts_node_device_destroy[] = { static const vshCmdOptDef opts_node_device_destroy[] = {
{.name = "name", {.name = "name",
.type = VSH_OT_ALIAS,
.flags = 0,
.help = "device"
},
{.name = "device",
.type = VSH_OT_DATA, .type = VSH_OT_DATA,
.flags = VSH_OFLAG_REQ, .flags = VSH_OFLAG_REQ,
.help = N_("name of the device to be destroyed") .help = N_("device name or wwn pair in 'wwnn,wwpn' format")
}, },
{.name = NULL} {.name = NULL}
}; };
@ -112,21 +117,47 @@ static bool
cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd) cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
{ {
virNodeDevicePtr dev = NULL; virNodeDevicePtr dev = NULL;
bool ret = true; bool ret = false;
const char *name = NULL; const char *device_value = NULL;
char **arr = NULL;
int narr;
if (vshCommandOptStringReq(ctl, cmd, "name", &name) < 0) if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
return false; return false;
dev = virNodeDeviceLookupByName(ctl->conn, name); if (strchr(device_value, ',')) {
narr = vshStringToArray(device_value, &arr);
if (narr != 2) {
vshError(ctl, _("Malformed device value '%s'"), device_value);
goto cleanup;
}
if (virNodeDeviceDestroy(dev) == 0) { if (!virValidateWWN(arr[0]) || !virValidateWWN(arr[1]))
vshPrint(ctl, _("Destroyed node device '%s'\n"), name); goto cleanup;
dev = virNodeDeviceLookupSCSIHostByWWN(ctl->conn, arr[0], arr[1], 0);
} else { } else {
vshError(ctl, _("Failed to destroy node device '%s'"), name); dev = virNodeDeviceLookupByName(ctl->conn, device_value);
ret = false;
} }
if (!dev) {
vshError(ctl, "%s '%s'", _("Could not find matching device"), device_value);
goto cleanup;
}
if (virNodeDeviceDestroy(dev) == 0) {
vshPrint(ctl, _("Destroyed node device '%s'\n"), device_value);
} else {
vshError(ctl, _("Failed to destroy node device '%s'"), device_value);
goto cleanup;
}
ret = true;
cleanup:
if (arr) {
VIR_FREE(*arr);
VIR_FREE(arr);
}
virNodeDeviceFree(dev); virNodeDeviceFree(dev);
return ret; return ret;
} }
@ -476,7 +507,7 @@ static const vshCmdOptDef opts_node_device_dumpxml[] = {
{.name = "device", {.name = "device",
.type = VSH_OT_DATA, .type = VSH_OT_DATA,
.flags = VSH_OFLAG_REQ, .flags = VSH_OFLAG_REQ,
.help = N_("device key") .help = N_("device name or wwn pair in 'wwnn,wwpn' format"),
}, },
{.name = NULL} {.name = NULL}
}; };
@ -484,26 +515,47 @@ static const vshCmdOptDef opts_node_device_dumpxml[] = {
static bool static bool
cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd) cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
{ {
const char *name = NULL;
virNodeDevicePtr device = NULL; virNodeDevicePtr device = NULL;
char *xml = NULL; char *xml = NULL;
const char *device_value = NULL;
char **arr = NULL;
int narr;
bool ret = false; bool ret = false;
if (vshCommandOptStringReq(ctl, cmd, "device", &name) < 0) if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
return false; return false;
if (!(device = virNodeDeviceLookupByName(ctl->conn, name))) { if (strchr(device_value, ',')) {
vshError(ctl, _("Could not find matching device '%s'"), name); narr = vshStringToArray(device_value, &arr);
return false; if (narr != 2) {
vshError(ctl, _("Malformed device value '%s'"), device_value);
goto cleanup;
}
if (!virValidateWWN(arr[0]) || !virValidateWWN(arr[1]))
goto cleanup;
device = virNodeDeviceLookupSCSIHostByWWN(ctl->conn, arr[0], arr[1], 0);
} else {
device = virNodeDeviceLookupByName(ctl->conn, device_value);
}
if (!device) {
vshError(ctl, "%s '%s'", _("Could not find matching device"), device_value);
goto cleanup;
} }
if (!(xml = virNodeDeviceGetXMLDesc(device, 0))) if (!(xml = virNodeDeviceGetXMLDesc(device, 0)))
goto cleanup; goto cleanup;
vshPrint(ctl, "%s\n", xml); vshPrint(ctl, "%s\n", xml);
ret = true;
ret = true;
cleanup: cleanup:
if (arr) {
VIR_FREE(*arr);
VIR_FREE(arr);
}
VIR_FREE(xml); VIR_FREE(xml);
virNodeDeviceFree(device); virNodeDeviceFree(device);
return ret; return ret;

View File

@ -1953,11 +1953,12 @@ host nodes are available for use, but this allows registration of
host hardware that libvirt did not automatically detect. I<file> host hardware that libvirt did not automatically detect. I<file>
contains xml for a top-level <device> description of a node device. contains xml for a top-level <device> description of a node device.
=item B<nodedev-destroy> I<nodedev> =item B<nodedev-destroy> I<device>
Destroy (stop) a device on the host. Note that this makes libvirt Destroy (stop) a device on the host. I<device> can be either device
quit managing a host device, and may even make that device unusable name or wwn pair in "wwnn,wwpn" format (only works for HBA). Note
by the rest of the physical host until a reboot. that this makes libvirt quit managing a host device, and may even make
that device unusable by the rest of the physical host until a reboot.
=item B<nodedev-detach> I<nodedev> =item B<nodedev-detach> I<nodedev>
@ -1967,12 +1968,14 @@ B<nodedev-reattach>, and is done automatically for managed devices.
For compatibility purposes, this command can also be spelled For compatibility purposes, this command can also be spelled
B<nodedev-dettach>. B<nodedev-dettach>.
=item B<nodedev-dumpxml> I<nodedev> =item B<nodedev-dumpxml> I<device>
Dump a <device> XML representation for the given node device, including Dump a <device> XML representation for the given node device, including
such information as the device name, which bus owns the device, the such information as the device name, which bus owns the device, the
vendor and product id, and any capabilities of the device usable by vendor and product id, and any capabilities of the device usable by
libvirt (such as whether device reset is supported). libvirt (such as whether device reset is supported). I<device> can
be either device name or wwn pair in "wwnn,wwpn" format (only works
for HBA).
=item B<nodedev-list> I<cap> I<--tree> =item B<nodedev-list> I<cap> I<--tree>