You need to sign in or sign up before continuing.
xm_internal.c 40.8 KB
Newer Older
1 2 3
/*
 * xm_internal.h: helper routines for dealing with inactive domains
 *
4
 * Copyright (C) 2006-2007, 2009-2013 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 "datatypes.h"
40
#include "xm_internal.h"
41
#include "xen_driver.h"
42
#include "xend_internal.h"
43
#include "xen_sxpr.h"
44
#include "xen_xm.h"
45
#include "virhash.h"
46
#include "virbuffer.h"
47
#include "viruuid.h"
48
#include "viralloc.h"
49
#include "virlog.h"
50
#include "count-one-bits.h"
51
#include "virstring.h"
52

53
#define VIR_FROM_THIS VIR_FROM_XENXM
54

55
#ifdef WITH_RHEL5_API
56 57
# define XEND_CONFIG_MAX_VERS_NET_TYPE_IOEMU 0
# define XEND_CONFIG_MIN_VERS_PVFB_NEWCONF 2
58
#else
59 60
# define XEND_CONFIG_MAX_VERS_NET_TYPE_IOEMU 3
# define XEND_CONFIG_MIN_VERS_PVFB_NEWCONF 3
61 62
#endif

63 64 65 66 67
/* 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

68
char * xenXMAutoAssignMac(void);
69 70 71 72
static int xenXMDomainAttachDeviceFlags(virDomainPtr domain, const char *xml,
                                        unsigned int flags);
static int xenXMDomainDetachDeviceFlags(virDomainPtr domain, const char *xml,
                                        unsigned int flags);
73

74 75 76 77 78 79 80
#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"
81
#define XM_XML_ERROR "Invalid xml"
82

83
struct xenUnifiedDriver xenXMDriver = {
E
Eric Blake 已提交
84 85 86 87 88 89 90
    .xenListDefinedDomains = xenXMListDefinedDomains,
    .xenNumOfDefinedDomains = xenXMNumOfDefinedDomains,
    .xenDomainCreate = xenXMDomainCreate,
    .xenDomainDefineXML = xenXMDomainDefineXML,
    .xenDomainUndefine = xenXMDomainUndefine,
    .xenDomainAttachDeviceFlags = xenXMDomainAttachDeviceFlags,
    .xenDomainDetachDeviceFlags = xenXMDomainDetachDeviceFlags,
91 92
};

93
#ifndef WITH_XEN_INOTIFY
94 95 96 97
static int xenInotifyActive(virConnectPtr conn ATTRIBUTE_UNUSED)
{
   return 0;
}
98
#else
99 100
static int xenInotifyActive(virConnectPtr conn)
{
101
   xenUnifiedPrivatePtr priv = conn->privateData;
102
   return priv->inotifyWatch > 0;
103
}
104
#endif
105

106 107

/* Release memory associated with a cached config object */
108
static void xenXMConfigFree(void *payload, const void *key ATTRIBUTE_UNUSED) {
109
    xenXMConfCachePtr entry = (xenXMConfCachePtr)payload;
110
    virDomainDefFree(entry->def);
E
Eric Blake 已提交
111
    VIR_FREE(entry->filename);
112
    VIR_FREE(entry);
113 114
}

115 116 117 118
struct xenXMConfigReaperData {
    xenUnifiedPrivatePtr priv;
    time_t now;
};
119

120
/* Remove any configs which were not refreshed recently */
121 122 123 124 125
static int
xenXMConfigReaper(const void *payload,
                  const void *key ATTRIBUTE_UNUSED,
                  const void *data)
{
126
    const struct xenXMConfigReaperData *args = data;
127 128
    xenXMConfCachePtr entry = (xenXMConfCachePtr)payload;

129 130
    /* We're going to purge this config file, so check if it
       is currently mapped as owner of a named domain. */
131
    if (entry->refreshedAt != args->now) {
132
        const char *olddomname = entry->def->name;
133
        char *nameowner = (char *)virHashLookup(args->priv->nameConfigMap, olddomname);
134
        if (nameowner && STREQ(nameowner, key)) {
135
            virHashRemoveEntry(args->priv->nameConfigMap, olddomname);
136
        }
137
        return 1;
138
    }
139
    return 0;
140 141
}

142 143

static virDomainDefPtr
144 145
xenXMConfigReadFile(virConnectPtr conn, const char *filename)
{
146 147
    virConfPtr conf;
    virDomainDefPtr def;
148
    xenUnifiedPrivatePtr priv = conn->privateData;
149

150
    if (!(conf = virConfReadFile(filename, 0)))
151 152
        return NULL;

M
Markus Groß 已提交
153
    def = xenParseXM(conf, priv->xendConfigVersion, priv->caps);
154 155 156 157 158 159
    virConfFree(conf);

    return def;
}

