qemu_hotplug.c 77.9 KB
Newer Older
1 2 3
/*
 * qemu_hotplug.h: QEMU device hotplug management
 *
4
 * Copyright (C) 2006-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */


#include <config.h>

#include "qemu_hotplug.h"
#include "qemu_capabilities.h"
#include "qemu_domain.h"
#include "qemu_command.h"
#include "qemu_bridge_filter.h"
#include "qemu_hostdev.h"
33
#include "domain_audit.h"
34 35 36 37 38
#include "domain_nwfilter.h"
#include "logging.h"
#include "virterror_internal.h"
#include "memory.h"
#include "pci.h"
E
Eric Blake 已提交
39
#include "virfile.h"
40
#include "qemu_cgroup.h"
41
#include "locking/domain_lock.h"
42
#include "network/bridge_driver.h"
43 44 45 46 47 48 49 50 51 52 53 54

#define VIR_FROM_THIS VIR_FROM_QEMU

int qemuDomainChangeEjectableMedia(struct qemud_driver *driver,
                                   virDomainObjPtr vm,
                                   virDomainDiskDefPtr disk,
                                   bool force)
{
    virDomainDiskDefPtr origdisk = NULL;
    int i;
    int ret;
    char *driveAlias = NULL;
55
    qemuDomainObjPrivatePtr priv = vm->privateData;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (vm->def->disks[i]->bus == disk->bus &&
            STREQ(vm->def->disks[i]->dst, disk->dst)) {
            origdisk = vm->def->disks[i];
            break;
        }
    }

    if (!origdisk) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("No device with bus '%s' and target '%s'"),
                        virDomainDiskBusTypeToString(disk->bus),
                        disk->dst);
        return -1;
    }

    if (!origdisk->info.alias) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("missing disk device alias name for %s"), origdisk->dst);
        return -1;
    }

    if (origdisk->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY &&
        origdisk->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Removable media not supported for %s device"),
                        virDomainDiskDeviceTypeToString(disk->device));
        return -1;
    }

87 88 89
    if (virDomainLockDiskAttach(driver->lockManager, vm, disk) < 0)
        return -1;

90
    if (virSecurityManagerSetImageLabel(driver->securityManager,
91
                                        vm->def, disk) < 0) {
92 93
        if (virDomainLockDiskDetach(driver->lockManager, vm, disk) < 0)
            VIR_WARN("Unable to release lock on %s", disk->src);
94
        return -1;
95
    }
96

97
    if (!(driveAlias = qemuDeviceDriveHostAlias(origdisk, priv->qemuCaps)))
98 99
        goto error;

100
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    if (disk->src) {
        const char *format = NULL;
        if (disk->type != VIR_DOMAIN_DISK_TYPE_DIR) {
            if (disk->driverType)
                format = disk->driverType;
            else if (origdisk->driverType)
                format = origdisk->driverType;
        }
        ret = qemuMonitorChangeMedia(priv->mon,
                                     driveAlias,
                                     disk->src, format);
    } else {
        ret = qemuMonitorEjectMedia(priv->mon, driveAlias, force);
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);

117
    virDomainAuditDisk(vm, origdisk->src, disk->src, "update", ret >= 0);
118 119 120 121

    if (ret < 0)
        goto error;

122
    if (virSecurityManagerRestoreImageLabel(driver->securityManager,
123
                                            vm->def, origdisk) < 0)
124 125
        VIR_WARN("Unable to restore security label on ejected image %s", origdisk->src);

126 127 128
    if (virDomainLockDiskDetach(driver->lockManager, vm, origdisk) < 0)
        VIR_WARN("Unable to release lock on disk %s", origdisk->src);

129 130 131 132 133 134 135 136 137 138 139 140 141
    VIR_FREE(origdisk->src);
    origdisk->src = disk->src;
    disk->src = NULL;
    origdisk->type = disk->type;

    VIR_FREE(driveAlias);

    virDomainDiskDefFree(disk);

    return ret;

error:
    VIR_FREE(driveAlias);
142

143
    if (virSecurityManagerRestoreImageLabel(driver->securityManager,
144
                                            vm->def, disk) < 0)
145
        VIR_WARN("Unable to restore security label on new media %s", disk->src);
146 147 148 149

    if (virDomainLockDiskDetach(driver->lockManager, vm, disk) < 0)
        VIR_WARN("Unable to release lock on %s", disk->src);

150 151 152
    return -1;
}

153 154 155 156 157
int
qemuDomainCheckEjectableMedia(struct qemud_driver *driver,
                             virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
158
    virHashTablePtr table;
159 160 161
    int ret = -1;
    int i;

162 163 164 165 166 167 168
    qemuDomainObjEnterMonitor(driver, vm);
    table = qemuMonitorGetBlockInfo(priv->mon);
    qemuDomainObjExitMonitor(driver, vm);

    if (!table)
        goto cleanup;

169 170
    for (i = 0; i < vm->def->ndisks; i++) {
        virDomainDiskDefPtr disk = vm->def->disks[i];
171
        struct qemuDomainDiskInfo *info;
172

173 174
        if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK ||
            disk->device == VIR_DOMAIN_DISK_DEVICE_LUN) {
175
                 continue;
176
        }
177

178 179
        info = qemuMonitorBlockInfoLookup(table, disk->info.alias);
        if (!info)
180 181
            goto cleanup;

182
        if (info->tray_open && disk->src)
183 184 185 186 187 188
            VIR_FREE(disk->src);
    }

    ret = 0;

cleanup:
189
    virHashFree(table);
190 191 192
    return ret;
}

193

194 195
int qemuDomainAttachPciDiskDevice(virConnectPtr conn,
                                  struct qemud_driver *driver,
196
                                  virDomainObjPtr vm,
197
                                  virDomainDiskDefPtr disk)
198 199 200 201 202 203
{
    int i, ret;
    const char* type = virDomainDiskBusTypeToString(disk->bus);
    qemuDomainObjPrivatePtr priv = vm->privateData;
    char *devstr = NULL;
    char *drivestr = NULL;
204
    bool releaseaddr = false;
205 206 207 208 209 210 211 212 213

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (STREQ(vm->def->disks[i]->dst, disk->dst)) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            _("target %s already exists"), disk->dst);
            return -1;
        }
    }

214 215 216
    if (virDomainLockDiskAttach(driver->lockManager, vm, disk) < 0)
        return -1;

217
    if (virSecurityManagerSetImageLabel(driver->securityManager,
218
                                        vm->def, disk) < 0) {
219 220
        if (virDomainLockDiskDetach(driver->lockManager, vm, disk) < 0)
            VIR_WARN("Unable to release lock on %s", disk->src);
221
        return -1;
222
    }
223

224
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
225 226
        if (qemuDomainPCIAddressEnsureAddr(priv->pciaddrs, &disk->info) < 0)
            goto error;
227
        releaseaddr = true;
228
        if (qemuAssignDeviceDiskAlias(disk, priv->qemuCaps) < 0)
229 230
            goto error;

231
        if (!(drivestr = qemuBuildDriveStr(conn, disk, false, priv->qemuCaps)))
232 233
            goto error;

234
        if (!(devstr = qemuBuildDriveDevStr(disk, 0, priv->qemuCaps)))
235 236 237 238 239 240 241 242
            goto error;
    }

    if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
        virReportOOMError();
        goto error;
    }

243
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
244
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
245 246 247 248 249 250 251 252 253 254 255
        ret = qemuMonitorAddDrive(priv->mon, drivestr);
        if (ret == 0) {
            ret = qemuMonitorAddDevice(priv->mon, devstr);
            if (ret < 0) {
                VIR_WARN("qemuMonitorAddDevice failed on %s (%s)",
                         drivestr, devstr);
                /* XXX should call 'drive_del' on error but this does not
                   exist yet */
            }
        }
    } else {
256
        virDomainDevicePCIAddress guestAddr = disk->info.addr.pci;
257 258 259 260 261 262 263 264 265 266 267
        ret = qemuMonitorAddPCIDisk(priv->mon,
                                    disk->src,
                                    type,
                                    &guestAddr);
        if (ret == 0) {
            disk->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
            memcpy(&disk->info.addr.pci, &guestAddr, sizeof(guestAddr));
        }
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);

268
    virDomainAuditDisk(vm, NULL, disk->src, "attach", ret >= 0);
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

    if (ret < 0)
        goto error;

    virDomainDiskInsertPreAlloced(vm->def, disk);

    VIR_FREE(devstr);
    VIR_FREE(drivestr);

    return 0;

error:
    VIR_FREE(devstr);
    VIR_FREE(drivestr);

284
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE) &&
285
        (disk->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) &&
286
        releaseaddr &&
287 288
        qemuDomainPCIAddressReleaseSlot(priv->pciaddrs,
                                        disk->info.addr.pci.slot) < 0)
289 290
        VIR_WARN("Unable to release PCI address on %s", disk->src);

291
    if (virSecurityManagerRestoreImageLabel(driver->securityManager,
292
                                            vm->def, disk) < 0)
293 294
        VIR_WARN("Unable to restore security label on %s", disk->src);

295 296 297
    if (virDomainLockDiskDetach(driver->lockManager, vm, disk) < 0)
        VIR_WARN("Unable to release lock on %s", disk->src);

298 299 300 301 302 303
    return -1;
}


int qemuDomainAttachPciControllerDevice(struct qemud_driver *driver,
                                        virDomainObjPtr vm,
304
                                        virDomainControllerDefPtr controller)
305 306 307 308 309 310
{
    int i;
    int ret = -1;
    const char* type = virDomainControllerTypeToString(controller->type);
    char *devstr = NULL;
    qemuDomainObjPrivatePtr priv = vm->privateData;
311
    bool releaseaddr = false;
312 313 314 315 316 317 318 319 320 321 322

    for (i = 0 ; i < vm->def->ncontrollers ; i++) {
        if ((vm->def->controllers[i]->type == controller->type) &&
            (vm->def->controllers[i]->idx == controller->idx)) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            _("target %s:%d already exists"),
                            type, controller->idx);
            return -1;
        }
    }

