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

agent: update resources list with the right device major-minor number #352

Merged
merged 1 commit into from
Sep 5, 2018
Merged
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Thanks @devimc! 😄

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)
}