Skip to content

Commit

Permalink
nixos/configurations/adrastea/vm: init with "add-vfio" package
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Dec 27, 2024
1 parent 72b6edd commit 37f4b35
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 0 deletions.
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
];
}

0 comments on commit 37f4b35

Please sign in to comment.