static int
160 161 162 163
xenXMConfigSaveFile(virConnectPtr conn,
                    const char *filename,
                    virDomainDefPtr def)
{
164
    virConfPtr conf;
165
    xenUnifiedPrivatePtr priv = conn->privateData;
166 167
    int ret;

M
Markus Groß 已提交
168
    if (!(conf = xenFormatXM(conn, def, priv->xendConfigVersion)))
169 170 171 172 173 174 175
        return -1;

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

D
Daniel P. Berrange 已提交
176 177 178

/*
 * Caller must hold the lock on 'conn->privateData' before
J
Ján Tomko 已提交
179
 * calling this function
D
Daniel P. Berrange 已提交
180
 */
181
int
182
xenXMConfigCacheRemoveFile(virConnectPtr conn, const char *filename)
183
{
184
    xenUnifiedPrivatePtr priv = conn->privateData;
185 186
    xenXMConfCachePtr entry;

187
    entry = virHashLookup(priv->configCache, filename);
188
    if (!entry) {
189
        VIR_DEBUG("No config entry for %s", filename);
190 191 192
        return 0;
    }

193 194
    virHashRemoveEntry(priv->nameConfigMap, entry->def->name);
    virHashRemoveEntry(priv->configCache, filename);
195
    VIR_DEBUG("Removed %s %s", entry->def->name, filename);
196 197 198 199
    return 0;
}


D
Daniel P. Berrange 已提交
200 201
/*
 * Caller must hold the lock on 'conn->privateData' before
J
Ján Tomko 已提交
202
 * calling this function
D
Daniel P. Berrange 已提交
203
 */
204 205 206
int
xenXMConfigCacheAddFile(virConnectPtr conn, const char *filename)
{
207
    xenUnifiedPrivatePtr priv = conn->privateData;
208 209 210 211 212
    xenXMConfCachePtr entry;
    struct stat st;
    int newborn = 0;
    time_t now = time(NULL);

213
    VIR_DEBUG("Adding file %s", filename);
214 215 216

    /* Get modified time */
    if ((stat(filename, &st) < 0)) {
217
        virReportSystemError(errno,
218 219
                             _("cannot stat: %s"),
                             filename);
220 221 222 223 224 225
        return -1;
    }

    /* Ignore zero length files, because inotify fires before
       any content has actually been created */
    if (st.st_size == 0) {
226
        VIR_DEBUG("Ignoring zero length file %s", filename);
227 228 229 230 231
        return -1;
    }

    /* If we already have a matching entry and it is not
    modified, then carry on to next one*/
232
    if ((entry = virHashLookup(priv->configCache, filename))) {
233 234 235 236 237 238 239 240 241 242
        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 */
243
        nameowner = (char *)virHashLookup(priv->nameConfigMap, entry->def->name);
244
        if (nameowner && STREQ(nameowner, filename)) {
245
            virHashRemoveEntry(priv->nameConfigMap, entry->def->name);
246 247 248 249 250 251 252 253
        }

        /* Clear existing config entry which needs refresh */
        virDomainDefFree(entry->def);
        entry->def = NULL;
    } else { /* Completely new entry */
        newborn = 1;
        if (VIR_ALLOC(entry) < 0) {
254
            virReportOOMError();
255 256
            return -1;
        }
E
Eric Blake 已提交
257 258 259 260 261
        if ((entry->filename = strdup(filename)) == NULL) {
            virReportOOMError();
            VIR_FREE(entry);
            return -1;
        }
262 263 264 265
    }
    entry->refreshedAt = now;

    if (!(entry->def = xenXMConfigReadFile(conn, entry->filename))) {
266
        VIR_DEBUG("Failed to read %s", entry->filename);
267
        if (!newborn)
268
            virHashSteal(priv->configCache, filename);
E
Eric Blake 已提交
269
        VIR_FREE(entry->filename);
270 271 272 273 274 275 276
        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) {
277
        if (virHashAddEntry(priv->configCache, entry->filename, entry) < 0) {
278
            virDomainDefFree(entry->def);
E
Eric Blake 已提交
279
            VIR_FREE(entry->filename);
280
            VIR_FREE(entry);
281 282
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("xenXMConfigCacheRefresh: virHashAddEntry"));
283 284 285 286 287 288 289
            return -1;
        }
    }

    /* See if we need to map this config file in as the primary owner
        * of the domain in question
        */
290
    if (!virHashLookup(priv->nameConfigMap, entry->def->name)) {
E
Eric Blake 已提交
291 292
        if (virHashAddEntry(priv->nameConfigMap, entry->def->name,
                            entry->filename) < 0) {
293
            virHashSteal(priv->configCache, filename);
294
            virDomainDefFree(entry->def);
E
Eric Blake 已提交
295
            VIR_FREE(entry->filename);
296 297 298
            VIR_FREE(entry);
        }
    }
299
    VIR_DEBUG("Added config %s %s", entry->def->name, filename);
300 301 302

    return 0;
}
303

304
/* This method is called by various methods to scan /etc/xen
D
Daniel P. Berrange 已提交
305 306 307 308 309 310
 * (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 已提交
311
 * calling this function
D
Daniel P. Berrange 已提交
312
 */
313 314 315
int
xenXMConfigCacheRefresh(virConnectPtr conn)
{
316
    xenUnifiedPrivatePtr priv = conn->privateData;
317 318 319 320
    DIR *dh;
    struct dirent *ent;
    time_t now = time(NULL);
    int ret = -1;
321
    struct xenXMConfigReaperData args;
322 323

    if (now == ((time_t)-1)) {
324
        virReportSystemError(errno,
325
                             "%s", _("cannot get time of day"));
326
        return -1;
327 328 329
    }

    /* Rate limit re-scans */
330
    if ((now - priv->lastRefresh) < XM_REFRESH_INTERVAL)
331
        return 0;
332

333
    priv->lastRefresh = now;
334 335

    /* Process the files in the config dir */
336
    if (!(dh = opendir(priv->configDir))) {
337
        virReportSystemError(errno,
338
                             _("cannot read directory %s"),
339
                             priv->configDir);
340
        return -1;
341 342 343 344
    }

    while ((ent = readdir(dh))) {
        struct stat st;
345
        char *path;
346 347 348 349 350 351

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

        /* Like 'dot' files... */
352
        if (STRPREFIX(ent->d_name, "."))
353 354
            continue;
        /* ...and the XenD server config file */
355
        if (STRPREFIX(ent->d_name, XEND_CONFIG_FILE))
356 357
            continue;
        /* ...and random PCI config cruft */
358
        if (STRPREFIX(ent->d_name, XEND_PCI_CONFIG_PREFIX))
359 360
            continue;
        /* ...and the example domain configs */
361
        if (STRPREFIX(ent->d_name, XM_EXAMPLE_PREFIX))
362 363
            continue;
        /* ...and the QEMU networking script */
364
        if (STRPREFIX(ent->d_name, QEMU_IF_SCRIPT))
365 366 367 368 369 370 371 372 373
            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 */
374 375 376 377
        if (!(path = virFileBuildPath(priv->configDir, ent->d_name, NULL))) {
            closedir(dh);
            return -1;
        }
378 379 380 381

        /* Skip anything which isn't a file (takes care of scripts/ subdir */
        if ((stat(path, &st) < 0) ||
            (!S_ISREG(st.st_mode))) {
382
            VIR_FREE(path);
383 384 385 386 387
            continue;
        }

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

        VIR_FREE(path);
393 394 395 396 397 398
    }

    /* 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 */
399 400
    args.now = now;
    args.priv = priv;
401
    virHashRemoveSet(priv->configCache, xenXMConfigReaper, &args);
402 403
    ret = 0;

404
    closedir(dh);
405

406
    return ret;
407 408 409 410
}


