Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add "exclude" option to --install #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ Prints similar to:
```bash
docker run --privileged --rm tonistiigi/binfmt --install all
docker run --privileged --rm tonistiigi/binfmt --install arm64,riscv64,arm

# install all architectures, except for riscv64
docker run --privileged --rm tonistiigi/binfmt --install all,-riscv64
```

## Uninstalling emulators
Expand Down
48 changes: 48 additions & 0 deletions cmd/binfmt/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import "github.com/containerd/containerd/platforms"

func newList() list {
return list{
included: map[string]bool{},
}
}

type list struct {
included map[string]bool
items []string
}

func (a *list) add(platform ...string) {
for _, v := range platform {
p := getArch(v)
if a.included[p] {
continue
}
a.items = append(a.items, p)
a.included[p] = true
}
}

// exclude the given platform.
func (a *list) exclude(platform string) {
if p := getArch(platform); a.included[p] {
b := a.items[:0]
for _, v := range a.items {
if v != p {
b = append(b, v)
}
}
a.items = b
} else {
a.included[p] = true
}
}

func getArch(platform string) string {
p, err := platforms.Parse(platform)
if err != nil {
return platform
}
return p.Architecture
}
24 changes: 24 additions & 0 deletions cmd/binfmt/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"testing"
)

func TestList(t *testing.T) {
all := []string{"amd64", "arm64", "s390x"}
allItems := len(all)

a := newList()
a.add(all...)
if len(a.items) != allItems {
t.Errorf("expected: %d, got: %d: %v", allItems, len(a.items), a.items)
}
a.add("amd64", "amd64")
if len(a.items) != allItems {
t.Errorf("expected: %d, got: %d: %v", allItems, len(a.items), a.items)
}
a.exclude("amd64")
if len(a.items) != allItems-1 {
t.Errorf("expected: %d, got: %d: %v", allItems-1, len(a.items), a.items)
}
}
30 changes: 14 additions & 16 deletions cmd/binfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (

func init() {
flag.StringVar(&mount, "mount", "/proc/sys/fs/binfmt_misc", "binfmt_misc mount point")
flag.StringVar(&toInstall, "install", "", "architectures to install")
flag.StringVar(&toInstall, "install", "", `architectures to install or exclude (prefix with "-" to exclude)`)
flag.StringVar(&toUninstall, "uninstall", "", "architectures to uninstall")
flag.BoolVar(&flVersion, "version", false, "display version")
}
Expand Down Expand Up @@ -145,19 +145,24 @@ func formatPlatforms(p []ocispecs.Platform) []string {
return str
}

func parseArch(in string) (out []string) {
func parseInstall(in string) (out []string) {
if in == "" {
return
}
archList := newList()
for _, v := range strings.Split(in, ",") {
p, err := platforms.Parse(v)
if err != nil {
out = append(out, v)
} else {
out = append(out, p.Architecture)
if v == "" {
continue
}
if v[0] == '-' {
archList.exclude(v[1:])
continue
}
if v == "all" {
archList.add(allArch()...)
}
}
return
return archList.items
}

func parseUninstall(in string) (out []string) {
Expand Down Expand Up @@ -211,14 +216,7 @@ func run() error {
}
}

var installArchs []string
if toInstall == "all" {
installArchs = allArch()
} else {
installArchs = parseArch(toInstall)
}

for _, name := range installArchs {
for _, name := range parseInstall(toInstall) {
err := install(name)
if err == nil {
log.Printf("installing: %s OK", name)
Expand Down
Loading