xm_internal.c 39.4 KB
Newer Older
1
/*
2
 * xm_internal.c: helper routines for dealing with inactive domains
3
 *
4
 * Copyright (C) 2006-2007, 2009-2014 Red Hat, Inc.
5
 * Copyright (C) 2006 Daniel P. Berrange
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
22 23 24
 *
 */

25
#include <config.h>
26

27 28 29
#include <dirent.h>
#include <time.h>
#include <sys/stat.h>
30
#include <limits.h>
31 32
#include <string.h>
#include <errno.h>
33 34 35 36 37

#include <unistd.h>
#include <stdint.h>
#include <xen/dom0_ops.h>

38
#include "virerror.h"
39
#include "virfile.h"
40
#include "datatypes.h"
41
#include "xm_internal.h"
42
#include "xen_driver.h"
43
#include "xend_internal.h"
44
#include "xen_sxpr.h"
45
#include "xen_xm.h"
46
#include "virhash.h"
47
#include "virbuffer.h"
48
#include "viruuid.h"
49
#include "viralloc.h"
50
#include "virlog.h"
51
#include "count-one-bits.h"
52
#include "virstring.h"
53

54
#define VIR_FROM_THIS VIR_FROM_XENXM
55

56 57
VIR_LOG_INIT("xen.xm_internal");

58 59
#define XEND_CONFIG_MAX_VERS_NET_TYPE_IOEMU 3
#define XEND_CONFIG_MIN_VERS_PVFB_NEWCONF 3
60

61 62 63 64 65
/* The true Xen limit varies but so far is always way
   less than 1024, which is the Linux kernel limit according
   to sched.h, so we'll match that for now */
#define XEN_MAX_PHYSICAL_CPU 1024

66 67
char * xenXMAutoAssignMac(void);

68 69 70 71 72 73 74
#define XM_REFRESH_INTERVAL 10

#define XM_CONFIG_DIR "/etc/xen"
#define XM_EXAMPLE_PREFIX "xmexample"
#define XEND_CONFIG_FILE "xend-config.sxp"
#define XEND_PCI_CONFIG_PREFIX "xend-pci-"
#define QEMU_IF_SCRIPT "qemu-ifup"
75
#define XM_XML_ERROR "Invalid xml"
76

77
#ifndef WITH_XEN_INOTIFY
78 79 80 81
static int xenInotifyActive(virConnectPtr conn ATTRIBUTE_UNUSED)
{
   return 0;
}
82
#else
83 84
static int xenInotifyActive(virConnectPtr conn)
{
85
   xenUnifiedPrivatePtr priv = conn->privateData;
86
   return priv->inotifyWatch > 0;
87
}
88
#endif
89

90 91

/* Release memory associated with a cached config object */
92 93
static void xenXMConfigFree(void *payload, const void *key ATTRIBUTE_UNUSED)
{
94
    xenXMConfCachePtr entry = (xenXMConfCachePtr)payload;
95
    virDomainDefFree(entry->def);
E
Eric Blake 已提交
96
    VIR_FREE(entry->filename);
97
    VIR_FREE(entry);
98 99
}

100 101 102 103
struct xenXMConfigReaperData {
    xenUnifiedPrivatePtr priv;
    time_t now;
};
104

105
/* Remove any configs which were not refreshed recently */
106 107 108 109 110
static int
xenXMConfigReaper(const void *payload,
                  const void *key ATTRIBUTE_UNUSED,
                  const void *data)
{
111
    const struct xenXMConfigReaperData *args = data;
112 113
    xenXMConfCachePtr entry = (xenXMConfCachePtr)payload;

114 115
    /* We're going to purge this config file, so check if it
       is currently mapped as owner of a named domain. */
116
    if (entry->refreshedAt != args->now) {
117
        const char *olddomname = entry->def->name;
118
        char *nameowner = (char *)virHashLookup(args->priv->nameConfigMap, olddomname);
119
        if (nameowner && STREQ(nameowner, key))
120
            virHashRemoveEntry(args->priv->nameConfigMap, olddomname);
121
        return 1;
122
    }
123
    return 0;
124 125
}

126 127

static virDomainDefPtr
128 129
xenXMConfigReadFile(virConnectPtr conn, const char *filename)
{
130 131
    virConfPtr conf;
    virDomainDefPtr def;
132
    xenUnifiedPrivatePtr priv = conn->privateData;
133

134
    if (!(conf = virConfReadFile(filename, 0)))
135 136
        return NULL;

137
    def = xenParseXM(conf, priv->caps, priv->xmlopt);
138 139 140 141 142 143
    virConfFree(conf);

    return def;
}

static int
144 145 146 147
xenXMConfigSaveFile(virConnectPtr conn,
                    const char *filename,
                    virDomainDefPtr def)
{
148 149 150
    virConfPtr conf;
    int ret;

151
    if (!(conf = xenFormatXM(conn, def)))
152 153 154 155 156 157 158
        return -1;

    ret = virConfWriteFile(filename, conf);
    virConfFree(conf);
    return ret;
}

D
Daniel P. Berrange 已提交
159 160 161

/*
 * Caller must hold the lock on 'conn->privateData' before
J
Ján Tomko 已提交
162
 * calling this function
D
Daniel P. Berrange 已提交
163
 */
164
int
165
xenXMConfigCacheRemoveFile(virConnectPtr conn, const char *filename)
166
{
167
    xenUnifiedPrivatePtr priv = conn->privateData;
168 169
    xenXMConfCachePtr entry;

170
    entry = virHashLookup(priv->configCache, filename);
171
    if (!entry) {
172
        VIR_DEBUG("No config entry for %s", filename);
173 174 175
        return 0;
    }

176 177
    virHashRemoveEntry(priv->nameConfigMap, entry->def->name);
    virHashRemoveEntry(priv->configCache, filename);
178
    VIR_DEBUG("Removed %s %s", entry->def->name, filename);
179 180 181 182
    return 0;
}


D
Daniel P. Berrange 已提交
183 184
/*
 * Caller must hold the lock on 'conn->privateData' before
J
Ján Tomko 已提交
185
 * calling this function
D
Daniel P. Berrange 已提交
186
 */
187
int
188
xenXMConfigCacheAddFile(virConnectPtr conn, const char *filename, time_t now)
189
{
190
    xenUnifiedPrivatePtr priv = conn->privateData;
191 192 193 194
    xenXMConfCachePtr entry;
    struct stat st;
    int newborn = 0;

195
    VIR_DEBUG("Adding file %s %lld", filename, (long long)now);
196 197 198

    /* Get modified time */
    if ((stat(filename, &st) < 0)) {
199
        virReportSystemError(errno,
200 201
                             _("cannot stat: %s"),
                             filename);
202 203 204 205 206 207
        return -1;
    }

    /* Ignore zero length files, because inotify fires before
       any content has actually been created */
    if (st.st_size == 0) {
208
        VIR_DEBUG("Ignoring zero length file %s", filename);
209 210 211 212 213
        return -1;
    }

    /* If we already have a matching entry and it is not
    modified, then carry on to next one*/
214
    if ((entry = virHashLookup(priv->configCache, filename))) {
215 216 217 218 219 220 221 222 223 224
        char *nameowner;

        if (entry->refreshedAt >= st.st_mtime) {
            entry->refreshedAt = now;
            /* return success if up-to-date */
            return 0;
        }

        /* If we currently own the name, then release it and
            re-acquire it later - just in case it was renamed */
225
        nameowner = (char *)virHashLookup(priv->nameConfigMap, entry->def->name);
226
        if (nameowner && STREQ(nameowner, filename))
227
            virHashRemoveEntry(priv->nameConfigMap, entry->def->name);
228 229 230 231 232 233

        /* Clear existing config entry which needs refresh */
        virDomainDefFree(entry->def);
        entry->def = NULL;
    } else { /* Completely new entry */
        newborn = 1;
234
        if (VIR_ALLOC(entry) < 0)
235
            return -1;
236
        if (VIR_STRDUP(entry->filename, filename) < 0) {
E
Eric Blake 已提交
237 238 239
            VIR_FREE(entry);
            return -1;
        }
240 241 242 243
    }
    entry->refreshedAt = now;

    if (!(entry->def = xenXMConfigReadFile(conn, entry->filename))) {
244
        VIR_DEBUG("Failed to read %s", entry->filename);
245
        if (!newborn)
246
            virHashSteal(priv->configCache, filename);
E
Eric Blake 已提交
247
        VIR_FREE(entry->filename);
248 249 250 251 252 253 254
        VIR_FREE(entry);
        return -1;
    }

    /* If its a completely new entry, it must be stuck into
        the cache (refresh'd entries are already registered) */
    if (newborn) {
255
        if (virHashAddEntry(priv->configCache, entry->filename, entry) < 0) {
256
            virDomainDefFree(entry->def);
E
Eric Blake 已提交
257
            VIR_FREE(entry->filename);
258
            VIR_FREE(entry);
259 260
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("xenXMConfigCacheRefresh: virHashAddEntry"));
261 262 263 264 265 266 267
            return -1;
        }
    }

    /* See if we need to map this config file in as the primary owner
        * of the domain in question
        */
268
    if (!virHashLookup(priv->nameConfigMap, entry->def->name)) {
E
Eric Blake 已提交
269 270
        if (virHashAddEntry(priv->nameConfigMap, entry->def->name,
                            entry->filename) < 0) {
271
            virHashSteal(priv->configCache, filename);
272
            virDomainDefFree(entry->def);
E
Eric Blake 已提交
273
            VIR_FREE(entry->filename);
274
            VIR_FREE(entry);
J
John Ferlan 已提交
275 276 277
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("xenXMConfigCacheRefresh: virHashAddEntry name"));
            return -1;
278 279
        }
    }
280
    VIR_DEBUG("Added config %s %s", entry->def->name, filename);
281 282 283

    return 0;
}
284

285
/* This method is called by various methods to scan /etc/xen
D
Daniel P. Berrange 已提交
286 287 288 289 290 291
 * (or whatever directory was set by  LIBVIRT_XM_CONFIG_DIR
 * environment variable) and process any domain configs. It
 * has rate-limited so never rescans more frequently than
 * once every X seconds
 *
 * Caller must hold the lock on 'conn->privateData' before
J
Ján Tomko 已提交
292
 * calling this function
D
Daniel P. Berrange 已提交
293
 */
294 295 296
int
xenXMConfigCacheRefresh(virConnectPtr conn)
{
297
    xenUnifiedPrivatePtr priv = conn->privateData;
298 299 300 301
    DIR *dh;
    struct dirent *ent;
    time_t now = time(NULL);
    int ret = -1;
302
    struct xenXMConfigReaperData args;
303 304

    if (now == ((time_t)-1)) {
305
        virReportSystemError(errno,
306
                             "%s", _("cannot get time of day"));
307
        return -1;
308 309 310
    }

    /* Rate limit re-scans */
311
    if ((now - priv->lastRefresh) < XM_REFRESH_INTERVAL)
312
        return 0;
313

314
    priv->lastRefresh = now;
315 316

    /* Process the files in the config dir */
J
Ján Tomko 已提交
317
    if (virDirOpen(&dh, priv->configDir) < 0)
318
        return -1;
319

E
Eric Blake 已提交
320
    while ((ret = virDirRead(dh, &ent, priv->configDir)) > 0) {
321
        struct stat st;
322
        char *path;
323 324 325 326 327 328

        /*
         * Skip a bunch of crufty files that clearly aren't config files
         */

        /* Like 'dot' files... */
329
        if (STRPREFIX(ent->d_name, "."))
330 331
            continue;
        /* ...and the XenD server config file */
332
        if (STRPREFIX(ent->d_name, XEND_CONFIG_FILE))
333 334
            continue;
        /* ...and random PCI config cruft */
335
        if (STRPREFIX(ent->d_name, XEND_PCI_CONFIG_PREFIX))
336 337
            continue;
        /* ...and the example domain configs */
338
        if (STRPREFIX(ent->d_name, XM_EXAMPLE_PREFIX))
339 340
            continue;
        /* ...and the QEMU networking script */
341
        if (STRPREFIX(ent->d_name, QEMU_IF_SCRIPT))
342 343 344 345 346 347 348 349 350
            continue;

        /* ...and editor backups */
        if (ent->d_name[0] == '#')
            continue;
        if (ent->d_name[strlen(ent->d_name)-1] == '~')
            continue;

        /* Build the full file path */
351
        if (!(path = virFileBuildPath(priv->configDir, ent->d_name, NULL))) {
J
Ján Tomko 已提交
352
            VIR_DIR_CLOSE(dh);
353 354
            return -1;
        }
355 356 357 358

        /* Skip anything which isn't a file (takes care of scripts/ subdir */
        if ((stat(path, &st) < 0) ||
            (!S_ISREG(st.st_mode))) {
359
            VIR_FREE(path);
360 361 362 363 364
            continue;
        }

        /* If we already have a matching entry and it is not
           modified, then carry on to next one*/
365
        if (xenXMConfigCacheAddFile(conn, path, now) < 0) {
J
Ján Tomko 已提交
366
            /* Ignoring errors, since a lot of stuff goes wrong in /etc/xen */
367
        }
368 369

        VIR_FREE(path);
370 371 372 373 374 375
    }

    /* Reap all entries which were not changed, by comparing
       their refresh timestamp - the timestamp should match
       'now' if they were refreshed. If timestamp doesn't match
       then the config is no longer on disk */
376 377
    args.now = now;
    args.priv = priv;
378
    virHashRemoveSet(priv->configCache, xenXMConfigReaper, &args);
379

J
Ján Tomko 已提交
380
    VIR_DIR_CLOSE(dh);
381

382
    return ret;
383 384 385 386
}