323
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
324 325
        if (qemuDomainPCIAddressEnsureAddr(priv->pciaddrs, &controller->info) < 0)
            goto cleanup;
326
        releaseaddr = true;
327 328 329
        if (qemuAssignDeviceControllerAlias(controller) < 0)
            goto cleanup;

330 331 332 333 334 335 336 337
        if (controller->type == VIR_DOMAIN_CONTROLLER_TYPE_USB &&
            controller->model == -1 &&
            !qemuCapsGet(priv->qemuCaps, QEMU_CAPS_PIIX3_USB_UHCI)) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            _("USB controller hotplug unsupported in this QEMU binary"));
            goto cleanup;
        }

338
        if (!(devstr = qemuBuildControllerDevStr(vm->def, controller, priv->qemuCaps, NULL))) {
339 340 341 342 343 344 345 346 347
            goto cleanup;
        }
    }

    if (VIR_REALLOC_N(vm->def->controllers, vm->def->ncontrollers+1) < 0) {
        virReportOOMError();
        goto cleanup;
    }

348
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
349
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
        ret = qemuMonitorAddDevice(priv->mon, devstr);
    } else {
        ret = qemuMonitorAttachPCIDiskController(priv->mon,
                                                 type,
                                                 &controller->info.addr.pci);
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);

    if (ret == 0) {
        controller->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
        virDomainControllerInsertPreAlloced(vm->def, controller);
    }

cleanup:
    if ((ret != 0) &&
365
        qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE) &&
366
        (controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) &&
367
        releaseaddr &&
368 369
        qemuDomainPCIAddressReleaseSlot(priv->pciaddrs,
                                        controller->info.addr.pci.slot) < 0)
370
        VIR_WARN("Unable to release PCI address on controller");
371 372 373 374 375 376 377 378 379

    VIR_FREE(devstr);
    return ret;
}


static virDomainControllerDefPtr
qemuDomainFindOrCreateSCSIDiskController(struct qemud_driver *driver,
                                         virDomainObjPtr vm,
380
                                         int controller)
381 382 383
{
    int i;
    virDomainControllerDefPtr cont;
384

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    for (i = 0 ; i < vm->def->ncontrollers ; i++) {
        cont = vm->def->controllers[i];

        if (cont->type != VIR_DOMAIN_CONTROLLER_TYPE_SCSI)
            continue;

        if (cont->idx == controller)
            return cont;
    }

    /* No SCSI controller present, for backward compatibility we
     * now hotplug a controller */
    if (VIR_ALLOC(cont) < 0) {
        virReportOOMError();
        return NULL;
    }
    cont->type = VIR_DOMAIN_CONTROLLER_TYPE_SCSI;
402
    cont->idx = controller;
403 404
    cont->model = -1;

405
    VIR_INFO("No SCSI controller present, hotplugging one");
406
    if (qemuDomainAttachPciControllerDevice(driver,
407
                                            vm, cont) < 0) {
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
        VIR_FREE(cont);
        return NULL;
    }

    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("guest unexpectedly quit"));
        /* cont doesn't need freeing here, since the reference
         * now held in def->controllers */
        return NULL;
    }

    return cont;
}


424 425
int qemuDomainAttachSCSIDisk(virConnectPtr conn,
                             struct qemud_driver *driver,
426
                             virDomainObjPtr vm,
427
                             virDomainDiskDefPtr disk)
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
{
    int i;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virDomainControllerDefPtr cont = NULL;
    char *drivestr = NULL;
    char *devstr = NULL;
    int ret = -1;

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (STREQ(vm->def->disks[i]->dst, disk->dst)) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            _("target %s already exists"), disk->dst);
            return -1;
        }
    }

444 445
    if (virDomainLockDiskAttach(driver->lockManager, vm, disk) < 0)
        return -1;
446

447
    if (virSecurityManagerSetImageLabel(driver->securityManager,
448
                                        vm->def, disk) < 0) {
449 450
        if (virDomainLockDiskDetach(driver->lockManager, vm, disk) < 0)
            VIR_WARN("Unable to release lock on %s", disk->src);
451
        return -1;
452
    }
453 454 455 456 457 458 459 460 461

    /* We should have an address already, so make sure */
    if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("unexpected disk address type %s"),
                        virDomainDeviceAddressTypeToString(disk->info.type));
        goto error;
    }

462 463
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
        if (qemuAssignDeviceDiskAlias(disk, priv->qemuCaps) < 0)
464
            goto error;
465
        if (!(devstr = qemuBuildDriveDevStr(disk, 0, priv->qemuCaps)))
466 467 468
            goto error;
    }

469
    if (!(drivestr = qemuBuildDriveStr(conn, disk, false, priv->qemuCaps)))
470 471 472
        goto error;

    for (i = 0 ; i <= disk->info.addr.drive.controller ; i++) {
473
        cont = qemuDomainFindOrCreateSCSIDiskController(driver, vm, i);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
        if (!cont)
            goto error;
    }

    /* Tell clang that "cont" is non-NULL.
       This is because disk->info.addr.driver.controller is unsigned,
       and hence the above loop must iterate at least once.  */
    sa_assert (cont);

    if (cont->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("SCSI controller %d was missing its PCI address"), cont->idx);
        goto error;
    }

    if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
        virReportOOMError();
        goto error;
    }

494
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
495
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
        ret = qemuMonitorAddDrive(priv->mon, drivestr);
        if (ret == 0) {
            ret = qemuMonitorAddDevice(priv->mon, devstr);
            if (ret < 0) {
                VIR_WARN("qemuMonitorAddDevice failed on %s (%s)",
                         drivestr, devstr);
                /* XXX should call 'drive_del' on error but this does not
                   exist yet */
            }
        }
    } else {
        virDomainDeviceDriveAddress driveAddr;
        ret = qemuMonitorAttachDrive(priv->mon,
                                     drivestr,
                                     &cont->info.addr.pci,
                                     &driveAddr);
        if (ret == 0) {
            /* XXX we should probably validate that the addr matches
             * our existing defined addr instead of overwriting */
            disk->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE;
516 517
            disk->info.addr.drive.bus = driveAddr.bus;
            disk->info.addr.drive.unit = driveAddr.unit;
518 519 520 521
        }
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);

522
    virDomainAuditDisk(vm, NULL, disk->src, "attach", ret >= 0);
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

    if (ret < 0)
        goto error;

    virDomainDiskInsertPreAlloced(vm->def, disk);

    VIR_FREE(devstr);
    VIR_FREE(drivestr);

    return 0;

error:
    VIR_FREE(devstr);
    VIR_FREE(drivestr);

538
    if (virSecurityManagerRestoreImageLabel(driver->securityManager,
539
                                            vm->def, disk) < 0)
540 541
        VIR_WARN("Unable to restore security label on %s", disk->src);

542 543 544
    if (virDomainLockDiskDetach(driver->lockManager, vm, disk) < 0)
        VIR_WARN("Unable to release lock on %s", disk->src);

545 546 547 548
    return -1;
}


549 550
int qemuDomainAttachUsbMassstorageDevice(virConnectPtr conn,
                                         struct qemud_driver *driver,
551
                                         virDomainObjPtr vm,
552
                                         virDomainDiskDefPtr disk)
553 554 555 556 557 558 559 560 561 562 563 564 565 566
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int i, ret;
    char *drivestr = NULL;
    char *devstr = NULL;

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (STREQ(vm->def->disks[i]->dst, disk->dst)) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            _("target %s already exists"), disk->dst);
            return -1;
        }
    }

567 568 569
    if (virDomainLockDiskAttach(driver->lockManager, vm, disk) < 0)
        return -1;

570
    if (virSecurityManagerSetImageLabel(driver->securityManager,
571
                                        vm->def, disk) < 0) {
572 573
        if (virDomainLockDiskDetach(driver->lockManager, vm, disk) < 0)
            VIR_WARN("Unable to release lock on %s", disk->src);
574
        return -1;
575
    }
576

577
    /* XXX not correct once we allow attaching a USB CDROM */
578 579 580 581 582 583
    if (!disk->src) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("disk source path is missing"));
        goto error;
    }

584 585
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
        if (qemuAssignDeviceDiskAlias(disk, priv->qemuCaps) < 0)
586
            goto error;
587
        if (!(drivestr = qemuBuildDriveStr(conn, disk, false, priv->qemuCaps)))
588
            goto error;
589
        if (!(devstr = qemuBuildDriveDevStr(disk, 0, priv->qemuCaps)))
590 591 592 593 594 595 596 597
            goto error;
    }

    if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
        virReportOOMError();
        goto error;
    }

598
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
599
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
        ret = qemuMonitorAddDrive(priv->mon, drivestr);
        if (ret == 0) {
            ret = qemuMonitorAddDevice(priv->mon, devstr);
            if (ret < 0) {
                VIR_WARN("qemuMonitorAddDevice failed on %s (%s)",
                         drivestr, devstr);
                /* XXX should call 'drive_del' on error but this does not
                   exist yet */
            }
        }
    } else {
        ret = qemuMonitorAddUSBDisk(priv->mon, disk->src);
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);

615
    virDomainAuditDisk(vm, NULL, disk->src, "attach", ret >= 0);
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

    if (ret < 0)
        goto error;

    virDomainDiskInsertPreAlloced(vm->def, disk);

    VIR_FREE(devstr);
    VIR_FREE(drivestr);

    return 0;

error:
    VIR_FREE(devstr);
    VIR_FREE(drivestr);

631
    if (virSecurityManagerRestoreImageLabel(driver->securityManager,
632
                                            vm->def, disk) < 0)
633 634
        VIR_WARN("Unable to restore security label on %s", disk->src);

635 636 637
    if (virDomainLockDiskDetach(driver->lockManager, vm, disk) < 0)
        VIR_WARN("Unable to release lock on %s", disk->src);

638 639 640 641 642 643 644 645
    return -1;
}


