mirror of
https://github.com/aljazceru/kata-containers.git
synced 2026-01-07 08:24:23 +01:00
Rootless fs driver inherits from FS and may overwrite its methods. All files and directories created by this driver are under a path accessible for the current user, typically this path is defined by the environment variable `XDG_RUNTIME_DIR`, if this variable is not defined, the default path `/run/user/$UID` is used instead, where $UID is the current user ID. fixes #2416 Signed-off-by: Julio Montes <julio.montes@intel.com>
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
// Copyright (c) 2020 Intel Corporation
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package fs
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
|
|
)
|
|
|
|
// default xdg runtime directory just in case XDG_RUNTIME_DIR is not set
|
|
var defaultXdgRuntimeDir = fmt.Sprintf("/run/user/%d", os.Getuid())
|
|
|
|
type RootlessFS struct {
|
|
// inherit from FS. Overwrite if needed.
|
|
*FS
|
|
}
|
|
|
|
func RootlessInit() (persistapi.PersistDriver, error) {
|
|
driver, err := Init()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Could not create Rootless FS driver: %v", err)
|
|
}
|
|
|
|
fsDriver, ok := driver.(*FS)
|
|
if !ok {
|
|
return nil, fmt.Errorf("Could not create Rootless FS driver")
|
|
}
|
|
|
|
// XDG_RUNTIME_DIR defines the base directory relative to
|
|
// which user-specific non-essential runtime files are stored.
|
|
rootlessDir := os.Getenv("XDG_RUNTIME_DIR")
|
|
if rootlessDir == "" {
|
|
rootlessDir = defaultXdgRuntimeDir
|
|
fsLog.WithField("default-runtime-dir", defaultXdgRuntimeDir).
|
|
Warnf("XDG_RUNTIME_DIR variable is not set. Using default runtime directory")
|
|
}
|
|
|
|
fsDriver.storageRootPath = filepath.Join(rootlessDir, fsDriver.storageRootPath)
|
|
fsDriver.driverName = "rootlessfs"
|
|
|
|
return &RootlessFS{fsDriver}, nil
|
|
}
|