/*
411 412 413 414
 * 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
415
 */
416
int
417 418 419
xenXMOpen(virConnectPtr conn,
          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
          unsigned int flags)
420
{
421 422
    xenUnifiedPrivatePtr priv = conn->privateData;

423
    virCheckFlags(VIR_CONNECT_RO, -1);
E
Eric Blake 已提交
424

425 426
    priv->configDir = XM_CONFIG_DIR;

427
    priv->configCache = virHashCreate(50, xenXMConfigFree);
428
    if (!priv->configCache)
429
        return -1;
430
    priv->nameConfigMap = virHashCreate(50, NULL);
431
    if (!priv->nameConfigMap) {
432
        virHashFree(priv->configCache);
433
        priv->configCache = NULL;
434
        return -1;
435
    }
436 437 438 439
    /* Force the cache to be reloaded next time that
     * xenXMConfigCacheRefresh is called.
     */
    priv->lastRefresh = 0;
440

441
    return 0;
442 443 444
}

/*
445 446
 * Free the cached config files associated with this
 * connection
447
 */
448 449 450
int
xenXMClose(virConnectPtr conn)
{
451 452
    xenUnifiedPrivatePtr priv = conn->privateData;

453 454
    virHashFree(priv->nameConfigMap);
    virHashFree(priv->configCache);
455

456
    return 0;
457 458
}

459 460 461 462
/*
 * Since these are all offline domains, the state is always SHUTOFF.
 */
int
463 464 465
xenXMDomainGetState(virDomainPtr domain ATTRIBUTE_UNUSED,
                    int *state,
                    int *reason)
466 467 468 469 470 471 472 473 474
{
    *state = VIR_DOMAIN_SHUTOFF;
    if (reason)
        *reason = 0;

    return 0;
}


475 476
/*
 * Since these are all offline domains, we only return info about
477
 * VCPUs and memory.
478
 */
479 480 481 482
int
xenXMDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
{
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
483
    const char *filename;
484 485
    xenXMConfCachePtr entry;

D
Daniel P. Berrange 已提交
486
    xenUnifiedLock(priv);
487 488

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
D
Daniel P. Berrange 已提交
489
        goto error;
490

491
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
492
        goto error;
493 494

    memset(info, 0, sizeof(virDomainInfo));
495 496
    info->maxMem = entry->def->mem.max_balloon;
    info->memory = entry->def->mem.cur_balloon;
497
    info->nrVirtCpu = entry->def->vcpus;
498 499 500
    info->state = VIR_DOMAIN_SHUTOFF;
    info->cpuTime = 0;

D
Daniel P. Berrange 已提交
501
    xenUnifiedUnlock(priv);
502
    return 0;
503

D
Daniel P. Berrange 已提交
504 505 506
error:
    xenUnifiedUnlock(priv);
    return -1;
507 508 509
}


510 511
/*
 * Turn a config record into a lump of XML describing the
512
 * domain, suitable for later feeding for virDomainCreateXML
513
 */
514 515
char *
xenXMDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
E
Eric Blake 已提交
516
{
517
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
518 519
    const char *filename;
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
520
    char *ret = NULL;
521

E
Eric Blake 已提交
522 523
    /* Flags checked by virDomainDefFormat */

524
    if (domain->id != -1)
525
        return NULL;
526

D
Daniel P. Berrange 已提交
527
    xenUnifiedLock(priv);
528 529

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
D
Daniel P. Berrange 已提交
530
        goto cleanup;
531

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

535
    ret = virDomainDefFormat(entry->def, flags);
536

D
Daniel P. Berrange 已提交
537 538 539
cleanup:
    xenUnifiedUnlock(priv);
    return ret;
540 541 542
}


543 544 545
/*
 * Update amount of memory in the config file
 */
546 547 548 549
int
xenXMDomainSetMemory(virDomainPtr domain, unsigned long memory)
{
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
550
    const char *filename;
551
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
552
    int ret = -1;
553

554 555 556 557
    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);
558
        return -1;
559
    }
560

D
Daniel P. Berrange 已提交
561
    xenUnifiedLock(priv);
562 563

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
D
Daniel P. Berrange 已提交
564
        goto cleanup;
565

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

569 570 571
    entry->def->mem.cur_balloon = memory;
    if (entry->def->mem.cur_balloon > entry->def->mem.max_balloon)
        entry->def->mem.cur_balloon = entry->def->mem.max_balloon;
572 573 574 575

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

D
Daniel P. Berrange 已提交
580 581 582
cleanup:
    xenUnifiedUnlock(priv);
    return ret;
583 584 585 586 587
}

/*
 * Update maximum memory limit in config
 */
588 589 590 591
int
xenXMDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
{
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
592
    const char *filename;
593
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
594
    int ret = -1;
595

596 597 598 599
    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);