/* XXX conn required for network -> bridge resolution */
int qemuDomainAttachNetDevice(virConnectPtr conn,
                              struct qemud_driver *driver,
                              virDomainObjPtr vm,
646
                              virDomainNetDefPtr net)
647 648 649 650
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    char *tapfd_name = NULL;
    int tapfd = -1;
651 652
    char *vhostfd_name = NULL;
    int vhostfd = -1;
653 654 655 656 657
    char *nicstr = NULL;
    char *netstr = NULL;
    int ret = -1;
    virDomainDevicePCIAddress guestAddr;
    int vlan;
658
    bool releaseaddr = false;
659 660
    bool iface_connected = false;
    int actualType;
661

662
    if (!qemuCapsGet(priv->qemuCaps, QEMU_CAPS_HOST_NET_ADD)) {
663 664 665 666 667
        qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                        _("installed qemu version does not support host_net_add"));
        return -1;
    }

668 669 670 671 672 673 674 675
    /* If appropriate, grab a physical device from the configured
     * network's pool of devices, or resolve bridge device name
     * to the one defined in the network definition.
     */
    if (networkAllocateActualDevice(net) < 0)
        goto cleanup;

    actualType = virDomainNetGetActualType(net);
676 677
    if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE ||
        actualType == VIR_DOMAIN_NET_TYPE_NETWORK) {
678
        if ((tapfd = qemuNetworkIfaceConnect(vm->def, conn, driver, net,
679
                                             priv->qemuCaps)) < 0)
680 681
            goto cleanup;
        iface_connected = true;
682
        if (qemuOpenVhostNet(vm->def, net, priv->qemuCaps, &vhostfd) < 0)
683
            goto cleanup;
684
    } else if (actualType == VIR_DOMAIN_NET_TYPE_DIRECT) {
685
        if ((tapfd = qemuPhysIfaceConnect(vm->def, driver, net,
686
                                          priv->qemuCaps,
687
                                          VIR_NETDEV_VPORT_PROFILE_OP_CREATE)) < 0)
688 689
            goto cleanup;
        iface_connected = true;
690
        if (qemuOpenVhostNet(vm->def, net, priv->qemuCaps, &vhostfd) < 0)
691
            goto cleanup;
692 693 694 695 696
    }

    if (VIR_REALLOC_N(vm->def->nets, vm->def->nnets+1) < 0)
        goto no_memory;

697 698
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_NET_NAME) ||
        qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
699 700 701 702
        if (qemuAssignDeviceNetAlias(vm->def, net, -1) < 0)
            goto cleanup;
    }

703
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE) &&
704 705 706
        qemuDomainPCIAddressEnsureAddr(priv->pciaddrs, &net->info) < 0)
        goto cleanup;

707 708
    releaseaddr = true;

709 710
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_NETDEV) &&
        qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
        vlan = -1;
    } else {
        vlan = qemuDomainNetVLAN(net);

        if (vlan < 0) {
            qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                            _("Unable to attach network devices without vlan"));
            goto cleanup;
        }
    }

    if (tapfd != -1) {
        if (virAsprintf(&tapfd_name, "fd-%s", net->info.alias) < 0)
            goto no_memory;
    }

727 728 729 730 731
    if (vhostfd != -1) {
        if (virAsprintf(&vhostfd_name, "vhostfd-%s", net->info.alias) < 0)
            goto no_memory;
    }

732 733
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_NETDEV) &&
        qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
734
        if (!(netstr = qemuBuildHostNetStr(net, ',',
735
                                           -1, tapfd_name, vhostfd_name)))
736
            goto cleanup;
737 738
    } else {
        if (!(netstr = qemuBuildHostNetStr(net, ' ',
739
                                           vlan, tapfd_name, vhostfd_name)))
740
            goto cleanup;
741 742
    }

743
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
744 745
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_NETDEV) &&
        qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
746 747
        if (qemuMonitorAddNetdev(priv->mon, netstr, tapfd, tapfd_name,
                                 vhostfd, vhostfd_name) < 0) {
748
            qemuDomainObjExitMonitorWithDriver(driver, vm);
749
            virDomainAuditNet(vm, NULL, net, "attach", false);
750
            goto cleanup;
751 752
        }
    } else {
753 754
        if (qemuMonitorAddHostNetwork(priv->mon, netstr, tapfd, tapfd_name,
                                      vhostfd, vhostfd_name) < 0) {
755
            qemuDomainObjExitMonitorWithDriver(driver, vm);
756
            virDomainAuditNet(vm, NULL, net, "attach", false);
757
            goto cleanup;
758 759 760 761 762
        }
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);

    VIR_FORCE_CLOSE(tapfd);
763
    VIR_FORCE_CLOSE(vhostfd);
764 765 766 767 768 769 770

    if (!virDomainObjIsActive(vm)) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("guest unexpectedly quit"));
        goto cleanup;
    }

771
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
772
        if (!(nicstr = qemuBuildNicDevStr(net, vlan, 0, priv->qemuCaps)))
773 774 775 776 777 778
            goto try_remove;
    } else {
        if (!(nicstr = qemuBuildNicStr(net, NULL, vlan)))
            goto try_remove;
    }

779
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
780
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
781 782
        if (qemuMonitorAddDevice(priv->mon, nicstr) < 0) {
            qemuDomainObjExitMonitorWithDriver(driver, vm);
783
            virDomainAuditNet(vm, NULL, net, "attach", false);
784 785 786
            goto try_remove;
        }
    } else {
787
        guestAddr = net->info.addr.pci;
788 789 790
        if (qemuMonitorAddPCINetwork(priv->mon, nicstr,
                                     &guestAddr) < 0) {
            qemuDomainObjExitMonitorWithDriver(driver, vm);
791
            virDomainAuditNet(vm, NULL, net, "attach", false);
792 793 794 795 796 797 798
            goto try_remove;
        }
        net->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
        memcpy(&net->info.addr.pci, &guestAddr, sizeof(guestAddr));
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    /* set link state */
    if (net->linkstate == VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN) {
        if (!net->info.alias) {
            qemuReportError(VIR_ERR_OPERATION_FAILED,
                            _("device alias not found: cannot set link state to down"));
        } else {
            qemuDomainObjEnterMonitorWithDriver(driver, vm);

            if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_NETDEV)) {
                if (qemuMonitorSetLink(priv->mon, net->info.alias, VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN) < 0) {
                    qemuDomainObjExitMonitorWithDriver(driver, vm);
                    virDomainAuditNet(vm, NULL, net, "attach", false);
                    goto try_remove;
                }
            } else {
                qemuReportError(VIR_ERR_OPERATION_FAILED,
                                _("setting of link state not supported: Link is up"));
            }

            qemuDomainObjExitMonitorWithDriver(driver, vm);
        }
        /* link set to down */
    }

823
    virDomainAuditNet(vm, NULL, net, "attach", true);
824 825 826 827 828 829

    ret = 0;

    vm->def->nets[vm->def->nnets++] = net;

cleanup:
830 831 832 833 834 835 836 837 838 839
    if (ret < 0) {
        if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE) &&
            (net->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) &&
            releaseaddr &&
            qemuDomainPCIAddressReleaseSlot(priv->pciaddrs,
                                            net->info.addr.pci.slot) < 0)
            VIR_WARN("Unable to release PCI address on NIC");

        if (iface_connected)
            virDomainConfNWFilterTeardown(net);
840

841 842
        networkReleaseActualDevice(net);
    }
843 844 845 846 847

    VIR_FREE(nicstr);
    VIR_FREE(netstr);
    VIR_FREE(tapfd_name);
    VIR_FORCE_CLOSE(tapfd);
848 849
    VIR_FREE(vhostfd_name);
    VIR_FORCE_CLOSE(vhostfd);
850 851 852 853 854 855 856 857

    return ret;

try_remove:
    if (!virDomainObjIsActive(vm))
        goto cleanup;

    if (vlan < 0) {
858 859
        if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_NETDEV) &&
            qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
860 861 862
            char *netdev_name;
            if (virAsprintf(&netdev_name, "host%s", net->info.alias) < 0)
                goto no_memory;
863
            qemuDomainObjEnterMonitorWithDriver(driver, vm);
864 865 866 867 868 869
            if (qemuMonitorRemoveNetdev(priv->mon, netdev_name) < 0)
                VIR_WARN("Failed to remove network backend for netdev %s",
                         netdev_name);
            qemuDomainObjExitMonitorWithDriver(driver, vm);
            VIR_FREE(netdev_name);
        } else {
870
            VIR_WARN("Unable to remove network backend");
871 872 873 874 875
        }
    } else {
        char *hostnet_name;
        if (virAsprintf(&hostnet_name, "host%s", net->info.alias) < 0)
            goto no_memory;
876
        qemuDomainObjEnterMonitorWithDriver(driver, vm);
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
        if (qemuMonitorRemoveHostNetwork(priv->mon, vlan, hostnet_name) < 0)
            VIR_WARN("Failed to remove network backend for vlan %d, net %s",
                     vlan, hostnet_name);
        qemuDomainObjExitMonitorWithDriver(driver, vm);
        VIR_FREE(hostnet_name);
    }
    goto cleanup;

no_memory:
    virReportOOMError();
    goto cleanup;
}


int qemuDomainAttachHostPciDevice(struct qemud_driver *driver,
                                  virDomainObjPtr vm,
893
                                  virDomainHostdevDefPtr hostdev)
894 895 896 897 898 899
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int ret;
    char *devstr = NULL;
    int configfd = -1;
    char *configfd_name = NULL;
900
    bool releaseaddr = false;
901 902 903 904 905 906

    if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0) {
        virReportOOMError();
        return -1;
    }

907
    if (qemuPrepareHostdevPCIDevices(driver, vm->def->name, &hostdev, 1) < 0)
908 909
        return -1;

910
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
911 912 913 914
        if (qemuAssignDeviceHostdevAlias(vm->def, hostdev, -1) < 0)
            goto error;
        if (qemuDomainPCIAddressEnsureAddr(priv->pciaddrs, &hostdev->info) < 0)
            goto error;
