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 <artem.chernyshev@talos-systems.com>
  • Loading branch information
Unix4ever committed Sep 22, 2021
1 parent d981156 commit e367f9d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
9 changes: 9 additions & 0 deletions blockdevice/blockdevice_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"os"

"golang.org/x/sys/unix"

"github.com/talos-systems/go-blockdevice/blockdevice/partition/gpt"
)

Expand All @@ -18,6 +20,13 @@ type BlockDevice struct {
f *os.File
}

const (
// ReadonlyMode readonly mode.
ReadonlyMode = unix.O_CLOEXEC | os.O_RDONLY
// DefaultMode read write.
DefaultMode = unix.O_CLOEXEC | os.O_RDWR
)

// Open initializes and returns a block device.
// TODO(andrewrynhard): Use BLKGETSIZE ioctl to get the size.
func Open(devname string, setters ...Option) (bd *BlockDevice, err error) {
Expand Down
8 changes: 6 additions & 2 deletions blockdevice/blockdevice_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ const (
BLKZEROOUT = 4735
)

// Fast wipe parameters.
const (
// FastWipeRange fast wipe block.
FastWipeRange = 1024 * 1024
// ReadonlyMode readonly mode.
ReadonlyMode = unix.O_CLOEXEC | os.O_RDONLY
// DefaultMode read write.
DefaultMode = unix.O_CLOEXEC | os.O_RDWR
)

// BlockDevice represents a block device.
Expand All @@ -49,7 +53,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
7 changes: 7 additions & 0 deletions blockdevice/blockdevice_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ type BlockDevice struct {
f *os.File
}

const (
// ReadonlyMode readonly mode.
ReadonlyMode = os.O_RDONLY
// DefaultMode read write.
DefaultMode = os.O_RDWR
)

// Open initializes and returns a block device.
// TODO(andrewrynhard): Use BLKGETSIZE ioctl to get the size.
func Open(devname string, setters ...Option) (bd *BlockDevice, err error) {
Expand Down
9 changes: 9 additions & 0 deletions blockdevice/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package blockdevice
type Options struct {
CreateGPT bool
ExclusiveLock bool
Mode int
}

// Option is the functional option func.
Expand All @@ -27,10 +28,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: DefaultMode,
}

for _, setter := range setters {
Expand Down
2 changes: 1 addition & 1 deletion blockdevice/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,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(blockdevice.ReadonlyMode))
if err != nil {
//nolint: errcheck
if sb, _ := filesystem.Probe(devpath); sb != nil {
Expand Down

0 comments on commit e367f9d

Please sign in to comment.