forked from solo-io/unik
/
device.go
92 lines (68 loc) · 1.45 KB
/
device.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package os
import (
"github.com/emc-advanced-dev/pkg/errors"
"fmt"
"os"
)
type DiskSize interface {
ToPartedFormat() string
ToBytes() Bytes
}
type Bytes int64
func (s Bytes) ToPartedFormat() string {
return fmt.Sprintf("%dB", uint64(s))
}
func (s Bytes) ToBytes() Bytes {
return s
}
type MegaBytes int64
func (s MegaBytes) ToPartedFormat() string {
return fmt.Sprintf("%dMiB", uint64(s))
}
func (s MegaBytes) ToBytes() Bytes {
return Bytes(s << 20)
}
type GigaBytes int64
func (s GigaBytes) ToPartedFormat() string {
return fmt.Sprintf("%dGiB", uint64(s))
}
func (s GigaBytes) ToBytes() Bytes {
return Bytes(s << 30)
}
type Sectors int64
const SectorSize = 512
func (s Sectors) ToPartedFormat() string {
return fmt.Sprintf("%ds", uint64(s))
}
func (s Sectors) ToBytes() Bytes {
return Bytes(s * SectorSize)
}
func ToSectors(b DiskSize) (Sectors, error) {
inBytes := b.ToBytes()
if inBytes%SectorSize != 0 {
return 0, errors.New("can't convert to sectors", nil)
}
return Sectors(inBytes / SectorSize), nil
}
type BlockDevice string
func (b BlockDevice) Name() string {
return string(b)
}
type Partitioner interface {
MakeTable() error
MakePart(partType string, start, size DiskSize) error
}
type Resource interface {
Acquire() (BlockDevice, error)
Release() error
}
type Part interface {
Resource
Size() DiskSize
Offset() DiskSize
Get() BlockDevice
}
func IsExists(f string) bool {
_, err := os.Stat(f)
return !os.IsNotExist(err)
}