915
        releaseaddr = true;
916
        if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_PCI_CONFIGFD)) {
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
            configfd = qemuOpenPCIConfig(hostdev);
            if (configfd >= 0) {
                if (virAsprintf(&configfd_name, "fd-%s",
                                hostdev->info.alias) < 0) {
                    virReportOOMError();
                    goto error;
                }
            }
        }

        if (!virDomainObjIsActive(vm)) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("guest unexpectedly quit during hotplug"));
            goto error;
        }

933
        if (!(devstr = qemuBuildPCIHostdevDevStr(hostdev, configfd_name,
934
                                                 priv->qemuCaps)))
935 936
            goto error;

937
        qemuDomainObjEnterMonitorWithDriver(driver, vm);
938 939
        ret = qemuMonitorAddDeviceWithFd(priv->mon, devstr,
                                         configfd, configfd_name);
940 941
        qemuDomainObjExitMonitorWithDriver(driver, vm);
    } else {
942
        virDomainDevicePCIAddress guestAddr = hostdev->info.addr.pci;
943

944
        qemuDomainObjEnterMonitorWithDriver(driver, vm);
945 946 947 948 949 950 951 952
        ret = qemuMonitorAddPCIHostDevice(priv->mon,
                                          &hostdev->source.subsys.u.pci,
                                          &guestAddr);
        qemuDomainObjExitMonitorWithDriver(driver, vm);

        hostdev->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
        memcpy(&hostdev->info.addr.pci, &guestAddr, sizeof(guestAddr));
    }
953
    virDomainAuditHostdev(vm, hostdev, "attach", ret == 0);
954 955 956 957 958 959 960 961 962 963 964 965
    if (ret < 0)
        goto error;

    vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;

    VIR_FREE(devstr);
    VIR_FREE(configfd_name);
    VIR_FORCE_CLOSE(configfd);

    return 0;

error:
966
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE) &&
967
        (hostdev->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) &&
968
        releaseaddr &&
969 970
        qemuDomainPCIAddressReleaseSlot(priv->pciaddrs,
                                        hostdev->info.addr.pci.slot) < 0)
971
        VIR_WARN("Unable to release PCI address on host device");
972

973
    qemuDomainReAttachHostdevDevices(driver, vm->def->name, &hostdev, 1);
974 975 976 977 978 979 980 981 982

    VIR_FREE(devstr);
    VIR_FREE(configfd_name);
    VIR_FORCE_CLOSE(configfd);

    return -1;
}


983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
int qemuDomainAttachRedirdevDevice(struct qemud_driver *driver,
                                   virDomainObjPtr vm,
                                   virDomainRedirdevDefPtr redirdev)
{
    int ret;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    char *devstr = NULL;

    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
        if (qemuAssignDeviceRedirdevAlias(vm->def, redirdev, -1) < 0)
            goto error;
        if (!(devstr = qemuBuildRedirdevDevStr(redirdev, priv->qemuCaps)))
            goto error;
    }

    if (VIR_REALLOC_N(vm->def->redirdevs, vm->def->nredirdevs+1) < 0) {
        virReportOOMError();
        goto error;
    }

    qemuDomainObjEnterMonitorWithDriver(driver, vm);
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE))
        ret = qemuMonitorAddDevice(priv->mon, devstr);
    else
        goto error;

    qemuDomainObjExitMonitorWithDriver(driver, vm);
    virDomainAuditRedirdev(vm, redirdev, "attach", ret == 0);
    if (ret < 0)
        goto error;

    vm->def->redirdevs[vm->def->nredirdevs++] = redirdev;

    VIR_FREE(devstr);

    return 0;

error:
    VIR_FREE(devstr);
    return -1;

}

1026 1027
int qemuDomainAttachHostUsbDevice(struct qemud_driver *driver,
                                  virDomainObjPtr vm,
1028
                                  virDomainHostdevDefPtr hostdev)
1029 1030 1031 1032 1033
{
    int ret;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    char *devstr = NULL;

1034
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
1035 1036
        if (qemuAssignDeviceHostdevAlias(vm->def, hostdev, -1) < 0)
            goto error;
1037
        if (!(devstr = qemuBuildUSBHostdevDevStr(hostdev, priv->qemuCaps)))
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
            goto error;
    }

    if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0) {
        virReportOOMError();
        goto error;
    }

    if (qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
        virCgroupPtr cgroup = NULL;
        usbDevice *usb;
1049
        qemuCgroupData data;
1050 1051 1052

        if (virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) !=0 ) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
1053
                            _("Unable to find cgroup for %s"),
1054 1055 1056 1057 1058 1059 1060 1061
                            vm->def->name);
            goto error;
        }

        if ((usb = usbGetDevice(hostdev->source.subsys.u.usb.bus,
                                hostdev->source.subsys.u.usb.device)) == NULL)
            goto error;

1062 1063
        data.vm = vm;
        data.cgroup = cgroup;
1064
        if (usbDeviceFileIterate(usb, qemuSetupHostUsbDeviceCgroup, &data) < 0)
1065 1066 1067
            goto error;
    }

1068
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
1069
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE))
1070 1071 1072 1073 1074 1075
        ret = qemuMonitorAddDevice(priv->mon, devstr);
    else
        ret = qemuMonitorAddUSBDeviceExact(priv->mon,
                                           hostdev->source.subsys.u.usb.bus,
                                           hostdev->source.subsys.u.usb.device);
    qemuDomainObjExitMonitorWithDriver(driver, vm);
1076
    virDomainAuditHostdev(vm, hostdev, "attach", ret == 0);
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
    if (ret < 0)
        goto error;

    vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;

    VIR_FREE(devstr);

    return 0;

error:
    VIR_FREE(devstr);
    return -1;
}


int qemuDomainAttachHostDevice(struct qemud_driver *driver,
                               virDomainObjPtr vm,
1094
                               virDomainHostdevDefPtr hostdev)
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
{
    if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
        qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("hostdev mode '%s' not supported"),
                        virDomainHostdevModeTypeToString(hostdev->mode));
        return -1;
    }

    /* Resolve USB product/vendor to bus/device */
    if (hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB &&
        hostdev->source.subsys.u.usb.vendor) {
1106 1107 1108
        if (qemuPrepareHostdevUSBDevices(driver, vm->def->name, &hostdev, 1) < 0)
            goto error;

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
        usbDevice *usb
            = usbFindDevice(hostdev->source.subsys.u.usb.vendor,
                            hostdev->source.subsys.u.usb.product);

        if (!usb)
            return -1;

        hostdev->source.subsys.u.usb.bus = usbDeviceGetBus(usb);
        hostdev->source.subsys.u.usb.device = usbDeviceGetDevno(usb);

        usbFreeDevice(usb);
    }


1123
    if (virSecurityManagerSetHostdevLabel(driver->securityManager,
1124
                                          vm->def, hostdev) < 0)
1125 1126 1127 1128 1129
        return -1;

    switch (hostdev->source.subsys.type) {
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
        if (qemuDomainAttachHostPciDevice(driver, vm,
1130
                                          hostdev) < 0)
1131 1132 1133 1134 1135
            goto error;
        break;

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
        if (qemuDomainAttachHostUsbDevice(driver, vm,
1136
                                          hostdev) < 0)
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
            goto error;
        break;

    default:
        qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("hostdev subsys type '%s' not supported"),
                        virDomainHostdevSubsysTypeToString(hostdev->source.subsys.type));
        goto error;
    }

    return 0;

error:
1150
    if (virSecurityManagerRestoreHostdevLabel(driver->securityManager,
1151
                                              vm->def, hostdev) < 0)
1152
        VIR_WARN("Unable to restore host device labelling on hotplug fail");
1153 1154 1155 1156

    return -1;
}

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
static virDomainNetDefPtr qemuDomainFindNet(virDomainObjPtr vm,
                                            virDomainNetDefPtr dev)
{
    int i;

    for (i = 0; i < vm->def->nnets; i++) {
        if (memcmp(vm->def->nets[i]->mac, dev->mac, VIR_MAC_BUFLEN) == 0)
            return vm->def->nets[i];
    }

    return NULL;
}

int qemuDomainChangeNetLinkState(struct qemud_driver *driver,
                                 virDomainObjPtr vm,
                                 virDomainNetDefPtr dev,
                                 int linkstate)
{
    int ret = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;

    VIR_DEBUG("dev: %s, state: %d", dev->info.alias, linkstate);

    if (!dev->info.alias) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("can't change link state: device alias not found"));
        return -1;
    }

    qemuDomainObjEnterMonitorWithDriver(driver, vm);

    ret = qemuMonitorSetLink(priv->mon, dev->info.alias, linkstate);
    if (ret < 0)
        goto cleanup;

    /* modify the device configuration */
    dev->linkstate = linkstate;

cleanup:
    qemuDomainObjExitMonitorWithDriver(driver, vm);

    return ret;
}

int qemuDomainChangeNet(struct qemud_driver *driver,
                        virDomainObjPtr vm,
                        virDomainPtr dom ATTRIBUTE_UNUSED,
                        virDomainNetDefPtr dev)

