~serge-hallyn/ubuntu/oneiric/libvirt/fix-shutdown

« back to all changes in this revision

Viewing changes to src/openvz/openvz_conf.c

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2010-11-02 16:26:51 UTC
  • mfrom: (1.2.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20101102162651-aq8tnbz58mdf01bf
Tags: 0.8.5-0ubuntu1
* New upstream release.
* Removed a slew of patches which have been
  applied upstream since 0.8.3.
  - 9012-apparmor-extra-tests.patch
  - 9013-apparmor-chardev.patch
  - 9015-Add-ubd-to-the-list-of-disk-prefixes.patch
  - 9016-Close-fd-s-of-persistent-tap-devices.patch
  - 9017-Make-sure-all-command-line-arguments-get-passed-to-U.patch
  - 9018-Make-umlConnectTapDevice-ask-brAddTap-for-a-persiste.patch
  - 9019-uml-fix-logic-bug-in-checking-reply-length.patch
  - 9021-Allow-chardev-of-type-file-for-UML-domains.patch
  - 9022-Rename-qemudShrinkDisks-to-virDomainDiskRemove-and-m.patch
  - 9023-Support-virDomainAttachDevice-and-virDomainDetachDev.patch
  - 9024-Explicitly-pass-uml_dir-argument-to-user-mode-linux.patch
  - 9025-Add-nwfilter-support-to-UML-driver.patch
  - 9026-Rebuild-network-filter-for-UML-guests-on-updates.patch
  - 9027-Make-newfilter-xml-transformations-endian-safe.patch
  - 9028-lp628055.patch
* Updated 9002-better_default_uri_virsh.patch to use vshStrdup,
  as now required in that file.  (use of strdup now causes compilation
  to fail)
* Removed 9008-run-as-root-by-default.patch, which has not been
  applied for awhile now, with no ill effects.
* Simple refresh of:
  - 0001-remove-RHism.diff.patch
  - 0003-allow-libvirt-group-to-access-the-socket.patch
  - 0004-fix-Debian-specific-path-to-hvm-loader.patch
  - 0006-patch-qemuMonitorTextGetMigrationStatus-to-intercept.patch
  - 9000-delayed_iff_up_bridge.patch
  - 9001-dont_clobber_existing_bridges.patch
  - 9003-better-default-arch.patch
  - 9004-libvirtd-group-name.patch
  - 9005-increase-unix-socket-timeout.patch
  - 9006-default-config-test-case.patch
  - 9009-autodetect-nc-params.patch
  - 9010-dont-disable-ipv6.patch
  - 9011-move-ebtables-script.patch
  - 9014-skip-nodeinfotest.patch
  - 9020-lp545795.patch
* Create a patch to include stdint.h so lxc_container.h, which
  #includes linux/fs.h, doesn't trip up on undefined uint64_t.

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
 
56
56
static char *openvzLocateConfDir(void);
57
57
static int openvzGetVPSUUID(int vpsid, char *uuidstr, size_t len);
58
 
static int openvzLocateConfFile(int vpsid, char *conffile, int maxlen, const char *ext);
 
58
static int openvzLocateConfFile(int vpsid, char **conffile, const char *ext);
59
59
static int openvzAssignUUIDs(void);
60
60
 
61
61
int
64
64
    int val;
65
65
 
66
66
    if (virStrToLong_i(str, NULL, 10, &val) < 0)
67
 
        return 0 ;
 
67
        return 0;
68
68
 
69
69
    return val;
70
70
}
338
338
    from_len = strlen(from);
339
339
    to_len = strlen(to);
340
340
 
341
 
    while((offset = strstr(str_start, from)))
 
341
    while ((offset = strstr(str_start, from)))
342
342
    {
343
343
        virBufferAdd(&buf, str_start, offset-str_start);
344
344
        virBufferAdd(&buf, to, to_len);
447
447
        return -1;
448
448
    }
449
449
 
