openvz_conf.c 24.6 KB
Newer Older
1 2 3 4 5
/*
 * openvz_conf.c: config functions for managing OpenVZ VEs
 *
 * Copyright (C) 2006, 2007 Binary Karma
 * Copyright (C) 2006 Shuveb Hussain
6
 * Copyright (C) 2007 Anoop Joe Cyriac
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
22
 * Authors:
23 24 25
 * Shuveb Hussain <shuveb@binarykarma.com>
 * Anoop Joe Cyriac <anoop@binarykarma.com>
 *
26 27
 */

28
#include <config.h>
J
Jim Meyering 已提交
29

30 31 32 33 34 35 36 37
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
#include <strings.h>
#include <time.h>
38 39 40 41
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
D
Daniel P. Berrange 已提交
42
#include <string.h>
43
#include <sys/utsname.h>
44
#include <sys/wait.h>
45

46
#include "virterror_internal.h"
47
#include "openvz_conf.h"
48 49
#include "uuid.h"
#include "buf.h"
50
#include "memory.h"
51
#include "util.h"
52
#include "nodeinfo.h"
53

54 55
#define VIR_FROM_THIS VIR_FROM_OPENVZ

56
static char *openvzLocateConfDir(void);
57
static int openvzGetVPSUUID(int vpsid, char *uuidstr, size_t len);
58
static int openvzLocateConfFile(int vpsid, char *conffile, int maxlen, const char *ext);
59
static int openvzAssignUUIDs(void);
60

61
int
62
strtoI(const char *str)
63 64 65
{
    int val;

66 67
    if (virStrToLong_i(str, NULL, 10, &val) < 0)
        return 0 ;
68

69 70 71
    return val;
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

static int
openvzExtractVersionInfo(const char *cmd, int *retversion)
{
    const char *const vzarg[] = { cmd, "--help", NULL };
    const char *const vzenv[] = { "LC_ALL=C", NULL };
    pid_t child;
    int newstdout = -1;
    int ret = -1, status;
    unsigned int major, minor, micro;
    unsigned int version;

    if (retversion)
        *retversion = 0;

87
    if (virExec(vzarg, vzenv, NULL,
88 89 90 91
                &child, -1, &newstdout, NULL, VIR_EXEC_NONE) < 0)
        return -1;

    char *help = NULL;
92
    int len = virFileReadLimFD(newstdout, 4096, &help);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    if (len < 0)
        goto cleanup2;

    if (sscanf(help, "vzctl version %u.%u.%u",
               &major, &minor, &micro) != 3) {
        goto cleanup2;
    }

    version = (major * 1000 * 1000) + (minor * 1000) + micro;

    if (retversion)
        *retversion = version;

    ret = 0;

cleanup2:
    VIR_FREE(help);
    if (close(newstdout) < 0)
        ret = -1;

rewait:
    if (waitpid(child, &status, 0) != child) {
        if (errno == EINTR)
            goto rewait;
        ret = -1;
    }

    return ret;
}

int openvzExtractVersion(virConnectPtr conn,
                         struct openvz_driver *driver)
{
    if (driver->version > 0)
        return 0;

    if (openvzExtractVersionInfo(VZCTL, &driver->version) < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
131
                    "%s", _("Could not extract vzctl version"));
132 133 134 135 136 137 138
        return -1;
    }

    return 0;
}


