virsh-nodedev.c 21.0 KB
Newer Older
1 2 3
/*
 * virsh-nodedev.c: Commands in node device group
 *
4
 * Copyright (C) 2005, 2007-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
18 19 20 21 22 23 24 25
 * <http://www.gnu.org/licenses/>.
 *
 *  Daniel Veillard <veillard@redhat.com>
 *  Karel Zak <kzak@redhat.com>
 *  Daniel P. Berrange <berrange@redhat.com>
 *
 */

E
Eric Blake 已提交
26 27 28 29
#include <config.h>
#include "virsh-nodedev.h"

#include "internal.h"
30
#include "virbuffer.h"
31
#include "viralloc.h"
32
#include "virfile.h"
33
#include "virstring.h"
34
#include "conf/node_device_conf.h"
E
Eric Blake 已提交
35

36 37 38 39
/*
 * "nodedev-create" command
 */
static const vshCmdInfo info_node_device_create[] = {
40 41 42 43 44 45 46 47 48 49
    {.name = "help",
     .data = N_("create a device defined "
                "by an XML file on the node")
    },
    {.name = "desc",
     .data = N_("Create a device on the node.  Note that this "
                "command creates devices on the physical host "
                "that can then be assigned to a virtual machine.")
    },
    {.name = NULL}
50 51 52
};

static const vshCmdOptDef opts_node_device_create[] = {
53 54 55 56 57 58
    {.name = "file",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("file containing an XML description of the device")
    },
    {.name = NULL}
59 60 61 62 63 64 65 66 67
};

static bool
cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
{
    virNodeDevicePtr dev = NULL;
    const char *from = NULL;
    bool ret = true;
    char *buffer;
68
    virshControlPtr priv = ctl->privData;
69

70
    if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
71 72
        return false;

E
Eric Blake 已提交
73
    if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0)
74 75
        return false;

76
    dev = virNodeDeviceCreateXML(priv->conn, buffer, 0);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    VIR_FREE(buffer);

    if (dev != NULL) {
        vshPrint(ctl, _("Node device %s created from %s\n"),
                 virNodeDeviceGetName(dev), from);
        virNodeDeviceFree(dev);
    } else {
        vshError(ctl, _("Failed to create node device from %s"), from);
        ret = false;
    }

    return ret;
}


/*
 * "nodedev-destroy" command
 */
static const vshCmdInfo info_node_device_destroy[] = {
96 97 98 99 100 101 102 103
    {.name = "help",
     .data = N_("destroy (stop) a device on the node")
    },
    {.name = "desc",
     .data = N_("Destroy a device on the node.  Note that this "
                "command destroys devices on the physical host")
    },
    {.name = NULL}
104 105 106
};

static const vshCmdOptDef opts_node_device_destroy[] = {
107
    {.name = "name",
108 109 110 111
     .type = VSH_OT_ALIAS,
     .help = "device"
    },
    {.name = "device",
112 113
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
114
     .help = N_("device name or wwn pair in 'wwnn,wwpn' format")
115 116
    },
    {.name = NULL}
117 118 119 120 121 122
};

static bool
cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
{
    virNodeDevicePtr dev = NULL;
123 124 125 126
    bool ret = false;
    const char *device_value = NULL;
    char **arr = NULL;
    int narr;
127
    virshControlPtr priv = ctl->privData;
128

129
    if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
130 131
        return false;

132 133 134 135 136 137 138 139 140 141
    if (strchr(device_value, ',')) {
        narr = vshStringToArray(device_value, &arr);
        if (narr != 2) {
            vshError(ctl, _("Malformed device value '%s'"), device_value);
            goto cleanup;
        }

        if (!virValidateWWN(arr[0]) || !virValidateWWN(arr[1]))
            goto cleanup;

142
        dev = virNodeDeviceLookupSCSIHostByWWN(priv->conn, arr[0], arr[1], 0);
143
    } else {
144
        dev = virNodeDeviceLookupByName(priv->conn, device_value);
145 146 147 148 149 150
    }

    if (!dev) {
        vshError(ctl, "%s '%s'", _("Could not find matching device"), device_value);
        goto cleanup;
    }
151 152

    if (virNodeDeviceDestroy(dev) == 0) {
153
        vshPrint(ctl, _("Destroyed node device '%s'\n"), device_value);
154
    } else {
155 156
        vshError(ctl, _("Failed to destroy node device '%s'"), device_value);
        goto cleanup;
157 158
    }

159
    ret = true;
160
 cleanup:
161
    virStringFreeList(arr);
162 163
    if (dev)
        virNodeDeviceFree(dev);
164 165 166
    return ret;
}