/*
387 388 389 390
 * The XM driver keeps a cache of config files as virDomainDefPtr
 * objects in the xenUnifiedPrivatePtr. Optionally inotify lets
 * us watch for changes (see separate driver), otherwise we poll
 * every few seconds
391
 */
392
int
393 394 395
xenXMOpen(virConnectPtr conn,
          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
          unsigned int flags)
396
{
397 398
    xenUnifiedPrivatePtr priv = conn->privateData;

399
    virCheckFlags(VIR_CONNECT_RO, -1);
E
Eric Blake 已提交
400

401 402
    priv->configDir = XM_CONFIG_DIR;

403
    priv->configCache = virHashCreate(50, xenXMConfigFree);
404
    if (!priv->configCache)
405
        return -1;
406
    priv->nameConfigMap = virHashCreate(50, NULL);
407
    if (!priv->nameConfigMap) {
408
        virHashFree(priv->configCache);
409
        priv->configCache = NULL;
410
        return -1;
411
    }
412 413 414 415
    /* Force the cache to be reloaded next time that
     * xenXMConfigCacheRefresh is called.
     */
    priv->lastRefresh = 0;
416

417
    return 0;
418 419 420
}

/*
421 422
 * Free the cached config files associated with this
 * connection
423
 */
424 425 426
int
xenXMClose(virConnectPtr conn)
{
427 428
    xenUnifiedPrivatePtr priv = conn->privateData;

429 430
    virHashFree(priv->nameConfigMap);
    virHashFree(priv->configCache);
431

432
    return 0;
433 434
}

435 436 437 438
/*
 * Since these are all offline domains, the state is always SHUTOFF.
 */
int
439 440
xenXMDomainGetState(virConnectPtr conn ATTRIBUTE_UNUSED,
                    virDomainDefPtr def ATTRIBUTE_UNUSED,
441 442
                    int *state,
                    int *reason)
443 444 445 446 447 448 449 450 451
{
    *state = VIR_DOMAIN_SHUTOFF;
    if (reason)
        *reason = 0;

    return 0;
}


452 453
/*
 * Since these are all offline domains, we only return info about
454
 * VCPUs and memory.
455
 */
456
int
457 458 459
xenXMDomainGetInfo(virConnectPtr conn,
                   virDomainDefPtr def,
                   virDomainInfoPtr info)
460
{
461
    xenUnifiedPrivatePtr priv = conn->privateData;
462
    const char *filename;
463 464
    xenXMConfCachePtr entry;

D
Daniel P. Berrange 已提交
465
    xenUnifiedLock(priv);
466

467
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
468
        goto error;
469

470
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
471
        goto error;
472 473

    memset(info, 0, sizeof(virDomainInfo));
474
    info->maxMem = virDomainDefGetMemoryTotal(entry->def);
475
    info->memory = entry->def->mem.cur_balloon;
476
    info->nrVirtCpu = virDomainDefGetVcpus(entry->def);
477 478 479
    info->state = VIR_DOMAIN_SHUTOFF;
    info->cpuTime = 0;

D
Daniel P. Berrange 已提交
480
    xenUnifiedUnlock(priv);
481
    return 0;
482

483
 error:
D
Daniel P. Berrange 已提交
484 485
    xenUnifiedUnlock(priv);
    return -1;
486 487 488
}


489 490
/*
 * Turn a config record into a lump of XML describing the
491
 * domain, suitable for later feeding for virDomainCreateXML
492
 */
493 494 495
virDomainDefPtr
xenXMDomainGetXMLDesc(virConnectPtr conn,
                      virDomainDefPtr def)
E
Eric Blake 已提交
496
{
497
    xenUnifiedPrivatePtr priv = conn->privateData;
498 499
    const char *filename;
    xenXMConfCachePtr entry;
500
    virDomainDefPtr ret = NULL;
501

E
Eric Blake 已提交
502 503
    /* Flags checked by virDomainDefFormat */

D
Daniel P. Berrange 已提交
504
    xenUnifiedLock(priv);
505

506
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
507
        goto cleanup;
508

509
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
510 511
        goto cleanup;

512 513 514
    ret = virDomainDefCopy(entry->def,
                           priv->caps,
                           priv->xmlopt,
515
                           NULL,
516
                           false);
517

518
 cleanup:
D
Daniel P. Berrange 已提交
519 520
    xenUnifiedUnlock(priv);
    return ret;
521 522 523
}


524 525 526
/*
 * Update amount of memory in the config file
 */
527
int
528 529 530
xenXMDomainSetMemory(virConnectPtr conn,
                     virDomainDefPtr def,
                     unsigned long memory)
531
{
532
    xenUnifiedPrivatePtr priv = conn->privateData;
533
    const char *filename;
534
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
535
    int ret = -1;
536

537 538 539 540
    if (memory < 1024 * MIN_XEN_GUEST_SIZE) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Memory %lu too small, min %lu"),
                       memory, (unsigned long)1024 * MIN_XEN_GUEST_SIZE);
541
        return -1;
542
    }
543

D
Daniel P. Berrange 已提交
544
    xenUnifiedLock(priv);
545

546
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
547
        goto cleanup;
548

549
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
550
        goto cleanup;
