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 24d439f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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 24d439f

Please sign in to comment.