Skip to content

Commit

Permalink
refactor: add devname into gpt.Partition, refactor probe package
Browse files Browse the repository at this point in the history
Necessary refactoring to prepare for disk encryption implementation.
Talos part is going to be changed pretty much.

Signed-off-by: Artem Chernyshev <[email protected]>
  • Loading branch information
Unix4ever authored and talos-bot committed Jan 26, 2021
1 parent f2728a5 commit 5a1c7f7
Show file tree
Hide file tree
Showing 16 changed files with 520 additions and 279 deletions.
5 changes: 4 additions & 1 deletion .golangci.yml
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-09-04T03:46:02Z by kres latest.
# Generated on 2021-01-21T16:47:42Z by kres latest.


# options for analysis running
Expand Down Expand Up @@ -125,6 +125,9 @@ linters:
- gomnd
- goerr113
- nestif
- wrapcheck
- paralleltest
- exhaustivestruct
disable-all: false
fast: false

Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# syntax = docker/dockerfile-upstream:1.1.7-experimental
# syntax = docker/dockerfile-upstream:1.2.0-labs

# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2020-10-23T19:30:04Z by kres e82c767-dirty.
# Generated on 2021-01-21T16:47:42Z by kres latest.

ARG TOOLCHAIN

Expand All @@ -17,14 +17,14 @@ RUN markdownlint --ignore "**/node_modules/**" --ignore '**/hack/chglog/**' --ru

# base toolchain image
FROM ${TOOLCHAIN} AS toolchain
RUN apk --update --no-cache add bash curl build-base
RUN apk --update --no-cache add bash curl build-base dosfstools

# build tools
FROM toolchain AS tools
ENV GO111MODULE on
ENV CGO_ENABLED 0
ENV GOPATH /go
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b /bin v1.30.0
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b /bin v1.33.0
ARG GOFUMPT_VERSION
RUN cd $(mktemp -d) \
&& go mod init tmp \
Expand Down
2 changes: 1 addition & 1 deletion blockdevice/blockdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type OutOfSpaceError interface {

// IsOutOfSpaceError checks if provided error is 'out of space'.
func IsOutOfSpaceError(err error) bool {
_, ok := err.(OutOfSpaceError)
_, ok := err.(OutOfSpaceError) //nolint:errorlint

return ok
}
15 changes: 15 additions & 0 deletions blockdevice/blockdevice_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,18 @@ func (bd *BlockDevice) Reset() error {
func (bd *BlockDevice) Wipe() error {
return fmt.Errorf("not implemented")
}

// OpenPartition opens another blockdevice using a partition of this block device.
func (bd *BlockDevice) OpenPartition(label string) (*BlockDevice, error) {
return nil, fmt.Errorf("not implemented")
}

// GetPartition returns partition by label if found.
func (bd *BlockDevice) GetPartition(label string) (*gpt.Partition, error) {
return nil, fmt.Errorf("not implemented")
}

// PartPath returns partition path by label, verifies that partition exists.
func (bd *BlockDevice) PartPath(label string) (string, error) {
return "", fmt.Errorf("not implemented")
}
46 changes: 46 additions & 0 deletions blockdevice/blockdevice_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,49 @@ func (bd *BlockDevice) Reset() error {

return g.Write()
}

// OpenPartition opens another blockdevice using a partition of this block device.
func (bd *BlockDevice) OpenPartition(label string) (*BlockDevice, error) {
g, err := bd.PartitionTable()
if err != nil {
return nil, err
}

part := g.Partitions().FindByName(label)
if part == nil {
return nil, os.ErrNotExist
}

path, err := part.Path()
if err != nil {
return nil, err
}

return Open(path)
}

// GetPartition returns partition by label if found.
func (bd *BlockDevice) GetPartition(label string) (*gpt.Partition, error) {
g, err := bd.PartitionTable()
if err != nil {
return nil, err
}

part := g.Partitions().FindByName(label)

if part == nil {
return nil, os.ErrNotExist
}

return part, nil
}

// PartPath returns partition path by label, verifies that partition exists.
func (bd *BlockDevice) PartPath(label string) (string, error) {
part, err := bd.GetPartition(label)
if err != nil {
return "", err
}

return part.Path()
}
4 changes: 3 additions & 1 deletion blockdevice/blockdevice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

package blockdevice_test

import "testing"
import (
"testing"
)

func TestEmpty(t *testing.T) {
// added for accurate coverage estimation
Expand Down
67 changes: 67 additions & 0 deletions blockdevice/filesystem/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,76 @@

package filesystem

import (
"encoding/binary"
"io"
"os"
"time"

"github.com/talos-systems/go-retry/retry"
"golang.org/x/sys/unix"

"github.com/talos-systems/go-blockdevice/blockdevice/filesystem/iso9660"
"github.com/talos-systems/go-blockdevice/blockdevice/filesystem/vfat"
"github.com/talos-systems/go-blockdevice/blockdevice/filesystem/xfs"
)

// SuperBlocker describes the requirements for file system super blocks.
type SuperBlocker interface {
Is() bool
Offset() int64
Type() string
}

const (
// Unknown filesystem.
Unknown string = "unknown"
)

// Probe checks partition type.
func Probe(path string) (sb SuperBlocker, err error) {
var f *os.File
// Sleep for up to 5s to wait for kernel to create the necessary device files.
// If we dont sleep this becomes racy in that the device file does not exist
// and it will fail to open.
err = retry.Constant(5*time.Second, retry.WithUnits((50 * time.Millisecond))).Retry(func() error {
if f, err = os.OpenFile(path, os.O_RDONLY|unix.O_CLOEXEC, os.ModeDevice); err != nil {
if os.IsNotExist(err) {
return retry.ExpectedError(err)
}

return retry.UnexpectedError(err)
}

return nil
})
if err != nil {
return nil, err
}

//nolint: errcheck
defer f.Close()

superblocks := []SuperBlocker{
&iso9660.SuperBlock{},
&vfat.SuperBlock{},
&xfs.SuperBlock{},
}

for _, sb := range superblocks {
if _, err = f.Seek(sb.Offset(), io.SeekStart); err != nil {
return nil, err
}

err = binary.Read(f, binary.BigEndian, sb)
if err != nil {
return nil, err
}

if sb.Is() {
return sb, nil
}
}

return nil, nil
}
2 changes: 1 addition & 1 deletion blockdevice/loopback/loopback.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
// 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 {
if err.(syscall.Errno) != 0 { //nolint:errorlint
return err
}

Expand Down
5 changes: 3 additions & 2 deletions blockdevice/partition/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Open(f *os.File) (g *GPT, err error) {
f: f,
l: l,
h: h,
e: &Partitions{h: h},
e: &Partitions{h: h, devname: f.Name()},
}

return g, nil
Expand Down Expand Up @@ -120,7 +120,7 @@ func New(f *os.File, setters ...Option) (g *GPT, err error) {
f: f,
l: l,
h: h,
e: &Partitions{h: h},
e: &Partitions{h: h, devname: f.Name()},
}

return g, nil
Expand Down Expand Up @@ -277,6 +277,7 @@ func (g *GPT) InsertAt(idx int, size uint64, setters ...PartitionOption) (*Parti
LastLBA: end,
Attributes: opts.Attibutes,
Name: opts.Name,
devname: g.e.devname,
}

g.e.p = append(g.e.p[:idx], append([]*Partition{partition}, g.e.p[idx:]...)...)
Expand Down
Loading

0 comments on commit 5a1c7f7

Please sign in to comment.