551

552
    entry->def->mem.cur_balloon = memory;
553 554
    if (entry->def->mem.cur_balloon > virDomainDefGetMemoryTotal(entry->def))
        entry->def->mem.cur_balloon = virDomainDefGetMemoryTotal(entry->def);
555 556 557 558

    /* If this fails, should we try to undo our changes to the
     * in-memory representation of the config file. I say not!
     */
559
    if (xenXMConfigSaveFile(conn, entry->filename, entry->def) < 0)
D
Daniel P. Berrange 已提交
560 561
        goto cleanup;
    ret = 0;
562

563
 cleanup:
D
Daniel P. Berrange 已提交
564 565
    xenUnifiedUnlock(priv);
    return ret;
566 567 568 569 570
}

/*
 * Update maximum memory limit in config
 */
571
int
572 573 574
xenXMDomainSetMaxMemory(virConnectPtr conn,
                        virDomainDefPtr def,
                        unsigned long memory)
575
{
576
    xenUnifiedPrivatePtr priv = conn->privateData;
577
    const char *filename;
578
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
579
    int ret = -1;
580

581 582 583 584
    if (memory < 1024 * MIN_XEN_GUEST_SIZE) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Memory %lu too small, min %lu"),
                       memory, (unsigned long)1024 * MIN_XEN_GUEST_SIZE);
585
        return -1;
586
    }
587

D
Daniel P. Berrange 已提交
588
    xenUnifiedLock(priv);
589

590
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
591
        goto cleanup;
592

593
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
594
        goto cleanup;
595

596 597
    if (entry->def->mem.cur_balloon > memory)
        entry->def->mem.cur_balloon = memory;
598

599
    virDomainDefSetMemoryTotal(entry->def, memory);
600 601 602
    /* If this fails, should we try to undo our changes to the
     * in-memory representation of the config file. I say not!
     */
603
    if (xenXMConfigSaveFile(conn, entry->filename, entry->def) < 0)
D
Daniel P. Berrange 已提交
604 605
        goto cleanup;
    ret = 0;
606

607
 cleanup:
D
Daniel P. Berrange 已提交
608 609
    xenUnifiedUnlock(priv);
    return ret;
610 611 612 613 614
}

/*
 * Get max memory limit from config
 */
615
unsigned long long
616 617
xenXMDomainGetMaxMemory(virConnectPtr conn,
                        virDomainDefPtr def)
618
{
619
    xenUnifiedPrivatePtr priv = conn->privateData;
620
    const char *filename;
621
    xenXMConfCachePtr entry;
622
    unsigned long long ret = 0;
623

D
Daniel P. Berrange 已提交
624
    xenUnifiedLock(priv);
625

626
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
627
        goto cleanup;
628

629
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
630 631
        goto cleanup;

632
    ret = virDomainDefGetMemoryTotal(entry->def);
633

634
 cleanup:
D
Daniel P. Berrange 已提交
635 636
    xenUnifiedUnlock(priv);
    return ret;
637 638
}

639 640
/*
 * xenXMDomainSetVcpusFlags:
641 642
 * @conn: the connection object
 * @def: domain configuration
643 644 645 646 647
 * @nvcpus: number of vcpus
 * @flags: bitwise-ORd from virDomainVcpuFlags
 *
 * Change virtual CPUs allocation of domain according to flags.
 *
648
 * Returns 0 on success, -1 if an error message was issued
649 650
 */
int
651 652
xenXMDomainSetVcpusFlags(virConnectPtr conn,
                         virDomainDefPtr def,
653
                         unsigned int vcpus,
654 655
                         unsigned int flags)
{
656
    xenUnifiedPrivatePtr priv = conn->privateData;
657 658 659 660 661
    const char *filename;
    xenXMConfCachePtr entry;
    int ret = -1;
    int max;

E
Eric Blake 已提交
662 663 664 665
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

666
    if (flags & VIR_DOMAIN_VCPU_LIVE) {
667 668
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("domain is not running"));
669 670 671 672 673
        return -1;
    }

    xenUnifiedLock(priv);

674
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
675 676 677 678 679 680
        goto cleanup;

    if (!(entry = virHashLookup(priv->configCache, filename)))
        goto cleanup;

    /* Hypervisor maximum. */
681
    if ((max = xenUnifiedConnectGetMaxVcpus(conn, NULL)) < 0) {
682 683
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not determine max vcpus for the domain"));
684 685 686 687 688
        goto cleanup;
    }
    /* Can't specify a current larger than stored maximum; but
     * reducing maximum can silently reduce current.  */
    if (!(flags & VIR_DOMAIN_VCPU_MAXIMUM))
689 690
        max = virDomainDefGetVcpusMax(entry->def);

691
    if (vcpus > max) {
692 693 694
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested vcpus is greater than max allowable"
                         " vcpus for the domain: %d > %d"), vcpus, max);
695 696 697 698
        goto cleanup;
    }

    if (flags & VIR_DOMAIN_VCPU_MAXIMUM) {
699
        if (virDomainDefSetVcpusMax(entry->def, vcpus, priv->xmlopt) < 0)
700
            goto cleanup;
701
    } else {
702 703
        if (virDomainDefSetVcpus(entry->def, vcpus) < 0)
            goto cleanup;
704 705 706
    }

    /* If this fails, should we try to undo our changes to the
707 708
     * in-memory representation of the config file. I say not!
     */
709
    if (xenXMConfigSaveFile(conn, entry->filename, entry->def) < 0)
D
Daniel P. Berrange 已提交
710 711
        goto cleanup;
    ret = 0;
712

713
 cleanup:
D
Daniel P. Berrange 已提交
714 715
    xenUnifiedUnlock(priv);
    return ret;
716 717
}

718 719
/**
 * xenXMDomainGetVcpusFlags:
720 721
 * @conn: the connection object
 * @def: domain configuration
722 723 724 725 726
 * @flags: bitwise-ORd from virDomainVcpuFlags
 *
 * Extract information about virtual CPUs of domain according to flags.
 *
 * Returns the number of vcpus on success, -1 if an error message was
727
 * issued
728 729
 */
int
730 731 732
xenXMDomainGetVcpusFlags(virConnectPtr conn,
                         virDomainDefPtr def,
                         unsigned int flags)
