From 357a8c3f550a91912332cb41ceac790caf00e9f1 Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Thu, 26 Nov 2020 14:41:07 +0100 Subject: [PATCH] Fix loopback device lookup In case /var/lib/kubelet is a symlink, "losetup -j " will show the device paths with symlinks fully evaluated. Fix the parsing routine and expand symlinks in the path that we want to find in "losetup -j" output. --- .../volumepathhandler/volume_path_handler_linux.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/volume/util/volumepathhandler/volume_path_handler_linux.go b/pkg/volume/util/volumepathhandler/volume_path_handler_linux.go index cf50fb2496c..15e3a10c4e8 100644 --- a/pkg/volume/util/volumepathhandler/volume_path_handler_linux.go +++ b/pkg/volume/util/volumepathhandler/volume_path_handler_linux.go @@ -131,6 +131,11 @@ func parseLosetupOutputForDevice(output []byte, path string) (string, error) { return "", errors.New(ErrDeviceNotFound) } + realPath, err := filepath.EvalSymlinks(path) + if err != nil { + return "", fmt.Errorf("failed to evaluate path %s: %s", path, err) + } + // losetup -j {path} returns device in the format: // /dev/loop1: [0073]:148662 ({path}) // /dev/loop2: [0073]:148662 (/dev/sdX) @@ -143,6 +148,12 @@ func parseLosetupOutputForDevice(output []byte, path string) (string, error) { var matched string scanner := bufio.NewScanner(strings.NewReader(s)) for scanner.Scan() { + // losetup output has symlinks expanded + if strings.HasSuffix(scanner.Text(), "("+realPath+")") { + matched = scanner.Text() + break + } + // Just in case losetup changes, check for the original path too if strings.HasSuffix(scanner.Text(), "("+path+")") { matched = scanner.Text() break