450
 
    while(!feof(fp)) {
 
450
    while (!feof(fp)) {
451
451
        if (fscanf(fp, "%d %s\n", &veid, status) != 2) {
452
452
            if (feof(fp))
453
453
                break;
507
507
                        veid);
508
508
            goto cleanup;
509
509
        } else if (ret > 0) {
510
 
            dom->def->vcpus = strtoI(temp);
 
510
            dom->def->maxvcpus = strtoI(temp);
511
511
        }
512
512
 
513
 
        if (ret == 0 || dom->def->vcpus == 0)
514
 
            dom->def->vcpus = openvzGetNodeCPUs();
 
513
        if (ret == 0 || dom->def->maxvcpus == 0)
 
514
            dom->def->maxvcpus = openvzGetNodeCPUs();
 
515
        dom->def->vcpus = dom->def->maxvcpus;
515
516
 
516
517
        /* XXX load rest of VM config data .... */
517
518
 
556
557
{
557
558
    char * temp_file = NULL;
558
559
    int fd = -1, temp_fd = -1;
559
 
    char line[PATH_MAX] ;
 
560
    char line[PATH_MAX];
560
561
 
561
562
    if (virAsprintf(&temp_file, "%s.tmp", conf_file)<0) {
562
563
        virReportOOMError();
572
573
        goto error;
573
574
    }
574
575
 
575
 
    while(1) {
 
576
    while (1) {
576
577
        if (openvz_readline(fd, line, sizeof(line)) <= 0)
577
578
            break;
578
579
 
606
607
        close(fd);
607
608
    if (temp_fd != -1)
608
609
        close(temp_fd);
609
 
    if(temp_file)
 
610
    if (temp_file)
610
611
        unlink(temp_file);
611
612
    VIR_FREE(temp_file);
612
613
    return -1;
615
616
int
616
617
openvzWriteVPSConfigParam(int vpsid, const char *param, const char *value)
617
618
{
618
 
    char conf_file[PATH_MAX];
 
619
    char *conf_file;
 
620
    int ret;
619
621
 
620
 
    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
 
622
    if (openvzLocateConfFile(vpsid, &conf_file, "conf") < 0)
621
623
        return -1;
622
624
 
623
 
    return openvzWriteConfigParam(conf_file, param, value);
 
625
    ret = openvzWriteConfigParam(conf_file, param, value);
 
626
    VIR_FREE(conf_file);
 
627
    return ret;
624
628
}
625
629
 
626
630
static int
627
631
openvzReadConfigParam(const char * conf_file ,const char * param, char *value, int maxlen)
628
632
{
629
 
    char line[PATH_MAX] ;
 
633
    char line[PATH_MAX];
630
634
    int ret, found = 0;
631
 
    int fd ;
 
635
    int fd;
632
636
    char * sf, * token;
633
637
    char *saveptr = NULL;
634
638
 
638
642
    if (fd == -1)
639
643
        return -1;
640
644
 
641
 
    while(1) {
 
645
    while (1) {
642
646
        ret = openvz_readline(fd, line, sizeof(line));
643
 
        if(ret <= 0)
 
647
        if (ret <= 0)
644
648
            break;
645
649
        saveptr = NULL;
646
650
        if (STREQLEN(line, param, strlen(param))) {
647
651
            sf = line;
648
652
            sf += strlen(param);
649
653
            if (sf[0] == '=' && sf[1] != '\0' ) {
650
 
                sf ++;
 
654
                sf++;
651
655
                if ((token = strtok_r(sf,"\"\t\n", &saveptr)) != NULL) {
652
656
                    if (virStrcpy(value, token, maxlen) == NULL) {
653
657
                        ret = -1;
663
667
    if (ret == 0 && found)
664
668
        ret = 1;
665
669
 
666
 
    return ret ;
 
670
    return ret;
667
671
}
668
672
 
669
673
/*
676
680
int
677
681
openvzReadVPSConfigParam(int vpsid ,const char * param, char *value, int maxlen)
678
682
{
679
 
    char conf_file[PATH_MAX] ;
 
683
    char *conf_file;
 
684
    int ret;
680
685
 
681
 
    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
 
686
    if (openvzLocateConfFile(vpsid, &conf_file, "conf") < 0)
682
687
        return -1;
683
688
 
684
 
    return openvzReadConfigParam(conf_file, param, value, maxlen);
 
689
    ret = openvzReadConfigParam(conf_file, param, value, maxlen);
 
690
    VIR_FREE(conf_file);
 
691
    return ret;
685
692
}
686
693
 
687
694
static int
700
707
        return -1;
701
708
    }
702
709
 
703
 
    while(1) {
 
710
    while (1) {
704
711
        if (openvz_readline(fd, line, sizeof(line)) <= 0)
705
712
            break;
706
713
 
736
743
    char * confdir = NULL;
737
744
    char * default_conf_file = NULL;
738
745
    char configfile_value[PATH_MAX];
739
 
    char conf_file[PATH_MAX];
 
746
    char *conf_file = NULL;
740
747
    int ret = -1;
741
748
 
742
 
    if(openvzReadConfigParam(VZ_CONF_FILE, "CONFIGFILE", configfile_value, PATH_MAX) < 0)
 
749
    if (openvzReadConfigParam(VZ_CONF_FILE, "CONFIGFILE", configfile_value,
 
750
                              PATH_MAX) < 0)
743
751
        goto cleanup;
744
752
 
745
753
    confdir = openvzLocateConfDir();
746
754
    if (confdir == NULL)
747
755
        goto cleanup;
748
756
 
749
 
    if (virAsprintf(&default_conf_file, "%s/ve-%s.conf-sample", confdir, configfile_value) < 0) {
 
757
    if (virAsprintf(&default_conf_file, "%s/ve-%s.conf-sample", confdir,
 
758
                    configfile_value) < 0) {
750
759
        virReportOOMError();
751
760
        goto cleanup;
752
761
    }
753
762
 
754
 
    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
 
763
    if (openvzLocateConfFile(vpsid, &conf_file, "conf") < 0)
755
764
        goto cleanup;
756
765
 
757
766
    if (openvz_copyfile(default_conf_file, conf_file)<0)
761
770
cleanup:
762
771
    VIR_FREE(confdir);
763
772
    VIR_FREE(default_conf_file);
 
773
    VIR_FREE(conf_file);
764
774
    return ret;
765
775
}
766
776
 
769
779
*         0 - OK
770
780
*/
771
781
static int
772
 
openvzLocateConfFile(int vpsid, char *conffile, int maxlen, const char *ext)
 
782
openvzLocateConfFile(int vpsid, char **conffile, const char *ext)
773
783
{
774
784
    char * confdir;
775
785
    int ret = 0;
778
788
    if (confdir == NULL)
779
789
        return -1;
780
790
 
781
 
    if (snprintf(conffile, maxlen, "%s/%d.%s",
782
 
                 confdir, vpsid, ext ? ext : "conf") >= maxlen)
 
791
    if (virAsprintf(conffile, "%s/%d.%s", confdir, vpsid,
 
792
                    ext ? ext : "conf") < 0) {
 
793
        virReportOOMError();
783
794
        ret = -1;
 
795
    }
784
796
 
785
797
    VIR_FREE(confdir);
786
798
    return ret;
792
804
    const char *conf_dir_list[] = {"/etc/vz/conf", "/usr/local/etc/conf", NULL};
793
805
    int i=0;
794
806
 
795
 
    while(conf_dir_list[i]) {
796
 
        if(!access(conf_dir_list[i], F_OK))
 
807
    while (conf_dir_list[i]) {
 
808
        if (!access(conf_dir_list[i], F_OK))
797
809
            return strdup(conf_dir_list[i]);
798
 
        i ++;
 
810
        i++;
799
811
    }
800
812
 
801
813
    return NULL;
808
820
    int n, rc;
809
821
    char c;
810
822
 
811
 
    for(n = 1; n < maxlen; n ++) {
812
 
        if( (rc = read(fd, &c, 1)) == 1) {
 
823
    for (n = 1; n < maxlen; n++) {
 
824
        if ( (rc = read(fd, &c, 1)) == 1) {
813
825
            *ptr++ = c;
814
 
            if(c == '\n')
 
826
            if (c == '\n')
815
827
                break;
816
 
        }
817
 
        else if(rc == 0) {
818
 
            if(n == 1)
 
828
        } else if (rc == 0) {
 
829
            if (n == 1)
819
830
                return 0; /* EOF condition */
820
831
            else
821
832
                break;
830
841
static int
831
842
openvzGetVPSUUID(int vpsid, char *uuidstr, size_t len)
832
843
{
833
 
    char conf_file[PATH_MAX];
 
844
    char *conf_file;
834
845
    char line[1024];
835
846
    char *saveptr = NULL;
836
847
    char *uuidbuf;
837
848
    char *iden;
838
849
    int fd, ret;
839
 
    int retval = 0;
 
850
    int retval = -1;
840
851
 
841
 
    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
842
 
       return -1;
 
852
    if (openvzLocateConfFile(vpsid, &conf_file, "conf") < 0)
 
853
        return -1;
843
854
 
844
855
    fd = open(conf_file, O_RDONLY);
845
 
    if(fd == -1)
846
 
        return -1;
 
856
    if (fd == -1)
 
857
        goto cleanup;
847
858
 
848
 
    while(1) {
 
859
    while (1) {
849
860
        ret = openvz_readline(fd, line, sizeof(line));
850
 
        if(ret == -1) {
851
 
            close(fd);
852
 
            return -1;
853
 
        }
 
861
        if (ret == -1)
 
862
            goto cleanup;
854
863
 
855
 
        if(ret == 0) { /* EoF, UUID was not found */
 
864
        if (ret == 0) { /* EoF, UUID was not found */
856
865
            uuidstr[0] = 0;
857
866
            break;
858
867
        }
861
870
        uuidbuf = strtok_r(NULL, "\n", &saveptr);
862
871
 
863
872
        if (iden != NULL && uuidbuf != NULL && STREQ(iden, "#UUID:")) {
864
 
            if (virStrcpy(uuidstr, uuidbuf, len) == NULL)
865
 
                retval = -1;
 
873
            if (virStrcpy(uuidstr, uuidbuf, len) == NULL) {
 
874
                openvzError(VIR_ERR_INTERNAL_ERROR,
 
875
                            _("invalid uuid %s"), uuidbuf);
 
876
                goto cleanup;
 
877
            }
866
878
            break;
867
879
        }
868
880
    }
869
 
    close(fd);
 
881
    retval = 0;
 
882
cleanup:
 
883
    if (fd >= 0)
 
884
        close(fd);
 
885
    VIR_FREE(conf_file);
870
886
 
871
887
    return retval;
872
888
}
877
893
int
878
894
openvzSetDefinedUUID(int vpsid, unsigned char *uuid)
879
895
{
880
 
    char conf_file[PATH_MAX];
 
896
    char *conf_file;
881
897
    char uuidstr[VIR_UUID_STRING_BUFLEN];
 
898
    int ret = -1;
882
899
 
883
900
    if (uuid == NULL)
884
901
        return -1;
885
902
 
886
 
    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0)
887
 
       return -1;
 
903
    if (openvzLocateConfFile(vpsid, &conf_file, "conf") < 0)
 
904
        return -1;
888
905
 
889
906
    if (openvzGetVPSUUID(vpsid, uuidstr, sizeof(uuidstr)))
890
 
        return -1;
 
907
        goto cleanup;
891
908
 
892
909
    if (uuidstr[0] == 0) {
893
910
        FILE *fp = fopen(conf_file, "a"); /* append */
894
911
        if (fp == NULL)
895
 
          return -1;
 
912
            goto cleanup;
896
913
 
897
914
        virUUIDFormat(uuid, uuidstr);
898
915
 
900
917
           and be careful always to close the stream.  */
901
918
        if ((fprintf(fp, "\n#UUID: %s\n", uuidstr) < 0)
902
919
            + (fclose(fp) == EOF))
903
 
            return -1;
 
920
            goto cleanup;
904
921
    }
905
922
 
906
 
    return 0;
 
923
    ret = 0;
 
924
cleanup:
 
925
    VIR_FREE(conf_file);
 
926
    return ret;
907
927
}
908
928
 
909
929
static int
939
959
        return -1;
940
960
 
941
961
    dp = opendir(conf_dir);
942
 
    if(dp == NULL) {
 
962
    if (dp == NULL) {
943
963
        VIR_FREE(conf_dir);
944
964
        return 0;
945
965
    }
946
966
 
947
 
    while((dent = readdir(dp))) {
 
967
    while ((dent = readdir(dp))) {
948
968
        res = sscanf(dent->d_name, "%d.%5s", &vpsid, ext);
949
 
        if(!(res == 2 && STREQ(ext, "conf")))
 
969
        if (!(res == 2 && STREQ(ext, "conf")))
950
970
            continue;
951
 
        if(vpsid > 0) /* '0.conf' belongs to the host, ignore it */
 
971
        if (vpsid > 0) /* '0.conf' belongs to the host, ignore it */
952
972
            openvzSetUUID(vpsid);
953
973
    }
954
974
    closedir(dp);