Handle device symlink.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2017-08-07 22:25:26 +00:00
parent be54e9a6c0
commit 4c5cea9258
2 changed files with 23 additions and 1 deletions

View File

@ -341,6 +341,10 @@ func addOCIDevices(g *generate.Generator, devs []*runtime.Device, privileged boo
UID: &hostDevice.Uid, UID: &hostDevice.Uid,
GID: &hostDevice.Gid, GID: &hostDevice.Gid,
} }
if hostDevice.Major == 0 && hostDevice.Minor == 0 {
// Invalid device, most likely a symbolic link, skip it.
continue
}
g.AddDevice(rd) g.AddDevice(rd)
} }
spec.Linux.Resources.Devices = []runtimespec.LinuxDeviceCgroup{ spec.Linux.Resources.Devices = []runtimespec.LinuxDeviceCgroup{
@ -352,7 +356,11 @@ func addOCIDevices(g *generate.Generator, devs []*runtime.Device, privileged boo
return nil return nil
} }
for _, device := range devs { for _, device := range devs {
dev, err := devices.DeviceFromPath(device.HostPath, device.Permissions) path, err := resolveSymbolicLink(device.HostPath)
if err != nil {
return err
}
dev, err := devices.DeviceFromPath(path, device.Permissions)
if err != nil { if err != nil {
return err return err
} }

View File

@ -20,6 +20,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -402,3 +403,16 @@ func (c *criContainerdService) ensureImageExists(ctx context.Context, ref string
} }
return &newImage, nil return &newImage, nil
} }
// resolveSymbolicLink resolves a possbile symlink path. If the path is a symlink, returns resolved
// path; if not, returns the original path.
func resolveSymbolicLink(path string) (string, error) {
info, err := os.Lstat(path)
if err != nil {
return "", err
}
if info.Mode()&os.ModeSymlink != os.ModeSymlink {
return path, nil
}
return filepath.EvalSymlinks(path)
}