-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdriver.go
270 lines (232 loc) · 5.96 KB
/
driver.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"os/exec"
"path"
"strings"
"sync"
"github.com/docker/go-plugins-helpers/volume"
)
type lvmDriver struct {
home string
vgConfig string
volumes map[string]*vol
count map[string]int
mu sync.RWMutex
}
type vol struct {
Name string `json:"name"`
MountPoint string `json:"mountpoint"`
}
func newDriver(home, vgConfig string) *lvmDriver {
return &lvmDriver{
home: home,
vgConfig: vgConfig,
volumes: make(map[string]*vol),
count: make(map[string]int),
}
}
func (l *lvmDriver) Create(req volume.Request) volume.Response {
l.mu.Lock()
defer l.mu.Unlock()
if v, exists := l.volumes[req.Name]; exists {
return resp(v.MountPoint)
}
vgName, err := getVolumegroupName(l.vgConfig)
if err != nil {
return resp(err)
}
cmdArgs := []string{"-n", req.Name}
if s, ok := req.Options["size"]; ok && s != "" {
cmdArgs = append(cmdArgs, "--size", s)
}
cmdArgs = append(cmdArgs, vgName)
cmd := exec.Command("lvcreate", cmdArgs...)
if out, err := cmd.CombinedOutput(); err != nil {
return resp(fmt.Errorf("%s", string(out)))
}
cmd = exec.Command("mkfs.xfs", fmt.Sprintf("/dev/%s/%s", vgName, req.Name))
if out, err := cmd.CombinedOutput(); err != nil {
return resp(fmt.Errorf("%s", string(out)))
}
mp := getMountpoint(l.home, req.Name)
if err := os.MkdirAll(mp, 0700); err != nil {
return resp(err)
}
v := &vol{req.Name, mp}
l.volumes[v.Name] = v
l.count[v.Name] = 0
if err := saveToDisk(l.volumes, l.count); err != nil {
return resp(err)
}
return resp(v.MountPoint)
}
func (l *lvmDriver) List(req volume.Request) volume.Response {
l.mu.RLock()
defer l.mu.RUnlock()
var ls []*volume.Volume
for _, vol := range l.volumes {
v := &volume.Volume{
Name: vol.Name,
Mountpoint: vol.MountPoint,
}
ls = append(ls, v)
}
return volume.Response{Volumes: ls}
}
func (l *lvmDriver) Get(req volume.Request) volume.Response {
l.mu.RLock()
defer l.mu.RUnlock()
v, exists := l.volumes[req.Name]
if !exists {
return resp(fmt.Errorf("no such volume"))
}
var res volume.Response
res.Volume = &volume.Volume{
Name: v.Name,
Mountpoint: v.MountPoint,
}
return res
}
func (l *lvmDriver) Remove(req volume.Request) volume.Response {
l.mu.Lock()
defer l.mu.Unlock()
if err := os.RemoveAll(getMountpoint(l.home, req.Name)); err != nil {
return resp(err)
}
vgName, err := getVolumegroupName(l.vgConfig)
if err != nil {
return resp(err)
}
cmd := exec.Command("lvremove", "--force", fmt.Sprintf("%s/%s", vgName, req.Name))
if out, err := cmd.CombinedOutput(); err != nil {
return resp(fmt.Errorf("%s", string(out)))
}
delete(l.count, req.Name)
delete(l.volumes, req.Name)
if err := saveToDisk(l.volumes, l.count); err != nil {
return resp(err)
}
return resp(getMountpoint(l.home, req.Name))
}
func (l *lvmDriver) Path(req volume.Request) volume.Response {
return resp(getMountpoint(l.home, req.Name))
}
func (l *lvmDriver) Mount(req volume.Request) volume.Response {
l.mu.Lock()
defer l.mu.Unlock()
l.count[req.Name]++
vgName, err := getVolumegroupName(l.vgConfig)
if err != nil {
return resp(err)
}
if l.count[req.Name] == 1 {
cmd := exec.Command("mount", fmt.Sprintf("/dev/%s/%s", vgName, req.Name), getMountpoint(l.home, req.Name))
if out, err := cmd.CombinedOutput(); err != nil {
return resp(fmt.Errorf("%s", string(out)))
}
}
if err := saveToDisk(l.volumes, l.count); err != nil {
return resp(err)
}
return resp(getMountpoint(l.home, req.Name))
}
func (l *lvmDriver) Unmount(req volume.Request) volume.Response {
l.mu.Lock()
defer l.mu.Unlock()
l.count[req.Name]--
if l.count[req.Name] == 0 {
cmd := exec.Command("umount", getMountpoint(l.home, req.Name))
if out, err := cmd.CombinedOutput(); err != nil {
return resp(fmt.Errorf("%s", string(out)))
}
}
if err := saveToDisk(l.volumes, l.count); err != nil {
return resp(err)
}
return resp(getMountpoint(l.home, req.Name))
}
var allowedConfKeys = map[string]bool{
"VOLUME_GROUP": true,
}
func getVolumegroupName(vgConfig string) (string, error) {
vgName := ""
inFile, err := os.Open(vgConfig)
if err != nil {
return "", err
}
defer inFile.Close()
scanner := bufio.NewScanner(inFile)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
str := scanner.Text()
if strings.HasPrefix(str, "#") {
continue
}
vgSlice := strings.SplitN(str, "=", 2)
if !allowedConfKeys[vgSlice[0]] || len(vgSlice) == 1 {
continue
}
vgName = vgSlice[1]
break
}
if err := scanner.Err(); err != nil {
return "", err
}
if vgName == "" {
return "", fmt.Errorf("Volume group name must be provided for volume creation. Please update the config file %s with volume group name.", vgConfig)
}
return strings.TrimSpace(vgName), nil
}
func getMountpoint(home, name string) string {
return path.Join(home, name)
}
func saveToDisk(volumes map[string]*vol, count map[string]int) error {
// Save volume store metadata.
fhVolumes, err := os.Create(lvmVolumesConfigPath)
if err != nil {
return err
}
defer fhVolumes.Close()
if err := json.NewEncoder(fhVolumes).Encode(&volumes); err != nil {
return err
}
// Save count store metadata.
fhCount, err := os.Create(lvmCountConfigPath)
if err != nil {
return err
}
defer fhCount.Close()
return json.NewEncoder(fhCount).Encode(&count)
}
func loadFromDisk(l *lvmDriver) error {
// Load volume store metadata
jsonVolumes, err := os.Open(lvmVolumesConfigPath)
if err != nil {
return err
}
defer jsonVolumes.Close()
if err := json.NewDecoder(jsonVolumes).Decode(&l.volumes); err != nil {
return err
}
// Load count store metadata
jsonCount, err := os.Open(lvmCountConfigPath)
if err != nil {
return err
}
defer jsonCount.Close()
return json.NewDecoder(jsonCount).Decode(&l.count)
}
func resp(r interface{}) volume.Response {
switch t := r.(type) {
case error:
return volume.Response{Err: t.Error()}
case string:
return volume.Response{Mountpoint: t}
default:
return volume.Response{Err: "bad value writing response"}
}
}