Skip to content

Commit

Permalink
ghostdog: add subcommand detect-efa for EFA Device Detection
Browse files Browse the repository at this point in the history
Signed-off-by: Yutong Sun <[email protected]>
  • Loading branch information
ytsssun committed Sep 13, 2024
1 parent fab7d74 commit ac11ef2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions sources/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sources/ghostdog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ argh.workspace = true
gptman.workspace = true
hex-literal.workspace = true
lazy_static.workspace = true
pciclient.workspace = true
signpost.workspace = true
snafu.workspace = true

Expand Down
1 change: 1 addition & 0 deletions sources/ghostdog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Current version: 0.1.0

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

## Colophon

Expand Down
22 changes: 21 additions & 1 deletion sources/ghostdog/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*!
ghostdog is a tool to manage ephemeral disks.
It can be called as a udev helper program to identify ephemeral disks.
It can also be called for EFA device detection which can be used for ExecCondition in systemd units.
*/

use argh::FromArgs;
Expand All @@ -10,7 +11,7 @@ use lazy_static::lazy_static;
use signpost::uuid_to_guid;
use snafu::{ensure, ResultExt};
use std::collections::HashSet;
use std::fs;
use std::{fs, process};
use std::io::{Read, Seek};
use std::path::PathBuf;
use std::process::Command;
Expand All @@ -30,8 +31,14 @@ struct Args {
enum SubCommand {
Scan(ScanArgs),
EbsDeviceName(EbsDeviceNameArgs),
DetectEfaDevice(DetectEfaDeviceArgs),
}

#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "detect-efa")]
/// Detect if EFA is attached.
struct DetectEfaDeviceArgs {}

#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "scan")]
/// Scan a device to see if it is an ephemeral disk.
Expand Down Expand Up @@ -63,10 +70,21 @@ fn run() -> Result<()> {
let device_name = find_device_name(format!("{}", path.display()))?;
emit_device_name(&device_name);
}
SubCommand::DetectEfaDevice(_) => {
is_efa_attached()?;
}
}
Ok(())
}

fn is_efa_attached() -> Result<()> {
if pciclient::is_efa_attached().context(error::CheckEfaFailureSnafu)? {
process::exit(0);
} else {
process::exit(1);
}
}

/// Find the device type by examining the partition table, if present.
fn find_device_type<R>(reader: &mut R) -> Result<String>
where
Expand Down Expand Up @@ -176,6 +194,8 @@ mod error {
},
#[snafu(display("Invalid device info for device '{}'", path.display()))]
InvalidDeviceInfo { path: std::path::PathBuf },
#[snafu(display("Failed to check if EFA device is attached: {}", source))]
CheckEfaFailure { source: pciclient::Error },
}
}

Expand Down

0 comments on commit ac11ef2

Please sign in to comment.