Skip to content

Commit

Permalink
fix: try to find cdrom disks
Browse files Browse the repository at this point in the history
It fix helps to find non-partition file systems.

Signed-off-by: Serge Logvinov <serge.logvinov@sinextra.dev>
Signed-off-by: Artem Chernyshev <artem.chernyshev@talos-systems.com>
  • Loading branch information
sergelogvinov authored and Unix4ever committed Sep 25, 2021
1 parent 667bf53 commit 70d2865
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
15 changes: 15 additions & 0 deletions blockdevice/loopback/loopback.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ func Loop(loopbackDevice, image *os.File) error {
unix.LOOP_SET_FD,
image.Fd(),
)
if e := errnoIsErr(err); e != nil {
return e
}

// Force kernel to scan partition table on loop device
status := &unix.LoopInfo64{
Flags: unix.LO_FLAGS_PARTSCAN,
}

_, _, err = syscall.Syscall(
syscall.SYS_IOCTL,
loopbackDevice.Fd(),
unix.LOOP_SET_STATUS64,
uintptr(unsafe.Pointer(status)),
)

return errnoIsErr(err)
}
Expand Down
15 changes: 13 additions & 2 deletions blockdevice/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ func All(options ...SelectOption) (all []*ProbedBlockDevice, err error) {
probed := probePartitions(devpath)

for _, dev := range probed {
add := true
add := false
for _, matches := range options {
add, err = matches(dev)
if err != nil {
if e := dev.Close(); e != nil {
return nil, e
}

return nil, err
break
}

if !add {
Expand Down Expand Up @@ -151,6 +151,9 @@ func probe(devpath string) (devpaths []string) {
// file system exists without a partition table.
bd, err := blockdevice.Open(devpath, blockdevice.WithMode(blockdevice.ReadonlyMode))
if err != nil {
// Now blockdevice.Open returns error if it cannot read devpath
// I believe filesystem.Probe always return sb = nil
// but i keep it here. May be it will fix in future.
//nolint: errcheck
if sb, _ := filesystem.Probe(devpath); sb != nil {
devpaths = append(devpaths, devpath)
Expand All @@ -166,6 +169,13 @@ func probe(devpath string) (devpaths []string) {
// has partitions.
pt, err := bd.PartitionTable()
if err != nil {
// If a partition table was not found, it is still possible that a
// file system exists without a partition table.
//nolint: errcheck
if sb, _ := filesystem.Probe(devpath); sb != nil {
devpaths = append(devpaths, devpath)
}

return devpaths
}

Expand Down Expand Up @@ -231,6 +241,7 @@ func GetPartitionWithName(name string) (part *gpt.Partition, err error) {
return device.GetPartition(name)
}

// Returns array with partitions or root readable device.
func probePartitions(devpath string) (probed []*ProbedBlockDevice) {
for _, path := range probe(devpath) {
var (
Expand Down
44 changes: 29 additions & 15 deletions blockdevice/probe/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,39 @@ func (suite *ProbeSuite) addPartition(name string, size uint64) *gpt.Partition {
return partition
}

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

func (suite *ProbeSuite) TestDevForPartitionLabel() {
size := uint64(1024 * 1024 * 256)
part := suite.addPartition("devpart1", size)

dev, err := probe.DevForPartitionLabel(suite.LoopbackDevice.Name(), "devpart1")
suite.Require().NoError(err)
path, err := part.Path()
suite.Require().NoError(err)
suite.Require().Equal(path, dev.Device().Name())
}

func (suite *ProbeSuite) TestGetDevWithPartitionName() {
size := uint64(1024 * 1024 * 512)
part := suite.addPartition("label2", size)
part := suite.addPartition("devlabel", size)

_, err := probe.GetPartitionWithName("label2")
dev, err := probe.GetDevWithPartitionName("devlabel")
suite.Require().NoError(err)
_, err = part.Path()
devpath, err := part.Path()
suite.Require().NoError(err)
suite.Require().Equal(devpath, dev.Path)
}

func (suite *ProbeSuite) TestGetDevWithFileSystemLabel() {
suite.setSystemLabel("GETLABELSYS")

dev, err := probe.GetDevWithFileSystemLabel("GETLABELSYS")
suite.Require().NoError(err)
suite.Require().Equal(suite.LoopbackDevice.Name(), dev.Path)
}

func (suite *ProbeSuite) TestProbeByPartitionLabel() {
Expand All @@ -68,17 +93,6 @@ func (suite *ProbeSuite) TestProbeByPartitionLabel() {
suite.Require().Equal(suite.LoopbackDevice.Name(), probed[0].Device().Name())
}

func (suite *ProbeSuite) TestDevForPartitionLabel() {
size := uint64(1024 * 1024 * 256)
part := suite.addPartition("label1", size)

dev, err := probe.DevForPartitionLabel(suite.LoopbackDevice.Name(), "label1")
suite.Require().NoError(err)
path, err := part.Path()
suite.Require().NoError(err)
suite.Require().Equal(path, dev.Device().Name())
}

func TestProbe(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("can't run the test as non-root")
Expand Down

0 comments on commit 70d2865

Please sign in to comment.