[PATCH 2/3] tests: Feed the fake sysfs root to qemuBuildCommandLine

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This creates a new directory "sysfsroot" under "tests", any sysfs
test input files should be put into it in future. Currently only
the scsi-generic tests need to use it, so only one file path is
created (bus/scsi/devices/0:0:0:0/scsi_generic/sg0/dev).
---
 src/qemu/qemu_cgroup.c                             |  3 +-
 src/qemu/qemu_command.c                            | 11 +++--
 src/qemu/qemu_command.h                            |  3 +-
 src/qemu/qemu_hostdev.c                            |  9 ++--
 src/qemu/qemu_hotplug.c                            |  4 +-
 src/security/security_dac.c                        |  6 ++-
 src/security/security_selinux.c                    |  6 ++-
 src/util/virscsi.c                                 | 51 ++++++++++++++++++----
 src/util/virscsi.h                                 |  6 ++-
 tests/qemuxml2argvtest.c                           |  4 +-
 .../bus/scsi/devices/0:0:0:0/scsi_generic/sg0/dev  |  1 +
 11 files changed, 77 insertions(+), 27 deletions(-)
 create mode 100644 tests/sysfsroot/bus/scsi/devices/0:0:0:0/scsi_generic/sg0/dev

diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index aaf94cf..55593e2 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -321,7 +321,8 @@ qemuSetupHostdevCGroup(virDomainObjPtr vm,
                                          dev->source.subsys.u.scsi.bus,
                                          dev->source.subsys.u.scsi.target,
                                          dev->source.subsys.u.scsi.unit,
-                                         dev->readonly)) == NULL)
+                                         dev->readonly,
+                                         NULL)) == NULL)
                 goto cleanup;
 
             if (virSCSIDeviceFileIterate(scsi,
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index e054c1f..9f49a7d 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -4746,7 +4746,8 @@ qemuBuildUSBHostdevUsbDevStr(virDomainHostdevDefPtr dev)
 
 char *
 qemuBuildSCSIHostdevDrvStr(virDomainHostdevDefPtr dev,
-                           virQEMUCapsPtr qemuCaps ATTRIBUTE_UNUSED)
+                           virQEMUCapsPtr qemuCaps ATTRIBUTE_UNUSED,
+                           const char *sysfs_root)
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     char *sg = NULL;
@@ -4754,7 +4755,8 @@ qemuBuildSCSIHostdevDrvStr(virDomainHostdevDefPtr dev,
     if (!(sg = virSCSIDeviceGetSgName(dev->source.subsys.u.scsi.adapter,
                                       dev->source.subsys.u.scsi.bus,
                                       dev->source.subsys.u.scsi.target,
-                                      dev->source.subsys.u.scsi.unit))) {
+                                      dev->source.subsys.u.scsi.unit,
+                                      sysfs_root))) {
         goto error;
     }
 
@@ -6401,7 +6403,7 @@ qemuBuildCommandLine(virConnectPtr conn,
                      int migrateFd,
                      virDomainSnapshotObjPtr snapshot,
                      enum virNetDevVPortProfileOp vmop,
-                     const char *sysfs_root ATTRIBUTE_UNUSED)
+                     const char *sysfs_root)
 {
     virErrorPtr originalError = NULL;
     int i, j;
@@ -8213,7 +8215,8 @@ qemuBuildCommandLine(virConnectPtr conn,
                 char *drvstr;
 
                 virCommandAddArg(cmd, "-drive");
-                if (!(drvstr = qemuBuildSCSIHostdevDrvStr(hostdev, qemuCaps)))
+                if (!(drvstr = qemuBuildSCSIHostdevDrvStr(hostdev, qemuCaps,
+                                                          sysfs_root)))
                     goto error;
                 virCommandAddArg(cmd, drvstr);
                 VIR_FREE(drvstr);
diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h
index 360d8cf..23a0fa5 100644
--- a/src/qemu/qemu_command.h
+++ b/src/qemu/qemu_command.h
@@ -140,7 +140,8 @@ char * qemuBuildUSBHostdevDevStr(virDomainHostdevDefPtr dev,
                                  virQEMUCapsPtr qemuCaps);
 
 char * qemuBuildSCSIHostdevDrvStr(virDomainHostdevDefPtr dev,
-                                  virQEMUCapsPtr qemuCaps);
+                                  virQEMUCapsPtr qemuCaps,
+                                  const char *sysfs_root);
 char * qemuBuildSCSIHostdevDevStr(virDomainDefPtr def,
                                   virDomainHostdevDefPtr dev,
                                   virQEMUCapsPtr qemuCaps);
diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c
index d5f94d5..44657d3 100644
--- a/src/qemu/qemu_hostdev.c
+++ b/src/qemu/qemu_hostdev.c
@@ -251,7 +251,8 @@ qemuUpdateActiveScsiHostdevs(virQEMUDriverPtr driver,
                                       hostdev->source.subsys.u.scsi.bus,
                                       hostdev->source.subsys.u.scsi.target,
                                       hostdev->source.subsys.u.scsi.unit,
-                                      hostdev->readonly)))
+                                      hostdev->readonly,
+                                      NULL)))
             goto cleanup;
 
         virSCSIDeviceSetUsedBy(scsi, def->name);
