]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
conf: use disk source accessors in lxc/
authorEric Blake <eblake@redhat.com>
Mon, 17 Mar 2014 20:49:05 +0000 (14:49 -0600)
committerEric Blake <eblake@redhat.com>
Mon, 24 Mar 2014 17:59:50 +0000 (11:59 -0600)
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>
src/lxc/lxc_cgroup.c
src/lxc/lxc_controller.c
src/lxc/lxc_driver.c

index 5a1718d1606a68f0824615fffefcedad04cc6230..da5ccf59bfefab512b7e89655eb53a8e50018fde 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2012 Red Hat, Inc.
+ * Copyright (C) 2010-2014 Red Hat, Inc.
  * Copyright IBM Corp. 2008
  *
  * lxc_cgroup.c: LXC cgroup helpers
@@ -372,11 +372,11 @@ static int virLXCCgroupSetupDeviceACL(virDomainDefPtr def,
 
     VIR_DEBUG("Allowing any disk block devs");
     for (i = 0; i < def->ndisks; i++) {
-        if (def->disks[i]->type != VIR_DOMAIN_DISK_TYPE_BLOCK)
+        if (!virDomainDiskSourceIsBlockType(def->disks[i]))
             continue;
 
         if (virCgroupAllowDevicePath(cgroup,
-                                     def->disks[i]->src,
+                                     virDomainDiskGetSource(def->disks[i]),
                                      (def->disks[i]->readonly ?
                                       VIR_CGROUP_DEVICE_READ :
                                       VIR_CGROUP_DEVICE_RW) |
index 874e19606be68a5aa53ad55bc3e4b9a83d19c5db..6ed13fb71c8e1393a9abe61d7fa095a1b32c0f20 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2013 Red Hat, Inc.
+ * Copyright (C) 2010-2014 Red Hat, Inc.
  * Copyright IBM Corp. 2008
  *
  * lxc_controller.c: linux container process controller
@@ -382,21 +382,24 @@ static int virLXCControllerSetupLoopDeviceDisk(virDomainDiskDefPtr disk)
 {
     int lofd;
     char *loname = NULL;
+    const char *src = virDomainDiskGetSource(disk);
 
-    if ((lofd = virFileLoopDeviceAssociate(disk->src, &loname)) < 0)
+    if ((lofd = virFileLoopDeviceAssociate(src, &loname)) < 0)
         return -1;
 
     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
      * the rest of container setup 'just works'
      */
-    disk->type = VIR_DOMAIN_DISK_TYPE_BLOCK;
-    VIR_FREE(disk->src);
-    disk->src = loname;
-    loname = NULL;
+    virDomainDiskSetType(disk, VIR_DOMAIN_DISK_TYPE_BLOCK);
+    if (virDomainDiskSetSource(disk, loname) < 0) {
+        VIR_FREE(loname);
+        return -1;
+    }
+    VIR_FREE(loname);
 
     return lofd;
 }
@@ -435,28 +438,33 @@ static int virLXCControllerSetupNBDDeviceFS(virDomainFSDefPtr fs)
 static int virLXCControllerSetupNBDDeviceDisk(virDomainDiskDefPtr disk)
 {
     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",
                        _("An explicit disk format must be specified"));
         return -1;
     }
 
-    if (virFileNBDDeviceAssociate(disk->src,
-                                  disk->format,
+    if (virFileNBDDeviceAssociate(src,
+                                  format,
                                   disk->readonly,
                                   &dev) < 0)
         return -1;
 
     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
      * the rest of container setup 'just works'
      */
-    disk->type = VIR_DOMAIN_DISK_TYPE_BLOCK;
-    VIR_FREE(disk->src);
-    disk->src = dev;
+    virDomainDiskSetType(disk, VIR_DOMAIN_DISK_TYPE_BLOCK);
+    if (virDomainDiskSetSource(disk, dev) < 0) {
+        VIR_FREE(dev);
+        return -1;
+    }
+    VIR_FREE(dev);
 
     return 0;
 }
@@ -519,23 +527,25 @@ static int virLXCControllerSetupLoopDevices(virLXCControllerPtr ctrl)
     for (i = 0; i < ctrl->def->ndisks; i++) {
         virDomainDiskDefPtr disk = ctrl->def->disks[i];
         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;
 
         /* If no driverName is set, we prefer 'loop' for
          * dealing with raw or undefined formats, otherwise
          * we use 'nbd'.
          */
-        if (STREQ_NULLABLE(disk->driverName, "loop") ||
-            (!disk->driverName &&
-             (disk->format == VIR_STORAGE_FILE_RAW ||
-              disk->format == VIR_STORAGE_FILE_NONE))) {
-            if (disk->format != VIR_STORAGE_FILE_RAW &&
-                disk->format != VIR_STORAGE_FILE_NONE) {
+        if (STREQ_NULLABLE(driver, "loop") ||
+            (!driver &&
+             (format == VIR_STORAGE_FILE_RAW ||
+              format == VIR_STORAGE_FILE_NONE))) {
+            if (format != VIR_STORAGE_FILE_RAW &&
+                format != VIR_STORAGE_FILE_NONE) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                _("disk format %s is not supported"),
-                               virStorageFileFormatTypeToString(disk->format));
+                               virStorageFileFormatTypeToString(format));
                 goto cleanup;
             }
 
@@ -553,8 +563,7 @@ static int virLXCControllerSetupLoopDevices(virLXCControllerPtr ctrl)
                 goto cleanup;
             }
             ctrl->loopDevFds[ctrl->nloopDevs - 1] = fd;
-        } else if (STREQ_NULLABLE(disk->driverName, "nbd") ||
-                   !disk->driverName) {
+        } else if (!driver || STREQ(driver, "nbd")) {
             if (disk->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT &&
                 disk->cachemode != VIR_DOMAIN_DISK_CACHE_DISABLE) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -567,7 +576,7 @@ static int virLXCControllerSetupLoopDevices(virLXCControllerPtr ctrl)
         } else {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("disk driver %s is not supported"),
-                           disk->driverName);
+                           driver);
             goto cleanup;
         }
     }
