Skip to content

Commit

Permalink
use nsenter to run kernel tasks
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Ramachandra Sekar <[email protected]>
  • Loading branch information
Varun Ramachandra Sekar committed Dec 9, 2024
1 parent f152735 commit 69cff40
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions cmd/nvidia-dra-plugin/vfio-device.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"fmt"
"os"
"os/exec"
"path/filepath"
Expand All @@ -26,6 +27,7 @@ import (
)

const (
hostNamespaceMount = "/proc/1/ns/mnt"
vfioPciModule = "vfio_pci"
vfioPciDriver = "vfio-pci"
nvidiaDriver = "nvidia"
Expand Down Expand Up @@ -80,8 +82,7 @@ func (vm *VfioPciManager) isVfioPCIModuleLoaded() bool {
}

func (vm *VfioPciManager) loadVfioPciModule() error {
cmd := exec.Command("modprobe", vm.vfioPciModule) //nolint:gosec
_, err := cmd.CombinedOutput()
_, err := execCommandInHostNamespace("modprobe", []string{vm.vfioPciModule}) //nolint:gosec
if err != nil {
return err
}
Expand Down Expand Up @@ -149,17 +150,15 @@ func changeDriver(pciAddress, driver string) error {
}

func unbindFromDriver(pciAddress, driverResetRetries string) error {
cmd := exec.Command(unbindFromDriverScript, pciAddress, driverResetRetries) //nolint:gosec
_, err := cmd.CombinedOutput()
_, err := execCommandInHostNamespace(unbindFromDriverScript, []string{pciAddress, driverResetRetries}) //nolint:gosec
if err != nil {
return err
}
return nil
}

func bindToDriver(pciAddress, driver string) error {
cmd := exec.Command(bindToDriverScript, pciAddress, driver) //nolint:gosec
_, err := cmd.CombinedOutput()
_, err := execCommandInHostNamespace(bindToDriverScript, []string{pciAddress, driver}) //nolint:gosec
if err != nil {
return err
}
Expand Down Expand Up @@ -190,3 +189,9 @@ func (vm *VfioPciManager) GetCDIContainerEdits(info *GpuInfo) *cdiapi.ContainerE
},
}
}

func execCommandInHostNamespace(cmd string, args []string) ([]byte, error) {
nsenterArgs := []string{fmt.Sprintf("--mount=%s", hostNamespaceMount), "--", cmd}
nsenterArgs = append(nsenterArgs, args...)
return exec.Command("nsenter", nsenterArgs...).CombinedOutput()
}

0 comments on commit 69cff40

Please sign in to comment.