Skip to content

Commit c451ad0

Browse files
committed
nixos-modules: move pci setup from host to microvm
1 parent 3d3429b commit c451ad0

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

nixos-modules/host/default.nix

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -161,41 +161,14 @@ in
161161
description = "Setup MicroVM '%i' devices for passthrough";
162162
before = [ "microvm@%i.service" ];
163163
partOf = [ "microvm@%i.service" ];
164-
unitConfig.ConditionPathExists = "${stateDir}/%i/current/share/microvm/pci-devices";
164+
unitConfig.ConditionPathExists = "${stateDir}/%i/current/bin/pci-setup";
165165
restartIfChanged = false;
166166
serviceConfig = {
167167
Type = "oneshot";
168168
RemainAfterExit = true;
169169
SyslogIdentifier = "microvm-pci-devices@%i";
170+
ExecStart = "${stateDir}/%i/current/bin/pci-setup";
170171
};
171-
# `ExecStart`
172-
scriptArgs = "%i";
173-
script = ''
174-
cd ${stateDir}/$1
175-
176-
${pkgs.kmod}/bin/modprobe vfio-pci
177-
178-
for path in $(cat current/share/microvm/pci-devices); do
179-
pushd /sys/bus/pci/devices/$path
180-
if [ -e driver ]; then
181-
echo $path > driver/unbind
182-
fi
183-
echo vfio-pci > driver_override
184-
echo $path > /sys/bus/pci/drivers_probe
185-
186-
# In order to access the vfio dev the permissions must be set
187-
# for the user/group running the VMM later.
188-
#
189-
# Insprired by https://www.kernel.org/doc/html/next/driver-api/vfio.html#vfio-usage-example
190-
#
191-
# assert we could get the IOMMU group number (=: name of VFIO dev)
192-
[[ -e iommu_group ]] || exit 1
193-
VFIO_DEV=$(basename $(readlink iommu_group))
194-
echo "Making VFIO device $VFIO_DEV accessible for user"
195-
chown ${user}:${group} /dev/vfio/$VFIO_DEV
196-
popd
197-
done
198-
'';
199172
};
200173

201174
"microvm-virtiofsd@" =

nixos-modules/microvm/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ in
1616
./system.nix
1717
./mounts.nix
1818
./interfaces.nix
19+
./pci-devices.nix
1920
./virtiofsd
2021
./graphics.nix
2122
./optimization.nix

nixos-modules/microvm/pci-devices.nix

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{ config, lib, pkgs, ... }:
2+
3+
let
4+
pciDevices = builtins.filter ({ bus, ... }:
5+
bus == "pci"
6+
) config.microvm.devices;
7+
8+
# TODO: don't hardcode but obtain from host config
9+
user = "microvm";
10+
group = "kvm";
11+
12+
in
13+
{
14+
microvm.binScripts.pci-setup = lib.mkIf (pciDevices != []) (''
15+
set -eou pipefail
16+
${pkgs.kmod}/bin/modprobe vfio-pci
17+
'' + lib.concatMapStrings ({ path, ... }: ''
18+
cd /sys/bus/pci/devices/${path}
19+
if [ -e driver ]; then
20+
echo ${path} > driver/unbind
21+
fi
22+
echo vfio-pci > driver_override
23+
echo ${path} > /sys/bus/pci/drivers_probe
24+
'' +
25+
# In order to access the vfio dev the permissions must be set
26+
# for the user/group running the VMM later.
27+
#
28+
# Insprired by https://www.kernel.org/doc/html/next/driver-api/vfio.html#vfio-usage-example
29+
#
30+
# assert we could get the IOMMU group number (=: name of VFIO dev)
31+
''
32+
[[ -e iommu_group ]] || exit 1
33+
VFIO_DEV=$(basename $(readlink iommu_group))
34+
echo "Making VFIO device $VFIO_DEV accessible for user"
35+
chown ${user}:${group} /dev/vfio/$VFIO_DEV
36+
'') pciDevices);
37+
}

0 commit comments

Comments
 (0)