PhysicalDisk: detects virtual disks

This commit is contained in:
李通洲
2026-03-31 15:56:04 +08:00
parent d4b1ae9751
commit 0c401a0112
6 changed files with 123 additions and 104 deletions
-17
View File
@@ -3,23 +3,6 @@
#define FF_PHYSICALDISK_TEMP_UNSET (-DBL_MAX)
typedef enum __attribute__((__packed__)) FFPhysicalDiskType {
FF_PHYSICALDISK_TYPE_NONE = 0,
// If neither is set, it's unknown
FF_PHYSICALDISK_TYPE_HDD = 1 << 0,
FF_PHYSICALDISK_TYPE_SSD = 1 << 1,
FF_PHYSICALDISK_TYPE_FIXED = 1 << 2,
FF_PHYSICALDISK_TYPE_REMOVABLE = 1 << 3,
FF_PHYSICALDISK_TYPE_READWRITE = 1 << 4,
FF_PHYSICALDISK_TYPE_READONLY = 1 << 5,
FF_PHYSICALDISK_TYPE_FORCE_UNSIGNED = UINT8_MAX,
} FFPhysicalDiskType;
static_assert(sizeof(FFPhysicalDiskType) == sizeof(uint8_t), "");
typedef struct FFPhysicalDiskResult {
FFstrbuf name;
FFstrbuf interconnect;
+27 -13
View File
@@ -77,13 +77,29 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options)
continue;
}
FF_STRBUF_AUTO_DESTROY interconnect = ffStrbufCreate();
FFPhysicalDiskType diskType = FF_PHYSICALDISK_TYPE_NONE;
FF_CFTYPE_AUTO_RELEASE CFDictionaryRef protocolCharacteristics = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertyProtocolCharacteristicsKey), kCFAllocatorDefault, kNilOptions);
if (protocolCharacteristics) {
if (ffCfDictGetString(protocolCharacteristics, CFSTR(kIOPropertyPhysicalInterconnectTypeKey), &interconnect) == NULL) {
if (ffStrbufEqualS(&interconnect, kIOPropertyPhysicalInterconnectTypeVirtual)) {
diskType |= FF_PHYSICALDISK_TYPE_VIRTUAL;
FF_STRBUF_AUTO_DESTROY location = ffStrbufCreate();
if (ffCfDictGetString(protocolCharacteristics, CFSTR(kIOPropertyPhysicalInterconnectLocationKey), &location) == NULL) {
ffStrbufAppendS(&interconnect, " - ");
ffStrbufAppend(&interconnect, &location);
}
}
}
}
FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result);
ffStrbufInit(&device->serial);
ffStrbufInit(&device->revision);
ffStrbufInitS(&device->name, deviceName);
ffStrbufInit(&device->devPath);
ffStrbufInit(&device->interconnect);
device->type = FF_PHYSICALDISK_TYPE_NONE;
ffStrbufInitMove(&device->interconnect, &interconnect);
device->type = diskType;
device->size = 0;
device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
@@ -109,10 +125,6 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options)
} else {
device->size = 0;
}
FF_CFTYPE_AUTO_RELEASE CFDictionaryRef protocolCharacteristics = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertyProtocolCharacteristicsKey), kCFAllocatorDefault, kNilOptions);
if (protocolCharacteristics) {
ffCfDictGetString(protocolCharacteristics, CFSTR(kIOPropertyPhysicalInterconnectTypeKey), &device->interconnect);
}
FF_CFTYPE_AUTO_RELEASE CFDictionaryRef deviceCharacteristics = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertyDeviceCharacteristicsKey), kCFAllocatorDefault, kNilOptions);
if (deviceCharacteristics) {
@@ -121,18 +133,20 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options)
ffCfDictGetString(deviceCharacteristics, CFSTR(kIOPropertyProductRevisionLevelKey), &device->revision);
ffStrbufTrimRightSpace(&device->revision);
CFStringRef mediumType = (CFStringRef) CFDictionaryGetValue(deviceCharacteristics, CFSTR(kIOPropertyMediumTypeKey));
if (mediumType) {
if (CFStringCompare(mediumType, CFSTR(kIOPropertyMediumTypeSolidStateKey), 0) == 0) {
device->type |= FF_PHYSICALDISK_TYPE_SSD;
} else if (CFStringCompare(mediumType, CFSTR(kIOPropertyMediumTypeRotationalKey), 0) == 0) {
device->type |= FF_PHYSICALDISK_TYPE_HDD;
if (!(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL)) {
CFStringRef mediumType = (CFStringRef) CFDictionaryGetValue(deviceCharacteristics, CFSTR(kIOPropertyMediumTypeKey));
if (mediumType) {
if (CFStringCompare(mediumType, CFSTR(kIOPropertyMediumTypeSolidStateKey), 0) == 0) {
device->type |= FF_PHYSICALDISK_TYPE_SSD;
} else if (CFStringCompare(mediumType, CFSTR(kIOPropertyMediumTypeRotationalKey), 0) == 0) {
device->type |= FF_PHYSICALDISK_TYPE_HDD;
}
}
}
}
#ifdef MAC_OS_X_VERSION_10_15
if (options->temp) {
if (!(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) && options->temp) {
FF_CFTYPE_AUTO_RELEASE CFBooleanRef nvmeSMARTCapable = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertyNVMeSMARTCapableKey), kCFAllocatorDefault, kNilOptions);
if (nvmeSMARTCapable && CFBooleanGetValue(nvmeSMARTCapable)) {
detectSsdTemp(entryPhysical, &device->temperature);
+29 -38
View File
@@ -28,13 +28,10 @@ static double detectNvmeTemp(int devfd) {
static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOptions* options, FFlist* result) {
int devfd = openat(dfd, "device", O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
if (devfd < 0) {
return; // virtual device
}
FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
{
if (devfd > 0) {
if (ffAppendFileBufferRelative(devfd, "vendor", &name)) {
ffStrbufTrimRightSpace(&name);
if (name.length > 0) {
@@ -68,23 +65,26 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption
if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix)) {
return;
}
} else {
ffStrbufSetS(&name, devName);
}
FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result);
device->type = FF_PHYSICALDISK_TYPE_NONE;
ffStrbufInitMove(&device->name, &name);
ffStrbufInitF(&device->devPath, "/dev/%s", devName);
ffStrbufInit(&device->serial);
ffStrbufInit(&device->revision);
ffStrbufInit(&device->interconnect);
device->type = devfd > 0 ? FF_PHYSICALDISK_TYPE_NONE : FF_PHYSICALDISK_TYPE_VIRTUAL;
device->size = 0;
device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
bool isVirtual = false;
{
ffStrbufInit(&device->interconnect);
bool isVirtio = false;
if (devfd > 0) {
if (ffStrStartsWith(devName, "nvme")) {
ffStrbufSetStatic(&device->interconnect, "NVMe");
} else if (ffStrStartsWith(devName, "mmcblk")) {
ffStrbufSetStatic(&device->interconnect, "MMC");
} else if (ffStrStartsWith(devName, "md")) {
ffStrbufSetStatic(&device->interconnect, "RAID");
isVirtual = true;
} else {
char pathSysDeviceLink[64];
snprintf(pathSysDeviceLink, ARRAY_SIZE(pathSysDeviceLink), "/sys/block/%s/device", devName);
@@ -99,8 +99,8 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption
} else if (strstr(pathSysDeviceReal, "/nvme") != NULL) {
ffStrbufSetStatic(&device->interconnect, "NVMe");
} else if (strstr(pathSysDeviceReal, "/virtio") != NULL) {
ffStrbufSetStatic(&device->interconnect, "Virtual");
isVirtual = true;
ffStrbufSetStatic(&device->interconnect, "VirtIO");
isVirtio = true; // VirtIO devices are virtual, but we still want to report it
} else {
if (ffAppendFileBufferRelative(devfd, "transport", &device->interconnect)) {
ffStrbufTrimRightSpace(&device->interconnect);
@@ -108,13 +108,28 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption
}
}
}
} else {
ffStrbufSetStatic(&device->interconnect, "Virtual");
}
if (!isVirtual) {
if (devfd > 0 && !isVirtio) {
char isRotationalChar = '1';
if (ffReadFileDataRelative(dfd, "queue/rotational", 1, &isRotationalChar) > 0) {
device->type |= isRotationalChar == '1' ? FF_PHYSICALDISK_TYPE_HDD : FF_PHYSICALDISK_TYPE_SSD;
}
if (ffReadFileBufferRelative(devfd, "serial", &device->serial)) {
ffStrbufTrimSpace(&device->serial);
}
if (ffReadFileBufferRelative(devfd, "firmware_rev", &device->revision) ||
ffReadFileBufferRelative(devfd, "rev", &device->revision)) {
ffStrbufTrimRightSpace(&device->revision);
}
if (options->temp) {
device->temperature = detectNvmeTemp(devfd);
}
}
{
@@ -141,30 +156,6 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption
device->type |= roChar == '1' ? FF_PHYSICALDISK_TYPE_READONLY : FF_PHYSICALDISK_TYPE_READWRITE;
}
}
{
ffStrbufInit(&device->serial);
if (ffReadFileBufferRelative(devfd, "serial", &device->serial)) {
ffStrbufTrimSpace(&device->serial);
}
}
{
ffStrbufInit(&device->revision);
if (ffReadFileBufferRelative(devfd, "firmware_rev", &device->revision)) {
ffStrbufTrimRightSpace(&device->revision);
} else {
if (ffReadFileBufferRelative(devfd, "rev", &device->revision)) {
ffStrbufTrimRightSpace(&device->revision);
}
}
}
if (options->temp) {
device->temperature = detectNvmeTemp(devfd);
} else {
device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
}
}
const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) {
@@ -31,10 +31,17 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic
}
FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result);
ffStrbufInit(&device->serial);
ffStrbufInit(&device->revision);
ffStrbufInit(&device->name);
ffStrbufInit(&device->devPath);
ffStrbufInit(&device->interconnect);
device->type = FF_PHYSICALDISK_TYPE_NONE;
device->size = 0;
device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
STORAGE_DEVICE_DESCRIPTOR* sdd = (STORAGE_DEVICE_DESCRIPTOR*) sddBuffer;
ffStrbufInit(&device->name);
if (sdd->VendorIdOffset != 0) {
ffStrbufSetS(&device->name, (const char*) sddBuffer + sdd->VendorIdOffset);
ffStrbufTrim(&device->name, ' ');
@@ -58,14 +65,12 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic
return true;
}
ffStrbufInitWS(&device->devPath, szDevice);
ffStrbufInit(&device->serial);
ffStrbufSetWS(&device->devPath, szDevice);
if (sdd->SerialNumberOffset != 0) {
ffStrbufSetS(&device->serial, (const char*) sddBuffer + sdd->SerialNumberOffset);
ffStrbufTrimSpace(&device->serial);
}
ffStrbufInit(&device->revision);
if (sdd->ProductRevisionOffset != 0) {
ffStrbufSetS(&device->revision, (const char*) sddBuffer + sdd->ProductRevisionOffset);
ffStrbufTrimRightSpace(&device->revision);
@@ -73,7 +78,6 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic
device->type |= sdd->RemovableMedia ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED;
ffStrbufInit(&device->interconnect);
switch (sdd->BusType) {
case BusTypeUnknown:
ffStrbufSetStatic(&device->interconnect, "Unknown");
@@ -88,7 +92,7 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic
ffStrbufSetStatic(&device->interconnect, "ATA");
break;
case BusType1394:
ffStrbufSetStatic(&device->interconnect, "1394");
ffStrbufSetStatic(&device->interconnect, "IEEE 1394");
break;
case BusTypeSsa:
ffStrbufSetStatic(&device->interconnect, "SSA");
@@ -119,12 +123,15 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic
break;
case BusTypeVirtual:
ffStrbufSetStatic(&device->interconnect, "Virtual");
device->type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
break;
case BusTypeFileBackedVirtual:
ffStrbufSetStatic(&device->interconnect, "File Backed Virtual");
device->type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
break;
case BusTypeSpaces:
ffStrbufSetStatic(&device->interconnect, "Spaces");
ffStrbufSetStatic(&device->interconnect, "Storage Spaces");
device->type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
break;
case BusTypeNvme:
ffStrbufSetStatic(&device->interconnect, "NVMe");
@@ -135,30 +142,14 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic
case BusTypeUfs:
ffStrbufSetStatic(&device->interconnect, "UFS");
break;
case 0x14 /*BusTypeNvmeof*/:
ffStrbufSetStatic(&device->interconnect, "NVMe-oF");
break;
default:
ffStrbufSetF(&device->interconnect, "Unknown (%d)", (int) sdd->BusType);
break;
}
{
DEVICE_SEEK_PENALTY_DESCRIPTOR dspd = {};
if (DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&(STORAGE_PROPERTY_QUERY) {
.PropertyId = StorageDeviceSeekPenaltyProperty,
.QueryType = PropertyStandardQuery,
},
sizeof(STORAGE_PROPERTY_QUERY),
&dspd,
sizeof(dspd),
&retSize,
NULL) &&
retSize == sizeof(dspd)) {
device->type |= dspd.IncursSeekPenalty ? FF_PHYSICALDISK_TYPE_HDD : FF_PHYSICALDISK_TYPE_SSD;
}
}
{
DISK_GEOMETRY_EX dge = {};
if (DeviceIoControl(
@@ -211,23 +202,41 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic
}
}
device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
if (options->temp) {
STORAGE_TEMPERATURE_DATA_DESCRIPTOR stdd = {};
if (!(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL)) {
DEVICE_SEEK_PENALTY_DESCRIPTOR dspd = {};
if (DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&(STORAGE_PROPERTY_QUERY) {
.PropertyId = StorageDeviceTemperatureProperty,
.PropertyId = StorageDeviceSeekPenaltyProperty,
.QueryType = PropertyStandardQuery,
},
sizeof(STORAGE_PROPERTY_QUERY),
&stdd,
sizeof(stdd),
&dspd,
sizeof(dspd),
&retSize,
NULL) &&
retSize == sizeof(stdd)) {
device->temperature = stdd.TemperatureInfo[0].Temperature;
retSize == sizeof(dspd)) {
device->type |= dspd.IncursSeekPenalty ? FF_PHYSICALDISK_TYPE_HDD : FF_PHYSICALDISK_TYPE_SSD;
}
if (options->temp) {
STORAGE_TEMPERATURE_DATA_DESCRIPTOR stdd = {};
if (DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&(STORAGE_PROPERTY_QUERY) {
.PropertyId = StorageDeviceTemperatureProperty,
.QueryType = PropertyStandardQuery,
},
sizeof(STORAGE_PROPERTY_QUERY),
&stdd,
sizeof(stdd),
&retSize,
NULL) &&
retSize == sizeof(stdd)) {
device->temperature = stdd.TemperatureInfo[0].Temperature;
}
}
}
+18
View File
@@ -2,6 +2,24 @@
#include "common/option.h"
typedef enum __attribute__((__packed__)) FFPhysicalDiskType {
FF_PHYSICALDISK_TYPE_NONE = 0,
// If none is set, it's unknown
FF_PHYSICALDISK_TYPE_HDD = 1 << 0,
FF_PHYSICALDISK_TYPE_SSD = 1 << 1,
FF_PHYSICALDISK_TYPE_VIRTUAL = 1 << 2,
FF_PHYSICALDISK_TYPE_FIXED = 1 << 3,
FF_PHYSICALDISK_TYPE_REMOVABLE = 1 << 4,
FF_PHYSICALDISK_TYPE_READWRITE = 1 << 5,
FF_PHYSICALDISK_TYPE_READONLY = 1 << 6,
FF_PHYSICALDISK_TYPE_FORCE_UNSIGNED = UINT8_MAX,
} FFPhysicalDiskType;
static_assert(sizeof(FFPhysicalDiskType) == sizeof(uint8_t), "");
typedef struct FFPhysicalDiskOptions {
FFModuleArgs moduleArgs;
+6 -2
View File
@@ -46,7 +46,9 @@ bool ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) {
ffStrbufClear(&buffer);
ffSizeAppendNum(dev->size, &buffer);
const char* physicalType = dev->type & FF_PHYSICALDISK_TYPE_HDD
const char* physicalType = dev->type & FF_PHYSICALDISK_TYPE_VIRTUAL
? "Virtual"
: dev->type & FF_PHYSICALDISK_TYPE_HDD
? "HDD"
: dev->type & FF_PHYSICALDISK_TYPE_SSD
? "SSD"
@@ -169,7 +171,9 @@ bool ffGeneratePhysicalDiskJsonResult(FFPhysicalDiskOptions* options, yyjson_mut
yyjson_mut_obj_add_strbuf(doc, obj, "devPath", &dev->devPath);
yyjson_mut_obj_add_strbuf(doc, obj, "interconnect", &dev->interconnect);
if (dev->type & FF_PHYSICALDISK_TYPE_HDD) {
if (dev->type & FF_PHYSICALDISK_TYPE_VIRTUAL) {
yyjson_mut_obj_add_str(doc, obj, "kind", "Virtual");
} else if (dev->type & FF_PHYSICALDISK_TYPE_HDD) {
yyjson_mut_obj_add_str(doc, obj, "kind", "HDD");
} else if (dev->type & FF_PHYSICALDISK_TYPE_SSD) {
yyjson_mut_obj_add_str(doc, obj, "kind", "SSD");