virtcontainers/persist: introduce rootless fs driver

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>
This commit is contained in:
Julio Montes 2020-01-31 19:32:01 +00:00
parent 768db1bdc4
commit ea8fb96c3e

View File

@ -0,0 +1,48 @@
// 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
}