@@ -906,7 +907,8 @@ qemuPrepareHostdevSCSIDevices(virQEMUDriverPtr driver,
                                       hostdev->source.subsys.u.scsi.bus,
                                       hostdev->source.subsys.u.scsi.target,
                                       hostdev->source.subsys.u.scsi.unit,
-                                      hostdev->readonly)))
+                                      hostdev->readonly,
+                                      NULL)))
             goto cleanup;
 
         if (scsi && virSCSIDeviceListAdd(list, scsi) < 0) {
@@ -1194,7 +1196,8 @@ qemuDomainReAttachHostScsiDevices(virQEMUDriverPtr driver,
                                       hostdev->source.subsys.u.scsi.bus,
                                       hostdev->source.subsys.u.scsi.target,
                                       hostdev->source.subsys.u.scsi.unit,
-                                      hostdev->readonly))) {
+                                      hostdev->readonly,
+                                      NULL))) {
             VIR_WARN("Unable to reattach SCSI device %s:%d:%d:%d on domain %s",
                      hostdev->source.subsys.u.scsi.adapter,
                      hostdev->source.subsys.u.scsi.bus,
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index d037c9d..85d6ccb 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -1226,7 +1226,7 @@ qemuDomainAttachHostScsiDevice(virQEMUDriverPtr driver,
     if (qemuAssignDeviceHostdevAlias(vm->def, hostdev, 0) < 0)
         goto cleanup;
 
-    if (!(drvstr = qemuBuildSCSIHostdevDrvStr(hostdev, priv->qemuCaps)))
+    if (!(drvstr = qemuBuildSCSIHostdevDrvStr(hostdev, priv->qemuCaps, NULL)))
         goto cleanup;
 
     if (!(devstr = qemuBuildSCSIHostdevDevStr(vm->def, hostdev, priv->qemuCaps)))
@@ -2543,7 +2543,7 @@ qemuDomainDetachHostScsiDevice(virQEMUDriverPtr driver,
         return -1;
     }
 
-    if (!(drvstr = qemuBuildSCSIHostdevDrvStr(detach, priv->qemuCaps)))
+    if (!(drvstr = qemuBuildSCSIHostdevDrvStr(detach, priv->qemuCaps, NULL)))
         goto cleanup;
     if (!(devstr = qemuBuildSCSIHostdevDevStr(vm->def, detach, priv->qemuCaps)))
         goto cleanup;
diff --git a/src/security/security_dac.c b/src/security/security_dac.c
index 6e6fcad..e896a1c 100644
--- a/src/security/security_dac.c
+++ b/src/security/security_dac.c
@@ -550,7 +550,8 @@ virSecurityDACSetSecurityHostdevLabel(virSecurityManagerPtr mgr,
                              dev->source.subsys.u.scsi.bus,
                              dev->source.subsys.u.scsi.target,
                              dev->source.subsys.u.scsi.unit,
-                             dev->readonly);
+                             dev->readonly,
+                             NULL);
 
         if (!scsi)
             goto done;
@@ -667,7 +668,8 @@ virSecurityDACRestoreSecurityHostdevLabel(virSecurityManagerPtr mgr,
                              dev->source.subsys.u.scsi.bus,
                              dev->source.subsys.u.scsi.target,
                              dev->source.subsys.u.scsi.unit,
-                             dev->readonly);
+                             dev->readonly,
+                             NULL);
 
         if (!scsi)
             goto done;
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index 5d108b9..298a66f 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -1370,7 +1370,8 @@ virSecuritySELinuxSetSecurityHostdevSubsysLabel(virDomainDefPtr def,
                              dev->source.subsys.u.scsi.bus,
                              dev->source.subsys.u.scsi.target,
                              dev->source.subsys.u.scsi.unit,
-                             dev->readonly);
+                             dev->readonly,
+                             NULL);
 
         if (!scsi)
             goto done;
