Merge pull request #4230 from thaJeztah/remove_libcontainer_system

Remove remaining uses of libcontainer/system package
This commit is contained in:
Phil Estes 2020-05-04 15:18:57 -04:00 committed by GitHub
commit b1f514641f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 18 deletions

View File

@ -22,12 +22,11 @@ import (
"archive/tar" "archive/tar"
"os" "os"
"strings" "strings"
"sync"
"syscall" "syscall"
"github.com/containerd/containerd/sys"
"github.com/containerd/continuity/fs" "github.com/containerd/continuity/fs"
"github.com/containerd/continuity/sysx" "github.com/containerd/continuity/sysx"
"github.com/opencontainers/runc/libcontainer/system"
"github.com/pkg/errors" "github.com/pkg/errors"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -84,21 +83,11 @@ func mkdir(path string, perm os.FileMode) error {
return os.Chmod(path, perm) return os.Chmod(path, perm)
} }
var (
inUserNS bool
nsOnce sync.Once
)
func setInUserNS() {
inUserNS = system.RunningInUserNS()
}
func skipFile(hdr *tar.Header) bool { func skipFile(hdr *tar.Header) bool {
switch hdr.Typeflag { switch hdr.Typeflag {
case tar.TypeBlock, tar.TypeChar: case tar.TypeBlock, tar.TypeChar:
// cannot create a device if running in user namespace // cannot create a device if running in user namespace
nsOnce.Do(setInUserNS) return sys.RunningInUserNS()
return inUserNS
default: default:
return false return false
} }

View File

@ -26,7 +26,7 @@ import (
"github.com/containerd/containerd/archive" "github.com/containerd/containerd/archive"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/mount" "github.com/containerd/containerd/mount"
"github.com/opencontainers/runc/libcontainer/system" "github.com/containerd/containerd/sys"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -35,7 +35,7 @@ func apply(ctx context.Context, mounts []mount.Mount, r io.Reader) error {
case len(mounts) == 1 && mounts[0].Type == "overlay": case len(mounts) == 1 && mounts[0].Type == "overlay":
// OverlayConvertWhiteout (mknod c 0 0) doesn't work in userns. // OverlayConvertWhiteout (mknod c 0 0) doesn't work in userns.
// https://github.com/containerd/containerd/issues/3762 // https://github.com/containerd/containerd/issues/3762
if system.RunningInUserNS() { if sys.RunningInUserNS() {
break break
} }
path, parents, err := getOverlayPath(mounts[0].Options) path, parents, err := getOverlayPath(mounts[0].Options)

View File

@ -24,8 +24,6 @@ import (
"os" "os"
"strconv" "strconv"
"strings" "strings"
"github.com/opencontainers/runc/libcontainer/system"
) )
// OOMScoreMaxKillable is the maximum score keeping the process killable by the oom killer // OOMScoreMaxKillable is the maximum score keeping the process killable by the oom killer
@ -40,7 +38,7 @@ func SetOOMScore(pid, score int) error {
} }
defer f.Close() defer f.Close()
if _, err = f.WriteString(strconv.Itoa(score)); err != nil { if _, err = f.WriteString(strconv.Itoa(score)); err != nil {
if os.IsPermission(err) && (system.RunningInUserNS() || RunningUnprivileged()) { if os.IsPermission(err) && (RunningInUserNS() || RunningUnprivileged()) {
return nil return nil
} }
return err return err

62
sys/userns_linux.go Normal file
View File

@ -0,0 +1,62 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sys
import (
"bufio"
"fmt"
"os"
"sync"
)
var (
inUserNS bool
nsOnce sync.Once
)
// RunningInUserNS detects whether we are currently running in a user namespace.
// Originally copied from github.com/lxc/lxd/shared/util.go
func RunningInUserNS() bool {
nsOnce.Do(func() {
file, err := os.Open("/proc/self/uid_map")
if err != nil {
// This kernel-provided file only exists if user namespaces are supported
return
}
defer file.Close()
buf := bufio.NewReader(file)
l, _, err := buf.ReadLine()
if err != nil {
return
}
line := string(l)
var a, b, c int64
fmt.Sscanf(line, "%d %d %d", &a, &b, &c)
/*
* We assume we are in the initial user namespace if we have a full
* range - 4294967295 uids starting at uid 0.
*/
if a == 0 && b == 0 && c == 4294967295 {
return
}
inUserNS = true
})
return inUserNS
}

25
sys/userns_unsupported.go Normal file
View File

@ -0,0 +1,25 @@
// +build !linux
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sys
// RunningInUserNS is a stub for non-Linux systems
// Always returns false
func RunningInUserNS() bool {
return false
}