167
struct virshNodeList {
168 169 170 171 172
    char **names;
    char **parents;
};

static const char *
173
virshNodeListLookup(int devid, bool parent, void *opaque)
174
{
175
    struct virshNodeList *arrays = opaque;
176 177 178 179 180
    if (parent)
        return arrays->parents[devid];
    return arrays->names[devid];
}

181
static int
182
virshNodeDeviceSorter(const void *a, const void *b)
183 184 185 186 187 188 189 190 191 192 193 194 195 196
{
    virNodeDevicePtr *na = (virNodeDevicePtr *) a;
    virNodeDevicePtr *nb = (virNodeDevicePtr *) b;

    if (*na && !*nb)
        return -1;

    if (!*na)
        return *nb != NULL;

    return vshStrcasecmp(virNodeDeviceGetName(*na),
                         virNodeDeviceGetName(*nb));
}

197
struct virshNodeDeviceList {
198 199 200
    virNodeDevicePtr *devices;
    size_t ndevices;
};
201
typedef struct virshNodeDeviceList *virshNodeDeviceListPtr;
202 203

static void
204
virshNodeDeviceListFree(virshNodeDeviceListPtr list)
205
{
206
    size_t i;
207

208
    if (list && list->devices) {
209 210 211 212 213 214 215 216 217
        for (i = 0; i < list->ndevices; i++) {
            if (list->devices[i])
                virNodeDeviceFree(list->devices[i]);
        }
        VIR_FREE(list->devices);
    }
    VIR_FREE(list);
}