139 140 141 142 143 144 145 146 147 148 149 150
virCapsPtr openvzCapsInit(void)
{
    struct utsname utsname;
    virCapsPtr caps;
    virCapsGuestPtr guest;

    uname(&utsname);

    if ((caps = virCapabilitiesNew(utsname.machine,
                                   0, 0)) == NULL)
        goto no_memory;

151
    if (nodeCapsInitNUMA(caps) < 0)
152 153
        goto no_memory;

154 155
    virCapabilitiesSetMacPrefix(caps, (unsigned char[]){ 0x52, 0x54, 0x00 });

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "exe",
                                         utsname.machine,
                                         sizeof(int) == 4 ? 32 : 8,
                                         NULL,
                                         NULL,
                                         0,
                                         NULL)) == NULL)
        goto no_memory;

    if (virCapabilitiesAddGuestDomain(guest,
                                      "openvz",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
        goto no_memory;
    return caps;

no_memory:
    virCapabilitiesFree(caps);
    return NULL;
}


181 182 183 184
static int
openvzReadNetworkConf(virConnectPtr conn,
                      virDomainDefPtr def,
                      int veid) {
185
    int ret;
186
    virDomainNetDefPtr net = NULL;
187 188 189 190 191 192 193 194
    char temp[4096];
    char *token, *saveptr = NULL;

    /*parse routing network configuration*
     * Sample from config:
     *   IP_ADDRESS="1.1.1.1 1.1.1.2"
     *   splited IPs by space
     */
195
    ret = openvzReadVPSConfigParam(veid, "IP_ADDRESS", temp, sizeof(temp));
196 197
    if (ret < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
198
                 _("Could not read 'IP_ADDRESS' from config for container %d"),
199 200 201 202 203
                  veid);
        goto error;
    } else if (ret > 0) {
        token = strtok_r(temp, " ", &saveptr);
        while (token != NULL) {
204
            if (VIR_ALLOC(net) < 0)
205 206 207 208 209 210 211 212
                goto no_memory;

            net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
            net->data.ethernet.ipaddr = strdup(token);

            if (net->data.ethernet.ipaddr == NULL)
                goto no_memory;

213 214 215 216 217
            if (VIR_REALLOC_N(def->nets, def->nnets + 1) < 0)
                goto no_memory;
            def->nets[def->nnets++] = net;
            net = NULL;

218 219 220 221 222 223 224 225 226
            token = strtok_r(NULL, " ", &saveptr);
        }
    }

    /*parse bridge devices*/
    /*Sample from config:
     *NETIF="ifname=eth10,mac=00:18:51:C1:05:EE,host_ifname=veth105.10,host_mac=00:18:51:8F:D9:F3"
     *devices splited by ';'
     */
227
    ret = openvzReadVPSConfigParam(veid, "NETIF", temp, sizeof(temp));
228 229
    if (ret < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
230
                     _("Could not read 'NETIF' from config for container %d"),
231 232 233 234 235 236
                     veid);
        goto error;
    } else if (ret > 0) {
        token = strtok_r(temp, ";", &saveptr);
        while (token != NULL) {
            /*add new device to list*/
237
            if (VIR_ALLOC(net) < 0)
238 239 240 241
                goto no_memory;

            net->type = VIR_DOMAIN_NET_TYPE_BRIDGE;

242
            char *p = token;
243 244 245 246 247
            char cpy_temp[32];
            int len;

            /*parse string*/
            do {
248
                char *next = strchrnul (token, ',');
249
                if (STRPREFIX(p, "ifname=")) {
250 251 252
                    /* skip in libvirt */
                } else if (STRPREFIX(p, "host_ifname=")) {
                    p += 12;
253 254 255
                    len = next - p;
                    if (len > 16) {
                        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
256
                                "%s", _("Too long network device name"));
257 258 259
                        goto error;
                    }

260 261 262
                    if (VIR_ALLOC_N(net->ifname, len+1) < 0)
                        goto no_memory;

C
Chris Lalancette 已提交
263 264 265 266 267
                    if (virStrncpy(net->ifname, p, len, len+1) == NULL) {
                        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                                    _("Network ifname %s too long for destination"), p);
                        goto error;
                    }
268 269 270 271 272 273 274 275 276
                } else if (STRPREFIX(p, "bridge=")) {
                    p += 7;
                    len = next - p;
                    if (len > 16) {
                        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                                "%s", _("Too long bridge device name"));
                        goto error;
                    }

277 278 279
                    if (VIR_ALLOC_N(net->data.bridge.brname, len+1) < 0)
                        goto no_memory;

C
Chris Lalancette 已提交
280 281 282 283 284
                    if (virStrncpy(net->data.bridge.brname, p, len, len+1) == NULL) {
                        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                                    _("Bridge name %s too long for destination"), p);
                        goto error;
                    }
285 286 287 288 289
                } else if (STRPREFIX(p, "mac=")) {
                    p += 4;
                    len = next - p;
                    if (len != 17) { //should be 17
                        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
290
                              "%s", _("Wrong length MAC address"));
291 292
                        goto error;
                    }
