-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
365 lines (311 loc) · 10.4 KB
/
storage.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
package main
import (
"encoding/json"
"errors"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"imuslab.com/arozos/mod/filesystem"
"imuslab.com/arozos/mod/permission"
"imuslab.com/arozos/mod/storage/bridge"
fs "imuslab.com/arozos/mod/filesystem"
storage "imuslab.com/arozos/mod/storage"
)
var (
baseStoragePool *storage.StoragePool //base storage pool, all user can access these virtual roots
//fsHandlers []*fs.FileSystemHandler //All File system handlers. All opened handles must be registered in here
//storagePools []*storage.StoragePool //All Storage pool opened
bridgeManager *bridge.Record //Manager to handle bridged FSH
storageHeartbeatTickerChan chan bool //Channel to stop the storage heartbeat ticker
)
func StorageInit() {
//Load the default handler for the user storage root
if !fs.FileExists(filepath.Clean(*root_directory) + "/") {
os.MkdirAll(filepath.Clean(*root_directory)+"/", 0755)
}
//Start loading the base storage pool
err := LoadBaseStoragePool()
if err != nil {
panic(err)
}
//Create a brdige record manager
bm := bridge.NewBridgeRecord("system/bridge.json")
bridgeManager = bm
}
func LoadBaseStoragePool() error {
//All fsh for the base pool
fsHandlers := []*fs.FileSystemHandler{}
//Use for Debian buster local file system
localFileSystem := "ext4"
if runtime.GOOS == "windows" {
localFileSystem = "ntfs"
}
baseHandler, err := fs.NewFileSystemHandler(fs.FileSystemOption{
Name: "User",
Uuid: "user",
Path: filepath.ToSlash(filepath.Clean(*root_directory)) + "/",
Hierarchy: "user",
Automount: false,
Filesystem: localFileSystem,
})
if err != nil {
systemWideLogger.PrintAndLog("Storage", "Failed to initiate user root storage directory: "+*root_directory+err.Error(), err)
return err
}
fsHandlers = append(fsHandlers, baseHandler)
//Load the tmp folder as storage unit
tmpHandler, err := fs.NewFileSystemHandler(fs.FileSystemOption{
Name: "tmp",
Uuid: "tmp",
Path: filepath.ToSlash(filepath.Clean(*tmp_directory)) + "/",
Hierarchy: "user",
Automount: false,
Filesystem: localFileSystem,
})
if err != nil {
systemWideLogger.PrintAndLog("Storage", "Failed to initiate tmp storage directory: "+*tmp_directory+err.Error(), err)
return err
}
fsHandlers = append(fsHandlers, tmpHandler)
//Load all the storage config from file
rawConfig, err := os.ReadFile(*storage_config_file)
if err != nil {
//File not found. Use internal storage only
systemWideLogger.PrintAndLog("Storage", "Storage configuration file not found. Using internal storage only.", err)
} else {
//Configuration loaded. Initializing handler
externalHandlers, err := fs.NewFileSystemHandlersFromJSON(rawConfig)
if err != nil {
systemWideLogger.PrintAndLog("Storage", "Failed to load storage configuration: "+err.Error()+" -- Skipping", err)
} else {
for _, thisHandler := range externalHandlers {
fsHandlers = append(fsHandlers, thisHandler)
systemWideLogger.PrintAndLog("Storage", thisHandler.Name+" Mounted as "+thisHandler.UUID+":/", err)
}
}
}
//Create a base storage pool for all users
sp, err := storage.NewStoragePool(fsHandlers, "system")
if err != nil {
systemWideLogger.PrintAndLog("Storage", "Failed to create base Storaeg Pool", err)
return err
}
//Update the storage pool permission to readwrite
sp.OtherPermission = wdfs.FsReadWrite
baseStoragePool = sp
return nil
}
// Initialize the storage connection health check for all fsh.
func storageHeartbeatTickerInit() {
ticker := time.NewTicker(60 * time.Second)
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
StoragePerformFileSystemAbstractionConnectionHeartbeat()
}
}
}()
storageHeartbeatTickerChan = done
}
// Perform heartbeat to all connected file system abstraction.
// Blocking function, use with go routine if needed
func StoragePerformFileSystemAbstractionConnectionHeartbeat() {
allFsh := GetAllLoadedFsh()
for _, thisFsh := range allFsh {
err := thisFsh.FileSystemAbstraction.Heartbeat()
if err != nil {
log.Println("[Storage] File System Abstraction from " + thisFsh.Name + " report an error: " + err.Error())
//Retreive the old startup config and close the pool
originalStartOption := filesystem.FileSystemOption{}
js, _ := json.Marshal(thisFsh.StartOptions)
json.Unmarshal(js, &originalStartOption)
//Create a new fsh from original start options
newfsh, err := filesystem.NewFileSystemHandler(originalStartOption)
if err != nil {
log.Println("[Storage] Unable to reconnect " + thisFsh.Name + ": " + err.Error())
continue
} else {
//New fsh created. Close the old one
thisFsh.Close()
}
//Pop this fsh from all storage pool that mounted this
sp := GetAllStoragePools()
parentsp := []*storage.StoragePool{}
for _, thissp := range sp {
if thissp.ContainDiskID(originalStartOption.Uuid) {
parentsp = append(parentsp, thissp)
thissp.DetachFsHandler(originalStartOption.Uuid)
}
}
//Add the new fsh to all the storage pools that have it originally
for _, pool := range parentsp {
err := pool.AttachFsHandler(newfsh)
if err != nil {
log.Println("[Storage] Attach fsh to pool failed: " + err.Error())
}
}
}
}
}
// Initialize group storage pool
func GroupStoragePoolInit() {
//Mount permission groups
for _, pg := range permissionHandler.PermissionGroups {
//For each group, check does this group has a config file
err := LoadStoragePoolForGroup(pg)
if err != nil {
continue
}
//Do something else, WIP
}
//Start editing interface for Storage Pool Editor
StoragePoolEditorInit()
}
func LoadStoragePoolForGroup(pg *permission.PermissionGroup) error {
expectedConfigPath := "./system/storage/" + pg.Name + ".json"
if fs.FileExists(expectedConfigPath) {
//Read the config file
pgStorageConfig, err := os.ReadFile(expectedConfigPath)
if err != nil {
systemWideLogger.PrintAndLog("Storage", "Failed to read config for "+pg.Name+": "+err.Error(), err)
return errors.New("Failed to read config for " + pg.Name + ": " + err.Error())
}
//Generate fsHandler form json
thisGroupFsHandlers, err := fs.NewFileSystemHandlersFromJSON(pgStorageConfig)
if err != nil {
systemWideLogger.PrintAndLog("Storage", "Failed to load storage configuration: "+err.Error(), err)
return errors.New("Failed to load storage configuration: " + err.Error())
}
//Show debug message
for _, thisHandler := range thisGroupFsHandlers {
systemWideLogger.PrintAndLog("Storage", thisHandler.Name+" Mounted as "+thisHandler.UUID+":/ for group "+pg.Name, err)
}
//Create a storage pool from these handlers
sp, err := storage.NewStoragePool(thisGroupFsHandlers, pg.Name)
if err != nil {
systemWideLogger.PrintAndLog("Storage", "Failed to create storage pool for "+pg.Name, err)
return errors.New("Failed to create storage pool for " + pg.Name)
}
//Set other permission to denied by default
sp.OtherPermission = wdfs.FsDenied
//Assign storage pool to group
pg.StoragePool = sp
} else {
//Storage configuration not exists. Fill in the basic information and move to next storage pool
//Create a new empty storage pool for this group
sp, err := storage.NewStoragePool([]*fs.FileSystemHandler{}, pg.Name)
if err != nil {
systemWideLogger.PrintAndLog("Storage", "Failed to create empty storage pool for group: "+pg.Name, err)
}
pg.StoragePool = sp
pg.StoragePool.OtherPermission = wdfs.FsDenied
}
return nil
}
// Check if a storage pool exists by its group owner name
func StoragePoolExists(poolOwner string) bool {
_, err := GetStoragePoolByOwner(poolOwner)
return err == nil
}
func GetAllStoragePools() []*storage.StoragePool {
//Append the base pool
results := []*storage.StoragePool{baseStoragePool}
//Add each permissionGroup's pool
for _, pg := range permissionHandler.PermissionGroups {
results = append(results, pg.StoragePool)
}
return results
}
func GetStoragePoolByOwner(owner string) (*storage.StoragePool, error) {
sps := GetAllStoragePools()
for _, pool := range sps {
if pool.Owner == owner {
return pool, nil
}
}
return nil, errors.New("Storage pool owned by " + owner + " not found")
}
func GetFSHandlerSubpathFromVpath(vpath string) (*fs.FileSystemHandler, string, error) {
VirtualRootID, subpath, err := fs.GetIDFromVirtualPath(vpath)
if err != nil {
return nil, "", errors.New("Unable to resolve requested path: " + err.Error())
}
fsh, err := GetFsHandlerByUUID(VirtualRootID)
if err != nil {
return nil, "", errors.New("Unable to resolve requested path: " + err.Error())
}
if fsh == nil || fsh.FileSystemAbstraction == nil {
return nil, "", errors.New("Unable to resolve requested path: " + err.Error())
}
if fsh.Closed {
return nil, "", errors.New("Target file system handler already closed")
}
return fsh, subpath, nil
}
func GetFsHandlerByUUID(uuid string) (*fs.FileSystemHandler, error) {
//Filter out the :/ fropm uuid if exists
if strings.Contains(uuid, ":") {
uuid = strings.Split(uuid, ":")[0]
}
var resultFsh *fs.FileSystemHandler = nil
allFsh := GetAllLoadedFsh()
for _, fsh := range allFsh {
if fsh.UUID == uuid && !fsh.Closed {
resultFsh = fsh
}
}
if resultFsh == nil {
return nil, errors.New("Filesystem handler with given UUID not found")
} else {
return resultFsh, nil
}
}
func GetAllLoadedFsh() []*fs.FileSystemHandler {
fshTmp := map[string]*fs.FileSystemHandler{}
allFsh := []*fs.FileSystemHandler{}
allStoragePools := GetAllStoragePools()
for _, thisSP := range allStoragePools {
for _, thisFsh := range thisSP.Storages {
fshPointer := thisFsh
fshTmp[thisFsh.UUID] = fshPointer
}
}
//Restructure the map to slice
for _, fsh := range fshTmp {
allFsh = append(allFsh, fsh)
}
return allFsh
}
func RegisterStorageSettings() {
//Storage Pool Configuration
registerSetting(settingModule{
Name: "Storage Pools",
Desc: "Storage Pool Mounting Configuration",
IconPath: "SystemAO/disk/smart/img/small_icon.png",
Group: "Disk",
StartDir: "SystemAO/storage/poolList.html",
RequireAdmin: true,
})
}
// CloseAllStorages Close all storage database
func CloseAllStorages() {
allFsh := GetAllLoadedFsh()
for _, fsh := range allFsh {
fsh.FilesystemDatabase.Close()
}
}
func closeAllStoragePools() {
//Stop the storage pool heartbeat
storageHeartbeatTickerChan <- true
//Close all storage pools
for _, sp := range GetAllStoragePools() {
sp.Close()
}
}