Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/configurations/adrastea/vm: init with "add-vfio" package #160

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
1 change: 1 addition & 0 deletions nixos/configurations/adrastea/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
./gitea.nix
./hardware-configuration.nix
./networking.nix
./vm
./wireguard.nix
];

Expand Down
7 changes: 7 additions & 0 deletions nixos/configurations/adrastea/vm/add-vfio/Cargo.lock

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

6 changes: 6 additions & 0 deletions nixos/configurations/adrastea/vm/add-vfio/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "add-vfio"
version = "0.1.0"
edition = "2021"

[dependencies]
17 changes: 17 additions & 0 deletions nixos/configurations/adrastea/vm/add-vfio/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
lib,
rustPlatform,
}:

rustPlatform.buildRustPackage {
name = "add-vfio";

src = ./.;

cargoHash = "sha256-uDN154Y3QFw6jksEA0Z+AFgB1n0s4EFznXIC7Q+xOtQ=";

meta = {
license = lib.licenses.unlicense;
maintainers = [ lib.maintainers.inclyc ];
};
}
48 changes: 48 additions & 0 deletions nixos/configurations/adrastea/vm/add-vfio/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::env;
use std::fs;
use std::path::Path;

fn main() {
// Take the isolating device from argv[1]
let isolating_device = env::args().nth(1).expect("No device specified");

let sys_bus_pci = Path::new("/sys/bus/pci");

let iommu_group_path = sys_bus_pci
.join("devices")
.join(&isolating_device)
.join("iommu_group")
.join("devices");

let iommu_group = fs::read_dir(&iommu_group_path).expect("Failed to read iommu group");

for entry in iommu_group {
let file_name = entry.expect("Failed to read entry").file_name();
let device = file_name.to_str().unwrap();

let device_dir = sys_bus_pci.join("devices").join(device);

let driver_path = device_dir.join("driver");
let driver = fs::read_link(&driver_path).unwrap_or_default();

if driver.to_string_lossy().contains("vfio-pci") {
println!("skip({}): this device is already bound to vfio-pci", device);
continue;
}

// Remove from previous driver
if device_dir.join(&driver).exists() {
println!("driver/remove({}): remove from previous driver ...", device);
let _ = fs::write(device_dir.join("driver/unbind"), device);
}

// Bind to vfio-pci
println!("bind ...");
fs::write(device_dir.join("driver_override"), "vfio-pci")
.expect("driver/override: Failed to write driver_override");

// Probe device
println!("probe({}) ...", device);
fs::write("/sys/bus/pci/drivers_probe", device).expect("Failed to probe device");
}
}
10 changes: 10 additions & 0 deletions nixos/configurations/adrastea/vm/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ pkgs, ... }:

let
add-vfio = pkgs.callPackage ./add-vfio { };
in
{
environment.systemPackages = [
add-vfio
];
}
Loading