Skip to content

Commit

Permalink
fix: return correct error value from blkpg functions
Browse files Browse the repository at this point in the history
`err` was declared, but not set properly.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira authored and talos-bot committed Oct 23, 2020
1 parent cebe43d commit 2cb9516
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 20 deletions.
5 changes: 2 additions & 3 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2020-09-04T03:46:02Z by kres latest.
# Generated on 2020-10-23T19:31:03Z by kres e82c767-dirty.

kind: pipeline
type: kubernetes
Expand All @@ -19,7 +19,7 @@ steps:
- sleep 5
- git fetch --tags
- install-ci-key
- docker buildx create --driver docker-container --platform linux/amd64 --name local --use unix:///var/outer-run/docker.sock
- docker buildx create --driver docker-container --platform linux/amd64 --name local-insecure --buildkitd-flags '--allow-insecure-entitlement security.insecure' --use unix:///var/outer-run/docker.sock
- docker buildx inspect --bootstrap
environment:
SSH_KEY:
Expand Down Expand Up @@ -132,7 +132,6 @@ services:
- --dns=8.8.4.4
- --mtu=1500
- --log-level=error
- --insecure-registry=http://registry.ci.svc:5000
privileged: true
volumes:
- name: outer-docker-socket
Expand Down
10 changes: 10 additions & 0 deletions .kres.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
kind: common.Docker
name: setup-ci
spec:
allowInsecure: true
---
kind: golang.UnitTests
name: unit-tests
spec:
requiresInsecure: true
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2020-09-04T03:46:02Z by kres latest.
# Generated on 2020-10-23T19:30:04Z by kres e82c767-dirty.

ARG TOOLCHAIN

Expand Down Expand Up @@ -55,12 +55,12 @@ RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/r
# runs unit-tests with race detector
FROM base AS unit-tests-race
ARG TESTPKGS
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp CGO_ENABLED=1 go test -v -race -count 1 ${TESTPKGS}
RUN --security=insecure --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp CGO_ENABLED=1 go test -v -race -count 1 ${TESTPKGS}

# runs unit-tests
FROM base AS unit-tests-run
ARG TESTPKGS
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp go test -v -covermode=atomic -coverprofile=coverage.txt -count 1 ${TESTPKGS}
RUN --security=insecure --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp go test -v -covermode=atomic -coverprofile=coverage.txt -count 1 ${TESTPKGS}

FROM scratch AS unit-tests
COPY --from=unit-tests-run /src/coverage.txt /coverage.txt
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2020-10-06T16:24:33Z by kres latest.
# Generated on 2020-10-23T19:30:04Z by kres e82c767-dirty.

# common variables

Expand Down Expand Up @@ -100,11 +100,11 @@ base: ## Prepare base toolchain

.PHONY: unit-tests
unit-tests: ## Performs unit tests
@$(MAKE) local-$@ DEST=$(ARTIFACTS)
@$(MAKE) local-$@ DEST=$(ARTIFACTS) TARGET_ARGS="--allow security.insecure"

.PHONY: unit-tests-race
unit-tests-race: ## Performs unit tests with race detection enabled.
@$(MAKE) target-$@
@$(MAKE) target-$@ TARGET_ARGS="--allow security.insecure"

.PHONY: coverage
coverage: ## Upload coverage data to codecov.io.
Expand Down
4 changes: 2 additions & 2 deletions blockdevice/blkpg/blkpg_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func inform(f *os.File, partition table.Partition, op int32) (err error) {
if errno != 0 {
switch errno { //nolint: exhaustive
case unix.EBUSY:
return retry.ExpectedError(err)
return retry.ExpectedError(errno)
default:
return retry.UnexpectedError(err)
return retry.UnexpectedError(errno)
}
}

Expand Down
126 changes: 126 additions & 0 deletions blockdevice/loopback/loopback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// Package loopback provides support for disk loopback devices (/dev/loopN).
package loopback

import (
"fmt"
"os"
"runtime"
"syscall"
"unsafe"

"golang.org/x/sys/unix"
)

// Copyright (c) 2017, Paul R. Tagliamonte <[email protected]>

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// syscalls will return an errno type (which implements error) for all calls,
// including success (errno 0). We only care about non-zero errnos.
func errnoIsErr(err error) error {
if err.(syscall.Errno) != 0 {
return err
}

return nil
}

// Loop given a handle to a Loopback device (such as /dev/loop0), and a handle
// to the image to loop mount (such as a squashfs or ext4fs image), performs
// the required call to loop the image to the provided block device.
func Loop(loopbackDevice, image *os.File) error {
_, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
loopbackDevice.Fd(),
unix.LOOP_SET_FD,
image.Fd(),
)

return errnoIsErr(err)
}

