Skip to content

Commit df3d1e6

Browse files
committed
tpm: Extend device config to support different device name inside container
Extend the device config to support a different device name for a device inside the container than on the host. Use this to for example create /dev/tpm0 inside the container for a /dev/tpm10 on the host. Signed-off-by: Stefan Berger <[email protected]>
1 parent 9d9b3df commit df3d1e6

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

libcontainer/configs/device.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type Device struct {
1818
// Path to the device.
1919
Path string `json:"path"`
2020

21+
// the name of the device inside the container (optional)
22+
Devpath string `json:"devpath"`
23+
2124
// Major is the device's major number.
2225
Major int64 `json:"major"`
2326

libcontainer/rootfs_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,12 @@ func bindMountDeviceNode(dest string, node *configs.Device) error {
488488

489489
// Creates the device node in the rootfs of the container.
490490
func createDeviceNode(rootfs string, node *configs.Device, bind bool) error {
491-
dest := filepath.Join(rootfs, node.Path)
491+
var dest string
492+
if node.Devpath != "" {
493+
dest = filepath.Join(rootfs, node.Devpath)
494+
} else {
495+
dest = filepath.Join(rootfs, node.Path)
496+
}
492497
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
493498
return err
494499
}

libcontainer/vtpm/vtpm-helper/vtpm_helper.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/opencontainers/runtime-spec/specs-go"
1515
)
1616

17-
func addVTPMDevice(spec *specs.Spec, config *configs.Config, hostpath string, major, minor uint32) {
17+
func addVTPMDevice(spec *specs.Spec, config *configs.Config, hostpath, devpath string, major, minor uint32) {
1818
device := &configs.Device{
1919
Type: 'c',
2020
Path: hostpath,
21+
Devpath: devpath,
2122
Major: int64(major),
2223
Minor: int64(minor),
2324
Permissions: "rwm",
@@ -62,7 +63,8 @@ func CreateVTPM(spec *specs.Spec, config *configs.Config, vtpmdev *specs.VTPM, d
6263
hostdev := vtpm.GetTPMDevname()
6364
major, minor := vtpm.GetMajorMinor()
6465

65-
addVTPMDevice(spec, config, hostdev, major, minor)
66+
devpath := fmt.Sprintf("/dev/tpm%d", devnum)
67+
addVTPMDevice(spec, config, hostdev, devpath, major, minor)
6668

6769
config.VTPMs = append(config.VTPMs, vtpm)
6870

@@ -79,7 +81,9 @@ func CreateVTPM(spec *specs.Spec, config *configs.Config, vtpmdev *specs.VTPM, d
7981
if fileInfo, err := os.Lstat(host_tpmrm); err == nil {
8082
if stat_t, ok := fileInfo.Sys().(*syscall.Stat_t); ok {
8183
devNumber := int(stat_t.Rdev)
82-
addVTPMDevice(spec, config, host_tpmrm, uint32(devices.Major(devNumber)), uint32(devices.Minor(devNumber)))
84+
85+
devpath = fmt.Sprintf("/dev/tpmrm%d", devnum)
86+
addVTPMDevice(spec, config, host_tpmrm, devpath, uint32(devices.Major(devNumber)), uint32(devices.Minor(devNumber)))
8387
}
8488
if uid != 0 {
8589
// adapt ownership of the device since only root can access it

0 commit comments

Comments
 (0)