Use device number to find uuid

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2017-10-09 06:10:43 +00:00
parent 9135ef2c07
commit 6cb3d27ed3
3 changed files with 12 additions and 14 deletions

View File

@ -45,7 +45,7 @@ type OS interface {
Unmount(target string, flags int) error
GetMounts() ([]*mount.Info, error)
LookupMount(path string) (containerdmount.Info, error)
DeviceUUID(device string) (string, error)
DeviceUUID(device uint64) (string, error)
}
// RealOS is used to dispatch the real system level operations.
@ -144,14 +144,9 @@ func blkrdev(device string) (uint64, error) {
return stat.Rdev, nil
}
// DeviceUUID gets device uuid of a device. The passed in device should be
// an absolute path of the device.
func (RealOS) DeviceUUID(device string) (string, error) {
rdev, err := blkrdev(device)
if err != nil {
return "", err
}
// DeviceUUID gets device uuid of a device. The passed in rdev should be
// linux device number.
func (RealOS) DeviceUUID(rdev uint64) (string, error) {
const uuidDir = "/dev/disk/by-uuid"
files, err := ioutil.ReadDir(uuidDir)
if err != nil {
@ -169,5 +164,5 @@ func (RealOS) DeviceUUID(device string) (string, error) {
return file.Name(), nil
}
}
return "", fmt.Errorf("device %q not found", device)
return "", fmt.Errorf("device %d not found", rdev)
}

View File

@ -52,7 +52,7 @@ type FakeOS struct {
UnmountFn func(target string, flags int) error
GetMountsFn func() ([]*mount.Info, error)
LookupMountFn func(path string) (containerdmount.Info, error)
DeviceUUIDFn func(device string) (string, error)
DeviceUUIDFn func(device uint64) (string, error)
calls []CalledDetail
errors map[string]error
}
@ -258,7 +258,7 @@ func (f *FakeOS) LookupMount(path string) (containerdmount.Info, error) {
}
// DeviceUUID is a fake call that invodes DeviceUUIDFn or just return nil.
func (f *FakeOS) DeviceUUID(device string) (string, error) {
func (f *FakeOS) DeviceUUID(device uint64) (string, error) {
f.appendCalls("DeviceUUID", device)
if err := f.getError("DeviceUUID"); err != nil {
return "", err

View File

@ -34,6 +34,7 @@ import (
runcapparmor "github.com/opencontainers/runc/libcontainer/apparmor"
runcseccomp "github.com/opencontainers/runc/libcontainer/seccomp"
"golang.org/x/net/context"
"golang.org/x/sys/unix"
"google.golang.org/grpc"
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
"k8s.io/kubernetes/pkg/kubelet/server/streaming"
@ -143,6 +144,7 @@ func NewCRIContainerdService(config options.Config) (CRIContainerdService, error
if err != nil {
return nil, fmt.Errorf("failed to get imagefs uuid of %q: %v", imageFSPath, err)
}
glog.V(2).Infof("Get device uuid %q for image filesystem %q", c.imageFSUUID, imageFSPath)
c.netPlugin, err = ocicni.InitCNI(config.NetworkPluginConfDir, config.NetworkPluginBinDir)
if err != nil {
@ -243,11 +245,12 @@ func (c *criContainerdService) Stop() {
// getDeviceUUID gets device uuid for a given path.
func (c *criContainerdService) getDeviceUUID(path string) (string, error) {
info, err := c.os.LookupMount(path)
mount, err := c.os.LookupMount(path)
if err != nil {
return "", err
}
return c.os.DeviceUUID(info.Source)
rdev := unix.Mkdev(uint32(mount.Major), uint32(mount.Minor))
return c.os.DeviceUUID(rdev)
}
// imageFSPath returns containerd image filesystem path.