Skip to content

Commit

Permalink
feat: make probe always open blockdevices in readonly mode
Browse files Browse the repository at this point in the history
Fixes: #44

Signed-off-by: Artem Chernyshev <[email protected]>
  • Loading branch information
Unix4ever committed Sep 22, 2021
1 parent d981156 commit a9cf3be
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion blockdevice/blockdevice_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Open(devname string, setters ...Option) (bd *BlockDevice, err error) {

var f *os.File

if f, err = os.OpenFile(devname, os.O_RDWR|unix.O_CLOEXEC, os.ModeDevice); err != nil {
if f, err = os.OpenFile(devname, opts.Mode, os.ModeDevice); err != nil {
return nil, err
}

Expand Down
15 changes: 15 additions & 0 deletions blockdevice/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

package blockdevice

import (
"os"

"golang.org/x/sys/unix"
)

// Options is the functional options struct.
type Options struct {
CreateGPT bool
ExclusiveLock bool
Mode int
}

// Option is the functional option func.
Expand All @@ -27,10 +34,18 @@ func WithExclusiveLock(o bool) Option {
}
}

// WithMode opens blockdevice in a specific mode.
func WithMode(value int) Option {
return func(args *Options) {
args.Mode = value
}
}

// NewDefaultOptions initializes a Options struct with default values.
func NewDefaultOptions(setters ...Option) *Options {
opts := &Options{
CreateGPT: false,
Mode: os.O_RDWR | unix.O_CLOEXEC,
}

for _, setter := range setters {
Expand Down
4 changes: 3 additions & 1 deletion blockdevice/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os"
"path/filepath"

"golang.org/x/sys/unix"

"github.com/talos-systems/go-blockdevice/blockdevice"
"github.com/talos-systems/go-blockdevice/blockdevice/filesystem"
"github.com/talos-systems/go-blockdevice/blockdevice/filesystem/iso9660"
Expand Down Expand Up @@ -149,7 +151,7 @@ func probe(devpath string) (devpaths []string) {
// Start by opening the block device.
// If a partition table was not found, it is still possible that a
// file system exists without a partition table.
bd, err := blockdevice.Open(devpath)
bd, err := blockdevice.Open(devpath, blockdevice.WithMode(os.O_RDONLY|unix.O_CLOEXEC))
if err != nil {
//nolint: errcheck
if sb, _ := filesystem.Probe(devpath); sb != nil {
Expand Down

0 comments on commit a9cf3be

Please sign in to comment.