Blob Blame History Raw
From d673797628091c365d603b306db81c63bda22e96 Mon Sep 17 00:00:00 2001
Message-Id: <d673797628091c365d603b306db81c63bda22e96@dist-git>
From: Osier Yang <jyang@redhat.com>
Date: Sun, 2 Feb 2014 23:09:40 +0800
Subject: [PATCH] util: Add one argument for several scsi utils

https://bugzilla.redhat.com/show_bug.cgi?id=957292

To support passing the path of the test data to the utils, one
more argument is added to virSCSIDeviceGetSgName,
virSCSIDeviceGetDevName, and virSCSIDeviceNew, and the related
code is changed accordingly.

Later tests for the scsi utils will be based on this patch.

Signed-off-by: Osier Yang <jyang@redhat.com>
(cherry picked from commit 10c9ceff6d07f65773ef638bd36fe1d6a5f3a5ac)
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/qemu/qemu_cgroup.c           |  3 ++-
 src/qemu/qemu_command.c          |  3 ++-
 src/qemu/qemu_command.h          |  3 ++-
 src/qemu/qemu_conf.c             | 12 ++++++++----
 src/qemu/qemu_hostdev.c          |  9 ++++++---
 src/security/security_apparmor.c |  3 ++-
 src/security/security_dac.c      |  6 ++++--
 src/security/security_selinux.c  |  6 ++++--
 src/util/virscsi.c               | 26 ++++++++++++++++----------
 src/util/virscsi.h               |  9 ++++++---
 tests/testutilsqemu.c            |  3 ++-
 11 files changed, 54 insertions(+), 29 deletions(-)

diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index 4d7ade1..e0e1041 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -290,7 +290,8 @@ qemuSetupHostdevCGroup(virDomainObjPtr vm,
             break;
 
         case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI:
-            if ((scsi = virSCSIDeviceNew(dev->source.subsys.u.scsi.adapter,
+            if ((scsi = virSCSIDeviceNew(NULL,
+                                         dev->source.subsys.u.scsi.adapter,
                                          dev->source.subsys.u.scsi.bus,
                                          dev->source.subsys.u.scsi.target,
                                          dev->source.subsys.u.scsi.unit,
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index debafa2..b5729a4 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -5608,7 +5608,8 @@ qemuBuildSCSIHostdevDrvStr(virDomainHostdevDefPtr dev,
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     char *sg = NULL;
 
-    sg = (callbacks->qemuGetSCSIDeviceSgName)(dev->source.subsys.u.scsi.adapter,
+    sg = (callbacks->qemuGetSCSIDeviceSgName)(NULL,
+                                              dev->source.subsys.u.scsi.adapter,
                                               dev->source.subsys.u.scsi.bus,
                                               dev->source.subsys.u.scsi.target,
                                               dev->source.subsys.u.scsi.unit);
diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h
index decc940..39da555 100644
--- a/src/qemu/qemu_command.h
+++ b/src/qemu/qemu_command.h
@@ -57,7 +57,8 @@
 typedef struct _qemuBuildCommandLineCallbacks qemuBuildCommandLineCallbacks;
 typedef qemuBuildCommandLineCallbacks *qemuBuildCommandLineCallbacksPtr;
 struct _qemuBuildCommandLineCallbacks {
-    char * (*qemuGetSCSIDeviceSgName) (const char *adapter,
+    char * (*qemuGetSCSIDeviceSgName) (const char *sysfs_prefix,
+                                       const char *adapter,
                                        unsigned int bus,
                                        unsigned int target,
                                        unsigned int unit);
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index e45180a..ba32959 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -726,7 +726,8 @@ qemuCheckSharedDevice(virHashTablePtr sharedDevices,
     } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) {
         hostdev = dev->data.hostdev;
 
-        if (!(hostdev_name = virSCSIDeviceGetDevName(hostdev->source.subsys.u.scsi.adapter,
+        if (!(hostdev_name = virSCSIDeviceGetDevName(NULL,
+                                                     hostdev->source.subsys.u.scsi.adapter,
                                                      hostdev->source.subsys.u.scsi.bus,
                                                      hostdev->source.subsys.u.scsi.target,
                                                      hostdev->source.subsys.u.scsi.unit)))
@@ -914,7 +915,8 @@ qemuAddSharedDevice(virQEMUDriverPtr driver,
         if (!(key = qemuGetSharedDeviceKey(disk->src)))
             goto cleanup;
     } else {
-        if (!(dev_name = virSCSIDeviceGetDevName(hostdev->source.subsys.u.scsi.adapter,
+        if (!(dev_name = virSCSIDeviceGetDevName(NULL,
+                                                 hostdev->source.subsys.u.scsi.adapter,
                                                  hostdev->source.subsys.u.scsi.bus,
                                                  hostdev->source.subsys.u.scsi.target,
                                                  hostdev->source.subsys.u.scsi.unit)))
@@ -1018,7 +1020,8 @@ qemuRemoveSharedDevice(virQEMUDriverPtr driver,
         if (!(key = qemuGetSharedDeviceKey(disk->src)))
             goto cleanup;
     } else {
-        if (!(dev_name = virSCSIDeviceGetDevName(hostdev->source.subsys.u.scsi.adapter,
+        if (!(dev_name = virSCSIDeviceGetDevName(NULL,
+                                                 hostdev->source.subsys.u.scsi.adapter,
                                                  hostdev->source.subsys.u.scsi.bus,
                                                  hostdev->source.subsys.u.scsi.target,
                                                  hostdev->source.subsys.u.scsi.unit)))
@@ -1102,7 +1105,8 @@ qemuSetUnprivSGIO(virDomainDeviceDefPtr dev)
               hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI))
             return 0;
 
-        if (!(hostdev_name = virSCSIDeviceGetDevName(hostdev->source.subsys.u.scsi.adapter,
+        if (!(hostdev_name = virSCSIDeviceGetDevName(NULL,
+                                                     hostdev->source.subsys.u.scsi.adapter,
                                                      hostdev->source.subsys.u.scsi.bus,
                                                      hostdev->source.subsys.u.scsi.target,
                                                      hostdev->source.subsys.u.scsi.unit)))
diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c
index 7282b15..49e17e1 100644
--- a/src/qemu/qemu_hostdev.c
+++ b/src/qemu/qemu_hostdev.c
@@ -264,7 +264,8 @@ qemuUpdateActiveScsiHostdevs(virQEMUDriverPtr driver,
             hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI)
             continue;
 
-        if (!(scsi = virSCSIDeviceNew(hostdev->source.subsys.u.scsi.adapter,
+        if (!(scsi = virSCSIDeviceNew(NULL,
+                                      hostdev->source.subsys.u.scsi.adapter,
                                       hostdev->source.subsys.u.scsi.bus,
                                       hostdev->source.subsys.u.scsi.target,
                                       hostdev->source.subsys.u.scsi.unit,
@@ -1101,7 +1102,8 @@ qemuPrepareHostdevSCSIDevices(virQEMUDriverPtr driver,
             goto cleanup;
         }
 
-        if (!(scsi = virSCSIDeviceNew(hostdev->source.subsys.u.scsi.adapter,
+        if (!(scsi = virSCSIDeviceNew(NULL,
+                                      hostdev->source.subsys.u.scsi.adapter,
                                       hostdev->source.subsys.u.scsi.bus,
                                       hostdev->source.subsys.u.scsi.target,
                                       hostdev->source.subsys.u.scsi.unit,
@@ -1405,7 +1407,8 @@ qemuDomainReAttachHostScsiDevices(virQEMUDriverPtr driver,
             hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI)
             continue;
 
-        if (!(scsi = virSCSIDeviceNew(hostdev->source.subsys.u.scsi.adapter,
+        if (!(scsi = virSCSIDeviceNew(NULL,
+                                      hostdev->source.subsys.u.scsi.adapter,
                                       hostdev->source.subsys.u.scsi.bus,
                                       hostdev->source.subsys.u.scsi.target,
                                       hostdev->source.subsys.u.scsi.unit,
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c
index 9c97e3b..fdf0acc 100644
--- a/src/security/security_apparmor.c
+++ b/src/security/security_apparmor.c
@@ -829,7 +829,8 @@ AppArmorSetSecurityHostdevLabel(virSecurityManagerPtr mgr,
 
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
         virSCSIDevicePtr scsi =
-            virSCSIDeviceNew(dev->source.subsys.u.scsi.adapter,
+            virSCSIDeviceNew(NULL,
+                             dev->source.subsys.u.scsi.adapter,
                              dev->source.subsys.u.scsi.bus,
                              dev->source.subsys.u.scsi.target,
                              dev->source.subsys.u.scsi.unit,
diff --git a/src/security/security_dac.c b/src/security/security_dac.c
index 26fa124..2a44ceb 100644
--- a/src/security/security_dac.c
+++ b/src/security/security_dac.c
@@ -528,7 +528,8 @@ virSecurityDACSetSecurityHostdevLabel(virSecurityManagerPtr mgr,
 
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
         virSCSIDevicePtr scsi =
-            virSCSIDeviceNew(dev->source.subsys.u.scsi.adapter,
+            virSCSIDeviceNew(NULL,
+                             dev->source.subsys.u.scsi.adapter,
                              dev->source.subsys.u.scsi.bus,
                              dev->source.subsys.u.scsi.target,
                              dev->source.subsys.u.scsi.unit,
@@ -646,7 +647,8 @@ virSecurityDACRestoreSecurityHostdevLabel(virSecurityManagerPtr mgr,
 
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
         virSCSIDevicePtr scsi =
-            virSCSIDeviceNew(dev->source.subsys.u.scsi.adapter,
+            virSCSIDeviceNew(NULL,
+                             dev->source.subsys.u.scsi.adapter,
                              dev->source.subsys.u.scsi.bus,
                              dev->source.subsys.u.scsi.target,
                              dev->source.subsys.u.scsi.unit,
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index dd25ba8..91c5ffb 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -1359,7 +1359,8 @@ virSecuritySELinuxSetSecurityHostdevSubsysLabel(virDomainDefPtr def,
 
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
         virSCSIDevicePtr scsi =
-            virSCSIDeviceNew(dev->source.subsys.u.scsi.adapter,
+            virSCSIDeviceNew(NULL,
+                             dev->source.subsys.u.scsi.adapter,
                              dev->source.subsys.u.scsi.bus,
                              dev->source.subsys.u.scsi.target,
                              dev->source.subsys.u.scsi.unit,
@@ -1551,7 +1552,8 @@ virSecuritySELinuxRestoreSecurityHostdevSubsysLabel(virSecurityManagerPtr mgr,
 
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
         virSCSIDevicePtr scsi =
-            virSCSIDeviceNew(dev->source.subsys.u.scsi.adapter,
+            virSCSIDeviceNew(NULL,
+                             dev->source.subsys.u.scsi.adapter,
                              dev->source.subsys.u.scsi.bus,
                              dev->source.subsys.u.scsi.target,
                              dev->source.subsys.u.scsi.unit,
diff --git a/src/util/virscsi.c b/src/util/virscsi.c
index 0d496ae..f4a28e0 100644
--- a/src/util/virscsi.c
+++ b/src/util/virscsi.c
@@ -102,7 +102,8 @@ virSCSIDeviceGetAdapterId(const char *adapter,
 }
 
 char *
-virSCSIDeviceGetSgName(const char *adapter,
+virSCSIDeviceGetSgName(const char *sysfs_prefix,
+                       const char *adapter,
                        unsigned int bus,
                        unsigned int target,
                        unsigned int unit)
@@ -112,13 +113,14 @@ virSCSIDeviceGetSgName(const char *adapter,
     char *path = NULL;
     char *sg = NULL;
     unsigned int adapter_id;
+    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
 
     if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0)
         return NULL;
 
     if (virAsprintf(&path,
-                    SYSFS_SCSI_DEVICES "/%d:%d:%d:%d/scsi_generic",
-                    adapter_id, bus, target, unit) < 0)
+                    "%s/%d:%d:%d:%d/scsi_generic",
+                    prefix, adapter_id, bus, target, unit) < 0)
         return NULL;
 
     if (!(dir = opendir(path))) {
@@ -145,7 +147,8 @@ cleanup:
  * on failure.
  */
 char *
-virSCSIDeviceGetDevName(const char *adapter,
+virSCSIDeviceGetDevName(const char *sysfs_prefix,
+                        const char *adapter,
                         unsigned int bus,
                         unsigned int target,
                         unsigned int unit)
@@ -155,13 +158,14 @@ virSCSIDeviceGetDevName(const char *adapter,
     char *path = NULL;
     char *name = NULL;
     unsigned int adapter_id;
+    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
 
     if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0)
         return NULL;
 
     if (virAsprintf(&path,
-                    SYSFS_SCSI_DEVICES "/%d:%d:%d:%d/block",
-                    adapter_id, bus, target, unit) < 0)
+                    "%s/%d:%d:%d:%d/block",
+                    prefix, adapter_id, bus, target, unit) < 0)
         return NULL;
 
     if (!(dir = opendir(path))) {
@@ -185,7 +189,8 @@ cleanup:
 }
 
 virSCSIDevicePtr
-virSCSIDeviceNew(const char *adapter,
+virSCSIDeviceNew(const char *sysfs_prefix,
+                 const char *adapter,
                  unsigned int bus,
                  unsigned int target,
                  unsigned int unit,
@@ -198,6 +203,7 @@ virSCSIDeviceNew(const char *adapter,
     char *model_path = NULL;
     char *vendor = NULL;
     char *model = NULL;
+    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
 
     if (VIR_ALLOC(dev) < 0)
         return NULL;
@@ -208,7 +214,7 @@ virSCSIDeviceNew(const char *adapter,
     dev->readonly = readonly;
     dev->shareable = shareable;
 
-    if (!(sg = virSCSIDeviceGetSgName(adapter, bus, target, unit)))
+    if (!(sg = virSCSIDeviceGetSgName(prefix, adapter, bus, target, unit)))
         goto cleanup;
 
     if (virSCSIDeviceGetAdapterId(adapter, &dev->adapter) < 0)
@@ -227,9 +233,9 @@ virSCSIDeviceNew(const char *adapter,
     }
 
     if (virAsprintf(&vendor_path,
-                    SYSFS_SCSI_DEVICES "/%s/vendor", dev->name) < 0 ||
+                    "%s/%s/vendor", prefix, dev->name) < 0 ||
         virAsprintf(&model_path,
-                    SYSFS_SCSI_DEVICES "/%s/model", dev->name) < 0)
+                    "%s/%s/model", prefix, dev->name) < 0)
         goto cleanup;
 
     if (virFileReadAll(vendor_path, 1024, &vendor) < 0)
diff --git a/src/util/virscsi.h b/src/util/virscsi.h
index 5bb91ed..ce9d3b4 100644
--- a/src/util/virscsi.h
+++ b/src/util/virscsi.h
@@ -33,16 +33,19 @@ typedef virSCSIDevice *virSCSIDevicePtr;
 typedef struct _virSCSIDeviceList virSCSIDeviceList;
 typedef virSCSIDeviceList *virSCSIDeviceListPtr;
 
-char *virSCSIDeviceGetSgName(const char *adapter,
+char *virSCSIDeviceGetSgName(const char *sysfs_prefix,
+                             const char *adapter,
                              unsigned int bus,
                              unsigned int target,
                              unsigned int unit);
-char *virSCSIDeviceGetDevName(const char *adapter,
+char *virSCSIDeviceGetDevName(const char *sysfs_prefix,
+                              const char *adapter,
                               unsigned int bus,
                               unsigned int target,
                               unsigned int unit);
 
-virSCSIDevicePtr virSCSIDeviceNew(const char *adapter,
+virSCSIDevicePtr virSCSIDeviceNew(const char *sysfs_prefix,
+                                  const char *adapter,
                                   unsigned int bus,
                                   unsigned int target,
                                   unsigned int unit,
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index fac83b2..86e7ee7 100644
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -292,7 +292,8 @@ cleanup:
 
 
 static char *
-testSCSIDeviceGetSgName(const char *adapter ATTRIBUTE_UNUSED,
+testSCSIDeviceGetSgName(const char *sysfs_prefix ATTRIBUTE_UNUSED,
+                        const char *adapter ATTRIBUTE_UNUSED,
                         unsigned int bus ATTRIBUTE_UNUSED,
                         unsigned int target ATTRIBUTE_UNUSED,
                         unsigned int unit ATTRIBUTE_UNUSED)
-- 
1.8.5.4