File tree Expand file tree Collapse file tree 3 files changed +40
-29
lines changed Expand file tree Collapse file tree 3 files changed +40
-29
lines changed Original file line number Diff line number Diff line change 161
161
description = "Setup MicroVM '%i' devices for passthrough" ;
162
162
before = [ "microvm@%i.service" ] ;
163
163
partOf = [ "microvm@%i.service" ] ;
164
- unitConfig . ConditionPathExists = "${ stateDir } /%i/current/share/microvm/ pci-devices " ;
164
+ unitConfig . ConditionPathExists = "${ stateDir } /%i/current/bin/ pci-setup " ;
165
165
restartIfChanged = false ;
166
166
serviceConfig = {
167
167
Type = "oneshot" ;
168
168
RemainAfterExit = true ;
169
169
SyslogIdentifier = "microvm-pci-devices@%i" ;
170
+ ExecStart = "${ stateDir } /%i/current/bin/pci-setup" ;
170
171
} ;
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
- '' ;
199
172
} ;
200
173
201
174
"microvm-virtiofsd@" =
Original file line number Diff line number Diff line change 16
16
./system.nix
17
17
./mounts.nix
18
18
./interfaces.nix
19
+ ./pci-devices.nix
19
20
./virtiofsd
20
21
./graphics.nix
21
22
./optimization.nix
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments