Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers/persist: introduce rootless fs driver
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Julio Montes committed Feb 12, 2020
1 parent 768db1b commit ea8fb96
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions virtcontainers/persist/fs/rootlessfs.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ea8fb96

Please sign in to comment.