/* Returns device name (e.g. "sdc") on success, or NULL * on failure. */ char * virSCSIDeviceGetDevName(const char *sysfs_prefix, const char *adapter, unsigned int bus, unsigned int target, unsigned long long unit) { DIR *dir = NULL; struct dirent *entry; 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, "%s/%d:%u:%u:%llu/block", prefix, adapter_id, bus, target, unit) < 0) return NULL; if (virDirOpen(&dir, path) < 0) goto cleanup; while (virDirRead(dir, &entry, path) > 0) { ignore_value(VIR_STRDUP(name, entry->d_name)); break; } cleanup: VIR_DIR_CLOSE(dir); VIR_FREE(path); return name; }
/* 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) 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; ignore_value(VIR_STRDUP(name, entry->d_name)); break; } cleanup: closedir(dir); VIR_FREE(path); return name; }
virSCSIDevicePtr virSCSIDeviceNew(const char *adapter, unsigned int bus, unsigned int target, unsigned int unit, bool readonly) { virSCSIDevicePtr dev, ret = NULL; char *sg = NULL; char *vendor_path = NULL; char *model_path = NULL; char *vendor = NULL; char *model = NULL; if (VIR_ALLOC(dev) < 0) return NULL; dev->bus = bus; dev->target = target; dev->unit = unit; dev->readonly = readonly; if (!(sg = virSCSIDeviceGetSgName(adapter, bus, target, unit))) goto cleanup; if (virSCSIDeviceGetAdapterId(adapter, &dev->adapter) < 0) goto cleanup; if (virAsprintf(&dev->name, "%d:%d:%d:%d", dev->adapter, dev->bus, dev->target, dev->unit) < 0 || virAsprintf(&dev->sg_path, "/dev/%s", sg) < 0) goto cleanup; if (access(dev->sg_path, F_OK) != 0) { virReportSystemError(errno, _("SCSI device '%s': could not access %s"), dev->name, dev->sg_path); 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) goto cleanup; if (virFileReadAll(vendor_path, 1024, &vendor) < 0) goto cleanup; if (virFileReadAll(model_path, 1024, &model) < 0) goto cleanup; virTrimSpaces(vendor, NULL); virTrimSpaces(model, NULL); if (virAsprintf(&dev->id, "%s:%s", vendor, model) < 0) goto cleanup; ret = dev; cleanup: VIR_FREE(sg); VIR_FREE(vendor); VIR_FREE(model); VIR_FREE(vendor_path); VIR_FREE(model_path); if (!ret) virSCSIDeviceFree(dev); return ret; }
virSCSIDevicePtr virSCSIDeviceNew(const char *sysfs_prefix, const char *adapter, unsigned int bus, unsigned int target, unsigned long long unit, bool readonly, bool shareable) { virSCSIDevicePtr dev, ret = NULL; char *sg = NULL; char *vendor_path = NULL; 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; dev->bus = bus; dev->target = target; dev->unit = unit; dev->readonly = readonly; dev->shareable = shareable; if (!(sg = virSCSIDeviceGetSgName(prefix, adapter, bus, target, unit))) goto cleanup; if (virSCSIDeviceGetAdapterId(adapter, &dev->adapter) < 0) goto cleanup; if (virAsprintf(&dev->name, "%d:%u:%u:%llu", dev->adapter, dev->bus, dev->target, dev->unit) < 0 || virAsprintf(&dev->sg_path, "%s/%s", sysfs_prefix ? sysfs_prefix : "/dev", sg) < 0) goto cleanup; if (!virFileExists(dev->sg_path)) { virReportSystemError(errno, _("SCSI device '%s': could not access %s"), dev->name, dev->sg_path); goto cleanup; } if (virAsprintf(&vendor_path, "%s/%s/vendor", prefix, dev->name) < 0 || virAsprintf(&model_path, "%s/%s/model", prefix, dev->name) < 0) goto cleanup; if (virFileReadAll(vendor_path, 1024, &vendor) < 0) goto cleanup; if (virFileReadAll(model_path, 1024, &model) < 0) goto cleanup; virTrimSpaces(vendor, NULL); virTrimSpaces(model, NULL); if (virAsprintf(&dev->id, "%s:%s", vendor, model) < 0) goto cleanup; ret = dev; cleanup: VIR_FREE(sg); VIR_FREE(vendor); VIR_FREE(model); VIR_FREE(vendor_path); VIR_FREE(model_path); if (!ret) virSCSIDeviceFree(dev); return ret; }