Skip to content

Commit e367f9d

Browse files
committed
feat: make probe always open blockdevices in readonly mode
Fixes: #44 Signed-off-by: Artem Chernyshev <artem.chernyshev@talos-systems.com>
1 parent d981156 commit e367f9d

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

blockdevice/blockdevice_darwin.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"fmt"
99
"os"
1010

11+
"golang.org/x/sys/unix"
12+
1113
"github.com/talos-systems/go-blockdevice/blockdevice/partition/gpt"
1214
)
1315

@@ -18,6 +20,13 @@ type BlockDevice struct {
1820
f *os.File
1921
}
2022

23+
const (
24+
// ReadonlyMode readonly mode.
25+
ReadonlyMode = unix.O_CLOEXEC | os.O_RDONLY
26+
// DefaultMode read write.
27+
DefaultMode = unix.O_CLOEXEC | os.O_RDWR
28+
)
29+
2130
// Open initializes and returns a block device.
2231
// TODO(andrewrynhard): Use BLKGETSIZE ioctl to get the size.
2332
func Open(devname string, setters ...Option) (bd *BlockDevice, err error) {

blockdevice/blockdevice_linux.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ const (
2929
BLKZEROOUT = 4735
3030
)
3131

32-
// Fast wipe parameters.
3332
const (
33+
// FastWipeRange fast wipe block.
3434
FastWipeRange = 1024 * 1024
35+
// ReadonlyMode readonly mode.
36+
ReadonlyMode = unix.O_CLOEXEC | os.O_RDONLY
37+
// DefaultMode read write.
38+
DefaultMode = unix.O_CLOEXEC | os.O_RDWR
3539
)
3640

3741
// BlockDevice represents a block device.
@@ -49,7 +53,7 @@ func Open(devname string, setters ...Option) (bd *BlockDevice, err error) {
4953

5054
var f *os.File
5155

52-
if f, err = os.OpenFile(devname, os.O_RDWR|unix.O_CLOEXEC, os.ModeDevice); err != nil {
56+
if f, err = os.OpenFile(devname, opts.Mode, os.ModeDevice); err != nil {
5357
return nil, err
5458
}
5559

blockdevice/blockdevice_windows.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ type BlockDevice struct {
1818
f *os.File
1919
}
2020

21+
const (
22+
// ReadonlyMode readonly mode.
23+
ReadonlyMode = os.O_RDONLY
24+
// DefaultMode read write.
25+
DefaultMode = os.O_RDWR
26+
)
27+
2128
// Open initializes and returns a block device.
2229
// TODO(andrewrynhard): Use BLKGETSIZE ioctl to get the size.
2330
func Open(devname string, setters ...Option) (bd *BlockDevice, err error) {

blockdevice/options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package blockdevice
88
type Options struct {
99
CreateGPT bool
1010
ExclusiveLock bool
11+
Mode int
1112
}
1213

1314
// Option is the functional option func.
@@ -27,10 +28,18 @@ func WithExclusiveLock(o bool) Option {
2728
}
2829
}
2930

31+
// WithMode opens blockdevice in a specific mode.
32+
func WithMode(value int) Option {
33+
return func(args *Options) {
34+
args.Mode = value
35+
}
36+
}
37+
3038
// NewDefaultOptions initializes a Options struct with default values.
3139
func NewDefaultOptions(setters ...Option) *Options {
3240
opts := &Options{
3341
CreateGPT: false,
42+
Mode: DefaultMode,
3443
}
3544

3645
for _, setter := range setters {

blockdevice/probe/probe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func probe(devpath string) (devpaths []string) {
149149
// Start by opening the block device.
150150
// If a partition table was not found, it is still possible that a
151151
// file system exists without a partition table.
152-
bd, err := blockdevice.Open(devpath)
152+
bd, err := blockdevice.Open(devpath, blockdevice.WithMode(blockdevice.ReadonlyMode))
153153
if err != nil {
154154
//nolint: errcheck
155155
if sb, _ := filesystem.Probe(devpath); sb != nil {

0 commit comments

Comments
 (0)