Skip to content

Commit

Permalink
Display version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Apr 30, 2021
1 parent 9a44d27 commit 7580efb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.git
Dockerfile
README.md
docker-bake.hcl
Expand Down
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ FROM --platform=$BUILDPLATFORM golang:1.15-alpine AS binfmt
COPY --from=xgo / /
ENV CGO_ENABLED=0
ARG TARGETPLATFORM
ARG QEMU_VERSION
WORKDIR /src
RUN apk add --no-cache git
RUN --mount=target=. \
TARGETPLATFORM=$TARGETPLATFORM go build -o /go/bin/binfmt ./cmd/binfmt
TARGETPLATFORM=$TARGETPLATFORM go build \
-ldflags "-X main.revision=$(git rev-parse --short HEAD) -X main.qemuVersion=${QEMU_VERSION}" \
-o /go/bin/binfmt ./cmd/binfmt

FROM scratch AS binaries
ARG BINARY_PREFIX
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ You can also uninstall all archs for a specific emulator:
docker run --privileged --rm tonistiigi/binfmt --uninstall qemu-*
```

## Display version

```bash
docker run --privileged --rm tonistiigi/binfmt --version
```
```
binfmt/9a44d27 qemu/v6.0.0 go/1.15.11
```

## Development commands

```bash
Expand Down
10 changes: 9 additions & 1 deletion cmd/binfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"

Expand All @@ -20,12 +21,14 @@ var (
mount string
toInstall string
toUninstall string
flVersion bool
)

func init() {
flag.StringVar(&mount, "mount", "/proc/sys/fs/binfmt_misc", "binfmt_misc mount point")
flag.StringVar(&toInstall, "install", "", "architectures to install")
flag.StringVar(&toUninstall, "uninstall", "", "architectures to uninstall")
flag.BoolVar(&flVersion, "version", false, "display version")
}

func uninstall(arch string) error {
Expand Down Expand Up @@ -155,14 +158,19 @@ func parseUninstall(in string) (out []string) {
}

func main() {
log.SetFlags(0) // no timestamps in logs
flag.Parse()

if err := run(); err != nil {
log.Printf("error: %+v", err)
}
}

func run() error {
if flVersion {
log.Printf("binfmt/%s qemu/%s go/%s", revision, qemuVersion, runtime.Version()[2:])
return nil
}

if _, err := os.Stat(filepath.Join(mount, "status")); err != nil {
if err := syscall.Mount("binfmt_misc", mount, "binfmt_misc", 0, ""); err != nil {
return errors.Wrapf(err, "cannot mount binfmt_misc filesystem at %s", mount)
Expand Down
6 changes: 6 additions & 0 deletions cmd/binfmt/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

var (
revision = "unknown"
qemuVersion = "unknown"
)

0 comments on commit 7580efb

Please sign in to comment.