600
        return -1;
601
    }
602

D
Daniel P. Berrange 已提交
603
    xenUnifiedLock(priv);
604 605

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
D
Daniel P. Berrange 已提交
606
        goto cleanup;
607

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

611 612 613
    entry->def->mem.max_balloon = memory;
    if (entry->def->mem.cur_balloon > entry->def->mem.max_balloon)
        entry->def->mem.cur_balloon = entry->def->mem.max_balloon;
614 615 616 617

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

D
Daniel P. Berrange 已提交
622 623 624
cleanup:
    xenUnifiedUnlock(priv);
    return ret;
625 626 627 628 629
}

/*
 * Get max memory limit from config
 */
630 631 632 633
unsigned long long
xenXMDomainGetMaxMemory(virDomainPtr domain)
{
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
634
    const char *filename;
635
    xenXMConfCachePtr entry;
636
    unsigned long long ret = 0;
637

638
    if (domain->id != -1)
639
        return 0;
640

D
Daniel P. Berrange 已提交
641
    xenUnifiedLock(priv);
642 643

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
D
Daniel P. Berrange 已提交
644
        goto cleanup;
645

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

649
    ret = entry->def->mem.max_balloon;
650

D
Daniel P. Berrange 已提交
651 652 653
cleanup:
    xenUnifiedUnlock(priv);
    return ret;
654 655
}

656 657 658 659 660 661 662 663
/*
 * xenXMDomainSetVcpusFlags:
 * @domain: pointer to domain object
 * @nvcpus: number of vcpus
 * @flags: bitwise-ORd from virDomainVcpuFlags
 *
 * Change virtual CPUs allocation of domain according to flags.
 *
664
 * Returns 0 on success, -1 if an error message was issued
665 666
 */
int
667 668
xenXMDomainSetVcpusFlags(virDomainPtr domain,
                         unsigned int vcpus,
669 670
                         unsigned int flags)
{
671
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
672 673 674 675 676
    const char *filename;
    xenXMConfCachePtr entry;
    int ret = -1;
    int max;

E
Eric Blake 已提交
677 678 679 680
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

681
    if (flags & VIR_DOMAIN_VCPU_LIVE) {
682 683
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("domain is not running"));
684 685 686 687 688 689 690 691 692 693 694 695
        return -1;
    }

    xenUnifiedLock(priv);

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
        goto cleanup;

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

    /* Hypervisor maximum. */
696
    if ((max = xenUnifiedConnectGetMaxVcpus(domain->conn, NULL)) < 0) {
697 698
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not determine max vcpus for the domain"));
699 700 701 702 703 704 705
        goto cleanup;
    }
    /* Can't specify a current larger than stored maximum; but
     * reducing maximum can silently reduce current.  */
    if (!(flags & VIR_DOMAIN_VCPU_MAXIMUM))
        max = entry->def->maxvcpus;
    if (vcpus > max) {
706 707 708
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested vcpus is greater than max allowable"
                         " vcpus for the domain: %d > %d"), vcpus, max);
709 710 711 712 713 714 715 716 717 718 719 720
        goto cleanup;
    }

    if (flags & VIR_DOMAIN_VCPU_MAXIMUM) {
        entry->def->maxvcpus = vcpus;
        if (entry->def->vcpus > vcpus)
            entry->def->vcpus = vcpus;
    } else {
        entry->def->vcpus = vcpus;
    }

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

D
Daniel P. Berrange 已提交
727 728 729
cleanup:
    xenUnifiedUnlock(priv);
    return ret;
730 731
}

732 733 734 735 736 737 738 739
/**
 * xenXMDomainGetVcpusFlags:
 * @domain: pointer to domain object
 * @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
740
 * issued
741 742 743 744
 */
int
xenXMDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
{
745
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
746 747 748 749
    const char *filename;
    xenXMConfCachePtr entry;
    int ret = -2;

E
Eric Blake 已提交
750 751 752 753
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

754
    if (flags & VIR_DOMAIN_VCPU_LIVE) {
755
        virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("domain not active"));
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
        return -1;
    }

    xenUnifiedLock(priv);

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
        goto cleanup;

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

    ret = ((flags & VIR_DOMAIN_VCPU_MAXIMUM) ? entry->def->maxvcpus
           : entry->def->vcpus);

cleanup:
    xenUnifiedUnlock(priv);
    return ret;
}

775 776 777 778 779 780 781 782 783 784 785
/**
 * xenXMDomainPinVcpu:
 * @domain: pointer to domain object
 * @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
 */
786 787 788 789 790
int
xenXMDomainPinVcpu(virDomainPtr domain,
                   unsigned int vcpu ATTRIBUTE_UNUSED,
                   unsigned char *cpumap,
                   int maplen)
791
{
792
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
793 794 795 796
    const char *filename;
    xenXMConfCachePtr entry;
    int ret = -1;

797
    if (maplen > (int)sizeof(cpumap_t)) {
798
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
799 800 801
        return -1;
    }

D
Daniel P. Berrange 已提交
802
    xenUnifiedLock(priv);
803 804

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name))) {
805
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("virHashLookup"));
D
Daniel P. Berrange 已提交
806
        goto cleanup;
807
    }
808
    if (!(entry = virHashLookup(priv->configCache, filename))) {
809 810
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("can't retrieve config file for domain"));
D
Daniel P. Berrange 已提交
811
        goto cleanup;
812 813
    }

H
Hu Tao 已提交
814
    virBitmapFree(entry->def->cpumask);
L
liguang 已提交
815 816 817
    entry->def->cpumask = virBitmapNewData(cpumap, maplen);
    if (!entry->def->cpumask)
        goto cleanup;
818
    if (xenXMConfigSaveFile(domain->conn, entry->filename, entry->def) < 0)
