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 186f119
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
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
4 changes: 4 additions & 0 deletions blockdevice/partition/gpt/gpt.go
Original file line number Diff line number Diff line change
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 186f119

Please sign in to comment.