-
Notifications
You must be signed in to change notification settings - Fork 30
/
util.go
413 lines (349 loc) · 10.7 KB
/
util.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package util
import (
"bytes"
"compress/gzip"
"context"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"io"
"io/fs"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"syscall"
"time"
"github.com/google/uuid"
lz4 "github.com/pierrec/lz4/v4"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
"k8s.io/apimachinery/pkg/util/wait"
mount "k8s.io/mount-utils"
)
const (
PreservedChecksumLength = 64
MountDir = "/var/lib/longhorn-backupstore-mounts"
)
var (
cmdTimeout = time.Minute // one minute by default
forceCleanupMountTimeout = 30 * time.Second
)
// NopCloser wraps an io.Witer as io.WriteCloser
// with noop Close
type NopCloser struct {
io.Writer
}
func (NopCloser) Close() error { return nil }
func fstypeToKind(fstype int64) (string, error) {
switch fstype {
case unix.NFS_SUPER_MAGIC:
return "nfs", nil
case unix.CIFS_SUPER_MAGIC, unix.SMB2_SUPER_MAGIC, unix.SMB_SUPER_MAGIC:
return "cifs", nil
default:
return "", fmt.Errorf("unknown fstype %v", fstype)
}
}
// GenerateName generates a 16-byte name
func GenerateName(prefix string) string {
suffix := strings.Replace(NewUUID(), "-", "", -1)
return prefix + "-" + suffix[:16]
}
// NewUUID generates an UUID
func NewUUID() string {
return uuid.New().String()
}
// GetChecksum gets the SHA256 of the given data
func GetChecksum(data []byte) string {
checksumBytes := sha512.Sum512(data)
checksum := hex.EncodeToString(checksumBytes[:])[:PreservedChecksumLength]
return checksum
}
// GetFileChecksum calculates the SHA256 of the file's content
func GetFileChecksum(filePath string) (string, error) {
f, err := os.Open(filePath)
if err != nil {
return "", err
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// CompressData compresses the given data using the specified compression method
func CompressData(method string, data []byte) (io.ReadSeeker, error) {
if method == "none" {
return bytes.NewReader(data), nil
}
var buffer bytes.Buffer
w, err := newCompressionWriter(method, &buffer)
if err != nil {
return nil, err
}
if _, err := w.Write(data); err != nil {
w.Close()
return nil, err
}
w.Close()
return bytes.NewReader(buffer.Bytes()), nil
}
// DecompressAndVerify decompresses the given data and verifies the data integrity
func DecompressAndVerify(method string, src io.Reader, checksum string) (io.Reader, error) {
r, err := newDecompressionReader(method, src)
if err != nil {
return nil, errors.Wrap(err, "failed to create decompression reader")
}
defer r.Close()
block, err := io.ReadAll(r)
if err != nil {
return nil, errors.Wrap(err, "failed to read decompressed data")
}
if GetChecksum(block) != checksum {
return nil, fmt.Errorf("checksum verification failed for block")
}
return bytes.NewReader(block), nil
}
func newCompressionWriter(method string, buffer io.Writer) (io.WriteCloser, error) {
switch method {
case "gzip":
return gzip.NewWriter(buffer), nil
case "lz4":
return lz4.NewWriter(buffer), nil
default:
return nil, fmt.Errorf("unsupported compression method: %v", method)
}
}
func newDecompressionReader(method string, r io.Reader) (io.ReadCloser, error) {
switch method {
case "none":
return io.NopCloser(r), nil
case "gzip":
return gzip.NewReader(r)
case "lz4":
return io.NopCloser(lz4.NewReader(r)), nil
default:
return nil, fmt.Errorf("unsupported decompression method: %v", method)
}
}
func Now() string {
return time.Now().UTC().Format(time.RFC3339)
}
func UnorderedEqual(x, y []string) bool {
if len(x) != len(y) {
return false
}
known := make(map[string]struct{})
for _, value := range x {
known[value] = struct{}{}
}
for _, value := range y {
if _, present := known[value]; !present {
return false
}
}
return true
}
func Filter(elements []string, predicate func(string) bool) []string {
var filtered []string
for _, elem := range elements {
if predicate(elem) {
filtered = append(filtered, elem)
}
}
return filtered
}
func ExtractNames(names []string, prefix, suffix string) []string {
result := []string{}
for _, f := range names {
// Remove additional slash if exists
f = strings.TrimLeft(f, "/")
// missing prefix or suffix
if !strings.HasPrefix(f, prefix) || !strings.HasSuffix(f, suffix) {
continue
}
f = strings.TrimPrefix(f, prefix)
f = strings.TrimSuffix(f, suffix)
if !ValidateName(f) {
logrus.Errorf("Invalid name %v was processed to extract name with prefix %v suffix %v",
f, prefix, suffix)
continue
}
result = append(result, f)
}
return result
}
// ValidateName validate the given string
func ValidateName(name string) bool {
validName := regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.-]+$`)
return validName.MatchString(name)
}
// Execute executes a command
func Execute(binary string, args []string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout)
defer cancel()
return execute(ctx, binary, args)
}
// ExecuteWithCustomTimeout executes a command with a specified timeout
func ExecuteWithCustomTimeout(binary string, args []string, timeout time.Duration) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return execute(ctx, binary, args)
}
func execute(ctx context.Context, binary string, args []string) (string, error) {
var output []byte
var err error
cmd := exec.CommandContext(ctx, binary, args...)
done := make(chan struct{})
go func() {
output, err = cmd.CombinedOutput()
close(done)
}()
select {
case <-done:
break
case <-ctx.Done():
return "", fmt.Errorf("timeout executing: %v %v, output %v, error %v", binary, args, string(output), err)
}
if err != nil {
return "", fmt.Errorf("failed to execute: %v %v, output %v, error %v", binary, args, string(output), err)
}
return string(output), nil
}
// UnescapeURL converts a escape character to a normal one.
func UnescapeURL(url string) string {
// Deal with escape in url inputted from bash
result := strings.Replace(url, "\\u0026", "&", 1)
result = strings.Replace(result, "u0026", "&", 1)
return result
}
// IsMounted checks if the mount point is mounted
func IsMounted(mountPoint string) bool {
output, err := Execute("mount", []string{})
if err != nil {
return false
}
lines := strings.Split(output, "\n")
for _, line := range lines {
if strings.Contains(line, " "+mountPoint+" ") {
return true
}
}
return false
}
func cleanupMount(mountDir string, mounter mount.Interface, log logrus.FieldLogger) error {
forceUnmounter, ok := mounter.(mount.MounterForceUnmounter)
if ok {
log.Infof("Trying to force clean up mount point %v", mountDir)
return mount.CleanupMountWithForce(mountDir, forceUnmounter, false, forceCleanupMountTimeout)
}
log.Infof("Trying to clean up mount point %v", mountDir)
return mount.CleanupMountPoint(mountDir, forceUnmounter, false)
}
// EnsureMountPoint checks if the mount point is valid. If it is invalid, clean up mount point.
func EnsureMountPoint(Kind, mountPoint string, mounter mount.Interface, log logrus.FieldLogger) (mounted bool, err error) {
defer func() {
if !mounted && err == nil {
if mkdirErr := os.MkdirAll(mountPoint, 0700); mkdirErr != nil {
err = errors.Wrapf(err, "cannot create mount directory %v", mountPoint)
}
}
}()
isMoundPoint, err := mounter.IsMountPoint(mountPoint)
if err == fs.ErrNotExist {
return false, nil
}
IsCorruptedMnt := mount.IsCorruptedMnt(err)
if !IsCorruptedMnt {
log.Warnf("Trying reading mount point %v to make sure it is healthy", mountPoint)
if _, readErr := os.ReadDir(mountPoint); readErr != nil {
log.WithError(readErr).Warnf("Mount point %v was identified as corrupted by ReadDir", mountPoint)
IsCorruptedMnt = true
}
}
if IsCorruptedMnt {
log.Warnf("Failed to check mount point %v (mounted=%v)", mountPoint, mounted)
if mntErr := cleanupMount(mountPoint, mounter, log); mntErr != nil {
return true, errors.Wrapf(mntErr, "failed to clean up corrupted mount point %v", mountPoint)
}
isMoundPoint = false
}
if !isMoundPoint {
return false, nil
}
var stat syscall.Statfs_t
if err := syscall.Statfs(mountPoint, &stat); err != nil {
return true, errors.Wrapf(err, "failed to statfs for mount point %v", mountPoint)
}
kind, err := fstypeToKind(int64(stat.Type))
if err != nil {
return true, errors.Wrapf(err, "failed to get kind for mount point %v", mountPoint)
}
if strings.Contains(kind, Kind) {
return true, nil
}
log.Warnf("Cleaning up the mount point %v because the fstype %v is changed to %v", mountPoint, kind, Kind)
if mntErr := cleanupMount(mountPoint, mounter, log); mntErr != nil {
return true, errors.Wrapf(mntErr, "failed to clean up mount point %v (%v) for %v protocol", kind, mountPoint, Kind)
}
return false, nil
}
// MountWithTimeout mounts the backup store to a given mount point with a specified timeout
func MountWithTimeout(mounter mount.Interface, source string, target string, fstype string,
options []string, sensitiveOptions []string, interval, timeout time.Duration) error {
mountComplete := false
err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(context.Context) (bool, error) {
err := mounter.MountSensitiveWithoutSystemd(source, target, fstype, options, sensitiveOptions)
mountComplete = true
return true, err
})
if !mountComplete {
return errors.Wrapf(err, "mounting %v share %v on %v timed out", fstype, source, target)
}
return err
}
// CleanUpMountPoints tries to clean up all existing mount points for existing backup stores
func CleanUpMountPoints(mounter mount.Interface, log logrus.FieldLogger) error {
return filepath.Walk(MountDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrapf(err, "failed to get file info of %v", path)
}
if !info.IsDir() {
return nil
}
isMountPoint, err := mounter.IsMountPoint(path)
if err != nil {
return errors.Wrapf(err, "failed to check if %s is not mounted", path)
}
if !isMountPoint {
return nil
}
if err := cleanupMount(path, mounter, log); err != nil {
return errors.Wrapf(err, "failed to clean up mount point %v", path)
}
return nil
})
}
func CheckBackupType(backupTarget string) (string, error) {
u, err := url.Parse(backupTarget)
if err != nil {
return "", err
}
return u.Scheme, nil
}
func SplitMountOptions(options []string) []string {
logrus.Infof("Splitting array of %d strings %v ", len(options), options)
if len(options) > 1 {
// Options in the form "nfsOptions=soft&nfsOptions=timeo=450&nfsOptions=retrans=3" are legal and
// are already split by url.Parse.
return options
}
// Options in the form "nfsOptions=soft,timeo=450,retrans=3" are more likely, but we must split them.
return strings.Split(options[0], ",")
}