]> xenbits.xensource.com Git - libvirt.git/commitdiff
utils: Add a helper to get the device name that sg device mapped to
authorOsier Yang <jyang@redhat.com>
Fri, 3 May 2013 18:07:36 +0000 (02:07 +0800)
committerOsier Yang <jyang@redhat.com>
Thu, 16 May 2013 15:50:00 +0000 (23:50 +0800)
E.g.

% sg_map
/dev/sg0  /dev/sda
/dev/sg1  /dev/sr0

What the helper gets for /dev/sg0 is /dev/sda, it will be used by
later patch.

src/libvirt_private.syms
src/util/virscsi.c
src/util/virscsi.h

index cc734da21e1b9f74dc60c0fecb678bd573fb13a3..392357f39b9d8053965884672c8d0b1ff7f86f71 100644 (file)
@@ -1730,6 +1730,7 @@ virSCSIDeviceFileIterate;
 virSCSIDeviceFree;
 virSCSIDeviceGetAdapter;
 virSCSIDeviceGetBus;
+virSCSIDeviceGetDevName;
 virSCSIDeviceGetName;
 virSCSIDeviceGetReadonly;
 virSCSIDeviceGetSgName;
index d6685faeedfb10adf33b90dd4dadadf506f5fb78..bdd06fdf72a43aadef9612cdae8191968f7dab37 100644 (file)
@@ -142,6 +142,53 @@ cleanup:
     return sg;
 }
 
+/* Returns device name (e.g. "sdc") on success, or NULL
+ * on failure.
+ */
+char *
+virSCSIDeviceGetDevName(const char *adapter,
+                        unsigned int bus,
+                        unsigned int target,
+                        unsigned int unit)
+{
+    DIR *dir = NULL;
+    struct dirent *entry;
+    char *path = NULL;
+    char *name = NULL;
+    unsigned int adapter_id;
+
+    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) {
+        virReportOOMError();
+        return NULL;
+    }
+
+    if (!(dir = opendir(path))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Failed to open %s"), path);
+        goto cleanup;
+    }
+
+    while ((entry = readdir(dir))) {
+        if (entry->d_name[0] == '.')
+            continue;
+
+        if (!(name = strdup(entry->d_name))) {
+            virReportOOMError();
+            goto cleanup;
+        }
+    }
+
+cleanup:
+    closedir(dir);
+    VIR_FREE(path);
+    return name;
+}
+
 virSCSIDevicePtr
 virSCSIDeviceNew(const char *adapter,
                  unsigned int bus,
index 8268cdfcb0dc7393f947a0c11177407b8a5b0725..cce5df4899cbd79e610cf4f6069a652b94645e05 100644 (file)
@@ -37,6 +37,10 @@ char *virSCSIDeviceGetSgName(const char *adapter,
                              unsigned int bus,
                              unsigned int target,
                              unsigned int unit);
+char *virSCSIDeviceGetDevName(const char *adapter,
+                              unsigned int bus,
+                              unsigned int target,
+                              unsigned int unit);
 
 virSCSIDevicePtr virSCSIDeviceNew(const char *adapter,
                                   unsigned int bus,