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

Commit

Permalink
cli: implement --rootless option
Browse files Browse the repository at this point in the history
By default virtcontainer auto-detects if the current process is running
rootless or not, but this behavior can change from commandline with the
--rootless option

fixes #2417

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Feb 12, 2020
1 parent 11bd456 commit c36c667
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
18 changes: 18 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ var runtimeFlags = []cli.Flag{
Value: defaultRootDirectory,
Usage: "root directory for storage of container state (this should be located in tmpfs)",
},
cli.StringFlag{
Name: "rootless",
Value: "auto",
Usage: "ignore cgroup permission errors ('true', 'false', or 'auto')",
},
cli.BoolFlag{
Name: showConfigPathsOption,
Usage: "show config file paths that will be checked for (in order)",
Expand Down Expand Up @@ -266,6 +271,19 @@ func beforeSubcommands(c *cli.Context) error {
return nil
}

r, err := parseBoolOrAuto(c.GlobalString("rootless"))
if err != nil {
return err
}
// If flag is true/false, assign the rootless flag.
// vc will not perform any auto-detection in that case.
// In case flag is nil or auto, vc detects if the runtime is running as rootless.
if r != nil {
rootless.SetRootless(*r)
}
// Support --systed-cgroup
// Issue: https://github.com/kata-containers/runtime/issues/2428

ignoreConfigLogs := false
var traceRootSpan string

Expand Down
12 changes: 12 additions & 0 deletions cli/utils.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) 2014 Docker, Inc.
// Copyright (c) 2017 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
Expand All @@ -8,6 +9,7 @@ package main
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/kata-containers/runtime/pkg/katautils"
Expand Down Expand Up @@ -131,3 +133,13 @@ func genericGetCPUDetails() (vendor, model string, err error) {

return vendor, model, nil
}

// from runC
// parseBoolOrAuto returns (nil, nil) if s is empty or "auto"
func parseBoolOrAuto(s string) (*bool, error) {
if s == "" || strings.ToLower(s) == "auto" {
return nil, nil
}
b, err := strconv.ParseBool(s)
return &b, err
}
21 changes: 11 additions & 10 deletions virtcontainers/pkg/rootless/rootless.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ import (
)

var (
// initRootless states whether the isRootless variable
// has been set yet
initRootless bool

// isRootless states whether execution is rootless or not
isRootless bool
// If nil, rootless is auto-detected
isRootless *bool

// lock for the initRootless and isRootless variables
rLock sync.Mutex
Expand All @@ -58,6 +55,10 @@ var (
IsRootless = isRootlessFunc
)

func SetRootless(rootless bool) {
isRootless = &rootless
}

// SetLogger sets up a logger for the rootless pkg
func SetLogger(ctx context.Context, logger *logrus.Entry) {
fields := rootlessLog.Data
Expand All @@ -68,9 +69,9 @@ func SetLogger(ctx context.Context, logger *logrus.Entry) {
func isRootlessFunc() bool {
rLock.Lock()
defer rLock.Unlock()
if !initRootless {
initRootless = true
isRootless = true
// auto-detect if nil
if isRootless == nil {
SetRootless(true)
// --rootless and --systemd-cgroup options must honoured
// but with the current implementation this is not possible
// https://github.com/kata-containers/runtime/issues/2412
Expand All @@ -80,9 +81,9 @@ func isRootlessFunc() bool {
if system.RunningInUserNS() {
return true
}
isRootless = false
SetRootless(false)
}
return isRootless
return *isRootless
}

// GetRootlessDir returns the path to the location for rootless
Expand Down
32 changes: 31 additions & 1 deletion virtcontainers/pkg/rootless/rootless_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
// Copyright (c) 2019 Intel Corporation
// Copyright (c) 2020 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

package rootless

import (
"os"
"testing"

"github.com/opencontainers/runc/libcontainer/system"
"github.com/stretchr/testify/assert"
)

func TestIsRootless(t *testing.T) {
assert := assert.New(t)
isRootless = nil

var rootless bool
if os.Getuid() != 0 {
rootless = true
} else {
rootless = system.RunningInUserNS()
}

assert.Equal(rootless, isRootlessFunc())

SetRootless(true)
assert.True(isRootlessFunc())

SetRootless(false)
assert.False(isRootlessFunc())

isRootless = nil
}

0 comments on commit c36c667

Please sign in to comment.