819 820 821 822 823
        goto cleanup;

    ret = 0;

 cleanup:
D
Daniel P. Berrange 已提交
824
    xenUnifiedUnlock(priv);
825
    return ret;
826 827
}

828 829 830
/*
 * Find an inactive domain based on its name
 */
831 832 833 834
virDomainPtr
xenXMDomainLookupByName(virConnectPtr conn, const char *domname)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
835
    const char *filename;
836
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
837
    virDomainPtr ret = NULL;
838

D
Daniel P. Berrange 已提交
839
    xenUnifiedLock(priv);
840

841
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
842
        goto cleanup;
843

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

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

D
Daniel P. Berrange 已提交
850 851
    if (!(ret = virGetDomain(conn, domname, entry->def->uuid)))
        goto cleanup;
852 853 854

    /* Ensure its marked inactive, because may be cached
       handle to a previously active domain */
855
    ret->id = -1;
856

D
Daniel P. Berrange 已提交
857 858
cleanup:
    xenUnifiedUnlock(priv);
859
    return ret;
860 861 862 863 864 865
}


/*
 * Hash table iterator to search for a domain based on UUID
 */
866 867 868 869 870
static int
xenXMDomainSearchForUUID(const void *payload,
                         const void *name ATTRIBUTE_UNUSED,
                         const void *data)
{
871 872 873
    const unsigned char *wantuuid = (const unsigned char *)data;
    const xenXMConfCachePtr entry = (const xenXMConfCachePtr)payload;

874
    if (!memcmp(entry->def->uuid, wantuuid, VIR_UUID_BUFLEN))
875
        return 1;
876

877
    return 0;
878 879 880 881 882
}

/*
 * Find an inactive domain based on its UUID
 */
883 884 885 886
virDomainPtr
xenXMDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
887
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
888
    virDomainPtr ret = NULL;
889

D
Daniel P. Berrange 已提交
890
    xenUnifiedLock(priv);
891

892
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
893
        goto cleanup;
894

D
Daniel P. Berrange 已提交
895 896
    if (!(entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID, (const void *)uuid)))
        goto cleanup;
897

D
Daniel P. Berrange 已提交
898 899
    if (!(ret = virGetDomain(conn, entry->def->name, uuid)))
        goto cleanup;
900

901 902
    /* Ensure its marked inactive, because may be cached
       handle to a previously active domain */
903
    ret->id = -1;
904

D
Daniel P. Berrange 已提交
905 906
cleanup:
    xenUnifiedUnlock(priv);
907
    return ret;
908 909 910 911 912 913
}


/*
 * Start a domain from an existing defined config file
 */
914 915 916
int
xenXMDomainCreate(virDomainPtr domain)
{
917
    char *sexpr;
D
Daniel P. Berrange 已提交
918
    int ret = -1;
919
    xenUnifiedPrivatePtr priv= domain->conn->privateData;
920 921 922
    const char *filename;
    xenXMConfCachePtr entry;

923
    if (domain->id != -1)
924
        return -1;
925

D
Daniel P. Berrange 已提交
926 927
    xenUnifiedLock(priv);

928
    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
D
Daniel P. Berrange 已提交
929
        goto error;
930

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

M
Markus Groß 已提交
934
    if (!(sexpr = xenFormatSxpr(domain->conn, entry->def, priv->xendConfigVersion)))
D
Daniel P. Berrange 已提交
935
        goto error;
936

937
    ret = xenDaemonDomainCreateXML(domain->conn, sexpr);
938
    VIR_FREE(sexpr);
D
Daniel P. Berrange 已提交
939 940
    if (ret != 0)
        goto error;
941

942
    if ((ret = xenDaemonDomainLookupByName_ids(domain->conn, domain->name,
D
Daniel P. Berrange 已提交
943 944
                                               entry->def->uuid)) < 0)
        goto error;
945
    domain->id = ret;
946

947
    if (xend_wait_for_devices(domain->conn, domain->name) < 0)
D
Daniel P. Berrange 已提交
948
        goto error;
949

950
    if (xenDaemonDomainResume(domain) < 0)
D
Daniel P. Berrange 已提交
951
        goto error;
952

D
Daniel P. Berrange 已提交
953
    xenUnifiedUnlock(priv);
954
    return 0;
955

D
Daniel P. Berrange 已提交
956
 error:
957
    if (domain->id != -1) {
958
        xenDaemonDomainDestroy(domain);
959
        domain->id = -1;
960
    }
D
Daniel P. Berrange 已提交
961
    xenUnifiedUnlock(priv);
962
    return -1;
963 964
}

965 966 967 968
/*
 * Create a config file for a domain, based on an XML
 * document describing its config
 */
969 970
virDomainPtr
xenXMDomainDefineXML(virConnectPtr conn, const char *xml)
971
{
972
    virDomainPtr ret;
973
    char *filename = NULL;
974
    const char *oldfilename;
975
    virDomainDefPtr def = NULL;
976
    virConfPtr conf = NULL;
977
    xenXMConfCachePtr entry = NULL;
978
    xenUnifiedPrivatePtr priv = conn->privateData;
979

D
Daniel P. Berrange 已提交
980 981
    xenUnifiedLock(priv);

982
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0) {
D
Daniel P. Berrange 已提交
983
        xenUnifiedUnlock(priv);
984
        return NULL;
D
Daniel P. Berrange 已提交
985
    }
986

987
    if (!(def = virDomainDefParseString(xml, priv->caps, priv->xmlopt,
M
Matthias Bolte 已提交
988
                                        1 << VIR_DOMAIN_VIRT_XEN,
D
Daniel P. Berrange 已提交
989
                                        VIR_DOMAIN_XML_INACTIVE))) {
990
        xenUnifiedUnlock(priv);
991
        return NULL;
D
Daniel P. Berrange 已提交
992
    }
