Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgroup/systemd: fix making CharDevice path in systemdProperties #3568

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}