Skip to content
API Documentation
BastionGuard™
DeviceEnumerator Class Reference

#include <device_enumerator.hpp>

Static Public Member Functions

static std::vector< DeviceInfo > enumerate_linux_block_devices (bool include_health_probe=false)

Detailed Description

Definition at line 20 of file device_enumerator.hpp.

Member Function Documentation

◆ enumerate_linux_block_devices()

std::vector< DeviceInfo > DeviceEnumerator::enumerate_linux_block_devices ( bool include_health_probe = false)
static

Definition at line 51 of file device_enumerator.cpp.

51 {
52 std::vector<DeviceInfo> out;
53 const fs::path sys_block{"/sys/block"};
54
55 if (!fs::exists(sys_block) || !fs::is_directory(sys_block)) {
56 return out;
57 }
58
59 for (const auto& entry : fs::directory_iterator(sys_block)) {
60 const auto name = entry.path().filename().string();
61 if (!is_real_disk_name(name)) {
62 continue;
63 }
64
65 DeviceInfo d;
66 d.name = name;
67 d.path = "/dev/" + name;
68 d.size_bytes = read_u64_file(entry.path() / "size") * 512ull;
69 d.model = read_text_file(entry.path() / "device/model");
70 d.transport = read_text_file(entry.path() / "device/subsystem" / "device" / "transport");
71 d.type = (starts_with(name, "loop") || starts_with(name, "dm-")) ? "virtual" : "disk";
72 d.removable = read_u64_file(entry.path() / "removable") != 0;
73
74 if (starts_with(name, "nvme")) {
75 d.transport = "NVMe";
76 } else if (starts_with(name, "md")) {
77 d.transport = "Linux RAID";
78 } else if (starts_with(name, "dm-")) {
79 d.transport = "Device Mapper";
80 } else if (starts_with(name, "loop")) {
81 d.transport = "Loop";
82 } else if (d.transport.empty()) {
83 d.transport = "Block";
84 }
85
86 if (include_health_probe) {
87 const auto health = DeviceHealthProbe::probe(d.path);
88 d.degraded = health.state != DeviceHealthState::Healthy;
89 d.health_summary = health.summary;
90 }
91
92 out.push_back(d);
93 }
94
95 return out;
96}
static DeviceHealthReport probe(const std::string &device_path, CancelCallback cancel_cb={})
bool is_real_disk_name(const std::string &name)
std::string read_text_file(const fs::path &p)
bool starts_with(const std::string &s, const std::string &prefix)
std::string name
std::string health_summary
std::string model
std::string type
std::string transport
std::string path

References DeviceInfo::degraded, DeviceInfo::health_summary, Healthy, DeviceInfo::model, DeviceInfo::name, DeviceInfo::path, DeviceHealthProbe::probe(), DeviceInfo::removable, DeviceInfo::size_bytes, DeviceInfo::transport, and DeviceInfo::type.

Referenced by MainWindow::refresh_devices().


The documentation for this class was generated from the following files: