nixos/vms: Add more parameters for PCIe passthrough
This commit is contained in:
parent
8ec6804f80
commit
0310da9227
@ -9,9 +9,6 @@
|
|||||||
inherit (builtins) listToAttrs;
|
inherit (builtins) listToAttrs;
|
||||||
inherit (lib) mkIf mkMerge optionals;
|
inherit (lib) mkIf mkMerge optionals;
|
||||||
|
|
||||||
wanBDF =
|
|
||||||
if config.my.build.isDevVM then "00:02.0" else "27:00.0";
|
|
||||||
|
|
||||||
vmLVM = vm: lv: {
|
vmLVM = vm: lv: {
|
||||||
name = lv;
|
name = lv;
|
||||||
backend = {
|
backend = {
|
||||||
@ -62,7 +59,12 @@
|
|||||||
(vmLVM "estuary" "nix")
|
(vmLVM "estuary" "nix")
|
||||||
(vmLVM "estuary" "persist")
|
(vmLVM "estuary" "persist")
|
||||||
]);
|
]);
|
||||||
hostDevices."${wanBDF}" = { };
|
hostDevices = {
|
||||||
|
net-wan0 = {
|
||||||
|
index = 0;
|
||||||
|
hostBDF = if config.my.build.isDevVM then "00:02.0" else "27:00.0";
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
shill = {
|
shill = {
|
||||||
|
@ -95,7 +95,10 @@ let
|
|||||||
|
|
||||||
hostDevOpts = with lib.types; {
|
hostDevOpts = with lib.types; {
|
||||||
options = {
|
options = {
|
||||||
|
index = mkOpt' ints.unsigned null "Index of device in guest (for root port chassis and slot).";
|
||||||
|
hostBDF = mkOpt' str null "PCI BDF of host device.";
|
||||||
bindVFIO = mkBoolOpt' true "Whether to automatically bind the device to vfio-pci.";
|
bindVFIO = mkBoolOpt' true "Whether to automatically bind the device to vfio-pci.";
|
||||||
|
extraOptions = mkOpt' qemuOpts { } "Extra QEMU options for the vfio-pci QEMU device.";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -137,7 +140,7 @@ let
|
|||||||
allHostDevs =
|
allHostDevs =
|
||||||
flatten
|
flatten
|
||||||
(map
|
(map
|
||||||
(i: mapAttrsToList (bdf: c: { inherit bdf; inherit (c) bindVFIO; }) i.hostDevices)
|
(i: mapAttrsToList (name: c: c // { inherit name; }) i.hostDevices)
|
||||||
(attrValues cfg.instances));
|
(attrValues cfg.instances));
|
||||||
|
|
||||||
mkQemuScript = n: i:
|
mkQemuScript = n: i:
|
||||||
@ -176,7 +179,10 @@ let
|
|||||||
"blockdev node-name=${d.name}-format,${d.formatBackendProp}=${d.name}-backend,${d.format}"
|
"blockdev node-name=${d.name}-format,${d.formatBackendProp}=${d.name}-backend,${d.format}"
|
||||||
("device ${d.frontend},id=${d.name},drive=${d.name}-format" + (extraQEMUOpts d.frontendOpts))
|
("device ${d.frontend},id=${d.name},drive=${d.name}-format" + (extraQEMUOpts d.frontendOpts))
|
||||||
]) i.drives)) ++
|
]) i.drives)) ++
|
||||||
(map (bdf: "device vfio-pci,host=${bdf}") (attrNames i.hostDevices));
|
(flatten (mapAttrsToList (id: c: [
|
||||||
|
"device pcie-root-port,id=${id}-port,chassis=${toString c.index},port=${toString c.index}"
|
||||||
|
("device vfio-pci,bus=${id}-port,host=${c.hostBDF}" + (extraQEMUOpts c.extraOptions))
|
||||||
|
]) i.hostDevices));
|
||||||
args = map (v: "-${v}") flags;
|
args = map (v: "-${v}") flags;
|
||||||
in
|
in
|
||||||
''
|
''
|
||||||
@ -193,7 +199,7 @@ in
|
|||||||
config = mkIf (cfg.instances != { }) {
|
config = mkIf (cfg.instances != { }) {
|
||||||
assertions = [
|
assertions = [
|
||||||
{
|
{
|
||||||
assertion = let bdfs = map (d: d.bdf) allHostDevs; in (unique bdfs) == bdfs;
|
assertion = let bdfs = map (d: d.hostBDF) allHostDevs; in (unique bdfs) == bdfs;
|
||||||
message = "VMs cannot share host devices!";
|
message = "VMs cannot share host devices!";
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
@ -215,7 +221,7 @@ in
|
|||||||
"etc/udev/rules.d/20-vfio-tags.rules"
|
"etc/udev/rules.d/20-vfio-tags.rules"
|
||||||
(concatMapStringsSep
|
(concatMapStringsSep
|
||||||
"\n"
|
"\n"
|
||||||
(d: ''ACTION=="add", SUBSYSTEM=="pci", KERNEL=="0000:${d.bdf}", TAG="vfio-pci-bind"'')
|
(d: ''ACTION=="add", SUBSYSTEM=="pci", KERNEL=="0000:${d.hostBDF}", TAG="vfio-pci-bind"'')
|
||||||
(filter (d: d.bindVFIO) allHostDevs)))
|
(filter (d: d.bindVFIO) allHostDevs)))
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user