k8s.io/kubernetes@v1.29.3/test/e2e_node/util_xfs_linux.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5  Copyright 2016 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package e2enode
    21  
    22  import (
    23  	"path/filepath"
    24  	"syscall"
    25  
    26  	"k8s.io/mount-utils"
    27  )
    28  
    29  func detectMountpoint(m mount.Interface, path string) string {
    30  	path, err := filepath.Abs(path)
    31  	if err == nil {
    32  		path, err = filepath.EvalSymlinks(path)
    33  	}
    34  	if err != nil {
    35  		return ""
    36  	}
    37  	for path != "" && path != "/" {
    38  		isNotMount, err := m.IsLikelyNotMountPoint(path)
    39  		if err != nil {
    40  			return ""
    41  		}
    42  		if !isNotMount {
    43  			return path
    44  		}
    45  		path = filepath.Dir(path)
    46  	}
    47  	return "/"
    48  }
    49  
    50  const (
    51  	xfsMagic = 0x58465342
    52  )
    53  
    54  // XFS over-allocates and then eventually removes that excess allocation.
    55  // That can lead to a file growing beyond its eventual size, causing
    56  // an unnecessary eviction:
    57  //
    58  // % ls -ls
    59  // total 32704
    60  // 32704 -rw-r--r-- 1 rkrawitz rkrawitz 20971520 Jan 15 13:16 foo.bin
    61  //
    62  // This issue can be hit regardless of the means used to count storage.
    63  // It is not present in ext4fs.
    64  func isXfs(dir string) bool {
    65  	mountpoint := detectMountpoint(mount.New(""), dir)
    66  	if mountpoint == "" {
    67  		return false
    68  	}
    69  	var buf syscall.Statfs_t
    70  	err := syscall.Statfs(mountpoint, &buf)
    71  	if err != nil {
    72  		return false
    73  	}
    74  	return buf.Type == xfsMagic
    75  }