-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nixos/configurations/adrastea/vm: init with "add-vfio" package
- Loading branch information
Showing
7 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
./gitea.nix | ||
./hardware-configuration.nix | ||
./networking.nix | ||
./vm | ||
./wireguard.nix | ||
]; | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} |