{
    virDomainNetDefPtr olddev = qemuDomainFindNet(vm, dev);
    int ret = 0;

    if (!olddev) {
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        _("cannot find existing network device to modify"));
        return -1;
    }

    if (olddev->type != dev->type) {
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        _("cannot change network interface type"));
        return -1;
    }

    switch (olddev->type) {
    case VIR_DOMAIN_NET_TYPE_USER:
        break;

    case VIR_DOMAIN_NET_TYPE_ETHERNET:
        if (STRNEQ_NULLABLE(olddev->data.ethernet.dev, dev->data.ethernet.dev) ||
1228
            STRNEQ_NULLABLE(olddev->script, dev->script) ||
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
            STRNEQ_NULLABLE(olddev->data.ethernet.ipaddr, dev->data.ethernet.ipaddr)) {
            qemuReportError(VIR_ERR_NO_SUPPORT,
                            _("cannot modify ethernet network device configuration"));
            return -1;
        }
        break;

    case VIR_DOMAIN_NET_TYPE_SERVER:
    case VIR_DOMAIN_NET_TYPE_CLIENT:
    case VIR_DOMAIN_NET_TYPE_MCAST:
        if (STRNEQ_NULLABLE(olddev->data.socket.address, dev->data.socket.address) ||
            olddev->data.socket.port != dev->data.socket.port) {
            qemuReportError(VIR_ERR_NO_SUPPORT,
                            _("cannot modify network socket device configuration"));
            return -1;
        }
        break;

    case VIR_DOMAIN_NET_TYPE_NETWORK:
        if (STRNEQ_NULLABLE(olddev->data.network.name, dev->data.network.name) ||
            STRNEQ_NULLABLE(olddev->data.network.portgroup, dev->data.network.portgroup) ||
1250
            !virNetDevVPortProfileEqual(olddev->data.network.virtPortProfile, dev->data.network.virtPortProfile)) {
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
            qemuReportError(VIR_ERR_NO_SUPPORT,
                            _("cannot modify network device configuration"));
            return -1;
        }

        break;

    case VIR_DOMAIN_NET_TYPE_INTERNAL:
        if (STRNEQ_NULLABLE(olddev->data.internal.name, dev->data.internal.name)) {
            qemuReportError(VIR_ERR_NO_SUPPORT,
                            _("cannot modify internal network device configuration"));
            return -1;
        }
        break;

    case VIR_DOMAIN_NET_TYPE_DIRECT:
        if (STRNEQ_NULLABLE(olddev->data.direct.linkdev, dev->data.direct.linkdev) ||
            olddev->data.direct.mode != dev->data.direct.mode ||
1269
            !virNetDevVPortProfileEqual(olddev->data.direct.virtPortProfile, dev->data.direct.virtPortProfile)) {
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
            qemuReportError(VIR_ERR_NO_SUPPORT,
                            _("cannot modify direct network device configuration"));
            return -1;
        }
        break;

    default:
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("unable to change config on '%s' network type"),
                        virDomainNetTypeToString(dev->type));
        break;

    }

    /* all other unmodifiable parameters */
    if (STRNEQ_NULLABLE(olddev->model, dev->model) ||
        STRNEQ_NULLABLE(olddev->filter, dev->filter)) {
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        _("cannot modify network device configuration"));
        return -1;
    }

    /* check if device name has been set, if no, retain the autogenerated one */
    if (dev->ifname &&
        STRNEQ_NULLABLE(olddev->ifname, dev->ifname)) {
        qemuReportError(VIR_ERR_NO_SUPPORT,
                        _("cannot modify network device configuration"));
        return -1;
    }

    if (olddev->linkstate != dev->linkstate) {
        if ((ret = qemuDomainChangeNetLinkState(driver, vm, olddev, dev->linkstate)) < 0)
            return ret;
    }

    return ret;
}


1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329

static virDomainGraphicsDefPtr qemuDomainFindGraphics(virDomainObjPtr vm,
                                                      virDomainGraphicsDefPtr dev)
{
    int i;

    for (i = 0 ; i < vm->def->ngraphics ; i++) {
        if (vm->def->graphics[i]->type == dev->type)
            return vm->def->graphics[i];
    }

    return NULL;
}


int
qemuDomainChangeGraphics(struct qemud_driver *driver,
                         virDomainObjPtr vm,
                         virDomainGraphicsDefPtr dev)
{
    virDomainGraphicsDefPtr olddev = qemuDomainFindGraphics(vm, dev);
1330
    const char *oldListenAddr, *newListenAddr;
1331
    const char *oldListenNetwork, *newListenNetwork;
1332 1333 1334 1335 1336 1337 1338 1339
    int ret = -1;

    if (!olddev) {
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("cannot find existing graphics device to modify"));
        return -1;
    }

1340 1341
    oldListenAddr = virDomainGraphicsListenGetAddress(olddev, 0);
    newListenAddr = virDomainGraphicsListenGetAddress(dev, 0);
1342 1343
    oldListenNetwork = virDomainGraphicsListenGetNetwork(olddev, 0);
    newListenNetwork = virDomainGraphicsListenGetNetwork(dev, 0);
1344

1345 1346 1347
    switch (dev->type) {
    case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
        if ((olddev->data.vnc.autoport != dev->data.vnc.autoport) ||
E
Eric Blake 已提交
1348 1349
            (!dev->data.vnc.autoport &&
             (olddev->data.vnc.port != dev->data.vnc.port))) {
1350 1351 1352 1353
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("cannot change port settings on vnc graphics"));
            return -1;
        }
1354
        if (STRNEQ_NULLABLE(oldListenAddr,newListenAddr)) {
1355 1356 1357 1358
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("cannot change listen address setting on vnc graphics"));
            return -1;
        }
1359 1360 1361 1362 1363
        if (STRNEQ_NULLABLE(oldListenNetwork,newListenNetwork)) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("cannot change listen network setting on vnc graphics"));
            return -1;
        }
1364 1365 1366 1367 1368 1369
        if (STRNEQ_NULLABLE(olddev->data.vnc.keymap, dev->data.vnc.keymap)) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("cannot change keymap setting on vnc graphics"));
            return -1;
        }

1370 1371 1372
        /* If a password lifetime was, or is set, or action if connected has
         * changed, then we must always run, even if new password matches
         * old password */
1373 1374
        if (olddev->data.vnc.auth.expires ||
            dev->data.vnc.auth.expires ||
1375
            olddev->data.vnc.auth.connected != dev->data.vnc.auth.connected ||
E
Eric Blake 已提交
1376 1377 1378 1379 1380 1381 1382 1383
            STRNEQ_NULLABLE(olddev->data.vnc.auth.passwd,
                            dev->data.vnc.auth.passwd)) {
            VIR_DEBUG("Updating password on VNC server %p %p",
                      dev->data.vnc.auth.passwd, driver->vncPassword);
            ret = qemuDomainChangeGraphicsPasswords(driver, vm,
                                                    VIR_DOMAIN_GRAPHICS_TYPE_VNC,
                                                    &dev->data.vnc.auth,
                                                    driver->vncPassword);
1384 1385 1386 1387 1388

            /* Steal the new dev's  char * reference */
            VIR_FREE(olddev->data.vnc.auth.passwd);
            olddev->data.vnc.auth.passwd = dev->data.vnc.auth.passwd;
            dev->data.vnc.auth.passwd = NULL;
1389 1390
            olddev->data.vnc.auth.validTo = dev->data.vnc.auth.validTo;
            olddev->data.vnc.auth.expires = dev->data.vnc.auth.expires;
1391
            olddev->data.vnc.auth.connected = dev->data.vnc.auth.connected;
1392 1393 1394 1395 1396
        } else {
            ret = 0;
        }
        break;

1397 1398
    case VIR_DOMAIN_GRAPHICS_TYPE_SPICE:
        if ((olddev->data.spice.autoport != dev->data.spice.autoport) ||
E
Eric Blake 已提交
1399 1400 1401 1402
            (!dev->data.spice.autoport &&
             (olddev->data.spice.port != dev->data.spice.port)) ||
            (!dev->data.spice.autoport &&
             (olddev->data.spice.tlsPort != dev->data.spice.tlsPort))) {
1403 1404 1405 1406
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("cannot change port settings on spice graphics"));
            return -1;
        }
1407
        if (STRNEQ_NULLABLE(oldListenAddr, newListenAddr)) {
1408 1409 1410 1411
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("cannot change listen address setting on spice graphics"));
            return -1;
        }
1412 1413
        if (STRNEQ_NULLABLE(oldListenNetwork,newListenNetwork)) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1414
                            _("cannot change listen network setting on spice graphics"));
1415 1416
            return -1;
        }
E
Eric Blake 已提交
1417 1418
        if (STRNEQ_NULLABLE(olddev->data.spice.keymap,
                            dev->data.spice.keymap)) {
1419 1420 1421 1422 1423 1424 1425 1426 1427
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("cannot change keymap setting on spice graphics"));
            return -1;
        }

        /* If a password lifetime was, or is set, then we must always run,
         * even if new password matches old password */
        if (olddev->data.spice.auth.expires ||
            dev->data.spice.auth.expires ||
1428
            olddev->data.spice.auth.connected != dev->data.spice.auth.connected ||
E
Eric Blake 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
            STRNEQ_NULLABLE(olddev->data.spice.auth.passwd,
                            dev->data.spice.auth.passwd)) {
            VIR_DEBUG("Updating password on SPICE server %p %p",
                      dev->data.spice.auth.passwd, driver->spicePassword);
            ret = qemuDomainChangeGraphicsPasswords(driver, vm,
                                                    VIR_DOMAIN_GRAPHICS_TYPE_SPICE,
                                                    &dev->data.spice.auth,
                                                    driver->spicePassword);

            /* Steal the new dev's char * reference */
1439 1440 1441 1442 1443
            VIR_FREE(olddev->data.spice.auth.passwd);
            olddev->data.spice.auth.passwd = dev->data.spice.auth.passwd;
            dev->data.spice.auth.passwd = NULL;
            olddev->data.spice.auth.validTo = dev->data.spice.auth.validTo;
            olddev->data.spice.auth.expires = dev->data.spice.auth.expires;
1444
            olddev->data.spice.auth.connected = dev->data.spice.auth.connected;
1445
        } else {
1446
            VIR_DEBUG("Not updating since password didn't change");
1447 1448
            ret = 0;
        }
E
Eric Blake 已提交
1449
        break;
1450

1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
    default:
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("unable to change config on '%s' graphics type"),
                        virDomainGraphicsTypeToString(dev->type));
        break;
    }

    return ret;
}


static inline int qemuFindDisk(virDomainDefPtr def, const char *dst)
{
    int i;

    for (i = 0 ; i < def->ndisks ; i++) {
        if (STREQ(def->disks[i]->dst, dst)) {
            return i;
        }
    }

    return -1;
}

