diff --git a/Makefile b/Makefile index b6c9e906c..bf4e91998 100644 --- a/Makefile +++ b/Makefile @@ -14,11 +14,13 @@ # limitations under the License. # Use bash, so that we can do process substitution. -SHELL = /bin/bash +SHELL := $(shell which bash) # Go tools. GO ?= go GO_MD2MAN ?= go-md2man +GOOS ?= $(shell go env GOOS) +GOARCH ?= $(shell go env GOARCH) export GO111MODULE=on # Set up the ... lovely ... GOPATH hacks. @@ -68,13 +70,19 @@ BASE_FLAGS := ${BUILD_FLAGS} -tags "${BUILDTAGS}" BASE_LDFLAGS := -s -w -X ${PROJECT}.gitCommit=${COMMIT} -X ${PROJECT}.version=${VERSION} # Specific build flags for build type. -DYN_BUILD_FLAGS := ${BASE_FLAGS} -buildmode=pie -ldflags "${BASE_LDFLAGS}" -TEST_BUILD_FLAGS := ${BASE_FLAGS} -buildmode=pie -ldflags "${BASE_LDFLAGS} -X ${PROJECT}/pkg/testutils.binaryType=test" +ifeq ($(GOOS), linux) + DYN_BUILD_FLAGS := ${BASE_FLAGS} -buildmode=pie -ldflags "${BASE_LDFLAGS}" + TEST_BUILD_FLAGS := ${BASE_FLAGS} -buildmode=pie -ldflags "${BASE_LDFLAGS} -X ${PROJECT}/pkg/testutils.binaryType=test" +else + DYN_BUILD_FLAGS := ${BASE_FLAGS} -ldflags "${BASE_LDFLAGS}" + TEST_BUILD_FLAGS := ${BASE_FLAGS} -ldflags "${BASE_LDFLAGS} -X ${PROJECT}/pkg/testutils.binaryType=test" +endif + STATIC_BUILD_FLAGS := ${BASE_FLAGS} -ldflags "${BASE_LDFLAGS} -extldflags '-static'" # Installation directories. DESTDIR ?= -PREFIX ?=/usr +PREFIX ?=/usr/local BINDIR ?=$(PREFIX)/bin MANDIR ?=$(PREFIX)/share/man @@ -85,7 +93,7 @@ GO_SRC = $(shell find . -type f -name '*.go') # NOTE: If you change these make sure you also update local-validate-build. umoci: $(GO_SRC) - $(GO) build ${DYN_BUILD_FLAGS} -o $(BUILD_DIR)/$@ ${CMD} + GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build ${DYN_BUILD_FLAGS} -o $(BUILD_DIR)/$@ ${CMD} umoci.static: $(GO_SRC) env CGO_ENABLED=0 $(GO) build ${STATIC_BUILD_FLAGS} -o $(BUILD_DIR)/$@ ${CMD} diff --git a/pkg/fseval/fseval_default.go b/pkg/fseval/fseval_default.go index 1cb03f51c..994d86dac 100644 --- a/pkg/fseval/fseval_default.go +++ b/pkg/fseval/fseval_default.go @@ -103,11 +103,6 @@ func (fs osFsEval) RemoveAll(path string) error { return os.RemoveAll(path) } -// Mknod is equivalent to unix.Mknod. -func (fs osFsEval) Mknod(path string, mode os.FileMode, dev uint64) error { - return unix.Mknod(path, uint32(mode), int(dev)) -} - // MkdirAll is equivalent to os.MkdirAll. func (fs osFsEval) MkdirAll(path string, perm os.FileMode) error { return os.MkdirAll(path, perm) diff --git a/pkg/fseval/mknod_freebsd.go b/pkg/fseval/mknod_freebsd.go new file mode 100644 index 000000000..51d44e9e4 --- /dev/null +++ b/pkg/fseval/mknod_freebsd.go @@ -0,0 +1,28 @@ +/* + * umoci: Umoci Modifies Open Containers' Images + * Copyright (C) 2016-2020 SUSE LLC + * + * 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 fseval + +import ( + "os" + + "golang.org/x/sys/unix" +) + +func (fs osFsEval) Mknod(path string, mode os.FileMode, dev uint64) error { + return unix.Mknod(path, uint32(mode), uint64(dev)) +} diff --git a/pkg/fseval/mknod_linux.go b/pkg/fseval/mknod_linux.go new file mode 100644 index 000000000..33a5a5ea2 --- /dev/null +++ b/pkg/fseval/mknod_linux.go @@ -0,0 +1,28 @@ +/* + * umoci: Umoci Modifies Open Containers' Images + * Copyright (C) 2016-2020 SUSE LLC + * + * 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 fseval + +import ( + "os" + + "golang.org/x/sys/unix" +) + +func (fs osFsEval) Mknod(path string, mode os.FileMode, dev uint64) error { + return unix.Mknod(path, uint32(mode), int(dev)) +} diff --git a/pkg/unpriv/mknod_freebsd.go b/pkg/unpriv/mknod_freebsd.go new file mode 100644 index 000000000..383860463 --- /dev/null +++ b/pkg/unpriv/mknod_freebsd.go @@ -0,0 +1,34 @@ +/* + * umoci: Umoci Modifies Open Containers' Images + * Copyright (C) 2016-2020 SUSE LLC + * + * 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 unpriv + +import ( + "os" + + "github.com/pkg/errors" + "golang.org/x/sys/unix" +) + +// Mknod is a wrapper around unix.Mknod which has been wrapped with unpriv.Wrap +// to make it possible to remove a path even if you do not currently have the +// required access bits to modify or resolve the path. +func Mknod(path string, mode os.FileMode, dev uint64) error { + return errors.Wrap(Wrap(path, func(path string) error { + return unix.Mknod(path, uint32(mode), uint64(dev)) + }), "unpriv.mknod") +} diff --git a/pkg/unpriv/mknod_linux.go b/pkg/unpriv/mknod_linux.go new file mode 100644 index 000000000..696f20f20 --- /dev/null +++ b/pkg/unpriv/mknod_linux.go @@ -0,0 +1,34 @@ +/* + * umoci: Umoci Modifies Open Containers' Images + * Copyright (C) 2016-2020 SUSE LLC + * + * 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 unpriv + +import ( + "os" + + "github.com/pkg/errors" + "golang.org/x/sys/unix" +) + +// Mknod is a wrapper around unix.Mknod which has been wrapped with unpriv.Wrap +// to make it possible to remove a path even if you do not currently have the +// required access bits to modify or resolve the path. +func Mknod(path string, mode os.FileMode, dev uint64) error { + return errors.Wrap(Wrap(path, func(path string) error { + return unix.Mknod(path, uint32(mode), int(dev)) + }), "unpriv.mknod") +} diff --git a/pkg/unpriv/unpriv.go b/pkg/unpriv/unpriv.go index 45614f0b6..055e63689 100644 --- a/pkg/unpriv/unpriv.go +++ b/pkg/unpriv/unpriv.go @@ -446,15 +446,6 @@ func MkdirAll(path string, perm os.FileMode) error { }), "unpriv.mkdirall") } -// Mknod is a wrapper around unix.Mknod which has been wrapped with unpriv.Wrap -// to make it possible to remove a path even if you do not currently have the -// required access bits to modify or resolve the path. -func Mknod(path string, mode os.FileMode, dev uint64) error { - return errors.Wrap(Wrap(path, func(path string) error { - return unix.Mknod(path, uint32(mode), int(dev)) - }), "unpriv.mknod") -} - // Llistxattr is a wrapper around system.Llistxattr which has been wrapped with // unpriv.Wrap to make it possible to remove a path even if you do not // currently have the required access bits to resolve the path.