Skip to content

Commit

Permalink
UPSTREAM: 96889: Fix loopback device lookup
Browse files Browse the repository at this point in the history
In case /var/lib/kubelet is a symlink, "losetup -j <device in
/var/lib/kubelet>" 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.
  • Loading branch information
jsafrane authored and soltysh committed Sep 8, 2021
1 parent 20854c0 commit 546fbf3
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/volume/util/volumepathhandler/volume_path_handler_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 546fbf3

Please sign in to comment.