/
lvmctrldserver.go
366 lines (340 loc) · 10.9 KB
/
lvmctrldserver.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lvmctrld
import (
"context"
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"
"time"
pb "github.com/aleofreddi/csi-sanlock-lvm/proto"
"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Credits to dave at https://stackoverflow.com/questions/40939261/golang-parse-strange-date-format
type lvmTime struct {
time.Time
}
func (t *lvmTime) UnmarshalJSON(buf []byte) error {
date, err := time.Parse("2006-01-02 15:04:05 -0700", strings.Trim(string(buf), `"`))
if err != nil {
return err
}
t.Time = date
return nil
}
type lvmReportLvs struct {
LvName string `json:"lv_name"`
VgName string `json:"vg_name"`
LvAttr string `json:"lv_attr"`
LvSize uint64 `json:"lv_size,string"`
PoolLv string `json:"pool_lv"`
Origin string `json:"origin"`
DataPercent string `json:"data_percent"`
MetadataPercent string `json:"metadata_percent"`
MovePv string `json:"move_pv"`
MirrorLog string `json:"mirror_log"`
CopyPercent string `json:"copy_percent"`
ConvertLv string `json:"convert_lv"`
LvTags string `json:"lv_tags"`
LvRole string `json:"lv_role"`
LvTime lvmTime `json:"lv_time,string"`
LvDeviceOpen string `json:"lv_device_open"`
}
type lvmReportVgs struct {
VgName string `json:"vg_name"`
PvCount uint32 `json:"pv_count,string"`
LvCount uint32 `json:"lv_count,string"`
SnapCount uint32 `json:"snap_count,string"`
VgAttr string `json:"vg_attr"`
VgSize uint64 `json:"vg_size,string"`
VgFree uint64 `json:"vg_free,string"`
VgTags string `json:"vg_tags"`
}
type lvmReport struct {
Report []struct {
Lvs []lvmReportLvs `json:"lv"`
Vgs []lvmReportVgs `json:"vg"`
} `json:"report"`
}
var (
// According to https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/logical_volume_manager_administration/lvm_tags,
// LVM tags should match the following regex.
tagRe = regexp.MustCompile("^[A-Za-z0-9_+.\\-/=!:#&]+$")
lvLockedRe = regexp.MustCompile(`(?mi)^\s*LV locked by other host`)
lvExists = regexp.MustCompile(`(?mi)^\s*Logical Volume "[^"]+" already exists in volume group`)
lvNotFound = []*regexp.Regexp{
regexp.MustCompile(`(?mi)^\s*Failed to find logical volume "[^"]+"`),
regexp.MustCompile(`(?mi)^\s*Logical volume \S+ not found in volume group \S+.`),
}
vgNotFound = regexp.MustCompile(`(?mi)^\s*Volume group "[^"]+" not found`)
lvOutOfRange = []*regexp.Regexp{
regexp.MustCompile(`(?mi)^\s*Volume group "[^"]+" has insufficient free space`),
regexp.MustCompile(`(?mi)^\s*Insufficient free space: \d+ extents needed, but only \d+ available`),
}
lvSizeMatches = regexp.MustCompile(`(?mi)^\s*New size \(\d+ extents\) matches existing size \(\d+ extents\)`)
)
type lvmctrldServer struct {
id uint16
cmd runner
}
func NewLvmctrldServer(id uint16) *lvmctrldServer {
return &lvmctrldServer{
id,
NewCommander(),
}
}
func (s lvmctrldServer) GetStatus(ctx context.Context, req *pb.GetStatusRequest) (*pb.GetStatusResponse, error) {
return &pb.GetStatusResponse{
NodeId: uint32(s.id),
}, nil
}
func (s lvmctrldServer) Vgs(_ context.Context, req *pb.VgsRequest) (*pb.VgsResponse, error) {
args := []string{
"--options", "vg_name,pv_count,lv_count,snap_count,vg_attr,vg_size,vg_free,vg_tags",
"--units", "b",
"--nosuffix",
"--reportformat", "json",
}
if req.GetSelect() != "" {
args = append(args, "-S", req.GetSelect())
}
if len(req.Target) > 0 {
args = append(args, req.Target...)
}
out, err := runReport(s.cmd, "vgs", args...)
if err != nil {
return nil, err
}
if len(out.Report) != 1 {
return nil, errors.New("unexpected multiple reports")
}
vgs := make([]*pb.VolumeGroup, len(out.Report[0].Vgs))
for i, v := range out.Report[0].Vgs {
vgs[i] = lvmToVolumeGroup(&v)
}
return &pb.VgsResponse{
Vgs: vgs,
}, nil
}
func (s lvmctrldServer) LvCreate(_ context.Context, req *pb.LvCreateRequest) (*pb.LvCreateResponse, error) {
args := []string{
"-y",
"-L", fmt.Sprintf("%db", req.Size),
}
args = lvmToActivationMode(args, req.GetActivate())
if req.Origin != "" {
args = append(args, "-s", req.Origin)
}
for _, tag := range req.LvTags {
if !tagRe.MatchString(tag) {
return nil, fmt.Errorf("invalid tag %s", tag)
}
args = append(args, "--addtag", tag)
}
args = append(args, "-n", fmt.Sprintf("%s/%s", req.VgName, req.LvName))
code, stdout, stderr, err := s.cmd.Exec("lvcreate", args...)
if code != 0 || err != nil {
return nil, parseLvmError(code, stdout, stderr)
}
return &pb.LvCreateResponse{}, nil
}
func (s lvmctrldServer) LvRemove(_ context.Context, req *pb.LvRemoveRequest) (*pb.LvRemoveResponse, error) {
args := []string{
"-f",
}
if req.GetSelect() != "" {
args = append(args, "-S", req.GetSelect())
}
if len(req.Target) > 0 {
args = append(args, req.Target...)
}
code, stdout, stderr, err := s.cmd.Exec("lvremove", args...)
if code != 0 || err != nil {
return nil, parseLvmError(code, stdout, stderr)
}
return &pb.LvRemoveResponse{}, nil
}
func (s lvmctrldServer) Lvs(_ context.Context, req *pb.LvsRequest) (*pb.LvsResponse, error) {
args := []string{
"--options", "lv_name,vg_name,lv_attr,lv_size,pool_lv,origin,data_percent,metadata_percent,move_pv,mirror_log,copy_percent,convert_lv,lv_tags,lv_role,lv_time,lv_device_open",
"--units", "b",
"--nosuffix",
"--reportformat", "json",
}
if req.Select != "" {
args = append(args, "-S", req.Select)
}
if len(req.Sort) > 0 {
args = append(args, "-O", strings.Join(req.Sort, ","))
}
if len(req.Target) > 0 {
args = append(args, req.Target...)
}
out, err := runReport(s.cmd, "lvs", args...)
if err != nil {
return nil, err
}
if len(out.Report) != 1 {
return nil, errors.New("unexpected multiple reports")
}
lvs := make([]*pb.LogicalVolume, len(out.Report[0].Lvs))
for i, v := range out.Report[0].Lvs {
lvs[i] = lvmToLogicalVolume(&v)
}
return &pb.LvsResponse{
Lvs: lvs,
}, nil
}
func (s lvmctrldServer) LvResize(ctx context.Context, req *pb.LvResizeRequest) (*pb.LvResizeResponse, error) {
args := []string{
"-f",
"-L", fmt.Sprintf("%db", req.Size),
fmt.Sprintf("%s/%s", req.VgName, req.LvName),
}
code, stdout, stderr, err := s.cmd.Exec("lvresize", args...)
if code != 0 || err != nil {
return nil, parseLvmError(code, stdout, stderr)
}
return &pb.LvResizeResponse{}, nil
}
func (s lvmctrldServer) LvChange(ctx context.Context, req *pb.LvChangeRequest) (*pb.LvChangeResponse, error) {
args := make([]string, 0)
args = lvmToActivationMode(args, req.GetActivate())
for _, tag := range req.AddTag {
args = append(args, "--addtag", tag)
}
for _, tag := range req.DelTag {
args = append(args, "--deltag", tag)
}
if req.GetSelect() != "" {
args = append(args, "-S", req.GetSelect())
}
if len(req.GetTarget()) > 0 {
args = append(args, req.GetTarget()...)
}
code, stdout, stderr, err := s.cmd.Exec("lvchange", args...)
if code != 0 || err != nil {
return nil, parseLvmError(code, stdout, stderr)
}
return &pb.LvChangeResponse{}, nil
}
func runReport(cmd runner, exe string, args ...string) (*lvmReport, error) {
code, stdout, stderr, err := cmd.Exec(exe, args...)
if code != 0 || err != nil {
return nil, parseLvmError(code, stdout, stderr)
}
var result lvmReport
if err := json.Unmarshal(stdout, &result); err != nil {
return nil, fmt.Errorf("failed to deserialize lvm report with error %v: %q", err, stdout)
}
return &result, nil
}
func lvmToActivationMode(args []string, activationMode pb.LvActivationMode) []string {
if activationMode == pb.LvActivationMode_LV_ACTIVATION_MODE_NONE {
return args
}
switch activationMode {
case pb.LvActivationMode_LV_ACTIVATION_MODE_ACTIVE_EXCLUSIVE:
return append(args, "-a", "ey")
case pb.LvActivationMode_LV_ACTIVATION_MODE_ACTIVE_SHARED:
return append(args, "-a", "sy")
case pb.LvActivationMode_LV_ACTIVATION_MODE_DEACTIVATE:
return append(args, "-a", "n")
default:
panic(fmt.Sprintf("unknown activation mode %d", activationMode))
}
}
func lvmToVolumeGroup(vg *lvmReportVgs) *pb.VolumeGroup {
return &pb.VolumeGroup{
VgName: vg.VgName,
PvCount: vg.PvCount,
LvCount: vg.LvCount,
SnapCount: vg.SnapCount,
VgAttr: vg.VgAttr,
VgSize: vg.VgSize,
VgFree: vg.VgFree,
VgTags: splitLvmField(vg.VgTags),
}
}
func lvmToLogicalVolume(lv *lvmReportLvs) *pb.LogicalVolume {
lvTime, _ := ptypes.TimestampProto(lv.LvTime.Time)
return &pb.LogicalVolume{
LvName: lv.LvName,
VgName: lv.VgName,
LvAttr: lv.LvAttr,
LvSize: lv.LvSize,
PoolLv: lv.PoolLv,
Origin: lv.Origin,
DataPercent: lv.DataPercent,
MetadataPercent: lv.MetadataPercent,
MovePv: lv.MovePv,
MirrorLog: lv.MirrorLog,
CopyPercent: lv.CopyPercent,
ConvertLv: lv.ConvertLv,
LvTags: splitLvmField(lv.LvTags),
LvRole: splitLvmField(lv.LvRole),
LvTime: lvTime,
LvDeviceOpen: decodeLvDeviceOpen(lv.LvDeviceOpen),
}
}
func decodeLvDeviceOpen(value string) pb.LvDeviceOpen {
if value == "open" {
return pb.LvDeviceOpen_LV_DEVICE_OPEN_OPEN
}
return pb.LvDeviceOpen_LV_DEVICE_OPEN_UNKNOWN
}
func splitLvmField(value string) []string {
if value == "" {
return []string{}
}
return strings.Split(value, ",")
}
func parseLvmError(code int, stdout, stderr []byte) error {
if code == 0 {
return nil
}
// On "ordinary" errors, lvm commands will return 5: anything other than 5 is then an unknown error
if code != 5 {
return fmt.Errorf("unexpected error: rc=%d stdout=%q stderr=%q", code, stdout, stderr)
}
// Check stderr to identify the failure reason
if lvExists.Match(stderr) {
return status.Errorf(codes.AlreadyExists, "target already exists")
}
if lvLockedRe.Match(stderr) {
return status.Errorf(codes.PermissionDenied, "target is locked by another host")
}
if vgNotFound.Match(stderr) {
return status.Errorf(codes.NotFound, "volume group does not exist")
}
for _, re := range lvNotFound {
if re.Match(stderr) {
return status.Errorf(codes.NotFound, "logical volume does not exist")
}
}
for _, re := range lvOutOfRange {
if re.Match(stderr) {
return status.Errorf(codes.OutOfRange, "insufficient free space")
}
}
if lvSizeMatches.Match(stderr) {
// We do not consider a size match an error
return nil
}
return fmt.Errorf("unexpected error: rc=%d stdout=%q stderr=%q", code, stdout, stderr)
}