218 219
static virshNodeDeviceListPtr
virshNodeDeviceListCollect(vshControl *ctl,
220 221 222 223
                         char **capnames,
                         int ncapnames,
                         unsigned int flags)
{
224
    virshNodeDeviceListPtr list = vshMalloc(ctl, sizeof(*list));
225
    size_t i;
226 227 228 229 230 231
    int ret;
    virNodeDevicePtr device;
    bool success = false;
    size_t deleted = 0;
    int ndevices = 0;
    char **names = NULL;
232
    virshControlPtr priv = ctl->privData;
233 234

    /* try the list with flags support (0.10.2 and later) */
235
    if ((ret = virConnectListAllNodeDevices(priv->conn,
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
                                            &list->devices,
                                            flags)) >= 0) {
        list->ndevices = ret;
        goto finished;
    }

    /* check if the command is actually supported */
    if (last_error && last_error->code == VIR_ERR_NO_SUPPORT)
        goto fallback;

    /* there was an error during the call */
    vshError(ctl, "%s", _("Failed to list node devices"));
    goto cleanup;


251
 fallback:
252 253 254
    /* fall back to old method (0.10.1 and older) */
    vshResetLibvirtError();

255
    ndevices = virNodeNumOfDevices(priv->conn, NULL, 0);
256 257 258 259 260 261 262 263 264 265
    if (ndevices < 0) {
        vshError(ctl, "%s", _("Failed to count node devices"));
        goto cleanup;
    }

    if (ndevices == 0)
        return list;

    names = vshMalloc(ctl, sizeof(char *) * ndevices);

266
    ndevices = virNodeListDevices(priv->conn, NULL, names, ndevices, 0);
267 268 269 270 271 272 273 274 275
    if (ndevices < 0) {
        vshError(ctl, "%s", _("Failed to list node devices"));
        goto cleanup;
    }

    list->devices = vshMalloc(ctl, sizeof(virNodeDevicePtr) * (ndevices));
    list->ndevices = 0;

    /* get the node devices */
276
    for (i = 0; i < ndevices; i++) {
277
        if (!(device = virNodeDeviceLookupByName(priv->conn, names[i])))
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
            continue;
        list->devices[list->ndevices++] = device;
    }

    /* truncate domains that weren't found */
    deleted = ndevices - list->ndevices;

    if (!capnames)
        goto finished;

    /* filter the list if the list was acquired by fallback means */
    for (i = 0; i < list->ndevices; i++) {
        char **caps = NULL;
        int ncaps = 0;
        bool match = false;

        device = list->devices[i];

        if ((ncaps = virNodeDeviceNumOfCaps(device)) < 0) {
            vshError(ctl, "%s", _("Failed to get capability numbers "
                                  "of the device"));
            goto cleanup;
        }

        caps = vshMalloc(ctl, sizeof(char *) * ncaps);

        if ((ncaps = virNodeDeviceListCaps(device, caps, ncaps)) < 0) {
            vshError(ctl, "%s", _("Failed to get capability names of the device"));
            VIR_FREE(caps);
            goto cleanup;
        }

310
        /* Check if the device's capability matches with provided
311 312
         * capabilities.
         */
313
        size_t j, k;
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        for (j = 0; j < ncaps; j++) {
            for (k = 0; k < ncapnames; k++) {
                if (STREQ(caps[j], capnames[k])) {
                    match = true;
                    break;
                }
            }
        }

        VIR_FREE(caps);

        if (!match)
            goto remove_entry;

        /* the device matched all filters, it may stay */
        continue;

331
 remove_entry:
332 333 334 335 336 337
        /* the device has to be removed as it failed one of the filters */
        virNodeDeviceFree(list->devices[i]);
        list->devices[i] = NULL;
        deleted++;
    }

338
 finished:
339 340 341
    /* sort the list */
    if (list->devices && list->ndevices)
        qsort(list->devices, list->ndevices,
342
              sizeof(*list->devices), virshNodeDeviceSorter);
343 344 345 346 347 348 349

    /* truncate the list if filter simulation deleted entries */
    if (deleted)
        VIR_SHRINK_N(list->devices, list->ndevices, deleted);

    success = true;

350
 cleanup:
351
    for (i = 0; ndevices != -1 && i < ndevices; i++)
352 353 354 355
        VIR_FREE(names[i]);
    VIR_FREE(names);

    if (!success) {
356
        virshNodeDeviceListFree(list);
357 358 359 360 361 362
        list = NULL;
    }

    return list;
}

363 364 365 366
/*
 * "nodedev-list" command
 */
static const vshCmdInfo info_node_list_devices[] = {
367 368 369 370 371 372 373
    {.name = "help",
     .data = N_("enumerate devices on this host")
    },
    {.name = "desc",
     .data = ""
    },
    {.name = NULL}
374 375 376
};

static const vshCmdOptDef opts_node_list_devices[] = {
377 378 379 380 381 382 383 384 385
    {.name = "tree",
     .type = VSH_OT_BOOL,
     .help = N_("list devices in a tree")
    },
    {.name = "cap",
     .type = VSH_OT_STRING,
     .help = N_("capability names, separated by comma")
    },
    {.name = NULL}
386 387 388 389 390
};