733
{
734
    xenUnifiedPrivatePtr priv = conn->privateData;
735 736
    const char *filename;
    xenXMConfCachePtr entry;
737
    int ret = -1;
738

E
Eric Blake 已提交
739 740 741 742
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

743
    if (flags & VIR_DOMAIN_VCPU_LIVE) {
744
        virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("domain not active"));
745 746 747 748 749
        return -1;
    }

    xenUnifiedLock(priv);

750
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
751 752 753 754 755
        goto cleanup;

    if (!(entry = virHashLookup(priv->configCache, filename)))
        goto cleanup;

756 757 758
    if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
        ret = virDomainDefGetVcpusMax(entry->def);
    else
759
        ret = virDomainDefGetVcpus(entry->def);
760

761
 cleanup:
762 763 764 765
    xenUnifiedUnlock(priv);
    return ret;
}

766 767
/**
 * xenXMDomainPinVcpu:
768 769
 * @conn: the connection object
 * @def: domain configuration
770 771 772 773 774 775 776 777
 * @vcpu: virtual CPU number (reserved)
 * @cpumap: pointer to a bit map of real CPUs (in 8-bit bytes)
 * @maplen: length of cpumap in bytes
 *
 * Set the vcpu affinity in config
 *
 * Returns 0 for success; -1 (with errno) on error
 */
778
int
779 780
xenXMDomainPinVcpu(virConnectPtr conn,
                   virDomainDefPtr def,
781 782 783
                   unsigned int vcpu ATTRIBUTE_UNUSED,
                   unsigned char *cpumap,
                   int maplen)
784
{
785
    xenUnifiedPrivatePtr priv = conn->privateData;
786 787 788 789
    const char *filename;
    xenXMConfCachePtr entry;
    int ret = -1;

790
    if (maplen > (int)sizeof(cpumap_t)) {
791
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
792 793 794
        return -1;
    }

D
Daniel P. Berrange 已提交
795
    xenUnifiedLock(priv);
796

797
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name))) {
798
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("virHashLookup"));
D
Daniel P. Berrange 已提交
799
        goto cleanup;
800
    }
801
    if (!(entry = virHashLookup(priv->configCache, filename))) {
802 803
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("can't retrieve config file for domain"));
D
Daniel P. Berrange 已提交
804
        goto cleanup;
805 806
    }

H
Hu Tao 已提交
807
    virBitmapFree(entry->def->cpumask);
L
liguang 已提交
808 809 810
    entry->def->cpumask = virBitmapNewData(cpumap, maplen);
    if (!entry->def->cpumask)
        goto cleanup;
811
    if (xenXMConfigSaveFile(conn, entry->filename, entry->def) < 0)
812 813 814 815 816
        goto cleanup;

    ret = 0;

 cleanup:
D
Daniel P. Berrange 已提交
817
    xenUnifiedUnlock(priv);
818
    return ret;
819 820
}

821 822 823
/*
 * Find an inactive domain based on its name
 */
824
virDomainDefPtr
825 826 827
xenXMDomainLookupByName(virConnectPtr conn, const char *domname)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
828
    const char *filename;
829
    xenXMConfCachePtr entry;
830
    virDomainDefPtr ret = NULL;
831

D
Daniel P. Berrange 已提交
832
    xenUnifiedLock(priv);
833

834
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
835
        goto cleanup;
836

837
    if (!(filename = virHashLookup(priv->nameConfigMap, domname)))
D
Daniel P. Berrange 已提交
838
        goto cleanup;
839

D
Daniel P. Berrange 已提交
840 841
    if (!(entry = virHashLookup(priv->configCache, filename)))
        goto cleanup;
842

843
    ret = virDomainDefNewFull(domname, entry->def->uuid, -1);
844

845
 cleanup:
D
Daniel P. Berrange 已提交
846
    xenUnifiedUnlock(priv);
847
    return ret;
848 849 850 851 852 853
}


/*
 * Hash table iterator to search for a domain based on UUID
 */
854 855 856 857 858
static int
xenXMDomainSearchForUUID(const void *payload,
                         const void *name ATTRIBUTE_UNUSED,
                         const void *data)
{
859 860
    const unsigned char *wantuuid = data;
    const xenXMConfCache *entry = payload;
861

862
    if (!memcmp(entry->def->uuid, wantuuid, VIR_UUID_BUFLEN))
863
        return 1;
864

865
    return 0;
866 867 868 869 870
}

/*
 * Find an inactive domain based on its UUID
 */
871
virDomainDefPtr
872 873 874
xenXMDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
875
    xenXMConfCachePtr entry;
876
    virDomainDefPtr ret = NULL;
877

D
Daniel P. Berrange 已提交
878
    xenUnifiedLock(priv);
879

880
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
881
        goto cleanup;
882

883 884
    if (!(entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID,
                                (const void *)uuid, NULL)))
D
Daniel P. Berrange 已提交
885
        goto cleanup;
886

887
    ret = virDomainDefNewFull(entry->def->name, uuid, -1);
888

889
 cleanup:
D
Daniel P. Berrange 已提交
890
    xenUnifiedUnlock(priv);
891
    return ret;
892 893 894 895 896 897
}


/*
 * Start a domain from an existing defined config file
 */
898
int
899 900
xenXMDomainCreate(virConnectPtr conn,
                  virDomainDefPtr def)
901
{
902
    char *sexpr;
D
Daniel P. Berrange 已提交
903
    int ret = -1;
904
    xenUnifiedPrivatePtr priv = conn->privateData;
905
    const char *filename;
906
    xenXMConfCachePtr entry = NULL;
907

D
Daniel P. Berrange 已提交
908 909
    xenUnifiedLock(priv);

910
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
911
        goto error;
912

913
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
914
        goto error;
915

916
    if (!(sexpr = xenFormatSxpr(conn, entry->def)))
D
Daniel P. Berrange 已提交
917
        goto error;
918

919
    ret = xenDaemonDomainCreateXML(conn, sexpr);
920
    VIR_FREE(sexpr);
D
Daniel P. Berrange 已提交
921 922
    if (ret != 0)
        goto error;
923

924
    if ((ret = xenDaemonDomainLookupByName_ids(conn, def->name,
D
Daniel P. Berrange 已提交
925 926
                                               entry->def->uuid)) < 0)
        goto error;
927
    def->id = ret;
928

929
    if (xend_wait_for_devices(conn, def->name) < 0)
D
Daniel P. Berrange 已提交
930
        goto error;
931

932
    if (xenDaemonDomainResume(conn, entry->def) < 0)
D
Daniel P. Berrange 已提交
933
        goto error;
934

D
Daniel P. Berrange 已提交
935
    xenUnifiedUnlock(priv);
936
    return 0;
937

D
Daniel P. Berrange 已提交
938
 error:
939 940 941
    if (def->id != -1 && entry) {
        xenDaemonDomainDestroy(conn, entry->def);
        def->id = -1;
942
    }
D
Daniel P. Berrange 已提交
943
    xenUnifiedUnlock(priv);
944
    return -1;
945 946
}