1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
static int qemuComparePCIDevice(virDomainDefPtr def ATTRIBUTE_UNUSED,
                                virDomainDeviceInfoPtr dev1,
                                void *opaque)
{
    virDomainDeviceInfoPtr dev2 = opaque;

    if (dev1->type !=  VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI ||
        dev2->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI)
        return 0;

    if (dev1->addr.pci.slot == dev2->addr.pci.slot &&
        dev1->addr.pci.function != dev2->addr.pci.function)
        return -1;
    return 0;
}

static bool qemuIsMultiFunctionDevice(virDomainDefPtr def,
                                      virDomainDeviceInfoPtr dev)
{
    if (virDomainDeviceInfoIterate(def, qemuComparePCIDevice, dev) < 0)
        return true;
    return false;
}

1499 1500 1501

int qemuDomainDetachPciDiskDevice(struct qemud_driver *driver,
                                  virDomainObjPtr vm,
1502
                                  virDomainDeviceDefPtr dev)
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
{
    int i, ret = -1;
    virDomainDiskDefPtr detach = NULL;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virCgroupPtr cgroup = NULL;
    char *drivestr = NULL;

    i = qemuFindDisk(vm->def, dev->data.disk->dst);

    if (i < 0) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("disk %s not found"), dev->data.disk->dst);
        goto cleanup;
    }

    detach = vm->def->disks[i];

1520 1521 1522 1523 1524 1525 1526
    if (qemuIsMultiFunctionDevice(vm->def, &detach->info)) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("cannot hot unplug multifunction PCI device: %s"),
                        dev->data.disk->dst);
        goto cleanup;
    }

1527 1528 1529
    if (qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
        if (virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) != 0) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
1530
                            _("Unable to find cgroup for %s"),
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
                            vm->def->name);
            goto cleanup;
        }
    }

    if (!virDomainDeviceAddressIsValid(&detach->info,
                                       VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI)) {
        qemuReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        _("device cannot be detached without a PCI address"));
        goto cleanup;
    }

    /* build the actual drive id string as the disk->info.alias doesn't
     * contain the QEMU_DRIVE_HOST_PREFIX that is passed to qemu */
    if (virAsprintf(&drivestr, "%s%s",
                    QEMU_DRIVE_HOST_PREFIX, detach->info.alias) < 0) {
        virReportOOMError();
        goto cleanup;
    }

1551
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
1552
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
1553
        if (qemuMonitorDelDevice(priv->mon, detach->info.alias) < 0) {
1554
            qemuDomainObjExitMonitorWithDriver(driver, vm);
1555
            virDomainAuditDisk(vm, detach->src, NULL, "detach", false);
1556 1557 1558 1559 1560
            goto cleanup;
        }
    } else {
        if (qemuMonitorRemovePCIDevice(priv->mon,
                                       &detach->info.addr.pci) < 0) {
1561
            qemuDomainObjExitMonitorWithDriver(driver, vm);
1562
            virDomainAuditDisk(vm, detach->src, NULL, "detach", false);
1563 1564 1565 1566 1567 1568 1569 1570 1571
            goto cleanup;
        }
    }

    /* disconnect guest from host device */
    qemuMonitorDriveDel(priv->mon, drivestr);

    qemuDomainObjExitMonitorWithDriver(driver, vm);

1572
    virDomainAuditDisk(vm, detach->src, NULL, "detach", true);
1573

1574
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE) &&
1575 1576
        qemuDomainPCIAddressReleaseSlot(priv->pciaddrs,
                                        detach->info.addr.pci.slot) < 0)
1577 1578 1579 1580 1581 1582
        VIR_WARN("Unable to release PCI address on %s", dev->data.disk->src);

    virDomainDiskRemove(vm->def, i);

    virDomainDiskDefFree(detach);

1583
    if (virSecurityManagerRestoreImageLabel(driver->securityManager,
1584
                                            vm->def, dev->data.disk) < 0)
1585 1586 1587
        VIR_WARN("Unable to restore security label on %s", dev->data.disk->src);

    if (cgroup != NULL) {
1588
        if (qemuTeardownDiskCgroup(driver, vm, cgroup, dev->data.disk) < 0)
1589 1590 1591 1592
            VIR_WARN("Failed to teardown cgroup for disk path %s",
                     NULLSTR(dev->data.disk->src));
    }

1593 1594 1595
    if (virDomainLockDiskDetach(driver->lockManager, vm, dev->data.disk) < 0)
        VIR_WARN("Unable to release lock on %s", dev->data.disk->src);

1596 1597 1598 1599 1600 1601 1602
    ret = 0;

cleanup:
    VIR_FREE(drivestr);
    return ret;
}

1603 1604
int qemuDomainDetachDiskDevice(struct qemud_driver *driver,
                               virDomainObjPtr vm,
1605
                               virDomainDeviceDefPtr dev)
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
{
    int i, ret = -1;
    virDomainDiskDefPtr detach = NULL;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virCgroupPtr cgroup = NULL;
    char *drivestr = NULL;

    i = qemuFindDisk(vm->def, dev->data.disk->dst);

    if (i < 0) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("disk %s not found"), dev->data.disk->dst);
        goto cleanup;
    }

1621
    if (!qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
1622 1623 1624
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("Underlying qemu does not support %s disk removal"),
                        virDomainDiskBusTypeToString(dev->data.disk->bus));
1625 1626 1627 1628 1629 1630 1631 1632
        goto cleanup;
    }

    detach = vm->def->disks[i];

    if (qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
        if (virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) != 0) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
1633
                            _("Unable to find cgroup for %s"),
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
                            vm->def->name);
            goto cleanup;
        }
    }

    /* build the actual drive id string as the disk->info.alias doesn't
     * contain the QEMU_DRIVE_HOST_PREFIX that is passed to qemu */
    if (virAsprintf(&drivestr, "%s%s",
                    QEMU_DRIVE_HOST_PREFIX, detach->info.alias) < 0) {
        virReportOOMError();
        goto cleanup;
    }

1647
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
1648
    if (qemuMonitorDelDevice(priv->mon, detach->info.alias) < 0) {
1649
        qemuDomainObjExitMonitorWithDriver(driver, vm);
1650
        virDomainAuditDisk(vm, detach->src, NULL, "detach", false);
1651 1652 1653 1654 1655 1656 1657 1658
        goto cleanup;
    }

    /* disconnect guest from host device */
    qemuMonitorDriveDel(priv->mon, drivestr);

    qemuDomainObjExitMonitorWithDriver(driver, vm);

1659
    virDomainAuditDisk(vm, detach->src, NULL, "detach", true);
1660 1661 1662 1663 1664

    virDomainDiskRemove(vm->def, i);

    virDomainDiskDefFree(detach);

1665
    if (virSecurityManagerRestoreImageLabel(driver->securityManager,
1666
                                            vm->def, dev->data.disk) < 0)
1667 1668 1669
        VIR_WARN("Unable to restore security label on %s", dev->data.disk->src);

    if (cgroup != NULL) {
1670
        if (qemuTeardownDiskCgroup(driver, vm, cgroup, dev->data.disk) < 0)
1671 1672 1673 1674
            VIR_WARN("Failed to teardown cgroup for disk path %s",
                     NULLSTR(dev->data.disk->src));
    }

1675 1676 1677
    if (virDomainLockDiskDetach(driver->lockManager, vm, dev->data.disk) < 0)
        VIR_WARN("Unable to release lock on disk %s", dev->data.disk->src);

1678 1679 1680 1681 1682 1683 1684 1685
    ret = 0;

cleanup:
    VIR_FREE(drivestr);
    virCgroupFree(&cgroup);
    return ret;
}

1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
static bool qemuDomainDiskControllerIsBusy(virDomainObjPtr vm,
                                           virDomainControllerDefPtr detach)
{
    int i;
    virDomainDiskDefPtr disk;

    for (i = 0; i < vm->def->ndisks; i++) {
        disk = vm->def->disks[i];
        if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE)
            /* the disk does not use disk controller */
            continue;

        /* check whether the disk uses this type controller */
        if (disk->bus == VIR_DOMAIN_DISK_BUS_IDE &&
            detach->type != VIR_DOMAIN_CONTROLLER_TYPE_IDE)
            continue;
        if (disk->bus == VIR_DOMAIN_DISK_BUS_FDC &&
            detach->type != VIR_DOMAIN_CONTROLLER_TYPE_FDC)
            continue;
        if (disk->bus == VIR_DOMAIN_DISK_BUS_SCSI &&
            detach->type != VIR_DOMAIN_CONTROLLER_TYPE_SCSI)
            continue;

        if (disk->info.addr.drive.controller == detach->idx)
            return true;
    }

    return false;
}

static bool qemuDomainControllerIsBusy(virDomainObjPtr vm,
                                       virDomainControllerDefPtr detach)
{
    switch (detach->type) {
    case VIR_DOMAIN_CONTROLLER_TYPE_IDE:
    case VIR_DOMAIN_CONTROLLER_TYPE_FDC:
    case VIR_DOMAIN_CONTROLLER_TYPE_SCSI:
        return qemuDomainDiskControllerIsBusy(vm, detach);

    case VIR_DOMAIN_CONTROLLER_TYPE_SATA:
    case VIR_DOMAIN_CONTROLLER_TYPE_VIRTIO_SERIAL:
    case VIR_DOMAIN_CONTROLLER_TYPE_CCID:
    default:
        /* libvirt does not support sata controller, and does not support to
         * detach virtio and smart card controller.
         */
        return true;
    }
}