static bool
cmdNodeListDevices(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{
391
    const char *cap_str = NULL;
392
    size_t i;
393 394
    bool tree = vshCommandOptBool(cmd, "tree");
    bool ret = true;
395 396 397
    unsigned int flags = 0;
    char **caps = NULL;
    int ncaps = 0;
398
    virshNodeDeviceListPtr list = NULL;
399
    int cap_type = -1;
400

401
    ignore_value(vshCommandOptStringQuiet(ctl, cmd, "cap", &cap_str));
402

403 404 405 406 407
    if (cap_str) {
        if (tree) {
            vshError(ctl, "%s", _("Options --tree and --cap are incompatible"));
            return false;
        }
408 409
        if ((ncaps = vshStringToArray(cap_str, &caps)) < 0)
            return false;
410 411
    }

412 413 414
    for (i = 0; i < ncaps; i++) {
        if ((cap_type = virNodeDevCapTypeFromString(caps[i])) < 0) {
            vshError(ctl, "%s", _("Invalid capability type"));
415 416
            ret = false;
            goto cleanup;
417 418
        }

419
        switch (cap_type) {
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
        case VIR_NODE_DEV_CAP_SYSTEM:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_SYSTEM;
            break;
        case VIR_NODE_DEV_CAP_PCI_DEV:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_PCI_DEV;
            break;
        case VIR_NODE_DEV_CAP_USB_DEV:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_USB_DEV;
            break;
        case VIR_NODE_DEV_CAP_USB_INTERFACE:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_USB_INTERFACE;
            break;
        case VIR_NODE_DEV_CAP_NET:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_NET;
            break;
        case VIR_NODE_DEV_CAP_SCSI_HOST:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI_HOST;
            break;
        case VIR_NODE_DEV_CAP_SCSI_TARGET:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI_TARGET;
            break;
        case VIR_NODE_DEV_CAP_SCSI:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI;
            break;
        case VIR_NODE_DEV_CAP_STORAGE:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_STORAGE;
            break;
447 448 449 450 451 452
        case VIR_NODE_DEV_CAP_FC_HOST:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_FC_HOST;
            break;
        case VIR_NODE_DEV_CAP_VPORTS:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_VPORTS;
            break;
453 454 455
        case VIR_NODE_DEV_CAP_SCSI_GENERIC:
            flags |= VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI_GENERIC;
            break;
456 457 458
        default:
            break;
        }
459
    }
460

461
    if (!(list = virshNodeDeviceListCollect(ctl, caps, ncaps, flags))) {
462 463 464 465
        ret = false;
        goto cleanup;
    }

466
    if (tree) {
467 468
        char **parents = vshMalloc(ctl, sizeof(char *) * list->ndevices);
        char **names = vshMalloc(ctl, sizeof(char *) * list->ndevices);
469
        struct virshNodeList arrays = { names, parents };
470 471 472

        for (i = 0; i < list->ndevices; i++)
            names[i] = vshStrdup(ctl, virNodeDeviceGetName(list->devices[i]));
473

474 475 476
        for (i = 0; i < list->ndevices; i++) {
            virNodeDevicePtr dev = list->devices[i];
            if (STRNEQ(names[i], "computer")) {
477 478 479 480 481 482
                const char *parent = virNodeDeviceGetParent(dev);
                parents[i] = parent ? vshStrdup(ctl, parent) : NULL;
            } else {
                parents[i] = NULL;
            }
        }
483

484
        for (i = 0; i < list->ndevices; i++) {
485
            if (parents[i] == NULL &&
486
                vshTreePrint(ctl, virshNodeListLookup, &arrays,
487
                             list->ndevices, i) < 0)
488 489
                ret = false;
        }
490

491
        for (i = 0; i < list->ndevices; i++)
492 493
            VIR_FREE(parents[i]);
        VIR_FREE(parents);
494 495 496
        for (i = 0; i < list->ndevices; i++)
            VIR_FREE(names[i]);
        VIR_FREE(names);
497
    } else {
498 499 500 501
        for (i = 0; i < list->ndevices; i++)
            vshPrint(ctl, "%s\n", virNodeDeviceGetName(list->devices[i]));
    }

502
 cleanup:
503
    virStringFreeList(caps);
504
    virshNodeDeviceListFree(list);
505 506 507 508 509 510 511
    return ret;
}

/*
 * "nodedev-dumpxml" command
 */
static const vshCmdInfo info_node_device_dumpxml[] = {
512 513 514 515 516 517 518
    {.name = "help",
     .data = N_("node device details in XML")
    },
    {.name = "desc",
     .data = N_("Output the node device details as an XML dump to stdout.")
    },
    {.name = NULL}
519 520 521 522
};


static const vshCmdOptDef opts_node_device_dumpxml[] = {
523 524 525
    {.name = "device",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
526
     .help = N_("device name or wwn pair in 'wwnn,wwpn' format"),
527 528
    },
    {.name = NULL}
529 530 531 532 533
};

static bool
cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
{
534 535
    virNodeDevicePtr device = NULL;
    char *xml = NULL;
536 537 538
    const char *device_value = NULL;
    char **arr = NULL;
    int narr;
539
    bool ret = false;
540
    virshControlPtr priv = ctl->privData;
541

542 543
    if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
         return false;
544

545 546 547 548 549 550 551 552 553 554
    if (strchr(device_value, ',')) {
        narr = vshStringToArray(device_value, &arr);
        if (narr != 2) {
            vshError(ctl, _("Malformed device value '%s'"), device_value);
            goto cleanup;
        }

        if (!virValidateWWN(arr[0]) || !virValidateWWN(arr[1]))
            goto cleanup;

555
        device = virNodeDeviceLookupSCSIHostByWWN(priv->conn, arr[0], arr[1], 0);
556
    } else {
557
        device = virNodeDeviceLookupByName(priv->conn, device_value);
558 559 560 561 562
    }

    if (!device) {
        vshError(ctl, "%s '%s'", _("Could not find matching device"), device_value);
        goto cleanup;
563 564
    }

565 566
    if (!(xml = virNodeDeviceGetXMLDesc(device, 0)))
        goto cleanup;
567 568

    vshPrint(ctl, "%s\n", xml);
569

570
    ret = true;
571
 cleanup:
572
    virStringFreeList(arr);
573
    VIR_FREE(xml);
574 575
    if (device)
        virNodeDeviceFree(device);
576
    return ret;
577 578 579 580 581 582
}

/*
 * "nodedev-detach" command
 */
static const vshCmdInfo info_node_device_detach[] = {
583 584 585 586 587 588 589
    {.name = "help",
     .data = N_("detach node device from its device driver")
    },
    {.name = "desc",
     .data = N_("Detach node device from its device driver before assigning to a domain.")
    },
    {.name = NULL}
590 591 592 593
};


static const vshCmdOptDef opts_node_device_detach[] = {
594 595 596 597 598
    {.name = "device",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("device key")
    },
599 600
    {.name = "driver",
     .type = VSH_OT_STRING,
601
     .help = N_("pci device assignment backend driver (e.g. 'vfio' or 'kvm')")
602
    },
603
    {.name = NULL}
604 605 606 607 608 609
};

static bool
cmdNodeDeviceDetach(vshControl *ctl, const vshCmd *cmd)
{
    const char *name = NULL;
610
    const char *driverName = NULL;
611 612
    virNodeDevicePtr device;
    bool ret = true;
613
    virshControlPtr priv = ctl->privData;
614

615
    if (vshCommandOptStringReq(ctl, cmd, "device", &name) < 0)
616
        return false;
617

618
    ignore_value(vshCommandOptStringQuiet(ctl, cmd, "driver", &driverName));
619

620
    if (!(device = virNodeDeviceLookupByName(priv->conn, name))) {
621
        vshError(ctl, _("Could not find matching device '%s'"), name);
622 623 624
        return false;
    }

625 626 627 628
    if (driverName) {
        /* we must use the newer API that accepts a driverName */
        if (virNodeDeviceDetachFlags(device, driverName, 0) < 0)
            ret = false;
629
    } else {
630 631 632 633
        /* Yes, our (old) public API is misspelled.  At least virsh
         * can accept either spelling.  */
        if (virNodeDeviceDettach(device) < 0)
            ret = false;
634
    }
635

636 637 638 639 640
    if (ret)
        vshPrint(ctl, _("Device %s detached\n"), name);
    else
        vshError(ctl, _("Failed to detach device %s"), name);

641 642 643 644 645 646 647 648
    virNodeDeviceFree(device);
    return ret;
}

/*
 * "nodedev-reattach" command
 */
static const vshCmdInfo info_node_device_reattach[] = {
649 650 651 652 653 654 655
    {.name = "help",
     .data = N_("reattach node device to its device driver")
    },
    {.name = "desc",
     .data = N_("Reattach node device to its device driver once released by the domain.")
    },
    {.name = NULL}
656 657 658 659
};


static const vshCmdOptDef opts_node_device_reattach[] = {
660 661 662 663 664 665
    {.name = "device",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("device key")
    },
    {.name = NULL}
666 667 668 669 670 671 672 673
};

static bool
cmdNodeDeviceReAttach(vshControl *ctl, const vshCmd *cmd)
{
    const char *name = NULL;
    virNodeDevicePtr device;
    bool ret = true;
674
    virshControlPtr priv = ctl->privData;
675

676
    if (vshCommandOptStringReq(ctl, cmd, "device", &name) < 0)
677
        return false;
678

679
    if (!(device = virNodeDeviceLookupByName(priv->conn, name))) {
680
        vshError(ctl, _("Could not find matching device '%s'"), name);
681 682 683 684 685 686 687 688 689
        return false;
    }

    if (virNodeDeviceReAttach(device) == 0) {
        vshPrint(ctl, _("Device %s re-attached\n"), name);
    } else {
        vshError(ctl, _("Failed to re-attach device %s"), name);
        ret = false;
    }
690

691 692 693 694 695 696 697 698
    virNodeDeviceFree(device);
    return ret;
}

/*
 * "nodedev-reset" command
 */
static const vshCmdInfo info_node_device_reset[] = {
699 700 701 702 703 704 705
    {.name = "help",
     .data = N_("reset node device")
    },
    {.name = "desc",
     .data = N_("Reset node device before or after assigning to a domain.")
    },
    {.name = NULL}
706 707 708 709
};


static const vshCmdOptDef opts_node_device_reset[] = {
710 711 712 713 714 715
    {.name = "device",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("device key")
    },
    {.name = NULL}
716 717 718 719 720 721 722 723
};

static bool
cmdNodeDeviceReset(vshControl *ctl, const vshCmd *cmd)
{
    const char *name = NULL;
    virNodeDevicePtr device;
    bool ret = true;
724
    virshControlPtr priv = ctl->privData;
725

726
    if (vshCommandOptStringReq(ctl, cmd, "device", &name) < 0)
727
        return false;
728

729
    if (!(device = virNodeDeviceLookupByName(priv->conn, name))) {
730
        vshError(ctl, _("Could not find matching device '%s'"), name);
731 732 733 734 735 736 737 738 739
        return false;
    }

    if (virNodeDeviceReset(device) == 0) {
        vshPrint(ctl, _("Device %s reset\n"), name);
    } else {
        vshError(ctl, _("Failed to reset device %s"), name);
        ret = false;
    }
740

741 742 743
    virNodeDeviceFree(device);
    return ret;
}
744

E
Eric Blake 已提交
745
const vshCmdDef nodedevCmds[] = {
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    {.name = "nodedev-create",
     .handler = cmdNodeDeviceCreate,
     .opts = opts_node_device_create,
     .info = info_node_device_create,
     .flags = 0
    },
    {.name = "nodedev-destroy",
     .handler = cmdNodeDeviceDestroy,
     .opts = opts_node_device_destroy,
     .info = info_node_device_destroy,
     .flags = 0
    },
    {.name = "nodedev-detach",
     .handler = cmdNodeDeviceDetach,
     .opts = opts_node_device_detach,
     .info = info_node_device_detach,
     .flags = 0
    },
    {.name = "nodedev-dettach",
     .handler = cmdNodeDeviceDetach,
     .opts = opts_node_device_detach,
     .info = info_node_device_detach,
     .flags = VSH_CMD_FLAG_ALIAS
    },
    {.name = "nodedev-dumpxml",
     .handler = cmdNodeDeviceDumpXML,
     .opts = opts_node_device_dumpxml,
     .info = info_node_device_dumpxml,
     .flags = 0
    },
    {.name = "nodedev-list",
     .handler = cmdNodeListDevices,
     .opts = opts_node_list_devices,
     .info = info_node_list_devices,
     .flags = 0
    },
    {.name = "nodedev-reattach",
     .handler = cmdNodeDeviceReAttach,
     .opts = opts_node_device_reattach,
     .info = info_node_device_reattach,
     .flags = 0
    },
    {.name = "nodedev-reset",
     .handler = cmdNodeDeviceReset,
     .opts = opts_node_device_reset,
     .info = info_node_device_reset,
     .flags = 0
    },
    {.name = NULL}
795
};