993

994 995 996
    if (!(conf = xenFormatXM(conn, def, priv->xendConfigVersion)))
        goto error;

997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
    /*
     * check that if there is another domain defined with the same uuid
     * it has the same name
     */
    if ((entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID,
                               (const void *)&(def->uuid))) != NULL) {
        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);
1008 1009 1010
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("domain '%s' is already defined with uuid %s"),
                           entry->def->name, uuidstr);
1011 1012 1013 1014 1015 1016
            entry = NULL;
            goto error;
        }
        entry = NULL;
    }

1017
    if (virHashLookup(priv->nameConfigMap, def->name)) {
1018 1019
        /* domain exists, we will overwrite it */

1020
        if (!(oldfilename = (char *)virHashLookup(priv->nameConfigMap, def->name))) {
1021 1022
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("can't retrieve config filename for domain to overwrite"));
1023 1024 1025
            goto error;
        }

1026
        if (!(entry = virHashLookup(priv->configCache, oldfilename))) {
1027 1028
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("can't retrieve config entry for domain to overwrite"));
1029 1030 1031 1032
            goto error;
        }

        /* Remove the name -> filename mapping */
1033
        if (virHashRemoveEntry(priv->nameConfigMap, def->name) < 0) {
1034 1035
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("failed to remove old domain from config map"));
1036 1037 1038 1039
            goto error;
        }

        /* Remove the config record itself */
1040
        if (virHashRemoveEntry(priv->configCache, oldfilename) < 0) {
1041 1042
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("failed to remove old domain from config map"));
1043 1044 1045 1046
            goto error;
        }

        entry = NULL;
1047
    }
1048

1049
    if (!(filename = virFileBuildPath(priv->configDir, def->name, NULL)))
1050 1051
        goto error;

1052
    if (virConfWriteFile(filename, conf) < 0)
1053 1054
        goto error;

1055
    if (VIR_ALLOC(entry) < 0) {
1056
        virReportOOMError();
1057
        goto error;
1058
    }
1059

1060
    if ((entry->refreshedAt = time(NULL)) == ((time_t)-1)) {
1061 1062
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to get current time"));
1063
        goto error;
1064
    }
1065

E
Eric Blake 已提交
1066 1067 1068 1069
    if ((entry->filename = strdup(filename)) == NULL) {
        virReportOOMError();
        goto error;
    }
1070
    entry->def = def;
1071

1072
    if (virHashAddEntry(priv->configCache, filename, entry) < 0) {
1073 1074
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to store config file handle"));
1075
        goto error;
1076
    }
1077

1078
    if (virHashAddEntry(priv->nameConfigMap, def->name, entry->filename) < 0) {
1079
        virHashSteal(priv->configCache, filename);
1080 1081
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unable to store config file handle"));
1082
        goto error;
1083 1084
    }

1085
    ret = virGetDomain(conn, def->name, def->uuid);
D
Daniel P. Berrange 已提交
1086
    xenUnifiedUnlock(priv);
1087
    VIR_FREE(filename);
1088
    return ret;
1089 1090

 error:
1091
    VIR_FREE(filename);
1092 1093
    if (entry)
        VIR_FREE(entry->filename);
1094
    VIR_FREE(entry);
1095
    virConfFree(conf);
1096
    virDomainDefFree(def);
D
Daniel P. Berrange 已提交
1097
    xenUnifiedUnlock(priv);
1098
    return NULL;
1099 1100 1101 1102 1103
}

/*
 * Delete a domain from disk
 */
1104 1105 1106 1107
int
xenXMDomainUndefine(virDomainPtr domain)
{
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
1108
    const char *filename;
1109
    xenXMConfCachePtr entry;
D
Daniel P. Berrange 已提交
1110 1111
    int ret = -1;

1112
    if (domain->id != -1)
1113
        return -1;
1114

D
Daniel P. Berrange 已提交
1115
    xenUnifiedLock(priv);
1116 1117

    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
D
Daniel P. Berrange 已提交
1118
        goto cleanup;
1119

1120
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1121
        goto cleanup;
1122 1123

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

1126 1127 1128
    /* Remove the name -> filename mapping */
    if (virHashRemoveEntry(priv->nameConfigMap, domain->name) < 0)
        goto cleanup;
1129

1130 1131 1132
    /* Remove the config record itself */
    if (virHashRemoveEntry(priv->configCache, entry->filename) < 0)
        goto cleanup;
1133

D
Daniel P. Berrange 已提交
1134 1135 1136 1137 1138
    ret = 0;

cleanup:
    xenUnifiedUnlock(priv);
    return ret;
1139 1140 1141 1142
}

struct xenXMListIteratorContext {
    virConnectPtr conn;
1143
    int oom;
1144 1145
    int max;
    int count;
1146
    char ** names;
1147 1148
};

1149 1150
static void
xenXMListIterator(void *payload ATTRIBUTE_UNUSED, const void *name, void *data) {
1151
    struct xenXMListIteratorContext *ctx = data;
1152 1153
    virDomainPtr dom = NULL;

1154 1155 1156
    if (ctx->oom)
        return;

1157 1158 1159
    if (ctx->count == ctx->max)
        return;

1160
    dom = xenDaemonLookupByName(ctx->conn, name);
1161
    if (!dom) {
1162 1163 1164 1165
        if (!(ctx->names[ctx->count] = strdup(name)))
            ctx->oom = 1;
        else
            ctx->count++;
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
    } else {
        virDomainFree(dom);
    }
}


/*
 * List all defined domains, filtered to remove any which
 * are currently running
 */
1176 1177 1178 1179
int
xenXMListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
1180
    struct xenXMListIteratorContext ctx;
1181
    int i, ret = -1;
1182

