/
commands.go
322 lines (286 loc) · 9.05 KB
/
commands.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
/*
Copyright 2017 Google Inc.
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 commands
import (
"errors"
"fmt"
"os/exec"
"strings"
"github.com/anywhy/lvmd/pkg/commands/parser"
"golang.org/x/net/context"
)
// ListLV lists lvm volumes
func ListLV(ctx context.Context, listspec string) ([]*parser.LV, error) {
cmd := exec.Command("lvs", "--units=b", "--separator=<:SEP:>", "--nosuffix", "--noheadings",
"-o", "lv_name,lv_size,lv_uuid,lv_attr,copy_percent,lv_kernel_major,lv_kernel_minor,lv_tags", "--nameprefixes", "-a", listspec)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
outStr := strings.TrimSpace(string(out))
outLines := strings.Split(outStr, "\n")
lvs := make([]*parser.LV, len(outLines))
for i, line := range outLines {
line = strings.TrimSpace(line)
lv, err := parser.ParseLV(line)
if err != nil {
return nil, err
}
lvs[i] = lv
}
return lvs, nil
}
// CreateLV creates a new volume
func CreateLV(ctx context.Context, vg string, name string, size uint64, mirrors uint32, tags []string) (string, error) {
if size == 0 {
return "", errors.New("size must be greater than 0")
}
args := []string{"-v", "-n", name, "-L", fmt.Sprintf("%db", size)}
if mirrors > 0 {
args = append(args, "-m", fmt.Sprintf("%d", mirrors), "--nosync")
}
for _, tag := range tags {
args = append(args, "--add-tag", tag)
}
args = append(args, vg)
cmd := exec.Command("lvcreate", args...)
out, err := cmd.CombinedOutput()
return string(out), err
}
// ProtectedTagName is a tag that prevents RemoveLV & RemoveVG from removing a volume
const ProtectedTagName = "protected"
// RemoveLV removes a volume
func RemoveLV(ctx context.Context, vg string, name string) (string, error) {
lvs, err := ListLV(ctx, fmt.Sprintf("%s/%s", vg, name))
if err != nil {
return "", fmt.Errorf("failed to list LVs: %v", err)
}
if len(lvs) != 1 {
return "", fmt.Errorf("expected 1 LV, got %d", len(lvs))
}
for _, tag := range lvs[0].Tags {
if tag == ProtectedTagName {
return "", errors.New("volume is protected")
}
}
cmd := exec.Command("lvremove", "-v", "-f", fmt.Sprintf("%s/%s", vg, name))
out, err := cmd.CombinedOutput()
return string(out), err
}
// CloneLV clones a volume via dd
func CloneLV(ctx context.Context, src, dest string) (string, error) {
// FIXME(farcaller): bloody insecure. And broken.
cmd := exec.Command("dd", fmt.Sprintf("if=%s", src), fmt.Sprintf("of=%s", dest), "bs=4M")
out, err := cmd.CombinedOutput()
return string(out), err
}
// AddTagLV add Volume Group tag
func AddTagLV(ctx context.Context, vg string, name string, tags []string) (string, error) {
lvs, err := ListLV(ctx, fmt.Sprintf("%s/%s", vg, name))
if err != nil {
return "", fmt.Errorf("failed to list LVs: %v", err)
}
if len(lvs) != 1 {
return "", fmt.Errorf("expected 1 LV, got %d", len(lvs))
}
args := make([]string, 0)
for _, tag := range tags {
args = append(args, "--addtag", tag)
}
args = append(args, fmt.Sprintf("%s/%s", vg, name))
cmd := exec.Command("lvchange", args...)
out, err := cmd.CombinedOutput()
return string(out), err
}
// RemoveTagLV remove Volume Group tag
func RemoveTagLV(ctx context.Context, vg string, name string, tags []string) (string, error) {
lvs, err := ListLV(ctx, fmt.Sprintf("%s/%s", vg, name))
if err != nil {
return "", fmt.Errorf("failed to list LVs: %v", err)
}
if len(lvs) != 1 {
return "", fmt.Errorf("expected 1 LV, got %d", len(lvs))
}
args := make([]string, 0)
for _, tag := range tags {
args = append(args, "--deltag", tag)
}
args = append(args, fmt.Sprintf("%s/%s", vg, name))
cmd := exec.Command("lvchange", args...)
out, err := cmd.CombinedOutput()
return string(out), err
}
// ExtendLV extend lv
func ExtendLV(ctx context.Context, size string, name string) (string, error) {
if size == "" || name == "" {
return "", fmt.Errorf("Paramters error: %v, %v", size, name)
}
cmd := exec.Command("lvextend", "-L", size, name)
out, err := cmd.CombinedOutput()
return string(out), err
}
// ListVG lists lvm vg
func ListVG(ctx context.Context) ([]*parser.VG, error) {
cmd := exec.Command("vgs", "--units=b", "--separator=<:SEP:>", "--nosuffix", "--noheadings",
"-o", "vg_name,vg_size,vg_free,vg_uuid,vg_tags", "--nameprefixes", "-a")
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
outStr := strings.TrimSpace(string(out))
outLines := strings.Split(outStr, "\n")
vgs := make([]*parser.VG, len(outLines))
for i, line := range outLines {
line = strings.TrimSpace(line)
vg, err := parser.ParseVG(line)
if err != nil {
return nil, err
}
vgs[i] = vg
}
return vgs, nil
}
// CreateVG create a Volume Group
func CreateVG(ctx context.Context, name string, physicalVolume string, tags []string) (string, error) {
args := []string{name, physicalVolume, "-v"}
for _, tag := range tags {
args = append(args, "--add-tag", tag)
}
cmd := exec.Command("vgcreate", args...)
out, err := cmd.CombinedOutput()
return string(out), err
}
// RemoveVG remove a Volume Group
func RemoveVG(ctx context.Context, name string) (string, error) {
vgs, err := ListVG(ctx)
if err != nil {
return "", fmt.Errorf("failed to list VGs: %v", err)
}
var vg *parser.VG
for _, v := range vgs {
if v.Name == name {
vg = v
break
}
}
if vg == nil {
return "", fmt.Errorf("could not find vg to delete")
}
for _, tag := range vg.Tags {
if tag == ProtectedTagName {
return "", errors.New("volume is protected")
}
}
cmd := exec.Command("vgremove", "-v", "-f", name)
out, err := cmd.CombinedOutput()
return string(out), err
}
// ExtendVG extend a Volume Group
func ExtendVG(ctx context.Context, name string, physicalVolume string) (string, error) {
cmd := exec.Command("vgextend", name, physicalVolume)
out, err := cmd.CombinedOutput()
return string(out), err
}
// ReduceVG reduce a Volume Group
func ReduceVG(ctx context.Context, name string, physicalVolume string) (string, error) {
cmd := exec.Command("vgreduce", name, physicalVolume)
out, err := cmd.CombinedOutput()
return string(out), err
}
// CreatePV create physics volume
func CreatePV(ctx context.Context, block string) (string, error) {
args := []string{block, "-y", "-v"}
cmd := exec.Command("pvcreate", args...)
out, err := cmd.CombinedOutput()
return string(out), err
}
// RemovePV remove physics volume
func RemovePV(ctx context.Context, block string) (string, error) {
args := []string{block, "-y", "-v"}
cmd := exec.Command("pvremove", args...)
out, err := cmd.CombinedOutput()
return string(out), err
}
// ListPV list physics volume
func ListPV(ctx context.Context) ([]*parser.PV, error) {
cmd := exec.Command("pvs", "--units=b", "--separator=<:SEP:>", "--nosuffix", "--noheadings",
"-o", "pv_name,pv_size,pv_used,pv_free,pv_fmt,pv_uuid", "--nameprefixes", "-a")
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
outStr := strings.TrimSpace(string(out))
outLines := strings.Split(outStr, "\n")
pvs := make([]*parser.PV, len(outLines))
for i, line := range outLines {
line = strings.TrimSpace(line)
pv, err := parser.ParsePV(line)
if err != nil {
return nil, err
}
pvs[i] = pv
}
return pvs, nil
}
// Validate validate block
func Validate(ctx context.Context, block string) (bool, error) {
cmd := exec.Command("udevadm", "info", "--query=property", block)
out, err := cmd.CombinedOutput()
if err != nil {
return false, err
}
out1Str := strings.TrimSpace(string(out))
if strings.Contains(out1Str, "ID_PART_TABLE") || strings.Contains(out1Str, "ID_FS_TYPE") {
return false, nil
}
out, err = exec.Command("blkid").Output()
if err != nil {
return false, err
}
outputs := strings.Split(string(out), "\n")
for _, l := range outputs {
if !strings.Contains(l, block) {
continue
}
if strings.Contains(l, "PTTYPE=") || strings.Contains(l, "TYPE=") {
return false, nil
}
}
return true, nil
}
// Destory destory block
func Destory(ctx context.Context, block string) (string, error) {
cmd := exec.Command("wipefs", "-af", block)
out, err := cmd.CombinedOutput()
return string(out), err
}
// Match match block
func Match(ctx context.Context, block string) string {
cmd := exec.Command("pvs", "--noheadings", "--separator=#", "--nosuffix", block)
out, err := cmd.CombinedOutput()
if err != nil {
return ""
}
outStr := strings.TrimSpace(string(out))
return strings.Split(outStr, "#")[1]
}
// GetPVNum get number of physics volume
func GetPVNum(ctx context.Context, name string) (string, error) {
cmd := exec.Command("vgs", "--noheadings", "--separator=#", "--nosuffix", name)
out, err := cmd.CombinedOutput()
if err != nil {
return "0", err
}
outStr := strings.TrimSpace(string(out))
return strings.Split(outStr, "#")[1], nil
}