Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
pkg/cgroups: update the list of devices for the hypervisor
Browse files Browse the repository at this point in the history
The hypervisor needs access to `/dev/vfio/vfio` to use VFIO devices.
Remove all devicemapper devices from the allowed list, the device cgroup
must be updated when before hotpluggin any device.

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Apr 24, 2020
1 parent 042e7a2 commit 6377fc4
Showing 1 changed file with 6 additions and 39 deletions.
45 changes: 6 additions & 39 deletions virtcontainers/pkg/cgroups/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/opencontainers/runc/libcontainer/specconv"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)

type Config struct {
Expand Down Expand Up @@ -74,56 +73,25 @@ func UseSystemdCgroup() bool {

// returns the list of devices that a hypervisor may need
func hypervisorDevices() []specs.LinuxDeviceCgroup {
wildcard := int64(-1)
devicemapperMajor := int64(253)

devices := []specs.LinuxDeviceCgroup{}

devices = append(devices,
// hypervisor needs access to all devicemapper devices,
// since they can be hotplugged in the VM.
specs.LinuxDeviceCgroup{
Allow: true,
Type: "b",
Major: &devicemapperMajor,
Minor: &wildcard,
Access: "rwm",
})

// Processes running in a device-cgroup are constrained, they have acccess
// only to the devices listed in the devices.list file.
// In order to run Virtual Machines and create virtqueues, hypervisors
// need access to certain character devices in the host, like kvm and vhost-net.
hypervisorDevices := []string{
"/dev/kvm", // To run virtual machines
"/dev/vhost-net", // To create virtqueues
"/dev/vfio/vfio", // To access VFIO devices
}

for _, device := range hypervisorDevices {
var st unix.Stat_t
linuxDevice := specs.LinuxDeviceCgroup{
Allow: true,
Access: "rwm",
}

if err := unix.Stat(device, &st); err != nil {
cgroupsLogger.WithError(err).WithField("device", device).Warn("Could not get device information")
ldevice, err := DeviceToLinuxDevice(device)
if err != nil {
cgroupsLogger.WithError(err).Warnf("Could not get device information")
continue
}

switch st.Mode & unix.S_IFMT {
case unix.S_IFCHR:
linuxDevice.Type = "c"
case unix.S_IFBLK:
linuxDevice.Type = "b"
}

major := int64(unix.Major(st.Rdev))
minor := int64(unix.Minor(st.Rdev))
linuxDevice.Major = &major
linuxDevice.Minor = &minor

devices = append(devices, linuxDevice)
devices = append(devices, ldevice)
}

return devices
Expand All @@ -134,8 +102,7 @@ func New(config *Config) (*Manager, error) {
var err error
useSystemdCgroup := UseSystemdCgroup()

devices := []specs.LinuxDeviceCgroup{}
copy(devices, config.Resources.Devices)
devices := config.Resources.Devices
devices = append(devices, hypervisorDevices()...)
// Do not modify original devices
config.Resources.Devices = devices
Expand Down

0 comments on commit 6377fc4

Please sign in to comment.