@@ -1569,7 +1570,8 @@ virSecuritySELinuxRestoreSecurityHostdevSubsysLabel(virSecurityManagerPtr mgr,
                              dev->source.subsys.u.scsi.bus,
                              dev->source.subsys.u.scsi.target,
                              dev->source.subsys.u.scsi.unit,
-                             dev->readonly);
+                             dev->readonly,
+                             NULL);
 
             if (!scsi)
                 goto done;
diff --git a/src/util/virscsi.c b/src/util/virscsi.c
index d6685fa..dca9a8d 100644
--- a/src/util/virscsi.c
+++ b/src/util/virscsi.c
@@ -100,26 +100,50 @@ virSCSIDeviceGetAdapterId(const char *adapter,
     return 0;
 }
 
+static char *
+virSCSIDeviceSysfsRoot(const char *sysfs_root)
+{
+    char *ret = NULL;
+
+    if (sysfs_root) {
+        if (virAsprintf(&ret, "%s/bus/scsi/devices", sysfs_root) < 0) {
+            virReportOOMError();
+            return NULL;
+        }
+    } else {
+        if (VIR_STRDUP(ret, SYSFS_SCSI_DEVICES) < 0)
+            return NULL;
+    }
+
+    return ret;
+}
+
 char *
 virSCSIDeviceGetSgName(const char *adapter,
                        unsigned int bus,
                        unsigned int target,
-                       unsigned int unit)
+                       unsigned int unit,
+                       const char *sysfs_root)
 {
     DIR *dir = NULL;
     struct dirent *entry;
     char *path = NULL;
     char *sg = NULL;
     unsigned int adapter_id;
+    char *sysfs_prefix = NULL;
 
     if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0)
         return NULL;
 