D
Daniel P. Berrange 已提交
1183
    xenUnifiedLock(priv);
1184

1185
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
1186
        goto cleanup;
1187

1188 1189
    if (maxnames > virHashSize(priv->configCache))
        maxnames = virHashSize(priv->configCache);
1190 1191

    ctx.conn = conn;
1192
    ctx.oom = 0;
1193 1194 1195 1196
    ctx.count = 0;
    ctx.max = maxnames;
    ctx.names = names;

1197
    virHashForEach(priv->nameConfigMap, xenXMListIterator, &ctx);
1198 1199 1200 1201 1202

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

1203
        virReportOOMError();
1204 1205 1206
        goto cleanup;
    }

D
Daniel P. Berrange 已提交
1207 1208 1209 1210 1211
    ret = ctx.count;

cleanup:
    xenUnifiedUnlock(priv);
    return ret;
1212 1213 1214 1215 1216 1217
}

/*
 * Return the maximum number of defined domains - not filtered
 * based on number running
 */
1218 1219 1220 1221
int
xenXMNumOfDefinedDomains(virConnectPtr conn)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
D
Daniel P. Berrange 已提交
1222
    int ret = -1;
1223

D
Daniel P. Berrange 已提交
1224
    xenUnifiedLock(priv);
1225

1226
    if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
D
Daniel P. Berrange 已提交
1227
        goto cleanup;
1228

D
Daniel P. Berrange 已提交
1229 1230 1231 1232 1233
    ret = virHashSize(priv->nameConfigMap);

cleanup:
    xenUnifiedUnlock(priv);
    return ret;
1234 1235
}

1236

1237
/**
1238
 * xenXMDomainAttachDeviceFlags:
1239 1240
 * @domain: pointer to domain object
 * @xml: pointer to XML description of device
1241
 * @flags: an OR'ed set of virDomainDeviceModifyFlags
J
Jim Meyering 已提交
1242
 *
1243 1244
 * Create a virtual device attachment to backend.
 * XML description is translated into config file.
1245 1246
 * This driver only supports device allocation to
 * persisted config.
J
Jim Meyering 已提交
1247
 *
1248 1249 1250
 * Returns 0 in case of success, -1 in case of failure.
 */
static int
1251 1252
xenXMDomainAttachDeviceFlags(virDomainPtr domain,
                             const char *xml,
E
Eric Blake 已提交
1253 1254
                             unsigned int flags)
{
1255 1256
    const char *filename = NULL;
    xenXMConfCachePtr entry = NULL;
1257 1258
    int ret = -1;
    virDomainDeviceDefPtr dev = NULL;
1259
    virDomainDefPtr def;
1260
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
1261

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

1264
    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) ||
E
Eric Blake 已提交
1265
        (domain->id != -1 && flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)) {
1266 1267
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Xm driver only supports modifying persistent config"));
1268
        return -1;
1269
    }
1270

D
Daniel P. Berrange 已提交
1271 1272
    xenUnifiedLock(priv);

1273
    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
D
Daniel P. Berrange 已提交
1274
        goto cleanup;
1275
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1276
        goto cleanup;
1277
    def = entry->def;
1278

1279 1280
    if (!(dev = virDomainDeviceDefParse(xml, entry->def,
                                        priv->caps,
1281
                                        priv->xmlopt,
1282
                                        VIR_DOMAIN_XML_INACTIVE)))
D
Daniel P. Berrange 已提交
1283
        goto cleanup;
1284

1285 1286 1287
    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
    {
1288
        if (virDomainDiskInsert(def, dev->data.disk) < 0) {
1289
            virReportOOMError();
1290
            goto cleanup;
1291 1292
        }
        dev->data.disk = NULL;
1293
    }
1294
    break;
1295

1296 1297
    case VIR_DOMAIN_DEVICE_NET:
    {
1298
        if (VIR_REALLOC_N(def->nets, def->nnets+1) < 0) {
1299
            virReportOOMError();
1300 1301 1302
            goto cleanup;
        }
        def->nets[def->nnets++] = dev->data.net;
1303 1304
        dev->data.net = NULL;
        break;
1305 1306
    }

1307
    default:
1308 1309
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Xm driver only supports adding disk or network devices"));
1310 1311 1312 1313 1314 1315
        goto cleanup;
    }

    /* If this fails, should we try to undo our changes to the
     * in-memory representation of the config file. I say not!
     */
1316
    if (xenXMConfigSaveFile(domain->conn, entry->filename, entry->def) < 0)
1317 1318 1319 1320 1321
        goto cleanup;

    ret = 0;

 cleanup:
1322
    virDomainDeviceDefFree(dev);
D
Daniel P. Berrange 已提交
1323
    xenUnifiedUnlock(priv);
1324 1325 1326 1327 1328
    return ret;
}