// LoopSetReadWrite clears the read-only flag on the loop devices.
func LoopSetReadWrite(loopbackDevice *os.File) error {
var status unix.LoopInfo64

_, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
loopbackDevice.Fd(),
unix.LOOP_GET_STATUS64,
uintptr(unsafe.Pointer(&status)),
)

if e := errnoIsErr(err); e != nil {
return e
}

status.Flags &= ^uint32(unix.LO_FLAGS_READ_ONLY)

_, _, err = syscall.Syscall(
syscall.SYS_IOCTL,
loopbackDevice.Fd(),
unix.LOOP_SET_STATUS64,
uintptr(unsafe.Pointer(&status)),
)

runtime.KeepAlive(status)

return errnoIsErr(err)
}

// Unloop given a handle to the Loopback device (such as /dev/loop0), preforms the
// required call to the image to unloop the file.
func Unloop(loopbackDevice *os.File) error {
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, loopbackDevice.Fd(), unix.LOOP_CLR_FD, 0)

return errnoIsErr(err)
}

// NextLoopDevice gets the next loopback device that isn't used.
//
// Under the hood this will ask loop-control for the LOOP_CTL_GET_FREE value, and interpolate
// that into the conventional GNU/Linux naming scheme for loopback devices, and os.Open
// that path.
func NextLoopDevice() (*os.File, error) {
loopInt, err := nextUnallocatedLoop()
if err != nil {
return nil, err
}

return os.OpenFile(fmt.Sprintf("/dev/loop%d", loopInt), os.O_RDWR, 0)
}

// Return the integer of the next loopback device we can use by calling
// loop-control with the LOOP_CTL_GET_FREE ioctl.
func nextUnallocatedLoop() (int, error) {
fd, err := os.OpenFile("/dev/loop-control", os.O_RDONLY, 0o644)
if err != nil {
return 0, err
}

defer fd.Close() //nolint: errcheck

index, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd.Fd(), unix.LOOP_CTL_GET_FREE, 0)

return int(index), errnoIsErr(err)
}
43 changes: 34 additions & 9 deletions blockdevice/table/gpt/gpt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/suite"

