Skip to content

Commit

Permalink
agent: update resources list with the right device major-minor number
Browse files Browse the repository at this point in the history
Host device's major-minor numbers are mapped to guest major-minor numbers,
for example in the host the major-minor number for /dev/loop0p1 is 259:1,
when it's attached to the VM now the major-minor number is 8:0, this
conversion must be reflected in devices and resources lists, the first list
is used to mount the device in the container and the second one is to update
the devices cgroup.

fixes kata-containers#351

Signed-off-by: Julio Montes <julio.montes@intel.com>
  • Loading branch information
Julio Montes authored and jshachm committed Nov 22, 2018
1 parent c57ca46 commit d1abf91
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
21 changes: 19 additions & 2 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,33 @@ func updateSpecDeviceList(device pb.Device, spec *pb.Spec) error {
// Update the spec
for idx, d := range spec.Linux.Devices {
if d.Path == device.ContainerPath {
hostMajor := spec.Linux.Devices[idx].Major
hostMinor := spec.Linux.Devices[idx].Minor
agentLog.WithFields(logrus.Fields{
"device-path": device.VmPath,
"host-device-major": spec.Linux.Devices[idx].Major,
"host-device-minor": spec.Linux.Devices[idx].Minor,
"host-device-major": hostMajor,
"host-device-minor": hostMinor,
"guest-device-major": major,
"guest-device-minor": minor,
}).Info("updating block device major/minor into the spec")

spec.Linux.Devices[idx].Major = major
spec.Linux.Devices[idx].Minor = minor

// there is no resource to update
if spec.Linux == nil || spec.Linux.Resources == nil {
return nil
}

// Resources must be updated since they are used to identify the
// device in the devices cgroup.
for idxRsrc, dRsrc := range spec.Linux.Resources.Devices {
if dRsrc.Major == hostMajor && dRsrc.Minor == hostMinor {
spec.Linux.Resources.Devices[idxRsrc].Major = major
spec.Linux.Resources.Devices[idxRsrc].Minor = minor
}
}

return nil
}
}
Expand Down
70 changes: 70 additions & 0 deletions device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,73 @@ func TestAddDevice(t *testing.T) {
}
}
}

func TestUpdateSpecDeviceList(t *testing.T) {
assert := assert.New(t)

var err error
spec := &pb.Spec{}
device := pb.Device{}
major := int64(7)
minor := int64(2)

//ContainerPath empty
err = updateSpecDeviceList(device, spec)
assert.Error(err)

device.ContainerPath = "/dev/null"

//Linux is nil
err = updateSpecDeviceList(device, spec)
assert.Error(err)

spec.Linux = &pb.Linux{}

/// Linux.Devices empty
err = updateSpecDeviceList(device, spec)
assert.Error(err)

spec.Linux.Devices = []pb.LinuxDevice{
{
Path: "/dev/null2",
Major: major,
Minor: minor,
},
}

// VmPath empty
err = updateSpecDeviceList(device, spec)
assert.Error(err)

device.VmPath = "/dev/null"

// guest and host path are not the same
err = updateSpecDeviceList(device, spec)
assert.Error(err)

spec.Linux.Devices[0].Path = device.ContainerPath

// spec.Linux.Resources is nil
err = updateSpecDeviceList(device, spec)
assert.NoError(err)

// update both devices and cgroup lists
spec.Linux.Devices = []pb.LinuxDevice{
{
Path: device.ContainerPath,
Major: major,
Minor: minor,
},
}
spec.Linux.Resources = &pb.LinuxResources{
Devices: []pb.LinuxDeviceCgroup{
{
Major: major,
Minor: minor,
},
},
}

err = updateSpecDeviceList(device, spec)
assert.NoError(err)
}

0 comments on commit d1abf91

Please sign in to comment.