Skip to content

Commit

Permalink
Change the read-only property to a attribute of the structure.
Browse files Browse the repository at this point in the history
Properties were intended to be things that does not apply to all disks.
The example was 'ephemeral'. On amazon, we would correctly identify
ephemeral disks, but on other platforms they *may* be ephemeral.

All disks are either writable or not, so lets just put this on the
structure.
  • Loading branch information
Scott Moser committed Dec 17, 2021
1 parent 226f8de commit 4c6822b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
19 changes: 12 additions & 7 deletions demo/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,29 @@ func diskShow(c *cli.Context) error {
}

func getDiskSet(mysys disko.System, paths ...string) (disko.DiskSet, error) {
matchAllSkipReadOnly := func(d disko.Disk) bool {
if _, ok := d.Properties[disko.ReadOnly]; ok == true {
return false
}
matchAll := func(d disko.Disk) bool {
return true
}

return getDiskSetFilter(mysys, matchAll, paths...)
}

func getDiskSetFilter(mysys disko.System, matcher disko.DiskFilter, paths ...string) (disko.DiskSet, error) {
if len(paths) == 0 || (len(paths) == 1 && paths[0] == "all") {
return mysys.ScanAllDisks(matchAllSkipReadOnly)
return mysys.ScanAllDisks(matcher)
}

return mysys.ScanDisks(matchAllSkipReadOnly, paths...)
return mysys.ScanDisks(matcher, paths...)
}

func diskWipe(c *cli.Context) error {
mysys := linux.System()

disks, err := getDiskSet(mysys, c.Args().Slice()...)
// only match read-write disks here.
disks, err := getDiskSetFilter(
mysys,
func(d disko.Disk) bool { return !d.ReadOnly },
c.Args().Slice()...)

if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ type Property string
const (
// Ephemeral - A cloud ephemeral disk.
Ephemeral Property = "EPHEMERAL"
// ReadOnly - A disk that cannot be modified, writes fail
ReadOnly Property = "READ-ONLY"
)

// PropertySet - a group of properties of a disk
Expand Down Expand Up @@ -324,6 +322,9 @@ type Disk struct {
// applicable it will return 0.
SectorSize uint `json:"sectorSize"`

// ReadOnly - cannot be written to.
ReadOnly bool `json:"read-only"`

// Type is the DiskType indicating the type of this disk. This value
// can be used to determine if the disk is of a particular media type like
// HDD, SSD or NVMe.
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/anuvu/disko
go 1.13

require (
github.com/Jeffail/gabs v1.4.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/rekby/gpt v0.0.0-20200219180433-a930afbc6edc
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down
42 changes: 25 additions & 17 deletions linux/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"regexp"
"strings"
"syscall"

"github.com/anuvu/disko"
Expand Down Expand Up @@ -83,23 +84,28 @@ func (ls *linuxSystem) ScanDisks(filter disko.DiskFilter,
return disks, nil
}

func getDiskReadOnlyProperty(d disko.UdevInfo) (bool, error) {
syspath, err := getSysPathForBlockDevicePath(d.Name)
func getDiskReadOnly(kname string) (bool, error) {
syspath, err := getSysPathForBlockDevicePath(kname)
if err != nil {
return false, err
}

syspathReadOnly := syspath + "/ro"
if disko.PathExists(syspathReadOnly) {
content, err := ioutil.ReadFile(syspathReadOnly)
if err != nil {
return false, fmt.Errorf("failed to read %s for %s", syspathReadOnly, d.Name)
}
if string(content) == "1\n" {
return true, nil
}
content, err := ioutil.ReadFile(syspathReadOnly)

if err != nil {
return false, err
}

val := strings.TrimRight(string(content), "\n")

if val == "1" {
return true, nil
} else if val == "0" {
return false, nil
}
return false, nil

return false, fmt.Errorf("unexpected value '%s' found in %s", syspathReadOnly, val)
}

func getDiskProperties(d disko.UdevInfo) disko.PropertySet {
Expand All @@ -113,15 +119,10 @@ func getDiskProperties(d disko.UdevInfo) disko.PropertySet {
props[disko.Ephemeral] = true
}

readOnly, err := getDiskReadOnlyProperty(d)
// only mark read-only prop if set
if err == nil && readOnly {
props[disko.ReadOnly] = readOnly
}

return props
}

// nolint: funlen
func (ls *linuxSystem) ScanDisk(devicePath string) (disko.Disk, error) {
var err error
var blockdev = true
Expand Down Expand Up @@ -167,6 +168,13 @@ func (ls *linuxSystem) ScanDisk(devicePath string) (disko.Disk, error) {
Properties: properties,
}

ro, err := getDiskReadOnly(disk.Name)
if err != nil {
return disk, err
}

disk.ReadOnly = ro

fh, err := os.Open(devicePath)
if err != nil {
return disk, err
Expand Down
9 changes: 0 additions & 9 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package disko

import (
"fmt"
"os"
"sort"
)

Expand Down Expand Up @@ -62,11 +61,3 @@ func findRangeGaps(ranges uRanges, min, max uint64) uRanges {

return ret
}

func PathExists(d string) bool {
_, err := os.Stat(d)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}

0 comments on commit 4c6822b

Please sign in to comment.