Skip to content

Commit

Permalink
fix: return partition table not exist when trying to read an empty dev
Browse files Browse the repository at this point in the history
Fixes: siderolabs#51

Signed-off-by: Artem Chernyshev <[email protected]>
  • Loading branch information
Unix4ever committed Nov 19, 2021
1 parent b9517d5 commit d873041
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
4 changes: 4 additions & 0 deletions blockdevice/blockdevice_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func Open(devname string, setters ...Option) (bd *BlockDevice, err error) {
}
}

if opts.IgnoreGPT {
return bd, nil
}

if opts.CreateGPT {
var g *gpt.GPT

Expand Down
9 changes: 9 additions & 0 deletions blockdevice/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package blockdevice
// Options is the functional options struct.
type Options struct {
CreateGPT bool
IgnoreGPT bool
ExclusiveLock bool
Mode int
}
Expand Down Expand Up @@ -35,10 +36,18 @@ func WithMode(value int) Option {
}
}

// WithoutGPT opens blockdevice ignoring GPT.
func WithoutGPT(value bool) Option {
return func(args *Options) {
args.IgnoreGPT = value
}
}

// NewDefaultOptions initializes a Options struct with default values.
func NewDefaultOptions(setters ...Option) *Options {
opts := &Options{
CreateGPT: false,
IgnoreGPT: false,
Mode: DefaultMode,
}

Expand Down
10 changes: 7 additions & 3 deletions blockdevice/partition/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ const (

var (
// ErrPartitionTableDoesNotExist indicates that the partition table does not exist.
ErrPartitionTableDoesNotExist = errors.New("does not exist")
ErrPartitionTableDoesNotExist = errors.New("block device partition table does not exist")
// ErrHeaderCRCMismatch indicates that the header CRC does not match what is on disk.
ErrHeaderCRCMismatch = errors.New("header CRC mismatch")
ErrHeaderCRCMismatch = errors.New("block device partition table header CRC mismatch")
// ErrEntriesCRCMismatch indicates that the partitions array CRC does not match what is on disk.
ErrEntriesCRCMismatch = errors.New("entries CRC mismatch")
ErrEntriesCRCMismatch = errors.New("block device partition table entries CRC mismatch")
)

type outOfSpaceError struct {
Expand Down Expand Up @@ -84,6 +84,10 @@ func Open(f *os.File) (g *GPT, err error) {
markMBRBootable: buf[0] == 0x80,
}

if err = h.DeserializeSignature(); err != nil {
return nil, ErrPartitionTableDoesNotExist
}

return g, nil
}

Expand Down
24 changes: 23 additions & 1 deletion blockdevice/partition/gpt/gpt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package gpt_test
import (
"encoding/binary"
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -35,7 +36,28 @@ func (suite *GPTSuite) SetupTest() {

func (suite *GPTSuite) TestEmpty() {
_, err := gpt.Open(suite.Dev)
suite.Require().Error(err)
suite.Require().EqualError(err, gpt.ErrPartitionTableDoesNotExist.Error())
}

func (suite *GPTSuite) TestWiped() {
g, err := gpt.New(suite.Dev)
suite.Require().NoError(err)

_, err = g.Add(1048576, gpt.WithPartitionName("boot"))
suite.Require().NoError(err)

_, err = g.Add(1048576, gpt.WithPartitionName("efi"))
suite.Require().NoError(err)

suite.Require().NoError(g.Write())

suite.validatePMBR(0x00)

cmd := exec.Command("wipefs", "-a", suite.Dev.Name())
suite.Require().NoError(cmd.Run())

g, err = gpt.Open(suite.Dev)
suite.Require().EqualError(err, gpt.ErrPartitionTableDoesNotExist.Error())
}

func (suite *GPTSuite) TestReset() {
Expand Down

0 comments on commit d873041

Please sign in to comment.