1736 1737
int qemuDomainDetachPciControllerDevice(struct qemud_driver *driver,
                                        virDomainObjPtr vm,
1738
                                        virDomainDeviceDefPtr dev)
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
{
    int i, ret = -1;
    virDomainControllerDefPtr detach = NULL;
    qemuDomainObjPrivatePtr priv = vm->privateData;

    for (i = 0 ; i < vm->def->ncontrollers ; i++) {
        if ((vm->def->controllers[i]->type == dev->data.controller->type) &&
            (vm->def->controllers[i]->idx == dev->data.controller->idx)) {
            detach = vm->def->controllers[i];
            break;
        }
    }

    if (!detach) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("disk controller %s:%d not found"),
                        virDomainControllerTypeToString(dev->data.controller->type),
                        dev->data.controller->idx);
        goto cleanup;
    }

    if (!virDomainDeviceAddressIsValid(&detach->info,
                                       VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI)) {
        qemuReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        _("device cannot be detached without a PCI address"));
        goto cleanup;
    }

1767 1768 1769 1770 1771 1772 1773
    if (qemuIsMultiFunctionDevice(vm->def, &detach->info)) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("cannot hot unplug multifunction PCI device: %s"),
                        dev->data.disk->dst);
        goto cleanup;
    }

1774 1775 1776 1777 1778 1779
    if (qemuDomainControllerIsBusy(vm, detach)) {
        qemuReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        _("device cannot be detached: device is busy"));
        goto cleanup;
    }

1780
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
1781 1782 1783 1784
        if (qemuAssignDeviceControllerAlias(detach) < 0)
            goto cleanup;
    }

1785
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
1786
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
1787
        if (qemuMonitorDelDevice(priv->mon, detach->info.alias)) {
1788
            qemuDomainObjExitMonitorWithDriver(driver, vm);
1789 1790 1791 1792 1793
            goto cleanup;
        }
    } else {
        if (qemuMonitorRemovePCIDevice(priv->mon,
                                       &detach->info.addr.pci) < 0) {
1794
            qemuDomainObjExitMonitorWithDriver(driver, vm);
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
            goto cleanup;
        }
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);

    if (vm->def->ncontrollers > 1) {
        memmove(vm->def->controllers + i,
                vm->def->controllers + i + 1,
                sizeof(*vm->def->controllers) *
                (vm->def->ncontrollers - (i + 1)));
        vm->def->ncontrollers--;
        if (VIR_REALLOC_N(vm->def->controllers, vm->def->ncontrollers) < 0) {
            /* ignore, harmless */
        }
    } else {
        VIR_FREE(vm->def->controllers);
        vm->def->ncontrollers = 0;
    }

1814
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE) &&
1815 1816
        qemuDomainPCIAddressReleaseSlot(priv->pciaddrs,
                                        detach->info.addr.pci.slot) < 0)
1817
        VIR_WARN("Unable to release PCI address on controller");
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828

    virDomainControllerDefFree(detach);

    ret = 0;

cleanup:
    return ret;
}

int qemuDomainDetachNetDevice(struct qemud_driver *driver,
                              virDomainObjPtr vm,
1829
                              virDomainDeviceDefPtr dev)
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
{
    int i, ret = -1;
    virDomainNetDefPtr detach = NULL;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int vlan;
    char *hostnet_name = NULL;

    for (i = 0 ; i < vm->def->nnets ; i++) {
        virDomainNetDefPtr net = vm->def->nets[i];

        if (!memcmp(net->mac, dev->data.net->mac,  sizeof(net->mac))) {
            detach = net;
            break;
        }
    }

    if (!detach) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("network device %02x:%02x:%02x:%02x:%02x:%02x not found"),
                        dev->data.net->mac[0], dev->data.net->mac[1],
                        dev->data.net->mac[2], dev->data.net->mac[3],
                        dev->data.net->mac[4], dev->data.net->mac[5]);
        goto cleanup;
    }

    if (!virDomainDeviceAddressIsValid(&detach->info,
                                       VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI)) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("device cannot be detached without a PCI address"));
        goto cleanup;
    }

1862 1863 1864 1865 1866 1867 1868
    if (qemuIsMultiFunctionDevice(vm->def, &detach->info)) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("cannot hot unplug multifunction PCI device :%s"),
                        dev->data.disk->dst);
        goto cleanup;
    }

1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
    if ((vlan = qemuDomainNetVLAN(detach)) < 0) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("unable to determine original VLAN"));
        goto cleanup;
    }

    if (virAsprintf(&hostnet_name, "host%s", detach->info.alias) < 0) {
        virReportOOMError();
        goto cleanup;
    }

1880
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
1881
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
1882
        if (qemuMonitorDelDevice(priv->mon, detach->info.alias) < 0) {
1883
            qemuDomainObjExitMonitorWithDriver(driver, vm);
1884
            virDomainAuditNet(vm, detach, NULL, "detach", false);
1885 1886 1887 1888 1889 1890
            goto cleanup;
        }
    } else {
        if (qemuMonitorRemovePCIDevice(priv->mon,
                                       &detach->info.addr.pci) < 0) {
            qemuDomainObjExitMonitorWithDriver(driver, vm);
1891
            virDomainAuditNet(vm, detach, NULL, "detach", false);
1892 1893 1894 1895
            goto cleanup;
        }
    }

1896 1897
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_NETDEV) &&
        qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
1898 1899
        if (qemuMonitorRemoveNetdev(priv->mon, hostnet_name) < 0) {
            qemuDomainObjExitMonitorWithDriver(driver, vm);
1900
            virDomainAuditNet(vm, detach, NULL, "detach", false);
1901 1902 1903 1904 1905
            goto cleanup;
        }
    } else {
        if (qemuMonitorRemoveHostNetwork(priv->mon, vlan, hostnet_name) < 0) {
            qemuDomainObjExitMonitorWithDriver(driver, vm);
1906
            virDomainAuditNet(vm, detach, NULL, "detach", false);
1907 1908 1909 1910 1911
            goto cleanup;
        }
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);

1912
    virDomainAuditNet(vm, detach, NULL, "detach", true);
1913

1914
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE) &&
1915 1916
        qemuDomainPCIAddressReleaseSlot(priv->pciaddrs,
                                        detach->info.addr.pci.slot) < 0)
1917
        VIR_WARN("Unable to release PCI address on NIC");
1918 1919 1920

    virDomainConfNWFilterTeardown(detach);

1921
    if (virDomainNetGetActualType(detach) == VIR_DOMAIN_NET_TYPE_DIRECT) {
1922 1923 1924 1925 1926 1927
        ignore_value(virNetDevMacVLanDeleteWithVPortProfile(
                         detach->ifname, detach->mac,
                         virDomainNetGetActualDirectDev(detach),
                         virDomainNetGetActualDirectMode(detach),
                         virDomainNetGetActualDirectVirtPortProfile(detach),
                         driver->stateDir));
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
        VIR_FREE(detach->ifname);
    }

    if ((driver->macFilter) && (detach->ifname != NULL)) {
        if ((errno = networkDisallowMacOnPort(driver,
                                              detach->ifname,
                                              detach->mac))) {
            virReportSystemError(errno,
             _("failed to remove ebtables rule on  '%s'"),
                                 detach->ifname);
        }
    }

1941
    networkReleaseActualDevice(detach);
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
    if (vm->def->nnets > 1) {
        memmove(vm->def->nets + i,
                vm->def->nets + i + 1,
                sizeof(*vm->def->nets) *
                (vm->def->nnets - (i + 1)));
        vm->def->nnets--;
        if (VIR_REALLOC_N(vm->def->nets, vm->def->nnets) < 0) {
            /* ignore, harmless */
        }
    } else {
        VIR_FREE(vm->def->nets);
        vm->def->nnets = 0;
    }
    virDomainNetDefFree(detach);

    ret = 0;

cleanup:
    VIR_FREE(hostnet_name);
    return ret;
}

1964 1965 1966 1967 1968
static int
qemuDomainDetachHostPciDevice(struct qemud_driver *driver,
                              virDomainObjPtr vm,
                              virDomainDeviceDefPtr dev,
                              virDomainHostdevDefPtr *detach_ret)
1969 1970 1971 1972 1973
{
    virDomainHostdevDefPtr detach = NULL;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int i, ret;
    pciDevice *pci;
1974
    pciDevice *activePci;
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002

    for (i = 0 ; i < vm->def->nhostdevs ; i++) {
        if (vm->def->hostdevs[i]->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
            vm->def->hostdevs[i]->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
            continue;

        unsigned domain   = vm->def->hostdevs[i]->source.subsys.u.pci.domain;
        unsigned bus      = vm->def->hostdevs[i]->source.subsys.u.pci.bus;
        unsigned slot     = vm->def->hostdevs[i]->source.subsys.u.pci.slot;
        unsigned function = vm->def->hostdevs[i]->source.subsys.u.pci.function;

        if (dev->data.hostdev->source.subsys.u.pci.domain   == domain &&
            dev->data.hostdev->source.subsys.u.pci.bus      == bus &&
            dev->data.hostdev->source.subsys.u.pci.slot     == slot &&
            dev->data.hostdev->source.subsys.u.pci.function == function) {
            detach = vm->def->hostdevs[i];
            break;
        }
    }

    if (!detach) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("host pci device %.4x:%.2x:%.2x.%.1x not found"),
                        dev->data.hostdev->source.subsys.u.pci.domain,
                        dev->data.hostdev->source.subsys.u.pci.bus,
                        dev->data.hostdev->source.subsys.u.pci.slot,
                        dev->data.hostdev->source.subsys.u.pci.function);
        return -1;
2003 2004 2005 2006 2007 2008 2009
    }

    if (qemuIsMultiFunctionDevice(vm->def, &detach->info)) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("cannot hot unplug multifunction PCI device: %s"),
                        dev->data.disk->dst);
        return -1;
2010 2011 2012 2013 2014 2015 2016 2017 2018
    }

    if (!virDomainDeviceAddressIsValid(&detach->info,
                                       VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI)) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("device cannot be detached without a PCI address"));
        return -1;
    }

2019
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
2020
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
2021
        ret = qemuMonitorDelDevice(priv->mon, detach->info.alias);
2022
    } else {
2023
        ret = qemuMonitorRemovePCIDevice(priv->mon, &detach->info.addr.pci);
2024 2025
    }
    qemuDomainObjExitMonitorWithDriver(driver, vm);