+    if (!(sysfs_prefix = virSCSIDeviceSysfsRoot(sysfs_root)))
+        return NULL;
+
     if (virAsprintf(&path,
-                    SYSFS_SCSI_DEVICES "/%d:%d:%d:%d/scsi_generic",
+                    "%s/%d:%d:%d:%d/scsi_generic",
+                    sysfs_prefix,
                     adapter_id, bus, target, unit) < 0) {
         virReportOOMError();
-        return NULL;
+        goto cleanup;
     }
 
     if (!(dir = opendir(path))) {
@@ -138,6 +162,7 @@ virSCSIDeviceGetSgName(const char *adapter,
 
 cleanup:
     closedir(dir);
+    VIR_FREE(sysfs_prefix);
     VIR_FREE(path);
     return sg;
 }
@@ -147,7 +172,8 @@ virSCSIDeviceNew(const char *adapter,
                  unsigned int bus,
                  unsigned int target,
                  unsigned int unit,
-                 bool readonly)
+                 bool readonly,
+                 const char *sysfs_root)
 {
     virSCSIDevicePtr dev, ret = NULL;
     char *sg = NULL;
@@ -155,6 +181,7 @@ virSCSIDeviceNew(const char *adapter,
     char *model_path = NULL;
     char *vendor = NULL;
     char *model = NULL;
+    char *sysfs_prefix = NULL;
 
     if (VIR_ALLOC(dev) < 0) {
         virReportOOMError();
@@ -166,7 +193,7 @@ virSCSIDeviceNew(const char *adapter,
     dev->unit = unit;
     dev->readonly = readonly;
 
-    if (!(sg = virSCSIDeviceGetSgName(adapter, bus, target, unit)))
+    if (!(sg = virSCSIDeviceGetSgName(adapter, bus, target, unit, sysfs_root)))
         goto cleanup;
 
     if (virSCSIDeviceGetAdapterId(adapter, &dev->adapter) < 0)
@@ -186,10 +213,15 @@ virSCSIDeviceNew(const char *adapter,
         goto cleanup;
     }
 
-    if (virAsprintf(&vendor_path,
-                    SYSFS_SCSI_DEVICES "/%s/vendor", dev->name) < 0 ||
-        virAsprintf(&model_path,
-                    SYSFS_SCSI_DEVICES "/%s/model", dev->name) < 0) {
+    if (!(sysfs_prefix = virSCSIDeviceSysfsRoot(sysfs_root)))
+        goto cleanup;
+
+    if (virAsprintf(&vendor_path, "%s/%s/vendor",
+                    sysfs_prefix,
+                    dev->name) < 0 ||
+        virAsprintf(&model_path, "%s/%s/model",
+                    sysfs_prefix,
+                    dev->name) < 0) {
         virReportOOMError();
         goto cleanup;
     }
@@ -210,6 +242,7 @@ virSCSIDeviceNew(const char *adapter,
 
     ret = dev;
 cleanup:
+    VIR_FREE(sysfs_prefix);
     VIR_FREE(sg);
     VIR_FREE(vendor);
     VIR_FREE(model);
diff --git a/src/util/virscsi.h b/src/util/virscsi.h
index 8268cdf..4e51d5e 100644
--- a/src/util/virscsi.h
+++ b/src/util/virscsi.h
@@ -36,13 +36,15 @@ typedef virSCSIDeviceList *virSCSIDeviceListPtr;
 char *virSCSIDeviceGetSgName(const char *adapter,
                              unsigned int bus,
                              unsigned int target,
-                             unsigned int unit);
+                             unsigned int unit,
+                             const char *sysfs_prefix);
 
 virSCSIDevicePtr virSCSIDeviceNew(const char *adapter,
                                   unsigned int bus,
                                   unsigned int target,
                                   unsigned int unit,
-                                  bool readonly);
+                                  bool readonly,
+                                  const char *sysfs_prefix);
 
 void virSCSIDeviceFree(virSCSIDevicePtr dev);
 void virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev, const char *name);
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 790857a..a975410 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -82,6 +82,8 @@ typedef enum {
     FLAG_JSON               = 1 << 3,
 } virQemuXML2ArgvTestFlags;
 
+# define SYSFS_ROOT "./sysfsroot"
+
 static int testCompareXMLToArgvFiles(const char *xml,
                                      const char *cmdline,
                                      virQEMUCapsPtr extraFlags,
@@ -158,7 +160,7 @@ static int testCompareXMLToArgvFiles(const char *xml,
                                      (flags & FLAG_JSON), extraFlags,
                                      migrateFrom, migrateFd, NULL,
                                      VIR_NETDEV_VPORT_PROFILE_OP_NO_OP,
-                                     NULL))) {
+                                     SYSFS_ROOT))) {
         if (flags & FLAG_EXPECT_FAILURE) {
             ret = 0;
             if (virTestGetDebug() > 1)
diff --git a/tests/sysfsroot/bus/scsi/devices/0:0:0:0/scsi_generic/sg0/dev b/tests/sysfsroot/bus/scsi/devices/0:0:0:0/scsi_generic/sg0/dev
new file mode 100644
index 0000000..992e920
--- /dev/null
+++ b/tests/sysfsroot/bus/scsi/devices/0:0:0:0/scsi_generic/sg0/dev
@@ -0,0 +1 @@
+21:0
-- 
1.8.1.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]