Skip to content

Commit

Permalink
cgroup/systemd: fix systemdProperties setting CharDevice path problem
Browse files Browse the repository at this point in the history
func systemdProperties will set CharDevice path like /dev/char/0:0,
but NVIDIA devices with major 195:* and minor 507:* can not be found in path /dev/char/x:x,
getNVIDIAEntryPath will fix this problem.

Signed-off-by: yangfeiyu20102011 <yangfeiyu20102011@163.com>
  • Loading branch information
yangfeiyu20102011 committed Aug 24, 2022
1 parent 4a51b04 commit 819cecc
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 1 deletion.
29 changes: 28 additions & 1 deletion libcontainer/cgroups/devices/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func systemdProperties(r *configs.Resources) ([]systemdDbus.Property, error) {
case devices.BlockDevice:
entry.Path = fmt.Sprintf("/dev/block/%d:%d", rule.Major, rule.Minor)
case devices.CharDevice:
entry.Path = fmt.Sprintf("/dev/char/%d:%d", rule.Major, rule.Minor)
entry.Path = getCharEntryPath(rule)
}
// systemd will issue a warning if the path we give here doesn't exist.
// Since all of this logic is best-effort anyway (we manually set these
Expand Down Expand Up @@ -239,3 +239,30 @@ func allowAllDevices() []systemdDbus.Property {
newProp("DeviceAllow", []deviceAllowEntry{}),
}
}

func getCharEntryPath(rule *devices.Rule) string {
switch rule.Major {
case 195:
// Special case for NVIDIA devices
switch rule.Minor {
case 254:
return "/dev/nvidia-modeset"
case 255:
return "/dev/nvidiactl"
default:
// nvidia0 /dev/nvidia0
// nvidia1 /dev/nvidia1
return fmt.Sprintf("/dev/nvidia%d", rule.Minor)
}
case 507:
// Special case for NVIDIA devices
switch rule.Minor {
case 0:
return "/dev/nvidia-uvm"
case 1:
return "/dev/nvidia-uvm-tools"
}
}

return fmt.Sprintf("/dev/char/%d:%d", rule.Major, rule.Minor)
}
91 changes: 91 additions & 0 deletions libcontainer/cgroups/devices/systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"os"
"os/exec"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -251,3 +252,93 @@ func newManager(t *testing.T, config *configs.Cgroup) (m cgroups.Manager) {

return m
}

func TestGetCharEntryPath(t *testing.T) {
tests := []struct {
name string
rule devices.Rule
expect string
}{
{
name: "normal char /dev/char/0:0",
rule: devices.Rule{
Type: devices.CharDevice,
Major: 0,
Minor: 0,
},
expect: "/dev/char/0:0",
},
{
name: "normal char /dev/char/188:188",
rule: devices.Rule{
Type: devices.CharDevice,
Major: 188,
Minor: 188,
},
expect: "/dev/char/188:188",
},
{
name: "nvidia /dev/nvidia0",
rule: devices.Rule{
Type: devices.CharDevice,
Major: 195,
Minor: 0,
},
expect: "/dev/nvidia0",
},
{
name: "nvidia /dev/nvidia30",
rule: devices.Rule{
Type: devices.CharDevice,
Major: 195,
Minor: 30,
},
expect: "/dev/nvidia30",
},
{
name: "nvidia /dev/nvidiactl",
rule: devices.Rule{
Type: devices.CharDevice,
Major: 195,
Minor: 255,
},
expect: "/dev/nvidiactl",
},
{
name: "nvidia /dev/nvidia-modeset",
rule: devices.Rule{
Type: devices.CharDevice,
Major: 195,
Minor: 254,
},
expect: "/dev/nvidia-modeset",
},
{
name: "nvidia /dev/nvidia-uvm",
rule: devices.Rule{
Type: devices.CharDevice,
Major: 507,
Minor: 0,
},
expect: "/dev/nvidia-uvm",
},
{
name: "nvidia /dev/nvidia-uvm-tools",
rule: devices.Rule{
Type: devices.CharDevice,
Major: 507,
Minor: 1,
},
expect: "/dev/nvidia-uvm-tools",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
path := getCharEntryPath(&test.rule)
if !reflect.DeepEqual(path, test.expect) {
t.Errorf("test %s error, expect %s but got %s", test.name, test.expect, path)
}
})
}
}

0 comments on commit 819cecc

Please sign in to comment.