diff --git a/blockdevice/blockdevice_linux.go b/blockdevice/blockdevice_linux.go index d8b771a..a60d3b5 100644 --- a/blockdevice/blockdevice_linux.go +++ b/blockdevice/blockdevice_linux.go @@ -5,7 +5,7 @@ package blockdevice import ( - "bytes" + "errors" "fmt" "io" "os" @@ -88,22 +88,16 @@ func Open(devname string, setters ...Option) (bd *BlockDevice, err error) { bd.g = g } else { - buf := make([]byte, 1) - // PMBR protective entry starts at 446. The partition type is at offset - // 4 from the start of the PMBR protective entry. - _, err = f.ReadAt(buf, 450) - if err != nil { + var g *gpt.GPT + if g, err = gpt.Open(f); err != nil { + if errors.Is(err, gpt.ErrPartitionTableDoesNotExist) { + return bd, nil + } + return nil, err } - // For GPT, the partition type should be 0xee (EFI GPT). - if bytes.Equal(buf, []byte{0xee}) { - var g *gpt.GPT - if g, err = gpt.Open(f); err != nil { - return nil, err - } - bd.g = g - } + bd.g = g } return bd, nil diff --git a/blockdevice/partition/gpt/gpt.go b/blockdevice/partition/gpt/gpt.go index c9cf452..640d055 100644 --- a/blockdevice/partition/gpt/gpt.go +++ b/blockdevice/partition/gpt/gpt.go @@ -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 { @@ -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 } diff --git a/blockdevice/partition/gpt/gpt_test.go b/blockdevice/partition/gpt/gpt_test.go index 7e73db6..f21a0d4 100644 --- a/blockdevice/partition/gpt/gpt_test.go +++ b/blockdevice/partition/gpt/gpt_test.go @@ -35,7 +35,7 @@ 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) TestReset() {