Skip to content

Commit

Permalink
Check the validity of the platforms
Browse files Browse the repository at this point in the history
platforms can still be added but some warning message
will be emitted if the platform cannot pass the validity
check.

Signed-off-by: Dave Chen <dave.chen@arm.com>
  • Loading branch information
chendave committed Mar 19, 2019
1 parent c354108 commit 6347a70
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 26 deletions.
4 changes: 4 additions & 0 deletions cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,13 @@ func setDefaultConfig(cfg *config.Config) {

if cfg.Workers.OCI.Platforms == nil {
cfg.Workers.OCI.Platforms = binfmt_misc.SupportedPlatforms()
} else {
binfmt_misc.ValidatePlatforms(cfg.Workers.OCI.Platforms)
}
if cfg.Workers.Containerd.Platforms == nil {
cfg.Workers.Containerd.Platforms = binfmt_misc.SupportedPlatforms()
} else {
binfmt_misc.ValidatePlatforms(cfg.Workers.Containerd.Platforms)
}

if system.RunningInUserNS() {
Expand Down
2 changes: 2 additions & 0 deletions cmd/buildkitd/main_containerd_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

ctd "github.com/containerd/containerd"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/util/binfmt_misc"
"github.com/moby/buildkit/worker"
"github.com/moby/buildkit/worker/base"
"github.com/moby/buildkit/worker/containerd"
Expand Down Expand Up @@ -142,6 +143,7 @@ func applyContainerdFlags(c *cli.Context, cfg *config.Config) error {
}

if platforms := c.GlobalStringSlice("containerd-worker-platform"); len(platforms) != 0 {
binfmt_misc.ValidatePlatforms(platforms)
cfg.Workers.Containerd.Platforms = platforms
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/buildkitd/main_oci_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/containerd/containerd/snapshots/overlay"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/executor/oci"
"github.com/moby/buildkit/util/binfmt_misc"
"github.com/moby/buildkit/worker"
"github.com/moby/buildkit/worker/base"
"github.com/moby/buildkit/worker/runc"
Expand Down Expand Up @@ -142,6 +143,7 @@ func applyOCIFlags(c *cli.Context, cfg *config.Config) error {
}

if platforms := c.GlobalStringSlice("oci-worker-platform"); len(platforms) != 0 {
binfmt_misc.ValidatePlatforms(platforms)
cfg.Workers.OCI.Platforms = platforms
}

Expand Down
4 changes: 2 additions & 2 deletions util/binfmt_misc/amd64_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func amd64Supported() bool {
return check(Binaryamd64) == nil
func amd64Supported() (string, error) {
return check(Binaryamd64)
}
4 changes: 2 additions & 2 deletions util/binfmt_misc/amd64_check_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func amd64Supported() bool {
return true
func amd64Supported() (string, error) {
return "", nil
}
4 changes: 2 additions & 2 deletions util/binfmt_misc/arm64_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func arm64Supported() bool {
return check(Binaryarm64) == nil
func arm64Supported() (string, error) {
return check(Binaryarm64)
}
4 changes: 2 additions & 2 deletions util/binfmt_misc/arm64_check_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func arm64Supported() bool {
return true
func arm64Supported() (string, error) {
return "", nil
}
4 changes: 2 additions & 2 deletions util/binfmt_misc/arm_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func armSupported() bool {
return check(Binaryarm) == nil
func armSupported() (string, error) {
return check(Binaryarm)
}
4 changes: 2 additions & 2 deletions util/binfmt_misc/arm_check_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func armSupported() bool {
return true
func armSupported() (string, error) {
return "", nil
}
18 changes: 12 additions & 6 deletions util/binfmt_misc/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,36 @@ import (
"path/filepath"
)

func check(bin string) error {
func check(bin string) (string, error) {
tmpdir, err := ioutil.TempDir("", "qemu-check")
if err != nil {
return err
return "", err
}
defer os.RemoveAll(tmpdir)
pp := filepath.Join(tmpdir, "check")

r, err := gzip.NewReader(bytes.NewReader([]byte(bin)))
if err != nil {
return err
return "", err
}
defer r.Close()

f, err := os.OpenFile(pp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
if err != nil {
return err
return "", err
}

if _, err := io.Copy(f, r); err != nil {
f.Close()
return err
return "", err
}
f.Close()

return exec.Command(pp).Run()
output, err := exec.Command(pp).CombinedOutput()
if err == nil {
cmd := exec.Command("chroot", ".", "/check")
cmd.Dir = tmpdir
output, err = cmd.CombinedOutput()
}
return string(output), err
}
66 changes: 58 additions & 8 deletions util/binfmt_misc/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"

"github.com/containerd/containerd/platforms"
"github.com/sirupsen/logrus"
)

var once sync.Once
Expand All @@ -14,19 +15,68 @@ func SupportedPlatforms() []string {
once.Do(func() {
def := platforms.DefaultString()
arr = append(arr, def)

if p := "linux/amd64"; def != p && amd64Supported() {
arr = append(arr, p)
if p := "linux/amd64"; def != p {
_, err := amd64Supported()
if err == nil {
arr = append(arr, p)
}
}
if p := "linux/arm64"; def != p && arm64Supported() {
arr = append(arr, p)
if p := "linux/arm64"; def != p {
_, err := arm64Supported()
if err == nil {
arr = append(arr, p)
}
}
if !strings.HasPrefix(def, "linux/arm/") && armSupported() {
arr = append(arr, "linux/arm/v7", "linux/arm/v6")
if !strings.HasPrefix(def, "linux/arm/") {
_, err := armSupported()
if err == nil {
arr = append(arr, "linux/arm/v7", "linux/arm/v6")
}
} else if def == "linux/arm/v7" {
arr = append(arr, "linux/arm/v6")
}
})

return arr
}

//Here, the func just validate the platforms and show warning message if there is,
//the end user could fix the issue based on those warning, and thus no need to drop
//the platfrom from the candidates.
func ValidatePlatforms(pfs []string) {
def := platforms.DefaultString()
for _, p := range pfs {
if p != def {
if p == "linux/amd64" {
output, err := amd64Supported()
if err != nil {
printErr(p, output, err)
}
}
if p == "linux/arm64" {
output, err := arm64Supported()
if err != nil {
printErr(p, output, err)
}
}
if strings.HasPrefix(p, "linux/arm/v6") || strings.HasPrefix(p, "linux/arm/v7") {
output, err := armSupported()
if err != nil {
printErr(p, output, err)
}
}
}
}
}

//Only show the warning when the platforms are maually specified, show the message for the platforms
//finding provided by `SupportedPlatforms` is a little annoying, no platform specifed but the warning
//message is shown might confuse end users.
func printErr(p string, output string, err error) {
if strings.Contains(err.Error(), "exec format error") {
logrus.Warnf("platform %s cannot pass the validation, kernel support for miscellaneous bianry may have not enabled.", p)
} else if strings.Contains(output, "No such file or director") {
logrus.Warnf("platforms %s cannot pass the validation, '-F' flag might have not set for 'binfmt_misc'.", p)
} else {
logrus.Warnf("platforms %s cannot pass the validation: %s", p, err.Error())
}
}

0 comments on commit 6347a70

Please sign in to comment.