/
merge.go
301 lines (251 loc) · 9.49 KB
/
merge.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
package v2
import (
"errors"
"strings"
"github.com/wissemmansouri/OpenIT.one-Common/utils"
"github.com/wissemmansouri/OpenIT.one-Common/utils/file"
"github.com/wissemmansouri/OpenIT.one-Common/utils/logger"
"github.com/wissemmansouri/OpenIT.one-LocalStorage/codegen"
"github.com/wissemmansouri/OpenIT.one-LocalStorage/pkg/mergerfs"
"github.com/wissemmansouri/OpenIT.one-LocalStorage/pkg/partition"
"github.com/wissemmansouri/OpenIT.one-LocalStorage/pkg/utils/command"
model2 "github.com/wissemmansouri/OpenIT.one-LocalStorage/service/model"
"github.com/tidwall/gjson"
"go.uber.org/zap"
"gorm.io/gorm"
)
var (
ErrMergeMountPointAlreadyExists = errors.New("merge mount point already exists")
ErrMergeMountPointDoesNotExist = errors.New("merge mount point does not exist")
ErrMergeMountPointSourceConflict = errors.New("source mount point should not be a child path of the merge mount point")
ErrNilReference = errors.New("reference is nil")
)
// Make sure the serial disk is removed from the merge list when it is deleted from database, to keep the database consistent.
func hookAfterDeleteVolume(db *gorm.DB, model interface{}) {
var targetVolumes []model2.Volume
switch t := model.(type) {
case model2.Volume:
targetVolumes = []model2.Volume{t}
case *model2.Volume:
targetVolumes = []model2.Volume{*t}
case []model2.Volume:
targetVolumes = t
case *[]model2.Volume:
targetVolumes = *t
default:
return
}
var merges []model2.Merge
if err := db.Model(&model2.Merge{}).Preload(model2.MergeSourceVolumes).Find(&merges).Error; err != nil {
logger.Error("failed to get merge list from database", zap.Error(err))
return
}
for i := range merges {
updatedVolumes := make([]*model2.Volume, 0)
for _, sourceVolume := range merges[i].SourceVolumes {
for _, targetVolume := range targetVolumes {
if sourceVolume.ID == targetVolume.ID {
break // skip including the volume to be deleted
}
updatedVolumes = append(updatedVolumes, sourceVolume)
}
}
if err := db.Model(&merges[i]).Association(model2.MergeSourceVolumes).Error; err != nil {
logger.Error("failed to enter association mode between merges and volumes", zap.Error(err), zap.Any("merge", merges[i]))
return
}
if err := db.Model(&merges[i]).Association(model2.MergeSourceVolumes).Replace(updatedVolumes); err != nil {
logger.Error("failed to update merge source volumes", zap.Error(err), zap.Any("merge", merges[i]), zap.Any("updatedVolumes", updatedVolumes))
return
}
}
}
func (s *LocalStorageService) GetMerges(mountPoint *string) ([]model2.Merge, error) {
mergesFromDB, err := s.GetMergeAllFromDB(mountPoint)
if err != nil {
return nil, err
}
for _, merge := range mergesFromDB {
merge.SourceVolumes = excludeVolumesWithWrongMountPointAndUUID(merge.SourceVolumes)
}
return mergesFromDB, nil
}
func (s *LocalStorageService) CreateMerge(merge *model2.Merge) error {
if merge == nil {
logger.Error("`merge` should not be nil")
return ErrNilReference
}
if err := file.IsNotExistMkDir(merge.MountPoint); err != nil {
return err
}
merge.SourceVolumes = excludeVolumesWithWrongMountPointAndUUID(merge.SourceVolumes)
sources, err := buildSources(merge)
if err != nil {
logger.Error("failed to build sources", zap.Error(err))
return err
}
// check if the mount point is empty before creating a new mergerfs mount
if bool, err := file.IsDirEmpty(merge.MountPoint); err != nil {
logger.Error("failed to check if the mount point is empty", zap.Error(err))
return err
} else if !bool {
logger.Error("mount point is not empty", zap.String("mountPoint", merge.MountPoint))
return ErrMountPointIsNotEmpty
}
// create a new merge by mounting mergerfs
source := strings.Join(sources, ":")
if _, err := s.Mount(codegen.Mount{
MountPoint: merge.MountPoint,
Fstype: &merge.FSType,
Source: &source,
}); err != nil {
logger.Error("failed to mount mergerfs", zap.Error(err), zap.String("mountPoint", merge.MountPoint), zap.String("source", source))
return err
}
return nil
}
func (s *LocalStorageService) UpdateMerge(merge *model2.Merge) error {
if merge == nil {
logger.Error("`merge` should not be nil")
return ErrNilReference
}
if !file.Exists(merge.MountPoint) {
return ErrMergeMountPointDoesNotExist
}
merge.SourceVolumes = excludeVolumesWithWrongMountPointAndUUID(merge.SourceVolumes)
sources, err := buildSources(merge)
if err != nil {
logger.Error("failed to build sources", zap.Error(err))
return err
}
// if it is already a merge point, check if the mount point is a mergerfs mount with the same sources
existingSources, err := mergerfs.GetSource(merge.MountPoint)
if err != nil {
logger.Error("failed to get mergerfs sources", zap.Error(err), zap.String("mountPoint", merge.MountPoint))
return err
}
if !utils.CompareStringSlices(sources, existingSources) {
// update the mergerfs sources if different sources
if err := mergerfs.SetSource(merge.MountPoint, sources); err != nil {
logger.Error("failed to set mergerfs sources", zap.Error(err), zap.String("mountPoint", merge.MountPoint), zap.Any("sources", sources))
return err
}
}
return nil
}
func (s *LocalStorageService) CheckMergeMount() {
mergesFromDB, err := s.GetMergeAllFromDB(nil)
if err != nil {
logger.Error("failed to get merge list from database", zap.Error(err))
return
}
mounts, err := s.GetMounts(codegen.GetMountsParams{})
if err != nil {
logger.Error("failed to get mount list from system", zap.Error(err))
return
}
for i := range mergesFromDB {
isMergeExist := false
// for each merge from database by mount point, check if it already mounted, i.e. a mergerfs mount
for _, mount := range mounts {
if mount.MountPoint == mergesFromDB[i].MountPoint {
if *mount.Fstype == mergesFromDB[i].FSType {
logger.Info("merge already exists", zap.Any("merge", mergesFromDB[i]))
isMergeExist = true
break
}
logger.Error("not a mergerfs mount point", zap.Any("mount", mount))
}
}
if isMergeExist {
if err := s.UpdateMerge(&mergesFromDB[i]); err != nil {
logger.Error("failed to update merge", zap.Error(err), zap.Any("merge", mergesFromDB[i]))
}
continue
} else {
if err := s.CreateMerge(&mergesFromDB[i]); err != nil {
logger.Error("failed to create merge", zap.Error(err), zap.Any("merge", mergesFromDB[i]))
}
}
}
}
// filter out any volume that are not mounted based on its UUID and mount point (in reality, could have a different disk mounted on the same path)
func excludeVolumesWithWrongMountPointAndUUID(volumes []*model2.Volume) []*model2.Volume {
return filterVolumes(volumes, func(v *model2.Volume) bool {
path, err := partition.GetDevicePath(v.UUID)
if err != nil {
logger.Error("failed to corresponding device path by volume UUID", zap.Error(err), zap.String("uuid", v.UUID))
return false
}
par := command.ExecLSBLKByPath(path)
pttype := gjson.GetBytes(par, "blockdevices.0.pttype")
if pttype.String() != "gpt" {
mountPoint := gjson.GetBytes(par, "blockdevices.0.mountpoint")
if mountPoint.String() != v.MountPoint {
logger.Error("mount point does not match actual", zap.Any("volume", v), zap.String("actual mount point", mountPoint.String()))
return false
}
return true
}
partitions, err := partition.GetPartitions(path)
if err != nil {
logger.Error("failed to corresponding partition of volume", zap.Error(err), zap.String("path", path))
return false
}
if len(partitions) != 1 {
logger.Error("there should be exactly one partition corresponding to the volume", zap.String("path", path), zap.Int("partitions", len(partitions)))
return false
}
if partitions[0].LSBLKProperties["MOUNTPOINT"] != v.MountPoint {
logger.Error("mount point does not match actual", zap.Any("volume", v), zap.String("actual mount point", partitions[0].LSBLKProperties["MOUNTPOINT"]))
return false
}
return true
})
}
func filterVolumes(volumes []*model2.Volume, filter func(*model2.Volume) bool) []*model2.Volume {
var filteredVolumes []*model2.Volume
for _, volume := range volumes {
result := filter(volume)
if result {
filteredVolumes = append(filteredVolumes, volume)
}
}
return filteredVolumes
}
func buildSources(merge *model2.Merge) ([]string, error) {
sources := make([]string, 0)
if merge.SourceBasePath != nil && *merge.SourceBasePath != "" {
// check if sourceBasePath is under mount point
if strings.HasPrefix(*merge.SourceBasePath, merge.MountPoint) {
logger.Error(
"source base path should not be a child path of the merge mount point",
zap.String("sourceBasePath", *merge.SourceBasePath),
zap.String("merge.MountPoint", merge.MountPoint),
)
return nil, ErrMergeMountPointSourceConflict
}
// create source path if it does not exists
if err := file.IsNotExistMkDir(*merge.SourceBasePath); err != nil {
return nil, err
}
sources = append(sources, *merge.SourceBasePath)
}
for _, sourceVolume := range merge.SourceVolumes {
if sourceVolume == nil {
logger.Error("one of the source volumes is nil", zap.Any("sourceVolumes", merge.SourceVolumes))
return nil, ErrNilReference
}
// check if sourceBasePath is under mount point
if strings.HasPrefix(sourceVolume.MountPoint, merge.MountPoint) {
logger.Error(
"mount point of source volume should not be a child path of the mount point",
zap.Any("sourceVolume.MountPoint", sourceVolume.MountPoint),
zap.Any("merge.MountPoint", merge.MountPoint),
)
return nil, ErrMergeMountPointSourceConflict
}
sources = append(sources, sourceVolume.MountPoint)
}
return sources, nil
}