storage.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package main
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "imuslab.com/arozos/mod/filesystem/hybridBackup"
  10. "imuslab.com/arozos/mod/permission"
  11. fs "imuslab.com/arozos/mod/filesystem"
  12. storage "imuslab.com/arozos/mod/storage"
  13. )
  14. var (
  15. baseStoragePool *storage.StoragePool //base storage pool, all user can access these virtual roots
  16. fsHandlers []*fs.FileSystemHandler //All File system handlers. All opened handles must be registered in here
  17. )
  18. func StorageInit() {
  19. //Load the default handler for the user storage root
  20. if !fileExists(filepath.Clean(*root_directory) + "/") {
  21. os.MkdirAll(filepath.Clean(*root_directory)+"/", 0755)
  22. }
  23. //Start loading the base storage pool
  24. err := LoadBaseStoragePool()
  25. if err != nil {
  26. panic(err)
  27. }
  28. }
  29. func LoadBaseStoragePool() error {
  30. //Use for Debian buster local file system
  31. localFileSystem := "ext4"
  32. if runtime.GOOS == "windows" {
  33. localFileSystem = "ntfs"
  34. }
  35. baseHandler, err := fs.NewFileSystemHandler(fs.FileSystemOption{
  36. Name: "User",
  37. Uuid: "user",
  38. Path: filepath.ToSlash(filepath.Clean(*root_directory)) + "/",
  39. Hierarchy: "user",
  40. Automount: false,
  41. Filesystem: localFileSystem,
  42. })
  43. if err != nil {
  44. log.Println("Failed to initiate user root storage directory: " + *root_directory)
  45. return err
  46. }
  47. fsHandlers = append(fsHandlers, baseHandler)
  48. //Load the tmp folder as storage unit
  49. tmpHandler, err := fs.NewFileSystemHandler(fs.FileSystemOption{
  50. Name: "tmp",
  51. Uuid: "tmp",
  52. Path: filepath.ToSlash(filepath.Clean(*tmp_directory)) + "/",
  53. Hierarchy: "user",
  54. Automount: false,
  55. Filesystem: localFileSystem,
  56. })
  57. if err != nil {
  58. log.Println("Failed to initiate tmp storage directory: " + *tmp_directory)
  59. return err
  60. }
  61. fsHandlers = append(fsHandlers, tmpHandler)
  62. //Load all the storage config from file
  63. rawConfig, err := ioutil.ReadFile(*storage_config_file)
  64. if err != nil {
  65. //File not found. Use internal storage only
  66. log.Println("Storage configuration file not found. Using internal storage only.")
  67. } else {
  68. //Configuration loaded. Initializing handler
  69. externalHandlers, err := fs.NewFileSystemHandlersFromJSON(rawConfig)
  70. if err != nil {
  71. log.Println("Failed to load storage configuration: " + err.Error() + " -- Skipping")
  72. } else {
  73. for _, thisHandler := range externalHandlers {
  74. fsHandlers = append(fsHandlers, thisHandler)
  75. log.Println(thisHandler.Name + " Mounted as " + thisHandler.UUID + ":/")
  76. }
  77. }
  78. }
  79. //Create a base storage pool for all users
  80. sp, err := storage.NewStoragePool(fsHandlers, "system")
  81. if err != nil {
  82. log.Println("Failed to create base Storaeg Pool")
  83. return err
  84. }
  85. //Update the storage pool permission to readwrite
  86. sp.OtherPermission = "readwrite"
  87. baseStoragePool = sp
  88. return nil
  89. }
  90. /*
  91. Initiate the backup handlers for backup drives
  92. This function must be called after the scheduler initiated.
  93. */
  94. func FilesystemDaemonInit() {
  95. for _, thisHandler := range fsHandlers {
  96. if thisHandler.Hierarchy == "backup" {
  97. //This is a backup drive. Generate it handler
  98. backupConfig := thisHandler.HierarchyConfig.(hybridBackup.BackupConfig)
  99. systemScheduler.CreateNewScheduledFunctionJob("backup-daemon ["+thisHandler.UUID+"]",
  100. "Backup daemon from "+backupConfig.ParentUID+":/ to "+backupConfig.DiskUID+":/",
  101. 60,
  102. func() (string, error) {
  103. return hybridBackup.HandleBackupProcess(&backupConfig)
  104. },
  105. )
  106. }
  107. //Add other type of handler here
  108. }
  109. }
  110. //Initialize group storage pool
  111. func GroupStoragePoolInit() {
  112. //Mount permission groups
  113. for _, pg := range permissionHandler.PermissionGroups {
  114. //For each group, check does this group has a config file
  115. err := LoadStoragePoolForGroup(pg)
  116. if err != nil {
  117. continue
  118. }
  119. //Do something else, WIP
  120. }
  121. //Start editing interface for Storage Pool Editor
  122. StoragePoolEditorInit()
  123. }
  124. func LoadStoragePoolForGroup(pg *permission.PermissionGroup) error {
  125. expectedConfigPath := "./system/storage/" + pg.Name + ".json"
  126. if fileExists(expectedConfigPath) {
  127. //Read the config file
  128. pgStorageConfig, err := ioutil.ReadFile(expectedConfigPath)
  129. if err != nil {
  130. log.Println("Failed to read config for " + pg.Name + ": " + err.Error())
  131. return errors.New("Failed to read config for " + pg.Name + ": " + err.Error())
  132. }
  133. //Generate fsHandler form json
  134. thisGroupFsHandlers, err := fs.NewFileSystemHandlersFromJSON(pgStorageConfig)
  135. if err != nil {
  136. log.Println("Failed to load storage configuration: " + err.Error())
  137. return errors.New("Failed to load storage configuration: " + err.Error())
  138. }
  139. //Add these to mounted handlers
  140. for _, thisHandler := range thisGroupFsHandlers {
  141. fsHandlers = append(fsHandlers, thisHandler)
  142. log.Println(thisHandler.Name + " Mounted as " + thisHandler.UUID + ":/ for group " + pg.Name)
  143. }
  144. //Create a storage pool from these handlers
  145. sp, err := storage.NewStoragePool(thisGroupFsHandlers, pg.Name)
  146. if err != nil {
  147. log.Println("Failed to create storage pool for " + pg.Name)
  148. return errors.New("Failed to create storage pool for " + pg.Name)
  149. }
  150. //Set other permission to denied by default
  151. sp.OtherPermission = "denied"
  152. //Assign storage pool to group
  153. pg.StoragePool = sp
  154. } else {
  155. //Storage configuration not exists. Fill in the basic information and move to next storage pool
  156. pg.StoragePool.Owner = pg.Name
  157. pg.StoragePool.OtherPermission = "denied"
  158. }
  159. return nil
  160. }
  161. func RegisterStorageSettings() {
  162. //Storage Pool Configuration
  163. registerSetting(settingModule{
  164. Name: "Storage Pools",
  165. Desc: "Storage Pool Mounting Configuration",
  166. IconPath: "SystemAO/disk/smart/img/small_icon.png",
  167. Group: "Disk",
  168. StartDir: "SystemAO/storage/poolList.html",
  169. RequireAdmin: true,
  170. })
  171. }
  172. //CloseAllStorages Close all storage database
  173. func CloseAllStorages() {
  174. for _, fsh := range fsHandlers {
  175. fsh.FilesystemDatabase.Close()
  176. }
  177. }