/**
1329
 * xenXMDomainDetachDeviceFlags:
1330 1331
 * @domain: pointer to domain object
 * @xml: pointer to XML description of device
1332
 * @flags: an OR'ed set of virDomainDeviceModifyFlags
J
Jim Meyering 已提交
1333
 *
1334
 * Destroy a virtual device attachment to backend.
1335 1336
 * This driver only supports device deallocation from
 * persisted config.
1337 1338 1339 1340
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
static int
1341 1342 1343 1344
xenXMDomainDetachDeviceFlags(virDomainPtr domain,
                             const char *xml,
                             unsigned int flags)
{
1345 1346
    const char *filename = NULL;
    xenXMConfCachePtr entry = NULL;
1347
    virDomainDeviceDefPtr dev = NULL;
1348
    virDomainDefPtr def;
1349
    int ret = -1;
1350
    int i;
1351
    xenUnifiedPrivatePtr priv = domain->conn->privateData;
1352

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

1355
    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) ||
E
Eric Blake 已提交
1356
        (domain->id != -1 && flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)) {
1357 1358
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Xm driver only supports modifying persistent config"));
1359
        return -1;
1360
    }
D
Daniel P. Berrange 已提交
1361 1362 1363

    xenUnifiedLock(priv);

1364
    if (!(filename = virHashLookup(priv->nameConfigMap, domain->name)))
D
Daniel P. Berrange 已提交
1365
        goto cleanup;
1366
    if (!(entry = virHashLookup(priv->configCache, filename)))
D
Daniel P. Berrange 已提交
1367
        goto cleanup;
1368
    def = entry->def;
1369

1370 1371
    if (!(dev = virDomainDeviceDefParse(xml, entry->def,
                                        priv->caps,
1372
                                        priv->xmlopt,
1373
                                        VIR_DOMAIN_XML_INACTIVE)))
D
Daniel P. Berrange 已提交
1374
        goto cleanup;
1375

1376 1377 1378
    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
    {
1379 1380
        for (i = 0 ; i < def->ndisks ; i++) {
            if (def->disks[i]->dst &&
1381
                dev->data.disk->dst &&
1382 1383 1384 1385 1386
                STREQ(def->disks[i]->dst, dev->data.disk->dst)) {
                virDomainDiskDefFree(def->disks[i]);
                if (i < (def->ndisks - 1))
                    memmove(def->disks + i,
                            def->disks + i + 1,
1387 1388
                            sizeof(*def->disks) *
                            (def->ndisks - (i + 1)));
1389
                def->ndisks--;
1390
                break;
1391 1392
            }
        }
1393 1394 1395 1396 1397
        break;
    }

    case VIR_DOMAIN_DEVICE_NET:
    {
1398
        for (i = 0 ; i < def->nnets ; i++) {
1399
            if (!virMacAddrCmp(&def->nets[i]->mac, &dev->data.net->mac)) {
1400 1401 1402 1403
                virDomainNetDefFree(def->nets[i]);
                if (i < (def->nnets - 1))
                    memmove(def->nets + i,
                            def->nets + i + 1,
1404 1405
                            sizeof(*def->nets) *
                            (def->nnets - (i + 1)));
1406
                def->nnets--;
1407 1408 1409 1410
                break;
            }
        }
        break;
1411
    }
1412
    default:
1413 1414 1415
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("device type '%s' cannot be detached"),
                       virDomainDeviceTypeToString(dev->type));
1416 1417 1418 1419 1420 1421
        goto cleanup;
    }

    /* If this fails, should we try to undo our changes to the
     * in-memory representation of the config file. I say not!
     */
1422
    if (xenXMConfigSaveFile(domain->conn, entry->filename, entry->def) < 0)
1423 1424 1425 1426 1427
        goto cleanup;

    ret = 0;

 cleanup:
1428
    virDomainDeviceDefFree(dev);
D
Daniel P. Berrange 已提交
1429
    xenUnifiedUnlock(priv);
1430
    return ret;
1431 1432
}

R
Richard W.M. Jones 已提交
1433
int
1434 1435 1436 1437 1438
xenXMDomainBlockPeek(virDomainPtr dom ATTRIBUTE_UNUSED,
                     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 已提交
1439
{
1440 1441
    virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                   _("block peeking not implemented"));
R
Richard W.M. Jones 已提交
1442 1443 1444
    return -1;
}

1445

1446 1447
static char *
xenXMAutostartLinkName(virDomainPtr dom)
1448 1449
{
    char *ret;
1450 1451
    if (virAsprintf(&ret, "/etc/xen/auto/%s", dom->name) < 0)
        return NULL;
1452 1453 1454
    return ret;
}

1455 1456
static char *
xenXMDomainConfigName(virDomainPtr dom)
1457 1458
{
    char *ret;
1459 1460
    if (virAsprintf(&ret, "/etc/xen/%s", dom->name) < 0)
        return NULL;
1461 1462 1463
    return ret;
}

1464 1465
int
xenXMDomainGetAutostart(virDomainPtr dom, int *autostart)
1466 1467 1468 1469 1470 1471
{
    char *linkname = xenXMAutostartLinkName(dom);
    char *config = xenXMDomainConfigName(dom);
    int ret = -1;

    if (!linkname || !config) {
1472
        virReportOOMError();
1473 1474 1475 1476 1477
        goto cleanup;
    }

    *autostart = virFileLinkPointsTo(linkname, config);
    if (*autostart < 0) {
1478
        virReportSystemError(errno,
1479 1480
                             _("cannot check link %s points to config %s"),
                             linkname, config);
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
        goto cleanup;
    }

    ret = 0;

cleanup:
    VIR_FREE(linkname);
    VIR_FREE(config);
    return ret;
}


1493 1494
int
xenXMDomainSetAutostart(virDomainPtr dom, int autostart)
1495 1496 1497 1498 1499 1500
{
    char *linkname = xenXMAutostartLinkName(dom);
    char *config = xenXMDomainConfigName(dom);
    int ret = -1;

    if (!linkname || !config) {
1501
        virReportOOMError();
1502 1503 1504 1505 1506 1507
        goto cleanup;
    }

    if (autostart) {
        if (symlink(config, linkname) < 0 &&
            errno != EEXIST) {
1508
            virReportSystemError(errno,
1509 1510
                                 _("failed to create link %s to %s"),
                                 config, linkname);
1511 1512 1513 1514 1515
            goto cleanup;
        }
    } else {
        if (unlink(linkname)  < 0 &&
            errno != ENOENT) {
1516
            virReportSystemError(errno,
1517 1518
                                 _("failed to remove link %s"),
                                 linkname);
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
            goto cleanup;
        }
    }
    ret = 0;

cleanup:
    VIR_FREE(linkname);
    VIR_FREE(config);

    return ret;
}