-
Notifications
You must be signed in to change notification settings - Fork 13
/
drive.go
61 lines (55 loc) · 1.44 KB
/
drive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package filesystem
import (
"os"
"path/filepath"
"regexp"
"time"
"github.com/mvisonneau/go-ebsnvme/pkg/ebsnvme"
)
//DoesDriveExistWithTimeout makes 10 attempts, 2 second sleep between each, to stat a drive to check for its existence
func DoesDriveExistWithTimeout(driveName string, maxAttempts int) bool {
var attempts int
for !DoesDriveExist(driveName) {
time.Sleep(time.Duration(2 * time.Second))
attempts++
if attempts >= maxAttempts {
return false
}
}
return true
}
//DoesDriveExist does a single stat call to check if a drive exists
func DoesDriveExist(driveName string) bool {
if _, err := os.Stat(driveName); os.IsNotExist(err) {
for _, file := range listNvmeBlockDevices() {
if d, _ := ebsnvme.ScanDevice(file); d.Name == driveName {
return true
}
}
return false
}
return true
}
// GetLocalBlockDeviceName returns the actual name of the block device seen
// within the instance (useful for nitros)
func GetActualBlockDeviceName(name string) (string, error) {
for _, device := range listNvmeBlockDevices() {
if d, _ := ebsnvme.ScanDevice(device); d.Name == name {
return device, nil
}
}
if _, err := os.Stat(name); os.IsNotExist(err) {
return "", err
}
return name, nil
}
func listNvmeBlockDevices() (devices []string) {
re := regexp.MustCompile("(^\\/dev\\/nvme[0-9]+n1$)")
f, _ := filepath.Glob("/dev/nvme*")
for _, d := range f {
if re.Match([]byte(d)) {
devices = append(devices, d)
}
}
return
}