static int lxcSetCpusetTune(virDomainDefPtr def, virConfPtr properties) { VIR_AUTOFREE(char *) value = NULL; virBitmapPtr nodeset = NULL; if (virConfGetValueString(properties, "lxc.cgroup.cpuset.cpus", &value) > 0) { if (virBitmapParse(value, &def->cpumask, VIR_DOMAIN_CPUMASK_LEN) < 0) return -1; def->placement_mode = VIR_DOMAIN_CPU_PLACEMENT_MODE_STATIC; VIR_FREE(value); } if (virConfGetValueString(properties, "lxc.cgroup.cpuset.mems", &value) > 0) { if (virBitmapParse(value, &nodeset, VIR_DOMAIN_CPUMASK_LEN) < 0) return -1; if (virDomainNumatuneSet(def->numa, def->placement_mode == VIR_DOMAIN_CPU_PLACEMENT_MODE_STATIC, VIR_DOMAIN_NUMATUNE_PLACEMENT_STATIC, VIR_DOMAIN_NUMATUNE_MEM_STRICT, nodeset) < 0) { virBitmapFree(nodeset); return -1; } virBitmapFree(nodeset); } return 0; }
static int lxcSetMemTune(virDomainDefPtr def, virConfPtr properties) { VIR_AUTOFREE(char *) value = NULL; unsigned long long size = 0; if (virConfGetValueString(properties, "lxc.cgroup.memory.limit_in_bytes", &value) > 0) { if (lxcConvertSize(value, &size) < 0) return -1; size = size / 1024; virDomainDefSetMemoryTotal(def, size); def->mem.hard_limit = virMemoryLimitTruncate(size); VIR_FREE(value); } if (virConfGetValueString(properties, "lxc.cgroup.memory.soft_limit_in_bytes", &value) > 0) { if (lxcConvertSize(value, &size) < 0) return -1; def->mem.soft_limit = virMemoryLimitTruncate(size / 1024); VIR_FREE(value); } if (virConfGetValueString(properties, "lxc.cgroup.memory.memsw.limit_in_bytes", &value) > 0) { if (lxcConvertSize(value, &size) < 0) return -1; def->mem.swap_hard_limit = virMemoryLimitTruncate(size / 1024); } return 0; }
static int lxcSetCpuTune(virDomainDefPtr def, virConfPtr properties) { VIR_AUTOFREE(char *) value = NULL; if (virConfGetValueString(properties, "lxc.cgroup.cpu.shares", &value) > 0) { if (virStrToLong_ull(value, NULL, 10, &def->cputune.shares) < 0) goto error; def->cputune.sharesSpecified = true; VIR_FREE(value); } if (virConfGetValueString(properties, "lxc.cgroup.cpu.cfs_quota_us", &value) > 0) { if (virStrToLong_ll(value, NULL, 10, &def->cputune.quota) < 0) goto error; VIR_FREE(value); } if (virConfGetValueString(properties, "lxc.cgroup.cpu.cfs_period_us", &value) > 0) { if (virStrToLong_ull(value, NULL, 10, &def->cputune.period) < 0) goto error; } return 0; error: virReportError(VIR_ERR_INTERNAL_ERROR, _("failed to parse integer: '%s'"), value); return -1; }
static int virLockDaemonConfigLoadOptions(virLockDaemonConfigPtr data, virConfPtr conf) { if (virConfGetValueUInt(conf, "log_level", &data->log_level) < 0) return -1; if (virConfGetValueString(conf, "log_filters", &data->log_filters) < 0) return -1; if (virConfGetValueString(conf, "log_outputs", &data->log_outputs) < 0) return -1; if (virConfGetValueUInt(conf, "max_clients", &data->max_clients) < 0) return -1; return 0; }
int virLXCLoadDriverConfig(virLXCDriverConfigPtr cfg, const char *filename) { virConfPtr conf; int ret = -1; /* Avoid error from non-existent or unreadable file. */ if (access(filename, R_OK) == -1) return 0; conf = virConfReadFile(filename, 0); if (!conf) return -1; if (virConfGetValueBool(conf, "log_with_libvirtd", &cfg->log_libvirtd) < 0) goto cleanup; if (virConfGetValueString(conf, "security_driver", &cfg->securityDriverName) < 0) goto cleanup; if (virConfGetValueBool(conf, "security_default_confined", &cfg->securityDefaultConfined) < 0) goto cleanup; if (virConfGetValueBool(conf, "security_require_confined", &cfg->securityRequireConfined) < 0) goto cleanup; ret = 0; cleanup: virConfFree(conf); return ret; }
static int virAdmGetDefaultURI(virConfPtr conf, char **uristr) { const char *defname = virGetEnvAllowSUID("LIBVIRT_ADMIN_DEFAULT_URI"); if (defname && *defname) { if (VIR_STRDUP(*uristr, defname) < 0) return -1; VIR_DEBUG("Using LIBVIRT_ADMIN_DEFAULT_URI '%s'", *uristr); } else { if (virConfGetValueString(conf, "admin_uri_default", uristr) < 0) return -1; if (*uristr) { VIR_DEBUG("Using config file uri '%s'", *uristr); } else { /* Since we can't probe connecting via any hypervisor driver as libvirt * does, if no explicit URI was given and neither the environment * variable, nor the configuration parameter had previously been set, * we set the default admin server URI to 'libvirtd://system'. */ if (VIR_STRDUP(*uristr, "libvirtd:///system") < 0) return -1; } } return 0; }
static int lxcCreateConsoles(virDomainDefPtr def, virConfPtr properties) { VIR_AUTOFREE(char *) value = NULL; int nbttys = 0; virDomainChrDefPtr console; size_t i; if (virConfGetValueString(properties, "lxc.tty.max", &value) <= 0) { virResetLastError(); /* Check for pre LXC 3.0 legacy key */ if (virConfGetValueString(properties, "lxc.tty", &value) <= 0) return 0; } if (virStrToLong_i(value, NULL, 10, &nbttys) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("failed to parse int: '%s'"), value); return -1; } if (VIR_ALLOC_N(def->consoles, nbttys) < 0) return -1; def->nconsoles = nbttys; for (i = 0; i < nbttys; i++) { if (!(console = virDomainChrDefNew(NULL))) goto error; console->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE; console->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_LXC; console->target.port = i; console->source->type = VIR_DOMAIN_CHR_TYPE_PTY; def->consoles[i] = console; } return 0; error: virDomainChrDefFree(console); return -1; }
static int virLockManagerLockDaemonLoadConfig(const char *configFile) { virConfPtr conf; int ret = -1; if (access(configFile, R_OK) == -1) { if (errno != ENOENT) { virReportSystemError(errno, _("Unable to access config file %s"), configFile); return -1; } return 0; } if (!(conf = virConfReadFile(configFile, 0))) return -1; if (virConfGetValueBool(conf, "auto_disk_leases", &driver->autoDiskLease) < 0) goto cleanup; if (virConfGetValueString(conf, "file_lockspace_dir", &driver->fileLockSpaceDir) < 0) goto cleanup; if (virConfGetValueString(conf, "lvm_lockspace_dir", &driver->lvmLockSpaceDir) < 0) goto cleanup; if (virConfGetValueString(conf, "scsi_lockspace_dir", &driver->scsiLockSpaceDir) < 0) goto cleanup; driver->requireLeaseForDisks = !driver->autoDiskLease; if (virConfGetValueBool(conf, "require_lease_for_disks", &driver->requireLeaseForDisks) < 0) goto cleanup; ret = 0; cleanup: virConfFree(conf); return ret; }
static int lxcSetRootfs(virDomainDefPtr def, virConfPtr properties) { int type = VIR_DOMAIN_FS_TYPE_MOUNT; VIR_AUTOFREE(char *) value = NULL; if (virConfGetValueString(properties, "lxc.rootfs.path", &value) <= 0) { virResetLastError(); /* Check for pre LXC 3.0 legacy key */ if (virConfGetValueString(properties, "lxc.rootfs", &value) <= 0) return -1; } if (STRPREFIX(value, "/dev/")) type = VIR_DOMAIN_FS_TYPE_BLOCK; if (lxcAddFSDef(def, type, value, "/", false, 0) < 0) return -1; return 0; }
static int lxcSetBlkioTune(virDomainDefPtr def, virConfPtr properties) { VIR_AUTOFREE(char *) value = NULL; if (virConfGetValueString(properties, "lxc.cgroup.blkio.weight", &value) > 0) { if (virStrToLong_ui(value, NULL, 10, &def->blkio.weight) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("failed to parse integer: '%s'"), value); return -1; } } if (virConfWalk(properties, lxcBlkioDeviceWalkCallback, def) < 0) return -1; return 0; }
static void lxcSetCapDrop(virDomainDefPtr def, virConfPtr properties) { VIR_AUTOFREE(char *) value = NULL; char **toDrop = NULL; const char *capString; size_t i; if (virConfGetValueString(properties, "lxc.cap.drop", &value) > 0) toDrop = virStringSplit(value, " ", 0); for (i = 0; i < VIR_DOMAIN_CAPS_FEATURE_LAST; i++) { capString = virDomainCapsFeatureTypeToString(i); if (toDrop != NULL && virStringListHasString((const char **)toDrop, capString)) def->caps_features[i] = VIR_TRISTATE_SWITCH_OFF; } def->features[VIR_DOMAIN_FEATURE_CAPABILITIES] = VIR_DOMAIN_CAPABILITIES_POLICY_ALLOW; virStringListFree(toDrop); }
virDomainDefPtr lxcParseConfigString(const char *config, virCapsPtr caps, virDomainXMLOptionPtr xmlopt) { virDomainDefPtr vmdef = NULL; virConfPtr properties = NULL; VIR_AUTOFREE(char *) value = NULL; if (!(properties = virConfReadString(config, VIR_CONF_FLAG_LXC_FORMAT))) return NULL; if (!(vmdef = virDomainDefNew())) goto error; if (virUUIDGenerate(vmdef->uuid) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to generate uuid")); goto error; } vmdef->id = -1; virDomainDefSetMemoryTotal(vmdef, 64 * 1024); vmdef->onReboot = VIR_DOMAIN_LIFECYCLE_ACTION_RESTART; vmdef->onCrash = VIR_DOMAIN_LIFECYCLE_ACTION_DESTROY; vmdef->onPoweroff = VIR_DOMAIN_LIFECYCLE_ACTION_DESTROY; vmdef->virtType = VIR_DOMAIN_VIRT_LXC; /* Value not handled by the LXC driver, setting to * minimum required to make XML parsing pass */ if (virDomainDefSetVcpusMax(vmdef, 1, xmlopt) < 0) goto error; if (virDomainDefSetVcpus(vmdef, 1) < 0) goto error; vmdef->nfss = 0; vmdef->os.type = VIR_DOMAIN_OSTYPE_EXE; if (virConfGetValueString(properties, "lxc.arch", &value) > 0) { virArch arch = virArchFromString(value); if (arch == VIR_ARCH_NONE && STREQ(value, "x86")) arch = VIR_ARCH_I686; else if (arch == VIR_ARCH_NONE && STREQ(value, "amd64")) arch = VIR_ARCH_X86_64; vmdef->os.arch = arch; VIR_FREE(value); } if (VIR_STRDUP(vmdef->os.init, "/sbin/init") < 0) goto error; if (virConfGetValueString(properties, "lxc.uts.name", &value) <= 0) { virResetLastError(); /* Check for pre LXC 3.0 legacy key */ if (virConfGetValueString(properties, "lxc.utsname", &value) <= 0) goto error; } if (VIR_STRDUP(vmdef->name, value) < 0) goto error; if (!vmdef->name && (VIR_STRDUP(vmdef->name, "unnamed") < 0)) goto error; if (lxcSetRootfs(vmdef, properties) < 0) goto error; /* LXC 3.0 uses "lxc.mount.fstab", while legacy used just "lxc.mount". * In either case, generate the error to use "lxc.mount.entry" instead */ if (virConfGetValue(properties, "lxc.mount.fstab") || virConfGetValue(properties, "lxc.mount")) { virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("lxc.mount.fstab or lxc.mount found, use " "lxc.mount.entry lines instead")); goto error; } /* Loop over lxc.mount.entry to add filesystem devices for them */ if (virConfWalk(properties, lxcFstabWalkCallback, vmdef) < 0) goto error; /* Network configuration */ if (lxcConvertNetworkSettings(vmdef, properties) < 0) goto error; /* Consoles */ if (lxcCreateConsoles(vmdef, properties) < 0) goto error; /* lxc.idmap or legacy lxc.id_map */ if (virConfWalk(properties, lxcIdmapWalkCallback, vmdef) < 0) goto error; /* lxc.cgroup.memory.* */ if (lxcSetMemTune(vmdef, properties) < 0) goto error; /* lxc.cgroup.cpu.* */ if (lxcSetCpuTune(vmdef, properties) < 0) goto error; /* lxc.cgroup.cpuset.* */ if (lxcSetCpusetTune(vmdef, properties) < 0) goto error; /* lxc.cgroup.blkio.* */ if (lxcSetBlkioTune(vmdef, properties) < 0) goto error; /* lxc.cap.drop */ lxcSetCapDrop(vmdef, properties); if (virDomainDefPostParse(vmdef, caps, VIR_DOMAIN_DEF_PARSE_ABI_UPDATE, xmlopt, NULL) < 0) goto cleanup; goto cleanup; error: virDomainDefFree(vmdef); vmdef = NULL; cleanup: virConfFree(properties); return vmdef; }