947 948 949 950
/*
 * Create a config file for a domain, based on an XML
 * document describing its config
 */
951 952
int
xenXMDomainDefineXML(virConnectPtr conn, virDomainDefPtr def)
953
{
954
    char *filename = NULL;
955
    const char *oldfilename;
956
    virConfPtr conf = NULL;
957
    xenXMConfCachePtr entry = NULL;
958
    xenUnifiedPrivatePtr priv = conn->privateData;
959

D
Daniel P. Berrange 已提交
960 961
    xenUnifiedLock(priv);

962
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0) {
D
Daniel P. Berrange 已提交
963
        xenUnifiedUnlock(priv);
964
        return -1;
D
Daniel P. Berrange 已提交
965
    }
966

967
    if (!(conf = xenFormatXM(conn, def)))
968 969
        goto error;

970 971 972 973 974
    /*
     * check that if there is another domain defined with the same uuid
     * it has the same name
     */
    if ((entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID,
975
                               (const void *)&(def->uuid), NULL)) != NULL) {
976 977 978 979 980
        if ((entry->def != NULL) && (entry->def->name != NULL) &&
            (STRNEQ(def->name, entry->def->name))) {
            char uuidstr[VIR_UUID_STRING_BUFLEN];

            virUUIDFormat(entry->def->uuid, uuidstr);
981 982 983
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("domain '%s' is already defined with uuid %s"),
                           entry->def->name, uuidstr);
984 985 986 987 988 989
            entry = NULL;
            goto error;
        }
        entry = NULL;
    }

990
    if (virHashLookup(priv->nameConfigMap, def->name)) {
991 992
        /* domain exists, we will overwrite it */

993
        if (!(oldfilename = (char *)virHashLookup(priv->nameConfigMap, def->name))) {
994 995
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("can't retrieve config filename for domain to overwrite"));
996 997 998
            goto error;
        }

999
        if (!(entry = virHashLookup(priv->configCache, oldfilename))) {
1000 1001
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("can't retrieve config entry for domain to overwrite"));
1002 1003 1004 1005
            goto error;
        }

        /* Remove the name -> filename mapping */
1006
        if (virHashRemoveEntry(priv->nameConfigMap, def->name) < 0) {
1007 1008
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("failed to remove old domain from config map"));
1009 1010 1011 1012
            goto error;
        }

        /* Remove the config record itself */
1013
        if (virHashRemoveEntry(priv->configCache, oldfilename) < 0) {
1014 1015
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("failed to remove old domain from config map"));
1016 1017 1018 1019
            goto error;
        }

        entry = NULL;
1020
    }
1021

1022
    if (!(filename = virFileBuildPath(priv->configDir, def->name, NULL)))
1023 1024
        goto error;

1025
    if (virConfWriteFile(filename, conf) < 0)
1026 1027
        goto error;

1028
    if (VIR_ALLOC(entry) < 0)
1029 1030
        goto error;

1031
    if ((entry->refreshedAt = time(NULL)) == ((time_t)-1)) {
1032 1033
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to get current time"));
1034
        goto error;
1035
    }
1036

1037
    if (VIR_STRDUP(entry->filename, filename) < 0)
E
Eric Blake 已提交
1038
        goto error;
1039
    entry->def = def;
1040

1041
    if (virHashAddEntry(priv->configCache, filename, entry) < 0) {
1042 1043
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to store config file handle"));
1044
        goto error;
1045
    }
1046

1047
    if (virHashAddEntry(priv->nameConfigMap, def->name, entry->filename) < 0) {
1048
        virHashSteal(priv->configCache, filename);
1049 1050
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to store config file handle"));
1051
        goto error;
1052 1053
    }

D
Daniel P. Berrange 已提交
1054
    xenUnifiedUnlock(priv);
1055
    VIR_FREE(filename);
1056
    return 0;
1057 1058

 error:
1059
    VIR_FREE(filename);
1060 1061
    if (entry)
        VIR_FREE(entry->filename);
1062
    VIR_FREE(entry);
1063
    virConfFree(conf);
D
Daniel P. Berrange 已提交
1064
    xenUnifiedUnlock(priv);
1065
    return -1;
1066 1067 1068 1069 1070
}

/*
 * Delete a domain from disk
 */
1071
int
1072 1073
xenXMDomainUndefine(virConnectPtr conn,
                    virDomainDefPtr def)
1074
{
1075
    xenUnifiedPrivatePtr priv = conn->privateData;
1076
    const char *filename;
1077
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
1078 1079 1080
    int ret = -1;

    xenUnifiedLock(priv);
1081

1082
    if (!(filename = virHashLookup(priv->nameConfigMap, def->name)))
D
Daniel P. Berrange 已提交
1083
        goto cleanup;
1084

1085
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1086
        goto cleanup;
1087 1088

    if (unlink(entry->filename) < 0)
D
Daniel P. Berrange 已提交
1089
        goto cleanup;
1090

1091
    /* Remove the name -> filename mapping */
1092
    if (virHashRemoveEntry(priv->nameConfigMap, def->name) < 0)
1093
        goto cleanup;
1094

1095 1096 1097
    /* Remove the config record itself */
    if (virHashRemoveEntry(priv->configCache, entry->filename) < 0)
        goto cleanup;
1098

D
Daniel P. Berrange 已提交
1099 1100
    ret = 0;

1101
 cleanup:
D
Daniel P. Berrange 已提交
1102 1103
    xenUnifiedUnlock(priv);
    return ret;
1104 1105 1106 1107
}

struct xenXMListIteratorContext {
    virConnectPtr conn;
1108
    int oom;
1109 1110
    int max;
    int count;
1111
    char ** names;
1112 1113
};

