Merge pull request #524 from AkihiroSuda/skip-rootless-if-unsupported

integration: skip rootless if unsupported
docker-18.09
Tõnis Tiigi 2018-07-20 13:40:13 -07:00 committed by GitHub
commit 962feb48e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 2 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/google/shlex" "github.com/google/shlex"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus"
) )
func init() { func init() {
@ -23,9 +24,11 @@ func init() {
if s := os.Getenv("BUILDKIT_INTEGRATION_ROOTLESS_IDPAIR"); s != "" { if s := os.Getenv("BUILDKIT_INTEGRATION_ROOTLESS_IDPAIR"); s != "" {
var uid, gid int var uid, gid int
if _, err := fmt.Sscanf(s, "%d:%d", &uid, &gid); err != nil { if _, err := fmt.Sscanf(s, "%d:%d", &uid, &gid); err != nil {
panic(errors.Errorf("unexpected BUILDKIT_INTEGRATION_ROOTLESS_IDPAIR: %q", s)) logrus.Fatalf("unexpected BUILDKIT_INTEGRATION_ROOTLESS_IDPAIR: %q", s)
}
if rootlessSupported(uid) {
register(&oci{uid: uid, gid: gid})
} }
register(&oci{uid: uid, gid: gid})
} }
} }
@ -155,3 +158,13 @@ func runBuildkitd(args []string, logs map[string]*bytes.Buffer, uid, gid int) (a
return return
} }
func rootlessSupported(uid int) bool {
cmd := exec.Command("sudo", "-u", fmt.Sprintf("#%d", uid), "-i", "--", "unshare", "-U", "true")
b, err := cmd.CombinedOutput()
if err != nil {
logrus.Warnf("rootless mode is not supported on this host: %v (%s)", err, string(b))
return false
}
return true
}