-
Notifications
You must be signed in to change notification settings - Fork 19
/
volumes.go
186 lines (177 loc) · 4.66 KB
/
volumes.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package manager
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"syscall"
"github.com/Symantec/Dominator/lib/format"
proto "github.com/Symantec/Dominator/proto/hypervisor"
)
const (
procMounts = "/proc/mounts"
sysClassBlock = "/sys/class/block"
)
type mountInfo struct {
mountPoint string
size uint64
}
func getFreeSpace(dirname string, freeSpaceTable map[string]uint64) (
uint64, error) {
if freeSpace, ok := freeSpaceTable[dirname]; ok {
return freeSpace, nil
}
var statbuf syscall.Statfs_t
if err := syscall.Statfs(dirname, &statbuf); err != nil {
return 0, fmt.Errorf("error statfsing: %s: %s", dirname, err)
}
freeSpace := uint64(statbuf.Bfree * uint64(statbuf.Bsize))
freeSpaceTable[dirname] = freeSpace
return freeSpace, nil
}
func getMounts() (map[string]string, error) {
file, err := os.Open(procMounts)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
mounts := make(map[string]string)
for scanner.Scan() {
line := scanner.Text()
var device, mountPoint, fsType, fsOptions, junk string
nScanned, err := fmt.Sscanf(line, "%s %s %s %s %s",
&device, &mountPoint, &fsType, &fsOptions, &junk)
if err != nil {
return nil, err
}
if nScanned < 4 {
return nil, errors.New(fmt.Sprintf("only read %d values from %s",
nScanned, line))
}
if mountPoint == "/boot" {
continue
}
if !strings.HasPrefix(device, "/dev/") {
continue
}
if target, err := filepath.EvalSymlinks(device); err != nil {
return nil, err
} else {
device = target
}
device = device[5:]
if _, ok := mounts[device]; !ok { // Pick the first mount point.
mounts[device] = mountPoint
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return mounts, nil
}
func getVolumeDirectories() ([]string, error) {
mounts, err := getMounts()
if err != nil {
return nil, err
}
var mountPointsToUse []string
biggestMounts := make(map[string]mountInfo)
for device, mountPoint := range mounts {
if device == "root" { // Ignore this dumb shit.
continue
}
sysDir := filepath.Join(sysClassBlock, device)
linkTarget, err := os.Readlink(sysDir)
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, err
}
_, err = os.Stat(filepath.Join(sysDir, "partition"))
if err != nil {
if os.IsNotExist(err) { // Not a partition: easy!
mountPointsToUse = append(mountPointsToUse, mountPoint)
continue
}
return nil, err
}
var statbuf syscall.Statfs_t
if err := syscall.Statfs(mountPoint, &statbuf); err != nil {
return nil, fmt.Errorf("error statfsing: %s: %s", mountPoint, err)
}
size := uint64(statbuf.Blocks * uint64(statbuf.Bsize))
parentDevice := filepath.Base(filepath.Dir(linkTarget))
if biggestMount, ok := biggestMounts[parentDevice]; !ok {
biggestMounts[parentDevice] = mountInfo{mountPoint, size}
} else if size > biggestMount.size {
biggestMounts[parentDevice] = mountInfo{mountPoint, size}
}
}
for _, biggestMount := range biggestMounts {
mountPointsToUse = append(mountPointsToUse, biggestMount.mountPoint)
}
var volumeDirectories []string
for _, mountPoint := range mountPointsToUse {
volumeDirectories = append(volumeDirectories,
filepath.Join(mountPoint, "hyper-volumes"))
}
sort.Strings(volumeDirectories)
return volumeDirectories, nil
}
func (m *Manager) findFreeSpace(size uint64, freeSpaceTable map[string]uint64,
position *int) (string, error) {
if *position >= len(m.volumeDirectories) {
*position = 0
}
startingPosition := *position
for {
freeSpace, err := getFreeSpace(m.volumeDirectories[*position],
freeSpaceTable)
if err != nil {
return "", err
}
if size < freeSpace {
dirname := m.volumeDirectories[*position]
freeSpaceTable[dirname] -= size
return dirname, nil
}
*position++
if *position >= len(m.volumeDirectories) {
*position = 0
}
if *position == startingPosition {
return "", fmt.Errorf("not enough free space for %s volume",
format.FormatBytes(size))
}
}
}
func (m *Manager) getVolumeDirectories(rootSize uint64,
volumes []proto.Volume, spreadVolumes bool) ([]string, error) {
sizes := make([]uint64, 1, len(volumes)+1)
sizes[0] = rootSize
for _, volume := range volumes {
if volume.Size > 0 {
sizes = append(sizes, volume.Size)
}
}
freeSpaceTable := make(map[string]uint64, len(m.volumeDirectories))
directoriesToUse := make([]string, 0, len(sizes))
position := 0
for len(sizes) > 0 {
dirname, err := m.findFreeSpace(sizes[0], freeSpaceTable, &position)
if err != nil {
return nil, err
}
directoriesToUse = append(directoriesToUse, dirname)
sizes = sizes[1:]
if spreadVolumes {
position++
}
}
return directoriesToUse, nil
}