@@ -1662,12 +1671,12 @@ static int virLXCControllerSetupDisk(virLXCControllerPtr ctrl,
     mode_t mode;
     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",
                        _("Can't setup disk for non-block device"));
         goto cleanup;
     }
-    if (def->src == NULL) {
+    if (!tmpsrc) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                        _("Can't setup disk without media"));
         goto cleanup;
@@ -1679,14 +1688,14 @@ static int virLXCControllerSetupDisk(virLXCControllerPtr ctrl,
 
     if (stat(def->src, &sb) < 0) {
         virReportSystemError(errno,
-                             _("Unable to access %s"), def->src);
+                             _("Unable to access %s"), tmpsrc);
         goto cleanup;
     }
 
     if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode)) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("Disk source %s must be a character/block device"),
-                       def->src);
+                       tmpsrc);
         goto cleanup;
     }
 
@@ -1704,7 +1713,7 @@ static int virLXCControllerSetupDisk(virLXCControllerPtr ctrl,
      * to that normally implied by the device name
      */
     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) {
         virReportSystemError(errno,
                              _("Unable to create device %s"),
index ce4064573c86b867f252678f1bacc3797f21911b..5ecc03f217ebb9acce945fe74177c02cfb3f10e1 100644 (file)
@@ -4023,6 +4023,7 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
     struct stat sb;
     char *file = NULL;
     int perms;
+    const char *src = NULL;
 
     if (!priv->initpid) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
@@ -4036,12 +4037,13 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
         goto cleanup;
     }
 
-    if (def->type != VIR_DOMAIN_DISK_TYPE_BLOCK) {
+    if (!virDomainDiskSourceIsBlockType(def)) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                        _("Can't setup disk for non-block device"));
         goto cleanup;
     }
-    if (def->src == NULL) {
+    src = virDomainDiskGetSource(def);
+    if (src == NULL) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                        _("Can't setup disk without media"));
         goto cleanup;
@@ -4053,16 +4055,16 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
         goto cleanup;
     }
 
-    if (stat(def->src, &sb) < 0) {
+    if (stat(src, &sb) < 0) {
         virReportSystemError(errno,
-                             _("Unable to access %s"), def->src);
+                             _("Unable to access %s"), src);
         goto cleanup;
     }
 
     if (!S_ISBLK(sb.st_mode)) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("Disk source %s must be a block device"),
-                       def->src);
+                       src);
         goto cleanup;
     }
 
@@ -4103,7 +4105,7 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
                                 minor(sb.st_rdev),
                                 perms) < 0)
             VIR_WARN("cannot deny device %s for domain %s",
-                     def->src, vm->def->name);
+                     src, vm->def->name);
         goto cleanup;
     }
 
@@ -4112,7 +4114,8 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
     ret = 0;
 
 cleanup:
-    virDomainAuditDisk(vm, NULL, def->src, "attach", ret == 0);
+    if (src)
+        virDomainAuditDisk(vm, NULL, src, "attach", ret == 0);
     VIR_FREE(file);
     return ret;
 }
@@ -4586,6 +4589,7 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
     virDomainDiskDefPtr def = NULL;
     int idx, ret = -1;
     char *dst = NULL;
+    const char *src;
 
     if (!priv->initpid) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
@@ -4602,6 +4606,7 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
     }
 
     def = vm->def->disks[idx];
+    src = virDomainDiskGetSource(def);
 
     if (virAsprintf(&dst, "/dev/%s", def->dst) < 0)
         goto cleanup;
@@ -4613,14 +4618,14 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
     }
 
     if (lxcDomainAttachDeviceUnlink(vm, dst) < 0) {
-        virDomainAuditDisk(vm, def->src, NULL, "detach", false);
+        virDomainAuditDisk(vm, src, NULL, "detach", false);
         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",
-                 def->src, vm->def->name);
+                 src, vm->def->name);
 
     virDomainDiskRemove(vm->def, idx);
     virDomainDiskDefFree(def);