C
Chris Lalancette 已提交
293 294 295 296 297
                    if (virStrncpy(cpy_temp, p, len, sizeof(cpy_temp)) == NULL) {
                        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                                    _("MAC address %s too long for destination"), p);
                        goto error;
                    }
298
                    if (virParseMacAddr(cpy_temp, net->mac) < 0) {
299
                        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
300
                              "%s", _("Wrong MAC address"));
301 302 303 304 305 306
                        goto error;
                    }
                }
                p = ++next;
            } while (p < token + strlen(token));

307 308 309 310 311
            if (VIR_REALLOC_N(def->nets, def->nnets + 1) < 0)
                goto no_memory;
            def->nets[def->nnets++] = net;
            net = NULL;

312 313 314 315
            token = strtok_r(NULL, ";", &saveptr);
        }
    }

316
    return 0;
317
no_memory:
318
    virReportOOMError();
319 320
error:
    virDomainNetDefFree(net);
321
    return -1;
322 323 324
}


325 326 327 328 329 330 331
/* utility function to replace 'from' by 'to' in 'str' */
static char*
openvz_replace(const char* str,
               const char* from,
               const char* to) {
    const char* offset = NULL;
    const char* str_start = str;
332 333
    int to_len;
    int from_len;
334 335
    virBuffer buf = VIR_BUFFER_INITIALIZER;

336
    if ((!from) || (!to))
337
        return NULL;
338 339
    from_len = strlen(from);
    to_len = strlen(to);
340 341 342 343 344 345 346 347

    while((offset = strstr(str_start, from)))
    {
        virBufferAdd(&buf, str_start, offset-str_start);
        virBufferAdd(&buf, to, to_len);
        str_start = offset + from_len;
    }

348
    virBufferAdd(&buf, str_start, -1);
349

350 351 352 353
    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        return NULL;
    }
354 355 356 357 358

    return virBufferContentAndReset(&buf);
}


359 360 361 362 363 364
static int
openvzReadFSConf(virConnectPtr conn,
                 virDomainDefPtr def,
                 int veid) {
    int ret;
    virDomainFSDefPtr fs = NULL;
365 366
    char* veid_str = NULL;
    char temp[100];
367

368
    ret = openvzReadVPSConfigParam(veid, "OSTEMPLATE", temp, sizeof(temp));
369 370
    if (ret < 0) {
        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
371
                    _("Could not read 'OSTEMPLATE' from config for container %d"),
372 373 374 375 376 377 378 379
                    veid);
        goto error;
    } else if (ret > 0) {
        if (VIR_ALLOC(fs) < 0)
            goto no_memory;

        fs->type = VIR_DOMAIN_FS_TYPE_TEMPLATE;
        fs->src = strdup(temp);
380 381 382 383 384
    } else {
        /* OSTEMPLATE was not found, VE was booted from a private dir directly */
        ret = openvzReadVPSConfigParam(veid, "VE_PRIVATE", temp, sizeof(temp));
        if (ret <= 0) {
            openvzError(conn, VIR_ERR_INTERNAL_ERROR,
385
                        _("Could not read 'VE_PRIVATE' from config for container %d"),
386 387 388
                        veid);
            goto error;
        }
389

390
        if (VIR_ALLOC(fs) < 0)
391 392
            goto no_memory;

393 394
        if (virAsprintf(&veid_str, "%d", veid) < 0)
            goto no_memory;
395 396 397

        fs->type = VIR_DOMAIN_FS_TYPE_MOUNT;
        fs->src = openvz_replace(temp, "$VEID", veid_str);
398

399
        VIR_FREE(veid_str);
400 401
    }

402 403 404 405 406 407 408 409 410 411
    fs->dst = strdup("/");

    if (fs->src == NULL || fs->dst == NULL)
        goto no_memory;

    if (VIR_REALLOC_N(def->fss, def->nfss + 1) < 0)
        goto no_memory;
    def->fss[def->nfss++] = fs;
    fs = NULL;

412 413
    return 0;
no_memory:
414
    virReportOOMError();