2026
    virDomainAuditHostdev(vm, detach, "detach", ret == 0);
2027 2028
    if (ret < 0)
        return -1;
2029 2030 2031 2032 2033

    pci = pciGetDevice(detach->source.subsys.u.pci.domain,
                       detach->source.subsys.u.pci.bus,
                       detach->source.subsys.u.pci.slot,
                       detach->source.subsys.u.pci.function);
2034 2035
    if (pci) {
        activePci = pciDeviceListSteal(driver->activePciHostdevs, pci);
2036 2037
        if (pciResetDevice(activePci, driver->activePciHostdevs,
                           driver->inactivePciHostdevs) == 0)
2038 2039
            qemuReattachPciDevice(activePci, driver);
        else
2040 2041
            ret = -1;
        pciFreeDevice(pci);
2042 2043 2044
        pciFreeDevice(activePci);
    } else {
        ret = -1;
2045 2046
    }

2047
    if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE) &&
2048 2049
        qemuDomainPCIAddressReleaseSlot(priv->pciaddrs,
                                        detach->info.addr.pci.slot) < 0)
2050
        VIR_WARN("Unable to release PCI address on host device");
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064

    if (vm->def->nhostdevs > 1) {
        memmove(vm->def->hostdevs + i,
                vm->def->hostdevs + i + 1,
                sizeof(*vm->def->hostdevs) *
                (vm->def->nhostdevs - (i + 1)));
        vm->def->nhostdevs--;
        if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs) < 0) {
            /* ignore, harmless */
        }
    } else {
        VIR_FREE(vm->def->hostdevs);
        vm->def->nhostdevs = 0;
    }
2065 2066 2067 2068
    if (detach_ret)
        *detach_ret = detach;
    else
        virDomainHostdevDefFree(detach);
2069 2070 2071 2072

    return ret;
}

2073 2074 2075 2076 2077
static int
qemuDomainDetachHostUsbDevice(struct qemud_driver *driver,
                              virDomainObjPtr vm,
                              virDomainDeviceDefPtr dev,
                              virDomainHostdevDefPtr *detach_ret)
2078 2079 2080
{
    virDomainHostdevDefPtr detach = NULL;
    qemuDomainObjPrivatePtr priv = vm->privateData;
2081
    usbDevice *usb;
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
    int i, ret;

    for (i = 0 ; i < vm->def->nhostdevs ; i++) {
        if (vm->def->hostdevs[i]->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
            vm->def->hostdevs[i]->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
            continue;

        unsigned bus = vm->def->hostdevs[i]->source.subsys.u.usb.bus;
        unsigned device = vm->def->hostdevs[i]->source.subsys.u.usb.device;
        unsigned product = vm->def->hostdevs[i]->source.subsys.u.usb.product;
        unsigned vendor = vm->def->hostdevs[i]->source.subsys.u.usb.vendor;

        if (dev->data.hostdev->source.subsys.u.usb.bus &&
            dev->data.hostdev->source.subsys.u.usb.device) {
            if (dev->data.hostdev->source.subsys.u.usb.bus == bus &&
                dev->data.hostdev->source.subsys.u.usb.device == device) {
                detach = vm->def->hostdevs[i];
                break;
            }
        } else {
            if (dev->data.hostdev->source.subsys.u.usb.product == product &&
                dev->data.hostdev->source.subsys.u.usb.vendor == vendor) {
                detach = vm->def->hostdevs[i];
                break;
            }
        }
    }

    if (!detach) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        _("host usb device %03d.%03d not found"),
                        dev->data.hostdev->source.subsys.u.usb.bus,
                        dev->data.hostdev->source.subsys.u.usb.device);
        return -1;
    }

    if (!detach->info.alias) {
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("device cannot be detached without a device alias"));
        return -1;
    }

2124
    if (!qemuCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
2125 2126 2127 2128 2129
        qemuReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("device cannot be detached with this QEMU version"));
        return -1;
    }

2130
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
2131
    ret = qemuMonitorDelDevice(priv->mon, detach->info.alias);
2132
    qemuDomainObjExitMonitorWithDriver(driver, vm);
2133
    virDomainAuditHostdev(vm, detach, "detach", ret == 0);
2134 2135
    if (ret < 0)
        return -1;
2136

2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
    usb = usbGetDevice(detach->source.subsys.u.usb.bus,
                       detach->source.subsys.u.usb.device);
    if (usb) {
        usbDeviceListDel(driver->activeUsbHostdevs, usb);
        usbFreeDevice(usb);
    } else {
        VIR_WARN("Unable to find device %03d.%03d in list of used USB devices",
                 detach->source.subsys.u.usb.bus,
                 detach->source.subsys.u.usb.device);
    }

2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
    if (vm->def->nhostdevs > 1) {
        memmove(vm->def->hostdevs + i,
                vm->def->hostdevs + i + 1,
                sizeof(*vm->def->hostdevs) *
                (vm->def->nhostdevs - (i + 1)));
        vm->def->nhostdevs--;
        if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs) < 0) {
            /* ignore, harmless */
        }
    } else {
        VIR_FREE(vm->def->hostdevs);
        vm->def->nhostdevs = 0;
    }
2161 2162 2163 2164
    if (detach_ret)
        *detach_ret = detach;
    else
        virDomainHostdevDefFree(detach);
2165 2166 2167 2168 2169 2170

    return ret;
}

int qemuDomainDetachHostDevice(struct qemud_driver *driver,
                               virDomainObjPtr vm,
2171
                               virDomainDeviceDefPtr dev)
2172 2173
{
    virDomainHostdevDefPtr hostdev = dev->data.hostdev;
2174
    virDomainHostdevDefPtr detach = NULL;
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
    int ret;

    if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
        qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("hostdev mode '%s' not supported"),
                        virDomainHostdevModeTypeToString(hostdev->mode));
        return -1;
    }

    switch (hostdev->source.subsys.type) {
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
2186
        ret = qemuDomainDetachHostPciDevice(driver, vm, dev, &detach);
2187
       break;
2188
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
2189
        ret = qemuDomainDetachHostUsbDevice(driver, vm, dev, &detach);
2190 2191 2192 2193 2194 2195 2196 2197
        break;
    default:
        qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("hostdev subsys type '%s' not supported"),
                        virDomainHostdevSubsysTypeToString(hostdev->source.subsys.type));
        return -1;
    }

2198 2199
    if (ret == 0 &&
        virSecurityManagerRestoreHostdevLabel(driver->securityManager,
2200
                                              vm->def, detach) < 0)
2201
        VIR_WARN("Failed to restore host device labelling");
2202

2203 2204
    virDomainHostdevDefFree(detach);

2205 2206
    return ret;
}
2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217

int
qemuDomainChangeGraphicsPasswords(struct qemud_driver *driver,
                                  virDomainObjPtr vm,
                                  int type,
                                  virDomainGraphicsAuthDefPtr auth,
                                  const char *defaultPasswd)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    time_t now = time(NULL);
    char expire_time [64];
2218
    const char *connected = NULL;
2219 2220 2221 2222 2223
    int ret;

    if (!auth->passwd && !driver->vncPassword)
        return 0;

2224 2225 2226
    if (auth->connected)
        connected = virDomainGraphicsAuthConnectedTypeToString(auth->connected);

2227
    qemuDomainObjEnterMonitorWithDriver(driver, vm);
2228 2229 2230
    ret = qemuMonitorSetPassword(priv->mon,
                                 type,
                                 auth->passwd ? auth->passwd : defaultPasswd,
2231
                                 connected);
2232 2233 2234 2235

    if (ret == -2) {
        if (type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2236
                            _("Graphics password only supported for VNC"));
2237 2238 2239 2240 2241 2242
            ret = -1;
        } else {
            ret = qemuMonitorSetVNCPassword(priv->mon,
                                            auth->passwd ? auth->passwd : defaultPasswd);
        }
    }
2243 2244 2245
    if (ret != 0)
        goto cleanup;

2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263
    if (auth->expires) {
        time_t lifetime = auth->validTo - now;
        if (lifetime <= 0)
            snprintf(expire_time, sizeof (expire_time), "now");
        else
            snprintf(expire_time, sizeof (expire_time), "%lu", (long unsigned)auth->validTo);
    } else {
        snprintf(expire_time, sizeof (expire_time), "never");
    }

    ret = qemuMonitorExpirePassword(priv->mon, type, expire_time);

    if (ret == -2) {
        /* XXX we could fake this with a timer */
        if (auth->expires) {
            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("Expiry of passwords is not supported"));
            ret = -1;
2264 2265
        } else {
            ret = 0;
2266 2267 2268
        }
    }

2269
cleanup:
2270 2271 2272 2273
    qemuDomainObjExitMonitorWithDriver(driver, vm);

    return ret;
}
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309

int qemuDomainAttachLease(struct qemud_driver *driver,
                          virDomainObjPtr vm,
                          virDomainLeaseDefPtr lease)
{
    if (virDomainLeaseInsertPreAlloc(vm->def) < 0)
        return -1;

    if (virDomainLockLeaseAttach(driver->lockManager, vm, lease) < 0) {
        virDomainLeaseInsertPreAlloced(vm->def, NULL);
        return -1;
    }

    virDomainLeaseInsertPreAlloced(vm->def, lease);
    return 0;
}

int qemuDomainDetachLease(struct qemud_driver *driver,
                          virDomainObjPtr vm,
                          virDomainLeaseDefPtr lease)
{
    int i;

    if ((i = virDomainLeaseIndex(vm->def, lease)) < 0) {
        qemuReportError(VIR_ERR_INVALID_ARG,
                        _("Lease %s in lockspace %s does not exist"),
                        lease->key, NULLSTR(lease->lockspace));
        return -1;
    }

    if (virDomainLockLeaseDetach(driver->lockManager, vm, lease) < 0)
        return -1;

    virDomainLeaseRemoveAt(vm->def, i);
    return 0;
}