Skip to content

Commit d629a21

Browse files
committed
ghostdog: add subcommand detect-efa for EFA Device Detection
Signed-off-by: Yutong Sun <yutongsu@amazon.com>
1 parent 2ee905e commit d629a21

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

sources/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sources/ghostdog/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ argh.workspace = true
1313
gptman.workspace = true
1414
hex-literal.workspace = true
1515
lazy_static.workspace = true
16+
pciclient.workspace = true
1617
signpost.workspace = true
1718
snafu.workspace = true
1819

sources/ghostdog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Current version: 0.1.0
44

55
ghostdog is a tool to manage ephemeral disks.
66
It can be called as a udev helper program to identify ephemeral disks.
7+
It can also be called for EFA device detection which can be used for ExecCondition in systemd units.
78

89
## Colophon
910

sources/ghostdog/src/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*!
22
ghostdog is a tool to manage ephemeral disks.
33
It can be called as a udev helper program to identify ephemeral disks.
4+
It can also be called for EFA device detection which can be used for ExecCondition in systemd units.
45
*/
56

67
use argh::FromArgs;
@@ -30,8 +31,14 @@ struct Args {
3031
enum SubCommand {
3132
Scan(ScanArgs),
3233
EbsDeviceName(EbsDeviceNameArgs),
34+
EfaPresent(EfaPresentArgs),
3335
}
3436

37+
#[derive(FromArgs, PartialEq, Debug)]
38+
#[argh(subcommand, name = "efa-present")]
39+
/// Detect if EFA devices are attached.
40+
struct EfaPresentArgs {}
41+
3542
#[derive(FromArgs, PartialEq, Debug)]
3643
#[argh(subcommand, name = "scan")]
3744
/// Scan a device to see if it is an ephemeral disk.
@@ -63,10 +70,21 @@ fn run() -> Result<()> {
6370
let device_name = find_device_name(format!("{}", path.display()))?;
6471
emit_device_name(&device_name);
6572
}
73+
SubCommand::EfaPresent(_) => {
74+
is_efa_attached()?;
75+
}
6676
}
6777
Ok(())
6878
}
6979

80+
fn is_efa_attached() -> Result<()> {
81+
if pciclient::is_efa_attached().context(error::CheckEfaFailureSnafu)? {
82+
Ok(())
83+
} else {
84+
Err(error::Error::NoEfaPresent)
85+
}
86+
}
87+
7088
/// Find the device type by examining the partition table, if present.
7189
fn find_device_type<R>(reader: &mut R) -> Result<String>
7290
where
@@ -176,6 +194,10 @@ mod error {
176194
},
177195
#[snafu(display("Invalid device info for device '{}'", path.display()))]
178196
InvalidDeviceInfo { path: std::path::PathBuf },
197+
#[snafu(display("Failed to check if EFA device is attached: {}", source))]
198+
CheckEfaFailure { source: pciclient::PciClientError },
199+
#[snafu(display("Did not detect EFA"))]
200+
NoEfaPresent,
179201
}
180202
}
181203

0 commit comments

Comments
 (0)