Skip to content

Commit

Permalink
feat: add ext4 filesystem detection logic
Browse files Browse the repository at this point in the history
This simply detects the ext filesystem superblock.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira committed Dec 5, 2022
1 parent 694ac62 commit e52e012
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
6 changes: 6 additions & 0 deletions blockdevice/filesystem/ext4/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// Package ext4 provides functions for working with the ext filesystems.
package ext4
51 changes: 51 additions & 0 deletions blockdevice/filesystem/ext4/superblock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package ext4

import (
"unsafe"
)

const (
// Magic is the ext magic signature.
Magic = 0xef53
)

// SuperBlock represents the ext filesystem super block.
//
// See https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#The_Super_Block for the reference.
type SuperBlock struct {
_ [0x38]uint8
Magic [2]uint8
_ [0x3e]uint8
Label [0x10]uint8
_ [0x378]uint8
}

// Is implements the SuperBlocker interface.
func (sb *SuperBlock) Is() bool {
return sb.Magic[1] == Magic>>8 && sb.Magic[0] == Magic&0xff
}

// Offset implements the SuperBlocker interface.
func (sb *SuperBlock) Offset() int64 {
return 0x400
}

// Type implements the SuperBlocker interface.
func (sb *SuperBlock) Type() string {
return "ext4"
}

// Encrypted implements the SuperBlocker interface.
func (sb *SuperBlock) Encrypted() bool {
return false
}

func init() {
if unsafe.Sizeof(SuperBlock{}) != 0x400 {
panic("ext4: SuperBlock size is not 0x400")
}
}
2 changes: 2 additions & 0 deletions blockdevice/filesystem/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/siderolabs/go-retry/retry"

"github.com/siderolabs/go-blockdevice/blockdevice/filesystem/ext4"
"github.com/siderolabs/go-blockdevice/blockdevice/filesystem/iso9660"
"github.com/siderolabs/go-blockdevice/blockdevice/filesystem/luks"
"github.com/siderolabs/go-blockdevice/blockdevice/filesystem/msdos"
Expand Down Expand Up @@ -67,6 +68,7 @@ func Probe(path string) (SuperBlocker, error) { //nolint:ireturn
&msdos.SuperBlock{},
&xfs.SuperBlock{},
&luks.SuperBlock{},
&ext4.SuperBlock{},
}

for _, sb := range superblocks {
Expand Down
6 changes: 6 additions & 0 deletions blockdevice/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/siderolabs/go-blockdevice/blockdevice"
"github.com/siderolabs/go-blockdevice/blockdevice/filesystem"
"github.com/siderolabs/go-blockdevice/blockdevice/filesystem/ext4"
"github.com/siderolabs/go-blockdevice/blockdevice/filesystem/iso9660"
"github.com/siderolabs/go-blockdevice/blockdevice/filesystem/msdos"
"github.com/siderolabs/go-blockdevice/blockdevice/filesystem/vfat"
Expand Down Expand Up @@ -73,6 +74,11 @@ func WithFileSystemLabel(label string) SelectOption {
if bytes.Equal(trimmed, []byte(label)) {
return true, nil
}
case *ext4.SuperBlock:
trimmed := bytes.Trim(sb.Label[:], " \x00")
if bytes.Equal(trimmed, []byte(label)) {
return true, nil
}
}
}

Expand Down
24 changes: 20 additions & 4 deletions blockdevice/probe/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ func (suite *ProbeSuite) addPartition(name string, size uint64, fatBits int) *gp
return partition
}

func (suite *ProbeSuite) setSystemLabel(name string, fatBits int) {
func (suite *ProbeSuite) setSystemLabelVFAT(name string, fatBits int) {
cmd := exec.Command("mkfs.vfat", "-F", strconv.Itoa(fatBits), "-n", name, suite.LoopbackDevice.Name())
suite.Require().NoError(cmd.Run())
}

func (suite *ProbeSuite) setSystemLabelEXT4(name string) {
cmd := exec.Command("mkfs.ext4", "-L", name, suite.LoopbackDevice.Name())
suite.Require().NoError(cmd.Run())
}

func (suite *ProbeSuite) TestDevForPartitionLabel() {
part12 := suite.addPartition("devpart12", 1024*1024, 12)
part32 := suite.addPartition("devpart32", 1024*1024*256, 32)
Expand Down Expand Up @@ -103,7 +108,7 @@ func (suite *ProbeSuite) TestGetDevWithPartitionName() {
func (suite *ProbeSuite) testGetDevWithFileSystemLabel(fatBits int) {
label := fmt.Sprintf("LABELSYS%d", fatBits)

suite.setSystemLabel(label, fatBits)
suite.setSystemLabelVFAT(label, fatBits)

dev, err := probe.GetDevWithFileSystemLabel(label)
suite.Require().NoError(err)
Expand All @@ -129,8 +134,8 @@ func (suite *ProbeSuite) TestProbeByPartitionLabel() {
suite.Require().Equal(suite.LoopbackDevice.Name(), probed[0].Device().Name())
}

func (suite *ProbeSuite) TestProbeByFilesystemLabelBlockdevice() {
suite.setSystemLabel("FSLBABELBD", 32)
func (suite *ProbeSuite) TestProbeByFilesystemLabelBlockdeviceVFAT() {
suite.setSystemLabelVFAT("FSLBABELBD", 32)

probed, err := probe.All(probe.WithFileSystemLabel("FSLBABELBD"))
suite.Require().NoError(err)
Expand All @@ -140,6 +145,17 @@ func (suite *ProbeSuite) TestProbeByFilesystemLabelBlockdevice() {
suite.Require().Equal(suite.LoopbackDevice.Name(), probed[0].Path)
}

func (suite *ProbeSuite) TestProbeByFilesystemLabelBlockdeviceEXT4() {
suite.setSystemLabelEXT4("EXTBD")

probed, err := probe.All(probe.WithFileSystemLabel("EXTBD"))
suite.Require().NoError(err)
suite.Require().Equal(1, len(probed))

suite.Require().Equal(suite.LoopbackDevice.Name(), probed[0].Device().Name())
suite.Require().Equal(suite.LoopbackDevice.Name(), probed[0].Path)
}

func (suite *ProbeSuite) TestProbeByFilesystemLabelPartition() {
suite.addPartition("FOO", 1024*1024*256, 16)
suite.addPartition("FSLABELPART", 1024*1024*2, 12)
Expand Down

0 comments on commit e52e012

Please sign in to comment.