Skip to content

Commit

Permalink
sometimes device is already registered and throws error when you try
Browse files Browse the repository at this point in the history
to register it again
  • Loading branch information
rafalop committed Jul 13, 2022
1 parent f51fd8e commit 5ef8e2d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cmd/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"github.com/spf13/cobra"
"io/ioutil"
"os"
"regexp"
"strings"
"time"
"unicode"
)

var registerCmd = &cobra.Command{
Expand All @@ -29,6 +33,10 @@ func RunRegister(devices []string) {
} else {
err := ioutil.WriteFile(write_path, []byte(device), 0)
if err != nil {
if checkSysfs(device) {
fmt.Println(device, "is already registered.")
return
}
fmt.Println(err)
}
all = allDevs()
Expand All @@ -45,3 +53,31 @@ func RunRegister(devices []string) {
fmt.Println()
return
}

// Helper to check for bcache in sysfs for a device (means kernel already knows about the device)
func checkSysfs(device string) bool {
var sysfsPath string
sn := strings.Split(device, "/")
shortName := sn[len(sn)-1]
regexpString := `[0-9]+`
matched, _ := regexp.Match(regexpString, []byte(shortName))
if matched {
baseDev := strings.TrimRightFunc(shortName, func(r rune) bool {
return unicode.IsNumber(r)
})
sysfsPath = SYSFS_BLOCK_ROOT + baseDev + `/` + shortName + `/bcache`
} else {
sysfsPath = SYSFS_BLOCK_ROOT + shortName + `/bcache`
}
fmt.Println("searching for path:" + sysfsPath)

// Check for sysfs path a couple of times (udev is meant to auto register)
for i := 0; i < 1; i++ {
if _, err := os.Stat(sysfsPath); !os.IsNotExist(err) {
fmt.Println("Found path: " + sysfsPath)
return true
}
time.Sleep(time.Duration(1) * time.Second)
}
return false
}

0 comments on commit 5ef8e2d

Please sign in to comment.