github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/fs/fs_linux.go (about)

     1  // Package fs provides mountpath and FQN abstractions and methods to resolve/map stored content
     2  /*
     3   * Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package fs
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"os/exec"
    11  	"strings"
    12  	"syscall"
    13  )
    14  
    15  func (mi *Mountpath) resolveFS() error {
    16  	var fsStats syscall.Statfs_t
    17  	if err := syscall.Statfs(mi.Path, &fsStats); err != nil {
    18  		return fmt.Errorf("cannot statfs fspath %q, err: %w", mi.Path, err)
    19  	}
    20  	fs, fsType, err := fqn2FsInfo(mi.Path)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	mi.Fs = fs
    26  	mi.FsType = fsType
    27  	mi.FsID = fsStats.Fsid.X__val
    28  	return nil
    29  }
    30  
    31  // fqn2FsInfo is used only at startup to store file systems for each mountpath.
    32  func fqn2FsInfo(fqn string) (fs, fsType string, err error) {
    33  	getFSCommand := fmt.Sprintf("df -PT '%s' | awk 'END{print $1,$2}'", fqn)
    34  	outputBytes, err := exec.Command("sh", "-c", getFSCommand).Output()
    35  	if err != nil || len(outputBytes) == 0 {
    36  		return "", "", fmt.Errorf("failed to retrieve FS info from path %q, err: %v", fqn, err)
    37  	}
    38  	info := strings.Split(string(outputBytes), " ")
    39  	if len(info) != 2 {
    40  		return "", "", fmt.Errorf("failed to retrieve FS info from path %q, err: invalid format", fqn)
    41  	}
    42  	return strings.TrimSpace(info[0]), strings.TrimSpace(info[1]), nil
    43  }
    44  
    45  // DirectOpen opens a file with direct disk access (with OS caching disabled).
    46  func DirectOpen(path string, flag int, perm os.FileMode) (*os.File, error) {
    47  	return os.OpenFile(path, syscall.O_DIRECT|flag, perm)
    48  }