ohmu
·
2026-10-15
1#include "physicaldisk.h"
2#include "common/debug.h"
3#include "common/io.h"
4#include "common/properties.h"
5#include "common/strutil.h"
6
7#include <ctype.h>
8#include <limits.h>
9#include <unistd.h>
10#include <fcntl.h>
11
12static double detectNvmeTemp(int devfd) {
13 char pathHwmon[] = "hwmon$/temp1_input";
14
15 for (char c = '0'; c <= '9'; c++) // hopefully there's only one digit
16 {
17 pathHwmon[strlen("hwmon")] = c;
18 char buffer[64];
19 ssize_t size = ffReadFileDataRelative(devfd, pathHwmon, ARRAY_SIZE(buffer), buffer);
20 if (size > 0) {
21 buffer[size] = '\0';
22 double temp = strtod(buffer, nullptr);
23 return temp > 0 && temp < 10000000 /*VMware*/ ? temp / 1000 : FF_PHYSICALDISK_TEMP_UNSET;
24 }
25 }
26
27 return FF_PHYSICALDISK_TEMP_UNSET;
28}
29
30static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOptions* options, FFlist* result) {
31 uint64_t size = 0;
32
33 {
34 char blkSize[32];
35 ssize_t fileSize = ffReadFileDataRelative(dfd, "size", ARRAY_SIZE(blkSize) - 1, blkSize);
36 if (fileSize > 0) {
37 blkSize[fileSize] = 0;
38 size = (uint64_t) strtoull(blkSize, nullptr, 10) * 512;
39 }
40 }
41
42 FFPhysicalDiskType type = FF_PHYSICALDISK_TYPE_NONE;
43 if (size == 0) {
44 if (options->hideType & FF_PHYSICALDISK_TYPE_UNUSED) {
45 return;
46 }
47
48 type |= FF_PHYSICALDISK_TYPE_UNUSED;
49 }
50
51 int devfd = openat(dfd, "device", O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
52
53 if (devfd < 0) {
54 if (options->hideType & FF_PHYSICALDISK_TYPE_VIRTUAL) {
55 return;
56 }
57
58 type |= FF_PHYSICALDISK_TYPE_VIRTUAL;
59 }
60
61 FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
62
63 if (devfd > 0) {
64 if (ffAppendFileBufferRelative(devfd, "vendor", &name)) {
65 ffStrbufTrimRightSpace(&name);
66 if (name.length > 0) {
67 ffStrbufAppendC(&name, ' ');
68 }
69 }
70
71 ffAppendFileBufferRelative(devfd, "model", &name);
72 ffStrbufTrimRightSpace(&name);
73
74 if (name.length == 0) {
75 ffStrbufSetS(&name, devName);
76 }
77
78 if (ffStrStartsWith(devName, "nvme")) {
79 int devid, nsid;
80 if (sscanf(devName, "nvme%dn%d", &devid, &nsid) == 2) {
81 bool multiNs = nsid > 1;
82 if (!multiNs) {
83 char pathSysBlock[32];
84 snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "/dev/nvme%dn2", devid);
85 multiNs = access(pathSysBlock, F_OK) == 0;
86 }
87 if (multiNs) {
88 // In Asahi Linux, there are multiple namespaces for the same NVMe drive.
89 ffStrbufAppendF(&name, " - %d", nsid);
90 }
91 }
92 }
93
94 if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix)) {
95 return;
96 }
97 } else {
98 ffStrbufSetS(&name, devName);
99 }
100
101 FFPhysicalDiskResult* device = FF_LIST_ADD(FFPhysicalDiskResult, *result);
102 ffStrbufInitMove(&device->name, &name);
103 ffStrbufInitF(&device->devPath, "/dev/%s", devName);
104 ffStrbufInit(&device->serial);
105 ffStrbufInit(&device->revision);
106 ffStrbufInit(&device->interconnect);
107 device->type = type;
108 device->size = size;
109 device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
110
111 bool isVirtio = false;
112 if (devfd > 0) {
113 if (ffStrStartsWith(devName, "nvme")) {
114 ffStrbufSetStatic(&device->interconnect, "NVMe");
115 } else if (ffStrStartsWith(devName, "mmcblk")) {
116 ffStrbufSetStatic(&device->interconnect, "MMC");
117 } else {
118 char pathSysDeviceLink[64];
119 snprintf(pathSysDeviceLink, ARRAY_SIZE(pathSysDeviceLink), "/sys/block/%s/device", devName);
120 char pathSysDeviceReal[PATH_MAX];
121 if (realpath(pathSysDeviceLink, pathSysDeviceReal)) {
122 if (strstr(pathSysDeviceReal, "/usb") != nullptr) {
123 ffStrbufSetStatic(&device->interconnect, "USB");
124 } else if (strstr(pathSysDeviceReal, "/ata") != nullptr) {
125 ffStrbufSetStatic(&device->interconnect, "ATA");
126 } else if (strstr(pathSysDeviceReal, "/scsi") != nullptr) {
127 ffStrbufSetStatic(&device->interconnect, "SCSI");
128 } else if (strstr(pathSysDeviceReal, "/nvme") != nullptr) {
129 ffStrbufSetStatic(&device->interconnect, "NVMe");
130 } else if (strstr(pathSysDeviceReal, "/virtio") != nullptr) {
131 ffStrbufSetStatic(&device->interconnect, "VirtIO");
132 isVirtio = true; // VirtIO devices are virtual, but we still want to report it
133 } else {
134 if (ffAppendFileBufferRelative(devfd, "transport", &device->interconnect)) {
135 ffStrbufTrimRightSpace(&device->interconnect);
136 }
137 }
138 }
139 }
140 } else {
141 ffStrbufSetStatic(&device->interconnect, "Virtual");
142 }
143
144 if (devfd > 0 && !isVirtio) {
145 char isRotationalChar = '1';
146 if (ffReadFileDataRelative(dfd, "queue/rotational", 1, &isRotationalChar) > 0) {
147 device->type |= isRotationalChar == '1' ? FF_PHYSICALDISK_TYPE_HDD : FF_PHYSICALDISK_TYPE_SSD;
148 }
149
150 if (ffReadFileBufferRelative(devfd, "serial", &device->serial)) {
151 ffStrbufTrimSpace(&device->serial);
152 }
153
154 if (ffReadFileBufferRelative(devfd, "firmware_rev", &device->revision) ||
155 ffReadFileBufferRelative(devfd, "rev", &device->revision)) {
156 ffStrbufTrimRightSpace(&device->revision);
157 }
158
159 if (options->temp) {
160 device->temperature = detectNvmeTemp(devfd);
161 }
162 }
163
164 {
165 char removableChar = '0';
166 if (ffReadFileDataRelative(dfd, "removable", 1, &removableChar) > 0) {
167 device->type |= removableChar == '1' ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED;
168 }
169 }
170
171 {
172 char roChar = '0';
173 if (ffReadFileDataRelative(dfd, "ro", 1, &roChar) > 0) {
174 device->type |= roChar == '1' ? FF_PHYSICALDISK_TYPE_READONLY : FF_PHYSICALDISK_TYPE_READWRITE;
175 }
176 }
177}
178
179const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) {
180 FF_AUTO_CLOSE_DIR DIR* sysBlockDirp = opendir("/sys/block/");
181 if (sysBlockDirp == nullptr) {
182 FF_DEBUG("opendir(\"/sys/block/\") failed: %s", strerror(errno));
183 return "opendir(\"/sys/block/\") == nullptr";
184 }
185
186 struct dirent* sysBlockEntry;
187 while ((sysBlockEntry = readdir(sysBlockDirp)) != nullptr) {
188 const char* const devName = sysBlockEntry->d_name;
189
190 if (devName[0] == '.') {
191 continue;
192 }
193
194 char pathSysBlock[sizeof("/sys/block/") + sizeof(sysBlockEntry->d_name)];
195 snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "/sys/block/%s", devName);
196
197 int dfd = openat(dirfd(sysBlockDirp), devName, O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
198 if (dfd > 0) {
199 parsePhysicalDisk(dfd, devName, options, result);
200 }
201 }
202
203 return nullptr;
204}