conf: use disk source accessors in lxc/

Part of a series of cleanups to use new accessor methods.

* src/lxc/lxc_cgroup.c (virLXCCgroupSetupDeviceACL): Use
accessors.
* src/lxc/lxc_controller.c (virLXCControllerSetupLoopDeviceDisk)
(virLXCControllerSetupNBDDeviceDisk)
(virLXCControllerSetupLoopDevices, virLXCControllerSetupDisk):
Likewise.
* src/lxc/lxc_driver.c (lxcDomainAttachDeviceDiskLive)
(lxcDomainDetachDeviceDiskLive): Likewise.

Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Eric Blake 2014-03-17 14:49:05 -06:00
parent 6b44ccf7fe
commit a7785ccfd7
3 changed files with 58 additions and 44 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2010-2012 Red Hat, Inc. * Copyright (C) 2010-2014 Red Hat, Inc.
* Copyright IBM Corp. 2008 * Copyright IBM Corp. 2008
* *
* lxc_cgroup.c: LXC cgroup helpers * lxc_cgroup.c: LXC cgroup helpers
@ -372,11 +372,11 @@ static int virLXCCgroupSetupDeviceACL(virDomainDefPtr def,
VIR_DEBUG("Allowing any disk block devs"); VIR_DEBUG("Allowing any disk block devs");
for (i = 0; i < def->ndisks; i++) { for (i = 0; i < def->ndisks; i++) {
if (def->disks[i]->type != VIR_DOMAIN_DISK_TYPE_BLOCK) if (!virDomainDiskSourceIsBlockType(def->disks[i]))
continue; continue;
if (virCgroupAllowDevicePath(cgroup, if (virCgroupAllowDevicePath(cgroup,
def->disks[i]->src, virDomainDiskGetSource(def->disks[i]),
(def->disks[i]->readonly ? (def->disks[i]->readonly ?
VIR_CGROUP_DEVICE_READ : VIR_CGROUP_DEVICE_READ :
VIR_CGROUP_DEVICE_RW) | VIR_CGROUP_DEVICE_RW) |

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2010-2013 Red Hat, Inc. * Copyright (C) 2010-2014 Red Hat, Inc.
* Copyright IBM Corp. 2008 * Copyright IBM Corp. 2008
* *
* lxc_controller.c: linux container process controller * lxc_controller.c: linux container process controller
@ -382,21 +382,24 @@ static int virLXCControllerSetupLoopDeviceDisk(virDomainDiskDefPtr disk)
{ {
int lofd; int lofd;
char *loname = NULL; char *loname = NULL;
const char *src = virDomainDiskGetSource(disk);
if ((lofd = virFileLoopDeviceAssociate(disk->src, &loname)) < 0) if ((lofd = virFileLoopDeviceAssociate(src, &loname)) < 0)
return -1; return -1;
VIR_DEBUG("Changing disk %s to use type=block for dev %s", VIR_DEBUG("Changing disk %s to use type=block for dev %s",
disk->src, loname); src, loname);
/* /*
* We now change it into a block device type, so that * We now change it into a block device type, so that
* the rest of container setup 'just works' * the rest of container setup 'just works'
*/ */
disk->type = VIR_DOMAIN_DISK_TYPE_BLOCK; virDomainDiskSetType(disk, VIR_DOMAIN_DISK_TYPE_BLOCK);
VIR_FREE(disk->src); if (virDomainDiskSetSource(disk, loname) < 0) {
disk->src = loname; VIR_FREE(loname);
loname = NULL; return -1;
}
VIR_FREE(loname);
return lofd; return lofd;
} }
@ -435,28 +438,33 @@ static int virLXCControllerSetupNBDDeviceFS(virDomainFSDefPtr fs)
static int virLXCControllerSetupNBDDeviceDisk(virDomainDiskDefPtr disk) static int virLXCControllerSetupNBDDeviceDisk(virDomainDiskDefPtr disk)
{ {
char *dev; char *dev;
const char *src = virDomainDiskGetSource(disk);
int format = virDomainDiskGetFormat(disk);
if (disk->format <= VIR_STORAGE_FILE_NONE) { if (format <= VIR_STORAGE_FILE_NONE) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("An explicit disk format must be specified")); _("An explicit disk format must be specified"));
return -1; return -1;
} }
if (virFileNBDDeviceAssociate(disk->src, if (virFileNBDDeviceAssociate(src,
disk->format, format,
disk->readonly, disk->readonly,
&dev) < 0) &dev) < 0)
return -1; return -1;
VIR_DEBUG("Changing disk %s to use type=block for dev %s", VIR_DEBUG("Changing disk %s to use type=block for dev %s",
disk->src, dev); src, dev);
/* /*
* We now change it into a block device type, so that * We now change it into a block device type, so that
* the rest of container setup 'just works' * the rest of container setup 'just works'
*/ */
disk->type = VIR_DOMAIN_DISK_TYPE_BLOCK; virDomainDiskSetType(disk, VIR_DOMAIN_DISK_TYPE_BLOCK);
VIR_FREE(disk->src); if (virDomainDiskSetSource(disk, dev) < 0) {
disk->src = dev; VIR_FREE(dev);
return -1;
}
VIR_FREE(dev);
return 0; return 0;
} }
@ -519,23 +527,25 @@ static int virLXCControllerSetupLoopDevices(virLXCControllerPtr ctrl)
for (i = 0; i < ctrl->def->ndisks; i++) { for (i = 0; i < ctrl->def->ndisks; i++) {
virDomainDiskDefPtr disk = ctrl->def->disks[i]; virDomainDiskDefPtr disk = ctrl->def->disks[i];
int fd; int fd;
const char *driver = virDomainDiskGetDriver(disk);
int format = virDomainDiskGetFormat(disk);
if (disk->type != VIR_DOMAIN_DISK_TYPE_FILE) if (virDomainDiskGetType(disk) != VIR_DOMAIN_DISK_TYPE_FILE)
continue; continue;
/* If no driverName is set, we prefer 'loop' for /* If no driverName is set, we prefer 'loop' for
* dealing with raw or undefined formats, otherwise * dealing with raw or undefined formats, otherwise
* we use 'nbd'. * we use 'nbd'.
*/ */
if (STREQ_NULLABLE(disk->driverName, "loop") || if (STREQ_NULLABLE(driver, "loop") ||
(!disk->driverName && (!driver &&
(disk->format == VIR_STORAGE_FILE_RAW || (format == VIR_STORAGE_FILE_RAW ||
disk->format == VIR_STORAGE_FILE_NONE))) { format == VIR_STORAGE_FILE_NONE))) {
if (disk->format != VIR_STORAGE_FILE_RAW && if (format != VIR_STORAGE_FILE_RAW &&
disk->format != VIR_STORAGE_FILE_NONE) { format != VIR_STORAGE_FILE_NONE) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("disk format %s is not supported"), _("disk format %s is not supported"),
virStorageFileFormatTypeToString(disk->format)); virStorageFileFormatTypeToString(format));
goto cleanup; goto cleanup;
} }
@ -553,8 +563,7 @@ static int virLXCControllerSetupLoopDevices(virLXCControllerPtr ctrl)
goto cleanup; goto cleanup;
} }
ctrl->loopDevFds[ctrl->nloopDevs - 1] = fd; ctrl->loopDevFds[ctrl->nloopDevs - 1] = fd;
} else if (STREQ_NULLABLE(disk->driverName, "nbd") || } else if (!driver || STREQ(driver, "nbd")) {
!disk->driverName) {
if (disk->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT && if (disk->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT &&
disk->cachemode != VIR_DOMAIN_DISK_CACHE_DISABLE) { disk->cachemode != VIR_DOMAIN_DISK_CACHE_DISABLE) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@ -567,7 +576,7 @@ static int virLXCControllerSetupLoopDevices(virLXCControllerPtr ctrl)
} else { } else {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("disk driver %s is not supported"), _("disk driver %s is not supported"),
disk->driverName); driver);
goto cleanup; goto cleanup;
} }
} }
@ -1662,12 +1671,12 @@ static int virLXCControllerSetupDisk(virLXCControllerPtr ctrl,
mode_t mode; mode_t mode;
char *tmpsrc = def->src; char *tmpsrc = def->src;
if (def->type != VIR_DOMAIN_DISK_TYPE_BLOCK) { if (virDomainDiskGetType(def) != VIR_DOMAIN_DISK_TYPE_BLOCK) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Can't setup disk for non-block device")); _("Can't setup disk for non-block device"));
goto cleanup; goto cleanup;
} }
if (def->src == NULL) { if (!tmpsrc) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Can't setup disk without media")); _("Can't setup disk without media"));
goto cleanup; goto cleanup;
@ -1679,14 +1688,14 @@ static int virLXCControllerSetupDisk(virLXCControllerPtr ctrl,
if (stat(def->src, &sb) < 0) { if (stat(def->src, &sb) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Unable to access %s"), def->src); _("Unable to access %s"), tmpsrc);
goto cleanup; goto cleanup;
} }
if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode)) { if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Disk source %s must be a character/block device"), _("Disk source %s must be a character/block device"),
def->src); tmpsrc);
goto cleanup; goto cleanup;
} }
@ -1704,7 +1713,7 @@ static int virLXCControllerSetupDisk(virLXCControllerPtr ctrl,
* to that normally implied by the device name * to that normally implied by the device name
*/ */
VIR_DEBUG("Creating dev %s (%d,%d) from %s", VIR_DEBUG("Creating dev %s (%d,%d) from %s",
dst, major(sb.st_rdev), minor(sb.st_rdev), def->src); dst, major(sb.st_rdev), minor(sb.st_rdev), tmpsrc);
if (mknod(dst, mode, sb.st_rdev) < 0) { if (mknod(dst, mode, sb.st_rdev) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Unable to create device %s"), _("Unable to create device %s"),

View File

@ -4023,6 +4023,7 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
struct stat sb; struct stat sb;
char *file = NULL; char *file = NULL;
int perms; int perms;
const char *src = NULL;
if (!priv->initpid) { if (!priv->initpid) {
virReportError(VIR_ERR_OPERATION_INVALID, "%s", virReportError(VIR_ERR_OPERATION_INVALID, "%s",
@ -4036,12 +4037,13 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
goto cleanup; goto cleanup;
} }
if (def->type != VIR_DOMAIN_DISK_TYPE_BLOCK) { if (!virDomainDiskSourceIsBlockType(def)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Can't setup disk for non-block device")); _("Can't setup disk for non-block device"));
goto cleanup; goto cleanup;
} }
if (def->src == NULL) { src = virDomainDiskGetSource(def);
if (src == NULL) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Can't setup disk without media")); _("Can't setup disk without media"));
goto cleanup; goto cleanup;
@ -4053,16 +4055,16 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
goto cleanup; goto cleanup;
} }
if (stat(def->src, &sb) < 0) { if (stat(src, &sb) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Unable to access %s"), def->src); _("Unable to access %s"), src);
goto cleanup; goto cleanup;
} }
if (!S_ISBLK(sb.st_mode)) { if (!S_ISBLK(sb.st_mode)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Disk source %s must be a block device"), _("Disk source %s must be a block device"),
def->src); src);
goto cleanup; goto cleanup;
} }
@ -4103,7 +4105,7 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
minor(sb.st_rdev), minor(sb.st_rdev),
perms) < 0) perms) < 0)
VIR_WARN("cannot deny device %s for domain %s", VIR_WARN("cannot deny device %s for domain %s",
def->src, vm->def->name); src, vm->def->name);
goto cleanup; goto cleanup;
} }
@ -4112,7 +4114,8 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
ret = 0; ret = 0;
cleanup: cleanup:
virDomainAuditDisk(vm, NULL, def->src, "attach", ret == 0); if (src)
virDomainAuditDisk(vm, NULL, src, "attach", ret == 0);
VIR_FREE(file); VIR_FREE(file);
return ret; return ret;
} }
@ -4586,6 +4589,7 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
virDomainDiskDefPtr def = NULL; virDomainDiskDefPtr def = NULL;
int idx, ret = -1; int idx, ret = -1;
char *dst = NULL; char *dst = NULL;
const char *src;
if (!priv->initpid) { if (!priv->initpid) {
virReportError(VIR_ERR_OPERATION_INVALID, "%s", virReportError(VIR_ERR_OPERATION_INVALID, "%s",
@ -4602,6 +4606,7 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
} }
def = vm->def->disks[idx]; def = vm->def->disks[idx];
src = virDomainDiskGetSource(def);
if (virAsprintf(&dst, "/dev/%s", def->dst) < 0) if (virAsprintf(&dst, "/dev/%s", def->dst) < 0)
goto cleanup; goto cleanup;
@ -4613,14 +4618,14 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
} }
if (lxcDomainAttachDeviceUnlink(vm, dst) < 0) { if (lxcDomainAttachDeviceUnlink(vm, dst) < 0) {
virDomainAuditDisk(vm, def->src, NULL, "detach", false); virDomainAuditDisk(vm, src, NULL, "detach", false);
goto cleanup; goto cleanup;
} }
virDomainAuditDisk(vm, def->src, NULL, "detach", true); virDomainAuditDisk(vm, src, NULL, "detach", true);
if (virCgroupDenyDevicePath(priv->cgroup, def->src, VIR_CGROUP_DEVICE_RWM) != 0) if (virCgroupDenyDevicePath(priv->cgroup, src, VIR_CGROUP_DEVICE_RWM) != 0)
VIR_WARN("cannot deny device %s for domain %s", VIR_WARN("cannot deny device %s for domain %s",
def->src, vm->def->name); src, vm->def->name);
virDomainDiskRemove(vm->def, idx); virDomainDiskRemove(vm->def, idx);
virDomainDiskDefFree(def); virDomainDiskDefFree(def);