github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/persist/fs/rootlessfs.go (about)

     1  // Copyright (c) 2020 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package fs
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
    14  )
    15  
    16  // default xdg runtime directory just in case XDG_RUNTIME_DIR is not set
    17  var defaultXdgRuntimeDir = fmt.Sprintf("/run/user/%d", os.Getuid())
    18  
    19  type RootlessFS struct {
    20  	// inherit from FS. Overwrite if needed.
    21  	*FS
    22  }
    23  
    24  func RootlessInit() (persistapi.PersistDriver, error) {
    25  	driver, err := Init()
    26  	if err != nil {
    27  		return nil, fmt.Errorf("Could not create Rootless FS driver: %v", err)
    28  	}
    29  
    30  	fsDriver, ok := driver.(*FS)
    31  	if !ok {
    32  		return nil, fmt.Errorf("Could not create Rootless FS driver")
    33  	}
    34  
    35  	// XDG_RUNTIME_DIR defines the base directory relative to
    36  	// which user-specific non-essential runtime files are stored.
    37  	rootlessDir := os.Getenv("XDG_RUNTIME_DIR")
    38  	if rootlessDir == "" {
    39  		rootlessDir = defaultXdgRuntimeDir
    40  		fsLog.WithField("default-runtime-dir", defaultXdgRuntimeDir).
    41  			Warnf("XDG_RUNTIME_DIR variable is not set. Using default runtime directory")
    42  	}
    43  
    44  	fsDriver.storageRootPath = filepath.Join(rootlessDir, fsDriver.storageRootPath)
    45  	fsDriver.driverName = "rootlessfs"
    46  
    47  	return &RootlessFS{fsDriver}, nil
    48  }