"github.com/talos-systems/go-blockdevice/blockdevice"
"github.com/talos-systems/go-blockdevice/blockdevice/loopback"
bdtable "github.com/talos-systems/go-blockdevice/blockdevice/table"
"github.com/talos-systems/go-blockdevice/blockdevice/table/gpt"
"github.com/talos-systems/go-blockdevice/blockdevice/table/gpt/partition"
Expand All @@ -28,7 +29,9 @@ const (
type GPTSuite struct {
suite.Suite

f *os.File
f *os.File
dev *os.File
loopbackDevice *os.File
}

func (suite *GPTSuite) SetupTest() {
Expand All @@ -38,15 +41,33 @@ func (suite *GPTSuite) SetupTest() {
suite.Require().NoError(err)

suite.Require().NoError(suite.f.Truncate(size))

suite.loopbackDevice, err = loopback.NextLoopDevice()
suite.Require().NoError(err)

suite.T().Logf("Using %s", suite.loopbackDevice.Name())

suite.Require().NoError(loopback.Loop(suite.loopbackDevice, suite.f))

suite.Require().NoError(loopback.LoopSetReadWrite(suite.loopbackDevice))

suite.dev, err = os.OpenFile(suite.loopbackDevice.Name(), os.O_RDWR, 0)
suite.Require().NoError(err)
}

func (suite *GPTSuite) TearDownTest() {
suite.Assert().NoError(suite.dev.Close())

if suite.loopbackDevice != nil {
suite.Assert().NoError(loopback.Unloop(suite.loopbackDevice))
}

suite.Assert().NoError(suite.f.Close())
suite.Assert().NoError(os.Remove(suite.f.Name()))
}

func (suite *GPTSuite) TestPartitionAdd() {
table, err := gpt.NewGPT("/dev/null", suite.f)
table, err := gpt.NewGPT(suite.loopbackDevice.Name(), suite.dev)
suite.Require().NoError(err)

_, err = table.New()
Expand Down Expand Up @@ -93,7 +114,7 @@ func (suite *GPTSuite) TestPartitionAdd() {
suite.Require().NoError(table.Write())

// re-read the partition table
table, err = gpt.NewGPT("/dev/null", suite.f)
table, err = gpt.NewGPT(suite.loopbackDevice.Name(), suite.dev)
suite.Require().NoError(err)

suite.Require().NoError(table.Read())
Expand All @@ -102,7 +123,7 @@ func (suite *GPTSuite) TestPartitionAdd() {
}

func (suite *GPTSuite) TestPartitionAddOutOfSpace() {
table, err := gpt.NewGPT("/dev/null", suite.f)
table, err := gpt.NewGPT(suite.loopbackDevice.Name(), suite.dev)
suite.Require().NoError(err)

_, err = table.New()
Expand Down Expand Up @@ -131,7 +152,7 @@ func (suite *GPTSuite) TestPartitionAddOutOfSpace() {
}

func (suite *GPTSuite) TestPartitionDelete() {
table, err := gpt.NewGPT("/dev/null", suite.f)
table, err := gpt.NewGPT(suite.loopbackDevice.Name(), suite.dev)
suite.Require().NoError(err)

_, err = table.New()
Expand Down Expand Up @@ -162,7 +183,7 @@ func (suite *GPTSuite) TestPartitionDelete() {
suite.Require().NoError(table.Write())

// re-read the partition table
table, err = gpt.NewGPT("/dev/null", suite.f)
table, err = gpt.NewGPT(suite.loopbackDevice.Name(), suite.dev)
suite.Require().NoError(err)

suite.Require().NoError(table.Read())
Expand All @@ -182,7 +203,7 @@ func (suite *GPTSuite) TestPartitionDelete() {
suite.Require().NoError(table.Write())

// re-read the partition table for the second time
table, err = gpt.NewGPT("/dev/null", suite.f)
table, err = gpt.NewGPT(suite.loopbackDevice.Name(), suite.dev)
suite.Require().NoError(err)

suite.Require().NoError(table.Read())
Expand All @@ -204,7 +225,7 @@ func (suite *GPTSuite) TestPartitionDelete() {
}

func (suite *GPTSuite) TestPartitionInsertAt() {
table, err := gpt.NewGPT("/dev/null", suite.f)
table, err := gpt.NewGPT(suite.loopbackDevice.Name(), suite.dev)
suite.Require().NoError(err)

_, err = table.New()
Expand Down Expand Up @@ -233,7 +254,7 @@ func (suite *GPTSuite) TestPartitionInsertAt() {
suite.Require().NoError(table.Write())

// re-read the partition table
table, err = gpt.NewGPT("/dev/null", suite.f)
table, err = gpt.NewGPT(suite.loopbackDevice.Name(), suite.dev)
suite.Require().NoError(err)

suite.Require().NoError(table.Read())
Expand Down Expand Up @@ -297,5 +318,9 @@ func (suite *GPTSuite) TestPartitionInsertAt() {
}

func TestGPTSuite(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("can't run the test as non-root")
}

suite.Run(t, new(GPTSuite))
}

0 comments on commit 2cb9516

Please sign in to comment.