415 416 417 418 419 420
error:
    virDomainFSDefFree(fs);
    return -1;
}


421 422 423 424 425 426
/* Free all memory associated with a openvz_driver structure */
void
openvzFreeDriver(struct openvz_driver *driver)
{
    if (!driver)
        return;
427

428
    virDomainObjListDeinit(&driver->domains);
429
    virCapabilitiesFree(driver->caps);
430
    VIR_FREE(driver);
431
}
D
Daniel Veillard 已提交
432 433 434



435
int openvzLoadDomains(struct openvz_driver *driver) {
436 437 438
    FILE *fp;
    int veid, ret;
    char status[16];
439
    char uuidstr[VIR_UUID_STRING_BUFLEN];
440
    virDomainObjPtr dom = NULL;
441
    char temp[50];
442

443 444
    if (openvzAssignUUIDs() < 0)
        return -1;
445

446
    if ((fp = popen(VZLIST " -a -ovpsid,status -H 2>/dev/null", "r")) == NULL) {
J
Jim Meyering 已提交
447
        openvzError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("popen failed"));
448 449 450 451
        return -1;
    }

    while(!feof(fp)) {
452
        if (fscanf(fp, "%d %s\n", &veid, status) != 2) {
453 454
            if (feof(fp))
                break;
455

456
            openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
457
                        "%s", _("Failed to parse vzlist output"));
458
            goto cleanup;
459
        }
460

461
        if (VIR_ALLOC(dom) < 0)
462
            goto no_memory;
463

464 465 466 467 468 469 470
        if (virMutexInit(&dom->lock) < 0) {
            openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
                        "%s", _("cannot initialize mutex"));
            VIR_FREE(dom);
            goto cleanup;
        }

471 472
        virDomainObjLock(dom);

473 474
        if (VIR_ALLOC(dom->def) < 0)
            goto no_memory;
475

476 477 478 479
        if (STREQ(status, "stopped"))
            dom->state = VIR_DOMAIN_SHUTOFF;
        else
            dom->state = VIR_DOMAIN_RUNNING;
480

481
        dom->refs = 1;
482 483
        dom->pid = veid;
        dom->def->id = dom->state == VIR_DOMAIN_SHUTOFF ? -1 : veid;
484 485
        /* XXX OpenVZ doesn't appear to have concept of a transient domain */
        dom->persistent = 1;
486

487
        if (virAsprintf(&dom->def->name, "%i", veid) < 0)
488
            goto no_memory;
489

490
        openvzGetVPSUUID(veid, uuidstr, sizeof(uuidstr));
491
        ret = virUUIDParse(uuidstr, dom->def->uuid);
492

493 494
        if (ret == -1) {
            openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
495
                        "%s", _("UUID in config file malformed"));
496
            goto cleanup;
497
        }
498

499 500 501 502
        if (!(dom->def->os.type = strdup("exe")))
            goto no_memory;
        if (!(dom->def->os.init = strdup("/sbin/init")))
            goto no_memory;
503

504
        ret = openvzReadVPSConfigParam(veid, "CPUS", temp, sizeof(temp));
505
        if (ret < 0) {
506
            openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
507
                        _("Could not read config for container %d"),
508 509
                        veid);
            goto cleanup;
510
        } else if (ret > 0) {
511
            dom->def->vcpus = strtoI(temp);
512 513
        }

514 515 516
        if (ret == 0 || dom->def->vcpus == 0)
            dom->def->vcpus = openvzGetNodeCPUs();

517
        /* XXX load rest of VM config data .... */
518

519
        openvzReadNetworkConf(NULL, dom->def, veid);
520
        openvzReadFSConf(NULL, dom->def, veid);
521

522 523
        virUUIDFormat(dom->def->uuid, uuidstr);
        if (virHashAddEntry(driver->domains.objs, uuidstr, dom) < 0)
524 525
            goto no_memory;

526
        virDomainObjUnlock(dom);
527
        dom = NULL;
528
    }
529

530
    fclose(fp);
531

532
    return 0;
533

534
 no_memory:
535
    virReportOOMError();
536

537 538
 cleanup:
    fclose(fp);
539 540
    if (dom)
        virDomainObjUnref(dom);
541
    return -1;