1114
static int
1115 1116 1117 1118
xenXMListIterator(void *payload ATTRIBUTE_UNUSED,
                  const void *name,
                  void *data)
{
1119
    struct xenXMListIteratorContext *ctx = data;
1120
    virDomainDefPtr def = NULL;
1121

1122
    if (ctx->oom)
1123
        return 0;
1124

1125
    if (ctx->count == ctx->max)
1126
        return 0;
1127

1128 1129
    def = xenDaemonLookupByName(ctx->conn, name);
    if (!def) {
1130
        if (VIR_STRDUP(ctx->names[ctx->count], name) < 0)
1131 1132 1133
            ctx->oom = 1;
        else
            ctx->count++;
1134
    } else {
1135
        virDomainDefFree(def);
1136
    }
1137
    return 0;
1138 1139 1140 1141 1142 1143 1144
}


/*
 * List all defined domains, filtered to remove any which
 * are currently running
 */
1145 1146 1147 1148
int
xenXMListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
1149
    struct xenXMListIteratorContext ctx;
1150 1151
    size_t i;
    int ret = -1;
1152

D
Daniel P. Berrange 已提交
1153
    xenUnifiedLock(priv);
1154

1155
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
1156
        goto cleanup;
1157

1158 1159
    if (maxnames > virHashSize(priv->configCache))
        maxnames = virHashSize(priv->configCache);
1160 1161

    ctx.conn = conn;
1162
    ctx.oom = 0;
1163 1164 1165 1166
    ctx.count = 0;
    ctx.max = maxnames;
    ctx.names = names;

1167
    virHashForEach(priv->nameConfigMap, xenXMListIterator, &ctx);
1168 1169 1170 1171 1172 1173 1174

    if (ctx.oom) {
        for (i = 0; i < ctx.count; i++)
            VIR_FREE(ctx.names[i]);
        goto cleanup;
    }

D
Daniel P. Berrange 已提交
1175 1176
    ret = ctx.count;

1177
 cleanup:
D
Daniel P. Berrange 已提交
1178 1179
    xenUnifiedUnlock(priv);
    return ret;
1180 1181 1182 1183 1184 1185
}

/*
 * Return the maximum number of defined domains - not filtered
 * based on number running
 */
1186 1187 1188 1189
int
xenXMNumOfDefinedDomains(virConnectPtr conn)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
D
Daniel P. Berrange 已提交
1190
    int ret = -1;
1191

D
Daniel P. Berrange 已提交
1192
    xenUnifiedLock(priv);
1193

1194
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
1195
        goto cleanup;
1196

D
Daniel P. Berrange 已提交
1197 1198
    ret = virHashSize(priv->nameConfigMap);

1199
 cleanup:
D
Daniel P. Berrange 已提交
1200 1201
    xenUnifiedUnlock(priv);
    return ret;
1202 1203
}

1204

1205
/**
1206
 * xenXMDomainAttachDeviceFlags:
1207 1208
 * @conn: the connection object
 * @minidef: domain configuration
1209
 * @xml: pointer to XML description of device
1210
 * @flags: an OR'ed set of virDomainDeviceModifyFlags
J
Jim Meyering 已提交
1211
 *
1212 1213
 * Create a virtual device attachment to backend.
 * XML description is translated into config file.
1214 1215
 * This driver only supports device allocation to
 * persisted config.
J
Jim Meyering 已提交
1216
 *
1217 1218
 * Returns 0 in case of success, -1 in case of failure.
 */
1219
int
1220 1221
xenXMDomainAttachDeviceFlags(virConnectPtr conn,
                             virDomainDefPtr minidef,
1222
                             const char *xml,
E
Eric Blake 已提交
1223 1224
                             unsigned int flags)
{
1225 1226
    const char *filename = NULL;
    xenXMConfCachePtr entry = NULL;
1227 1228
    int ret = -1;
    virDomainDeviceDefPtr dev = NULL;
1229
    virDomainDefPtr def;
1230
    xenUnifiedPrivatePtr priv = conn->privateData;
1231

E
Eric Blake 已提交
1232 1233
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1);

1234
    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) ||
1235
        (minidef->id != -1 && flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)) {
1236 1237
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Xm driver only supports modifying persistent config"));
1238
        return -1;
1239
    }
1240

D
Daniel P. Berrange 已提交
1241 1242
    xenUnifiedLock(priv);

1243
    if (!(filename = virHashLookup(priv->nameConfigMap, minidef->name)))
D
Daniel P. Berrange 已提交
1244
        goto cleanup;
1245
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1246
        goto cleanup;
1247
    def = entry->def;
1248

1249 1250
    if (!(dev = virDomainDeviceDefParse(xml, entry->def,
                                        priv->caps,
1251
                                        priv->xmlopt,
1252
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE)))
D
Daniel P. Berrange 已提交
1253
        goto cleanup;
1254

1255 1256 1257
    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
    {
1258
        if (virDomainDiskInsert(def, dev->data.disk) < 0)
1259
            goto cleanup;
1260
        dev->data.disk = NULL;
1261
    }
1262
    break;
1263

1264 1265
    case VIR_DOMAIN_DEVICE_NET:
    {
1266
        if (VIR_APPEND_ELEMENT(def->nets, def->nnets, dev->data.net) < 0)
1267
            goto cleanup;
1268
        break;
1269 1270
    }

1271
    default:
1272 1273
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Xm driver only supports adding disk or network devices"));
1274 1275 1276 1277 1278 1279
        goto cleanup;
    }

    /* If this fails, should we try to undo our changes to the
     * in-memory representation of the config file. I say not!
     */
1280
    if (xenXMConfigSaveFile(conn, entry->filename, entry->def) < 0)
1281 1282 1283 1284 1285
        goto cleanup;

    ret = 0;

 cleanup:
1286
    virDomainDeviceDefFree(dev);
D
Daniel P. Berrange 已提交
1287
    xenUnifiedUnlock(priv);
1288 1289 1290 1291 1292
    return ret;
}


