-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.go
68 lines (55 loc) · 1.24 KB
/
util.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
62
63
64
65
66
67
68
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"time"
)
func getFilesystemType(dev string) (string, error) {
out, err := exec.Command("blkid", "-s", "TYPE", "-o", "value", dev).CombinedOutput()
if err != nil {
if len(out) == 0 {
return "", nil
}
return "", errors.New(string(out))
}
return string(out), nil
}
func formatFilesystem(dev string, label string) error {
out, err := exec.Command("mkfs.ext4", "-L", label, dev).CombinedOutput()
if err != nil {
return errors.New(string(out))
}
return nil
}
func findDeviceWithTimeout(volId string) (string, error) {
devicePaths := []string{
"/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_%s",
"/dev/disk/by-id/virtio-%.20s",
}
for i := 1; i <= 10; i++ {
time.Sleep(500 * time.Millisecond)
for _, devicePath := range devicePaths {
dev := fmt.Sprintf(devicePath, volId)
if _, err := os.Stat(dev); err != nil {
if !os.IsNotExist(err) {
return "", err
}
} else {
return dev, nil
}
}
}
return "", fmt.Errorf("Block device not found")
}
func isDirectoryPresent(path string) (bool, error) {
stat, err := os.Stat(path)
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
} else {
return stat.IsDir(), nil
}
}