Report full errors from virCgroupNew*

Instead of returning raw errno values, report full libvirt
errors in virCgroupNew* functions.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2013-07-04 16:49:24 +01:00
parent f5384eed3f
commit b64dabff27
9 changed files with 418 additions and 443 deletions

View File

@ -1156,6 +1156,7 @@ virCgroupAddTaskController;
virCgroupAllowDevice;
virCgroupAllowDeviceMajor;
virCgroupAllowDevicePath;
virCgroupAvailable;
virCgroupControllerTypeFromString;
virCgroupControllerTypeToString;
virCgroupDenyAllDevices;
@ -1188,6 +1189,7 @@ virCgroupNewDomainDriver;
virCgroupNewDomainPartition;
virCgroupNewDriver;
virCgroupNewEmulator;
virCgroupNewIgnoreError;
virCgroupNewPartition;
virCgroupNewSelf;
virCgroupNewVcpu;

View File

@ -306,33 +306,29 @@ cleanup:
int virLXCCgroupGetMeminfo(virLXCMeminfoPtr meminfo)
{
int ret;
int ret = -1, rc;
virCgroupPtr cgroup;
ret = virCgroupNewSelf(&cgroup);
if (ret < 0) {
virReportSystemError(-ret, "%s",
_("Unable to get cgroup for container"));
return ret;
}
if (virCgroupNewSelf(&cgroup) < 0)
return -1;
ret = virLXCCgroupGetMemStat(cgroup, meminfo);
if (ret < 0) {
virReportSystemError(-ret, "%s",
rc = virLXCCgroupGetMemStat(cgroup, meminfo);
if (rc < 0) {
virReportSystemError(-rc, "%s",
_("Unable to get memory cgroup stat info"));
goto cleanup;
}
ret = virLXCCgroupGetMemTotal(cgroup, meminfo);
if (ret < 0) {
virReportSystemError(-ret, "%s",
rc = virLXCCgroupGetMemTotal(cgroup, meminfo);
if (rc < 0) {
virReportSystemError(-rc, "%s",
_("Unable to get memory cgroup total"));
goto cleanup;
}
ret = virLXCCgroupGetMemUsage(cgroup, meminfo);
if (ret < 0) {
virReportSystemError(-ret, "%s",
rc = virLXCCgroupGetMemUsage(cgroup, meminfo);
if (rc < 0) {
virReportSystemError(-rc, "%s",
_("Unable to get memory cgroup stat usage"));
goto cleanup;
}
@ -541,7 +537,6 @@ cleanup:
virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def, bool startup)
{
int rc;
virCgroupPtr parent = NULL;
virCgroupPtr cgroup = NULL;
@ -569,50 +564,30 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def, bool startup)
}
/* We only auto-create the default partition. In other
* cases we expec the sysadmin/app to have done so */
rc = virCgroupNewPartition(def->resource->partition,
STREQ(def->resource->partition, "/machine"),
-1,
&parent);
if (rc != 0) {
virReportSystemError(-rc,
_("Unable to initialize %s cgroup"),
def->resource->partition);
if (virCgroupNewPartition(def->resource->partition,
STREQ(def->resource->partition, "/machine"),
-1,
&parent) < 0)
goto cleanup;
}
rc = virCgroupNewDomainPartition(parent,
"lxc",
def->name,
true,
&cgroup);
if (rc != 0) {
virReportSystemError(-rc,
_("Unable to create cgroup for %s"),
def->name);
if (virCgroupNewDomainPartition(parent,
"lxc",
def->name,
true,
&cgroup) < 0)
goto cleanup;
}
} else {
rc = virCgroupNewDriver("lxc",
true,
-1,
&parent);
if (rc != 0) {
virReportSystemError(-rc,
_("Unable to create cgroup for %s"),
def->name);
if (virCgroupNewDriver("lxc",
true,
-1,
&parent) < 0)
goto cleanup;
}
rc = virCgroupNewDomainDriver(parent,
def->name,
true,
&cgroup);
if (rc != 0) {
virReportSystemError(-rc,
_("Unable to create cgroup for %s"),
def->name);
if (virCgroupNewDomainDriver(parent,
def->name,
true,
&cgroup) < 0)
goto cleanup;
}
}
cleanup:

View File

@ -1466,7 +1466,6 @@ static int lxcContainerSetupPivotRoot(virDomainDefPtr vmDef,
virSecurityManagerPtr securityDriver)
{
virCgroupPtr cgroup = NULL;
int rc;
int ret = -1;
char *sec_mount_options;
char *stateDir = NULL;
@ -1478,11 +1477,8 @@ static int lxcContainerSetupPivotRoot(virDomainDefPtr vmDef,
/* Before pivoting we need to identify any
* cgroups controllers that are mounted */
if ((rc = virCgroupNewSelf(&cgroup)) != 0) {
virReportSystemError(-rc, "%s",
_("Cannot identify cgroup placement"));
if (virCgroupNewSelf(&cgroup) < 0)
goto cleanup;
}
if (virFileResolveAllLinks(LXC_STATE_DIR, &stateDir) < 0)
goto cleanup;

View File

@ -139,8 +139,10 @@ static int lxcProcReadMeminfo(char *hostpath, virDomainDefPtr def,
virBuffer buffer = VIR_BUFFER_INITIALIZER;
virBufferPtr new_meminfo = &buffer;
if ((res = virLXCCgroupGetMeminfo(&meminfo)) < 0)
return res;
if (virLXCCgroupGetMeminfo(&meminfo) < 0) {
virErrorSetErrnoFromLastError();
return -errno;
}
fd = fopen(hostpath, "r");
if (fd == NULL) {

View File

@ -731,6 +731,9 @@ qemuInitCgroup(virQEMUDriverPtr driver,
if (!cfg->privileged)
goto done;
if (!virCgroupAvailable())
goto done;
virCgroupFree(&priv->cgroup);
if (!vm->def->resource && startup) {
@ -757,64 +760,38 @@ qemuInitCgroup(virQEMUDriverPtr driver,
}
/* We only auto-create the default partition. In other
* cases we expec the sysadmin/app to have done so */
rc = virCgroupNewPartition(vm->def->resource->partition,
STREQ(vm->def->resource->partition, "/machine"),
cfg->cgroupControllers,
&parent);
if (rc != 0) {
if (rc == -ENXIO ||
rc == -EPERM ||
rc == -EACCES) { /* No cgroups mounts == success */
VIR_DEBUG("No cgroups present/configured/accessible, ignoring error");
if (virCgroupNewPartition(vm->def->resource->partition,
STREQ(vm->def->resource->partition, "/machine"),
cfg->cgroupControllers,
&parent) < 0) {
if (virCgroupNewIgnoreError())
goto done;
}
virReportSystemError(-rc,
_("Unable to initialize %s cgroup"),
vm->def->resource->partition);
goto cleanup;
}
rc = virCgroupNewDomainPartition(parent,
"qemu",
vm->def->name,
true,
&priv->cgroup);
if (rc != 0) {
virReportSystemError(-rc,
_("Unable to create cgroup for %s"),
vm->def->name);
if (virCgroupNewDomainPartition(parent,
"qemu",
vm->def->name,
true,
&priv->cgroup) < 0)
goto cleanup;
}
} else {
rc = virCgroupNewDriver("qemu",
true,
cfg->cgroupControllers,
&parent);
if (rc != 0) {
if (rc == -ENXIO ||
rc == -EPERM ||
rc == -EACCES) { /* No cgroups mounts == success */
VIR_DEBUG("No cgroups present/configured/accessible, ignoring error");
if (virCgroupNewDriver("qemu",
true,
cfg->cgroupControllers,
&parent) < 0) {
if (virCgroupNewIgnoreError())
goto done;
}
virReportSystemError(-rc,
_("Unable to create cgroup for %s"),
vm->def->name);
goto cleanup;
}
rc = virCgroupNewDomainDriver(parent,
vm->def->name,
true,
&priv->cgroup);
if (rc != 0) {
virReportSystemError(-rc,
_("Unable to create cgroup for %s"),
vm->def->name);
if (virCgroupNewDomainDriver(parent,
vm->def->name,
true,
&priv->cgroup) < 0)
goto cleanup;
}
}
done:
@ -994,14 +971,8 @@ qemuSetupCgroupForVcpu(virDomainObjPtr vm)
}
for (i = 0; i < priv->nvcpupids; i++) {
rc = virCgroupNewVcpu(priv->cgroup, i, true, &cgroup_vcpu);
if (rc < 0) {
virReportSystemError(-rc,
_("Unable to create vcpu cgroup for %s(vcpu:"
" %zu)"),
vm->def->name, i);
if (virCgroupNewVcpu(priv->cgroup, i, true, &cgroup_vcpu) < 0)
goto cleanup;
}
/* move the thread for vcpu to sub dir */
rc = virCgroupAddTask(cgroup_vcpu, priv->vcpupids[i]);
@ -1061,7 +1032,7 @@ qemuSetupCgroupForEmulator(virQEMUDriverPtr driver,
qemuDomainObjPrivatePtr priv = vm->privateData;
unsigned long long period = vm->def->cputune.emulator_period;
long long quota = vm->def->cputune.emulator_quota;
int rc;
int rc = -1;
if ((period || quota) &&
!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
@ -1073,13 +1044,8 @@ qemuSetupCgroupForEmulator(virQEMUDriverPtr driver,
if (priv->cgroup == NULL)
return 0; /* Not supported, so claim success */
rc = virCgroupNewEmulator(priv->cgroup, true, &cgroup_emulator);
if (rc < 0) {
virReportSystemError(-rc,
_("Unable to create emulator cgroup for %s"),
vm->def->name);
if (virCgroupNewEmulator(priv->cgroup, true, &cgroup_emulator) < 0)
goto cleanup;
}
rc = virCgroupMoveTask(priv->cgroup, cgroup_emulator);
if (rc < 0) {

View File

@ -3940,14 +3940,8 @@ static int qemuDomainHotplugVcpus(virQEMUDriverPtr driver,
if (priv->cgroup) {
int rv = -1;
/* Create cgroup for the onlined vcpu */
rv = virCgroupNewVcpu(priv->cgroup, i, true, &cgroup_vcpu);
if (rv < 0) {
virReportSystemError(-rv,
_("Unable to create vcpu cgroup for %s(vcpu:"
" %zu)"),
vm->def->name, i);
if (virCgroupNewVcpu(priv->cgroup, i, true, &cgroup_vcpu) < 0)
goto cleanup;
}
/* Add vcpu thread to the cgroup */
rv = virCgroupAddTask(cgroup_vcpu, cpupids[i]);
@ -4008,16 +4002,8 @@ static int qemuDomainHotplugVcpus(virQEMUDriverPtr driver,
virDomainVcpuPinDefPtr vcpupin = NULL;
if (priv->cgroup) {
int rv = -1;
rv = virCgroupNewVcpu(priv->cgroup, i, false, &cgroup_vcpu);
if (rv < 0) {
virReportSystemError(-rv,
_("Unable to access vcpu cgroup for %s(vcpu:"
" %zu)"),
vm->def->name, i);
if (virCgroupNewVcpu(priv->cgroup, i, false, &cgroup_vcpu) < 0)
goto cleanup;
}
/* Remove cgroup for the offlined vcpu */
virCgroupRemove(cgroup_vcpu);
@ -4358,8 +4344,9 @@ qemuDomainPinVcpuFlags(virDomainPtr dom,
/* Configure the corresponding cpuset cgroup before set affinity. */
if (virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET)) {
if (virCgroupNewVcpu(priv->cgroup, vcpu, false, &cgroup_vcpu) == 0 &&
qemuSetupCgroupVcpuPin(cgroup_vcpu, newVcpuPin, newVcpuPinNum, vcpu) < 0) {
if (virCgroupNewVcpu(priv->cgroup, vcpu, false, &cgroup_vcpu) < 0)
goto cleanup;
if (qemuSetupCgroupVcpuPin(cgroup_vcpu, newVcpuPin, newVcpuPinNum, vcpu) < 0) {
virReportError(VIR_ERR_OPERATION_INVALID,
_("failed to set cpuset.cpus in cgroup"
" for vcpu %d"), vcpu);
@ -4620,16 +4607,15 @@ qemuDomainPinEmulator(virDomainPtr dom,
VIR_CGROUP_CONTROLLER_CPUSET)) {
/*
* Configure the corresponding cpuset cgroup.
* If no cgroup for domain or hypervisor exists, do nothing.
*/
if (virCgroupNewEmulator(priv->cgroup, false, &cgroup_emulator) == 0) {
if (qemuSetupCgroupEmulatorPin(cgroup_emulator,
newVcpuPin[0]->cpumask) < 0) {
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
_("failed to set cpuset.cpus in cgroup"
" for emulator threads"));
goto cleanup;
}
if (virCgroupNewEmulator(priv->cgroup, false, &cgroup_emulator) < 0)
goto cleanup;
if (qemuSetupCgroupEmulatorPin(cgroup_emulator,
newVcpuPin[0]->cpumask) < 0) {
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
_("failed to set cpuset.cpus in cgroup"
" for emulator threads"));
goto cleanup;
}
} else {
if (virProcessSetAffinity(pid, pcpumap) < 0) {
@ -8596,7 +8582,6 @@ qemuSetVcpusBWLive(virDomainObjPtr vm, virCgroupPtr cgroup,
size_t i;
qemuDomainObjPrivatePtr priv = vm->privateData;
virCgroupPtr cgroup_vcpu = NULL;
int rc;
if (period == 0 && quota == 0)
return 0;
@ -8607,14 +8592,8 @@ qemuSetVcpusBWLive(virDomainObjPtr vm, virCgroupPtr cgroup,
*/
if (priv->nvcpupids != 0 && priv->vcpupids[0] != vm->pid) {
for (i = 0; i < priv->nvcpupids; i++) {
rc = virCgroupNewVcpu(cgroup, i, false, &cgroup_vcpu);
if (rc < 0) {
virReportSystemError(-rc,
_("Unable to find vcpu cgroup for %s(vcpu:"
" %zu)"),
vm->def->name, i);
if (virCgroupNewVcpu(cgroup, i, false, &cgroup_vcpu) < 0)
goto cleanup;
}
if (qemuSetupCgroupVcpuBW(cgroup_vcpu, period, quota) < 0)
goto cleanup;
@ -8636,7 +8615,6 @@ qemuSetEmulatorBandwidthLive(virDomainObjPtr vm, virCgroupPtr cgroup,
{
qemuDomainObjPrivatePtr priv = vm->privateData;
virCgroupPtr cgroup_emulator = NULL;
int rc;
if (period == 0 && quota == 0)
return 0;
@ -8645,13 +8623,8 @@ qemuSetEmulatorBandwidthLive(virDomainObjPtr vm, virCgroupPtr cgroup,
return 0;
}
rc = virCgroupNewEmulator(cgroup, false, &cgroup_emulator);
if (rc < 0) {
virReportSystemError(-rc,
_("Unable to find emulator cgroup for %s"),
vm->def->name);
if (virCgroupNewEmulator(cgroup, false, &cgroup_emulator) < 0)
goto cleanup;
}
if (qemuSetupCgroupVcpuBW(cgroup_emulator, period, quota) < 0)
goto cleanup;
@ -8897,13 +8870,8 @@ qemuGetVcpusBWLive(virDomainObjPtr vm,
}
/* get period and quota for vcpu0 */
rc = virCgroupNewVcpu(priv->cgroup, 0, false, &cgroup_vcpu);
if (!cgroup_vcpu) {
virReportSystemError(-rc,
_("Unable to find vcpu cgroup for %s(vcpu: 0)"),
vm->def->name);
if (virCgroupNewVcpu(priv->cgroup, 0, false, &cgroup_vcpu) < 0)
goto cleanup;
}
rc = qemuGetVcpuBWLive(cgroup_vcpu, period, quota);
if (rc < 0)
@ -8935,13 +8903,8 @@ qemuGetEmulatorBandwidthLive(virDomainObjPtr vm, virCgroupPtr cgroup,
}
/* get period and quota for emulator */
rc = virCgroupNewEmulator(cgroup, false, &cgroup_emulator);
if (!cgroup_emulator) {
virReportSystemError(-rc,
_("Unable to find emulator cgroup for %s"),
vm->def->name);
if (virCgroupNewEmulator(cgroup, false, &cgroup_emulator) < 0)
goto cleanup;
}
rc = qemuGetVcpuBWLive(cgroup_emulator, period, quota);
if (rc < 0)
@ -15537,11 +15500,8 @@ getSumVcpuPercpuStats(virDomainObjPtr vm,
unsigned long long tmp;
size_t j;
if (virCgroupNewVcpu(priv->cgroup, i, false, &group_vcpu) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("error accessing cgroup cpuacct for vcpu"));
if (virCgroupNewVcpu(priv->cgroup, i, false, &group_vcpu) < 0)
goto cleanup;
}
if (virCgroupGetCpuacctPercpuUsage(group_vcpu, &buf) < 0)
goto cleanup;

File diff suppressed because it is too large Load Diff

View File

@ -46,6 +46,8 @@ enum {
VIR_ENUM_DECL(virCgroupController);
bool virCgroupAvailable(void);
int virCgroupNewPartition(const char *path,
bool create,
int controllers,
@ -84,6 +86,8 @@ int virCgroupNewEmulator(virCgroupPtr domain,
virCgroupPtr *group)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
bool virCgroupNewIgnoreError(void);
int virCgroupPathOfController(virCgroupPtr group,
int controller,
const char *key,

View File

@ -136,6 +136,18 @@ cleanup:
}
# define ENSURE_ERRNO(en) \
do { \
if (!virLastErrorIsSystemErrno(en)) { \
virErrorPtr err = virGetLastError(); \
fprintf(stderr, "Did not get " #en " error code: %d:%d\n", \
err ? err->code : 0, err ? err->int1 : 0); \
goto cleanup; \
} } while (0)
/* Asking for impossible combination since CPU is co-mounted */
static int testCgroupNewForDriver(const void *args ATTRIBUTE_UNUSED)
{
virCgroupPtr cgroup = NULL;
@ -160,26 +172,28 @@ static int testCgroupNewForDriver(const void *args ATTRIBUTE_UNUSED)
[VIR_CGROUP_CONTROLLER_BLKIO] = "/libvirt/lxc",
};
if ((rv = virCgroupNewDriver("lxc", false, -1, &cgroup)) != -ENOENT) {
if ((rv = virCgroupNewDriver("lxc", false, -1, &cgroup)) != -1) {
fprintf(stderr, "Unexpected found LXC cgroup: %d\n", -rv);
goto cleanup;
}
ENSURE_ERRNO(ENOENT);
/* Asking for impossible combination since CPU is co-mounted */
if ((rv = virCgroupNewDriver("lxc", true,
(1 << VIR_CGROUP_CONTROLLER_CPU),
&cgroup)) != -EINVAL) {
&cgroup)) != -1) {
fprintf(stderr, "Should not have created LXC cgroup: %d\n", -rv);
goto cleanup;
}
ENSURE_ERRNO(EINVAL);
/* Asking for impossible combination since devices is not mounted */
if ((rv = virCgroupNewDriver("lxc", true,
(1 << VIR_CGROUP_CONTROLLER_DEVICES),
&cgroup)) != -ENXIO) {
&cgroup)) != -1) {
fprintf(stderr, "Should not have created LXC cgroup: %d\n", -rv);
goto cleanup;
}
ENSURE_ERRNO(ENXIO);
/* Asking for small combination since devices is not mounted */
if ((rv = virCgroupNewDriver("lxc", true,
@ -264,26 +278,29 @@ static int testCgroupNewForPartition(const void *args ATTRIBUTE_UNUSED)
[VIR_CGROUP_CONTROLLER_BLKIO] = "/virtualmachines.partition",
};
if ((rv = virCgroupNewPartition("/virtualmachines", false, -1, &cgroup)) != -ENOENT) {
if ((rv = virCgroupNewPartition("/virtualmachines", false, -1, &cgroup)) != -1) {
fprintf(stderr, "Unexpected found /virtualmachines cgroup: %d\n", -rv);
goto cleanup;
}
ENSURE_ERRNO(ENOENT);
/* Asking for impossible combination since CPU is co-mounted */
if ((rv = virCgroupNewPartition("/virtualmachines", true,
(1 << VIR_CGROUP_CONTROLLER_CPU),
&cgroup)) != -EINVAL) {
&cgroup)) != -1) {
fprintf(stderr, "Should not have created /virtualmachines cgroup: %d\n", -rv);
goto cleanup;
}
ENSURE_ERRNO(EINVAL);
/* Asking for impossible combination since devices is not mounted */
if ((rv = virCgroupNewPartition("/virtualmachines", true,
(1 << VIR_CGROUP_CONTROLLER_DEVICES),
&cgroup)) != -ENXIO) {
&cgroup)) != -1) {
fprintf(stderr, "Should not have created /virtualmachines cgroup: %d\n", -rv);
goto cleanup;
}
ENSURE_ERRNO(ENXIO);
/* Asking for small combination since devices is not mounted */
if ((rv = virCgroupNewPartition("/virtualmachines", true,
@ -324,16 +341,18 @@ static int testCgroupNewForPartitionNested(const void *args ATTRIBUTE_UNUSED)
[VIR_CGROUP_CONTROLLER_BLKIO] = "/deployment.partition/production.partition",
};
if ((rv = virCgroupNewPartition("/deployment/production", false, -1, &cgroup)) != -ENOENT) {
if ((rv = virCgroupNewPartition("/deployment/production", false, -1, &cgroup)) != -1) {
fprintf(stderr, "Unexpected found /deployment/production cgroup: %d\n", -rv);
goto cleanup;
}
ENSURE_ERRNO(ENOENT);
/* Should not work, since we require /deployment to be pre-created */
if ((rv = virCgroupNewPartition("/deployment/production", true, -1, &cgroup)) != -ENOENT) {
if ((rv = virCgroupNewPartition("/deployment/production", true, -1, &cgroup)) != -1) {
fprintf(stderr, "Unexpected created /deployment/production cgroup: %d\n", -rv);
goto cleanup;
}
ENSURE_ERRNO(ENOENT);
if ((rv = virCgroupNewPartition("/deployment", true, -1, &cgroup)) != 0) {
fprintf(stderr, "Failed to create /deployment cgroup: %d\n", -rv);
@ -370,16 +389,18 @@ static int testCgroupNewForPartitionNestedDeep(const void *args ATTRIBUTE_UNUSED
[VIR_CGROUP_CONTROLLER_BLKIO] = "/user/berrange.user/production.partition",
};
if ((rv = virCgroupNewPartition("/user/berrange.user/production", false, -1, &cgroup)) != -ENOENT) {
if ((rv = virCgroupNewPartition("/user/berrange.user/production", false, -1, &cgroup)) != -1) {
fprintf(stderr, "Unexpected found /user/berrange.user/production cgroup: %d\n", -rv);
goto cleanup;
}
ENSURE_ERRNO(ENOENT);
/* Should not work, since we require /user/berrange.user to be pre-created */
if ((rv = virCgroupNewPartition("/user/berrange.user/production", true, -1, &cgroup)) != -ENOENT) {
if ((rv = virCgroupNewPartition("/user/berrange.user/production", true, -1, &cgroup)) != -1) {
fprintf(stderr, "Unexpected created /user/berrange.user/production cgroup: %d\n", -rv);
goto cleanup;
}
ENSURE_ERRNO(ENOENT);
if ((rv = virCgroupNewPartition("/user", true, -1, &cgroup)) != 0) {
fprintf(stderr, "Failed to create /user/berrange.user cgroup: %d\n", -rv);