542 543
}

544 545 546 547 548
unsigned int
openvzGetNodeCPUs(void)
{
    virNodeInfo nodeinfo;

549
    if (nodeGetInfo(NULL, &nodeinfo) < 0)
550 551 552 553
        return 0;

    return nodeinfo.cpus;
}
554

555 556
static int
openvzWriteConfigParam(const char * conf_file, const char *param, const char *value)
557
{
558 559
    char * temp_file = NULL;
    int fd = -1, temp_fd = -1;
560 561
    char line[PATH_MAX] ;

562
    if (virAsprintf(&temp_file, "%s.tmp", conf_file)<0) {
563
        virReportOOMError();
564
        return -1;
565
    }
566 567 568

    fd = open(conf_file, O_RDONLY);
    if (fd == -1)
569
        goto error;
570 571 572
    temp_fd = open(temp_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (temp_fd == -1) {
        close(fd);
573
        goto error;
574 575 576 577 578 579
    }

    while(1) {
        if (openvz_readline(fd, line, sizeof(line)) <= 0)
            break;

580
        if (!(STRPREFIX(line, param) && line[strlen(param)] == '=')) {
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
            if (safewrite(temp_fd, line, strlen(line)) !=
                strlen(line))
                goto error;
        }
    }

    if (safewrite(temp_fd, param, strlen(param)) < 0 ||
        safewrite(temp_fd, "=\"", 2) < 0 ||
        safewrite(temp_fd, value, strlen(value)) < 0 ||
        safewrite(temp_fd, "\"\n", 2) < 0)
        goto error;

    if (close(fd) < 0)
        goto error;
    fd = -1;
    if (close(temp_fd) < 0)
        goto error;
    temp_fd = -1;

    if (rename(temp_file, conf_file) < 0)
        goto error;

    return 0;

error:
    if (fd != -1)
        close(fd);
    if (temp_fd != -1)
        close(temp_fd);
610 611 612
    if(temp_file)
        unlink(temp_file);
    VIR_FREE(temp_file);
613 614 615
    return -1;
}

616
int
617 618 619 620 621 622 623 624 625 626 627 628
openvzWriteVPSConfigParam(int vpsid, const char *param, const char *value)
{
    char conf_file[PATH_MAX];

    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
        return -1;

    return openvzWriteConfigParam(conf_file, param, value);
}

static int
openvzReadConfigParam(const char * conf_file ,const char * param, char *value, int maxlen)
629 630 631 632 633 634 635 636 637 638
{
    char line[PATH_MAX] ;
    int ret, found = 0;
    int fd ;
    char * sf, * token;
    char *saveptr = NULL;

    value[0] = 0;

    fd = open(conf_file, O_RDONLY);
D
Daniel Veillard 已提交
639
    if (fd == -1)
640 641 642 643 644 645 646
        return -1;

    while(1) {
        ret = openvz_readline(fd, line, sizeof(line));
        if(ret <= 0)
            break;
        saveptr = NULL;
D
Daniel Veillard 已提交
647
        if (STREQLEN(line, param, strlen(param))) {
648 649
            sf = line;
            sf += strlen(param);
650 651 652
            if (sf[0] == '=' && sf[1] != '\0' ) {
                sf ++;
                if ((token = strtok_r(sf,"\"\t\n", &saveptr)) != NULL) {
C
Chris Lalancette 已提交
653 654 655 656
                    if (virStrcpy(value, token, maxlen) == NULL) {
                        ret = -1;
                        break;
                    }
657 658
                    found = 1;
                }
659
            }
D
Daniel Veillard 已提交
660
       }
661 662 663 664 665 666 667 668 669
    }
    close(fd);

    if (ret == 0 && found)
        ret = 1;

    return ret ;
}

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
/*
* Read parameter from container config
* sample: 133, "OSTEMPLATE", value, 1024
* return: -1 - error
*	   0 - don't found
*          1 - OK
*/
int
openvzReadVPSConfigParam(int vpsid ,const char * param, char *value, int maxlen)
{
    char conf_file[PATH_MAX] ;

    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
        return -1;

    return openvzReadConfigParam(conf_file, param, value, maxlen);
}

static int
openvz_copyfile(char* from_path, char* to_path)
{
    char line[PATH_MAX];
    int fd, copy_fd;
    int bytes_read;

    fd = open(from_path, O_RDONLY);
    if (fd == -1)
        return -1;
    copy_fd = open(to_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (copy_fd == -1) {
        close(fd);
        return -1;
    }

    while(1) {
        if (openvz_readline(fd, line, sizeof(line)) <= 0)
            break;

        bytes_read = strlen(line);
        if (safewrite(copy_fd, line, bytes_read) != bytes_read)
            goto error;
    }

    if (close(fd) < 0)
        goto error;
    fd = -1;
    if (close(copy_fd) < 0)
        goto error;

    return 0;

error:
    if (fd != -1)
        close(fd);
    if (copy_fd != -1)
        close(copy_fd);
    return -1;
}

/*
* Copy the default config to the VE conf file
* return: -1 - error
*          0 - OK
*/
int
openvzCopyDefaultConfig(int vpsid)
{
    char * confdir = NULL;
    char * default_conf_file = NULL;
    char configfile_value[PATH_MAX];
    char conf_file[PATH_MAX];
    int ret = -1;

    if(openvzReadConfigParam(VZ_CONF_FILE, "CONFIGFILE", configfile_value, PATH_MAX) < 0)
        goto cleanup;

    confdir = openvzLocateConfDir();
    if (confdir == NULL)
        goto cleanup;

750
    if (virAsprintf(&default_conf_file, "%s/ve-%s.conf-sample", confdir, configfile_value) < 0) {
751
        virReportOOMError();
752
        goto cleanup;
753
    }
754 755 756 757 758 759 760 761 762 763 764 765 766 767

    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
        goto cleanup;

    if (openvz_copyfile(default_conf_file, conf_file)<0)
        goto cleanup;

    ret = 0;
cleanup:
    VIR_FREE(confdir);
    VIR_FREE(default_conf_file);
    return ret;
}

768 769 770 771 772
/* Locate config file of container
* return -1 - error
*         0 - OK
*/
static int
773
openvzLocateConfFile(int vpsid, char *conffile, int maxlen, const char *ext)
774 775 776 777 778 779 780 781
{
    char * confdir;
    int ret = 0;

    confdir = openvzLocateConfDir();
    if (confdir == NULL)
        return -1;

782 783
    if (snprintf(conffile, maxlen, "%s/%d.%s",
                 confdir, vpsid, ext ? ext : "conf") >= maxlen)
784 785 786 787 788 789
        ret = -1;

    VIR_FREE(confdir);
    return ret;
}

790
static char
791
*openvzLocateConfDir(void)
792 793 794 795 796 797
{
    const char *conf_dir_list[] = {"/etc/vz/conf", "/usr/local/etc/conf", NULL};
    int i=0;

    while(conf_dir_list[i]) {
        if(!access(conf_dir_list[i], F_OK))
798
            return strdup(conf_dir_list[i]);
799 800 801 802 803 804 805
        i ++;
    }

    return NULL;
}

/* Richard Steven's classic readline() function */
806
int
807
openvz_readline(int fd, char *ptr, int maxlen)
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
{
    int n, rc;
    char c;

    for(n = 1; n < maxlen; n ++) {
        if( (rc = read(fd, &c, 1)) == 1) {
            *ptr++ = c;
            if(c == '\n')
                break;
        }
        else if(rc == 0) {
            if(n == 1)
                return 0; /* EOF condition */
            else
                break;
        }
        else
            return -1; /* error */
    }
    *ptr = 0;
    return n;
}

831
static int
832
openvzGetVPSUUID(int vpsid, char *uuidstr, size_t len)
833 834 835
{
    char conf_file[PATH_MAX];
    char line[1024];
836
    char uuidbuf[1024];
837 838
    char iden[1024];
    int fd, ret;
C
Chris Lalancette 已提交
839
    int retval = 0;
840

841
    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
842
       return -1;
843

844
    fd = open(conf_file, O_RDONLY);
845 846 847 848 849
    if(fd == -1)
        return -1;

    while(1) {
        ret = openvz_readline(fd, line, sizeof(line));
850 851
        if(ret == -1) {
            close(fd);
852
            return -1;
853
        }
854 855

        if(ret == 0) { /* EoF, UUID was not found */
856
            uuidstr[0] = 0;
857 858 859
            break;
        }

860
        sscanf(line, "%s %s\n", iden, uuidbuf);
861
        if(STREQ(iden, "#UUID:")) {
C
Chris Lalancette 已提交
862 863
            if (virStrcpy(uuidstr, uuidbuf, len) == NULL)
                retval = -1;
864 865 866
            break;
        }
    }
867 868
    close(fd);

C
Chris Lalancette 已提交
869
    return retval;
870 871 872 873 874
}

/* Do actual checking for UUID presence in conf file,
 * assign if not present.
 */
875 876
int
openvzSetDefinedUUID(int vpsid, unsigned char *uuid)
877 878
{
    char conf_file[PATH_MAX];
879
    char uuidstr[VIR_UUID_STRING_BUFLEN];
880 881 882

    if (uuid == NULL)
        return -1;
883

884
    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
885
       return -1;
886

887
    if (openvzGetVPSUUID(vpsid, uuidstr, sizeof(uuidstr)))
888 889
        return -1;

J
Jim Meyering 已提交
890
    if (uuidstr[0] == 0) {
891 892 893
        FILE *fp = fopen(conf_file, "a"); /* append */
        if (fp == NULL)
          return -1;
894

895 896
        virUUIDFormat(uuid, uuidstr);

897 898 899 900 901
        /* Record failure if fprintf or fclose fails,
           and be careful always to close the stream.  */
        if ((fprintf(fp, "\n#UUID: %s\n", uuidstr) < 0)
            + (fclose(fp) == EOF))
            return -1;
902 903 904 905 906
    }

    return 0;
}

907 908 909 910
static int
openvzSetUUID(int vpsid){
    unsigned char uuid[VIR_UUID_BUFLEN];

911 912 913 914 915
    if (virUUIDGenerate(uuid)) {
        openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
                    "%s", _("Failed to generate UUID"));
        return -1;
    }
916 917 918 919

    return openvzSetDefinedUUID(vpsid, uuid);
}

920 921 922 923 924 925 926
/*
 * Scan VPS config files and see if they have a UUID.
 * If not, assign one. Just append one to the config
 * file as comment so that the OpenVZ tools ignore it.
 *
 */

927
static int openvzAssignUUIDs(void)
928 929 930 931 932 933 934 935
{
    DIR *dp;
    struct dirent *dent;
    char *conf_dir;
    int vpsid, res;
    char ext[8];

    conf_dir = openvzLocateConfDir();
936 937
    if (conf_dir == NULL)
        return -1;
938 939 940

    dp = opendir(conf_dir);
    if(dp == NULL) {
941
        VIR_FREE(conf_dir);
942 943 944 945 946
        return 0;
    }

    while((dent = readdir(dp))) {
        res = sscanf(dent->d_name, "%d.%5s", &vpsid, ext);
947
        if(!(res == 2 && STREQ(ext, "conf")))
948 949 950 951 952
            continue;
        if(vpsid > 0) /* '0.conf' belongs to the host, ignore it */
            openvzSetUUID(vpsid);
    }
    closedir(dp);
953
    VIR_FREE(conf_dir);
954 955
    return 0;
}
956 957 958 959 960 961 962 963 964 965 966


/*
 * Return CTID from name
 *
 */

int openvzGetVEID(const char *name) {
    char *cmd;
    int veid;
    FILE *fp;
967
    bool ok;
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982

    if (virAsprintf(&cmd, "%s %s -ovpsid -H", VZLIST, name) < 0) {
        openvzError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                    _("virAsprintf failed"));
        return -1;
    }

    fp = popen(cmd, "r");
    VIR_FREE(cmd);

    if (fp == NULL) {
        openvzError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("popen failed"));
        return -1;
    }

983
    ok = fscanf(fp, "%d\n", &veid ) == 1;
984
    fclose(fp);
985 986 987 988 989
    if (ok && veid >= 0)
        return veid;

    openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
                "%s", _("Failed to parse vzlist output"));
990 991
    return -1;
}