/**
1293
 * xenXMDomainDetachDeviceFlags:
1294 1295
 * @conn: the connection object
 * @minidef: domain configuration
1296
 * @xml: pointer to XML description of device
1297
 * @flags: an OR'ed set of virDomainDeviceModifyFlags
J
Jim Meyering 已提交
1298
 *
1299
 * Destroy a virtual device attachment to backend.
1300 1301
 * This driver only supports device deallocation from
 * persisted config.
1302 1303 1304
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
1305
int
1306 1307
xenXMDomainDetachDeviceFlags(virConnectPtr conn,
                             virDomainDefPtr minidef,
1308 1309 1310
                             const char *xml,
                             unsigned int flags)
{
1311 1312
    const char *filename = NULL;
    xenXMConfCachePtr entry = NULL;
1313
    virDomainDeviceDefPtr dev = NULL;
1314
    virDomainDefPtr def;
1315
    int ret = -1;
1316
    size_t i;
1317
    xenUnifiedPrivatePtr priv = conn->privateData;
1318

E
Eric Blake 已提交
1319 1320
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1);

1321
    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) ||
1322
        (minidef->id != -1 && flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)) {
1323 1324
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Xm driver only supports modifying persistent config"));
1325
        return -1;
1326
    }
D
Daniel P. Berrange 已提交
1327 1328 1329

    xenUnifiedLock(priv);

1330
    if (!(filename = virHashLookup(priv->nameConfigMap, minidef->name)))
D
Daniel P. Berrange 已提交
1331
        goto cleanup;
1332
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1333
        goto cleanup;
1334
    def = entry->def;
1335

1336 1337
    if (!(dev = virDomainDeviceDefParse(xml, entry->def,
                                        priv->caps,
1338
                                        priv->xmlopt,
1339 1340
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE |
                                        VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE)))
D
Daniel P. Berrange 已提交
1341
        goto cleanup;
1342

1343 1344 1345
    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
    {
1346
        for (i = 0; i < def->ndisks; i++) {
1347
            if (def->disks[i]->dst &&
1348
                dev->data.disk->dst &&
1349 1350
                STREQ(def->disks[i]->dst, dev->data.disk->dst)) {
                virDomainDiskDefFree(def->disks[i]);
1351
                VIR_DELETE_ELEMENT(def->disks, i, def->ndisks);
1352
                break;
1353 1354
            }
        }
1355 1356 1357 1358 1359
        break;
    }

    case VIR_DOMAIN_DEVICE_NET:
    {
1360
        for (i = 0; i < def->nnets; i++) {
1361
            if (!virMacAddrCmp(&def->nets[i]->mac, &dev->data.net->mac)) {
1362
                virDomainNetDefFree(def->nets[i]);
1363
                VIR_DELETE_ELEMENT(def->nets, i, def->nnets);
1364 1365 1366 1367
                break;
            }
        }
        break;
1368
    }
1369
    default:
1370 1371 1372
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("device type '%s' cannot be detached"),
                       virDomainDeviceTypeToString(dev->type));
1373 1374 1375 1376 1377 1378
        goto cleanup;
    }

    /* If this fails, should we try to undo our changes to the
     * in-memory representation of the config file. I say not!
     */
1379
    if (xenXMConfigSaveFile(conn, entry->filename, entry->def) < 0)
1380 1381 1382 1383 1384
        goto cleanup;

    ret = 0;

 cleanup:
1385
    virDomainDeviceDefFree(dev);
D
Daniel P. Berrange 已提交
1386
    xenUnifiedUnlock(priv);
1387
    return ret;
1388 1389
}

R
Richard W.M. Jones 已提交
1390
int
1391 1392
xenXMDomainBlockPeek(virConnectPtr conn ATTRIBUTE_UNUSED,
                     virDomainDefPtr def ATTRIBUTE_UNUSED,
1393 1394 1395 1396
                     const char *path ATTRIBUTE_UNUSED,
                     unsigned long long offset ATTRIBUTE_UNUSED,
                     size_t size ATTRIBUTE_UNUSED,
                     void *buffer ATTRIBUTE_UNUSED)
R
Richard W.M. Jones 已提交
1397
{
1398 1399
    virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                   _("block peeking not implemented"));
R
Richard W.M. Jones 已提交
1400 1401 1402
    return -1;
}

1403

1404
static char *
1405
xenXMAutostartLinkName(virDomainDefPtr def)
1406 1407
{
    char *ret;
1408
    if (virAsprintf(&ret, "/etc/xen/auto/%s", def->name) < 0)
1409
        return NULL;
1410 1411 1412
    return ret;
}

1413
static char *
1414
xenXMDomainConfigName(virDomainDefPtr def)
1415 1416
{
    char *ret;
1417
    if (virAsprintf(&ret, "/etc/xen/%s", def->name) < 0)
1418
        return NULL;
1419 1420 1421
    return ret;
}

1422
int
1423 1424
xenXMDomainGetAutostart(virDomainDefPtr def,
                        int *autostart)
1425
{
1426
    char *config = xenXMDomainConfigName(def);
1427 1428
    int ret = -1;

1429
    if (!config)
1430 1431
        goto cleanup;

1432
    *autostart = virFileRelLinkPointsTo("/etc/xen/auto/", def->name, config);
1433
    if (*autostart < 0) {
1434
        virReportSystemError(errno,
1435 1436 1437
                             _("cannot check link /etc/xen/auto/%s points "
                               "to config %s"),
                             def->name, config);
1438 1439 1440 1441 1442
        goto cleanup;
    }

    ret = 0;

1443
 cleanup:
1444 1445 1446 1447 1448
    VIR_FREE(config);
    return ret;
}


1449
int
1450 1451
xenXMDomainSetAutostart(virDomainDefPtr def,
                        int autostart)
1452
{
1453 1454
    char *linkname = xenXMAutostartLinkName(def);
    char *config = xenXMDomainConfigName(def);
1455 1456
    int ret = -1;

1457
    if (!linkname || !config)
1458 1459 1460 1461 1462
        goto cleanup;

    if (autostart) {
        if (symlink(config, linkname) < 0 &&
            errno != EEXIST) {
1463
            virReportSystemError(errno,
1464 1465
                                 _("failed to create link %s to %s"),
                                 config, linkname);
1466 1467 1468 1469 1470
            goto cleanup;
        }
    } else {
        if (unlink(linkname)  < 0 &&
            errno != ENOENT) {
1471
            virReportSystemError(errno,
1472 1473
                                 _("failed to remove link %s"),
                                 linkname);
1474 1475 1476 1477 1478
            goto cleanup;
        }
    }
    ret = 0;

1479
 cleanup:
1480 1481 1482 1483 1484
    VIR_FREE(linkname);
    VIR_FREE(config);

    return ret;
}