From 28d3ad952fb0fae83b347ace99150cbcbc0dcba1 Mon Sep 17 00:00:00 2001 From: Osier Yang Date: Sat, 4 May 2013 02:07:36 +0800 Subject: [PATCH] utils: Add a helper to get the device name that sg device mapped to 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 | 1 + src/util/virscsi.c | 47 ++++++++++++++++++++++++++++++++++++++++ src/util/virscsi.h | 4 ++++ 3 files changed, 52 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index cc734da21e..392357f39b 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1730,6 +1730,7 @@ virSCSIDeviceFileIterate; virSCSIDeviceFree; virSCSIDeviceGetAdapter; virSCSIDeviceGetBus; +virSCSIDeviceGetDevName; virSCSIDeviceGetName; virSCSIDeviceGetReadonly; virSCSIDeviceGetSgName; diff --git a/src/util/virscsi.c b/src/util/virscsi.c index d6685faeed..bdd06fdf72 100644 --- a/src/util/virscsi.c +++ b/src/util/virscsi.c @@ -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, diff --git a/src/util/virscsi.h b/src/util/virscsi.h index 8268cdfcb0..cce5df4899 100644 --- a/src/util/virscsi.h +++ b/src/util/virscsi.h @@ -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, -- GitLab