storage.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. //Get its parent mount point for backup
  100. parentFileSystemHandler, err := GetFsHandlerByUUID(backupConfig.ParentUID)
  101. if err != nil {
  102. log.Println("Virtual Root with UUID: " + backupConfig.ParentUID + " not loaded. Unable to start backup process.")
  103. break
  104. }
  105. backupConfig.ParentPath = parentFileSystemHandler.Path
  106. //Debug backup execution
  107. backupConfig.CycleCounter = 1
  108. hybridBackup.HandleBackupProcess(&backupConfig)
  109. //Create a scheudler for this disk
  110. systemScheduler.CreateNewScheduledFunctionJob("backup-daemon ["+thisHandler.UUID+"]",
  111. "Backup daemon from "+backupConfig.ParentUID+":/ to "+backupConfig.DiskUID+":/",
  112. 60,
  113. func() (string, error) {
  114. return hybridBackup.HandleBackupProcess(&backupConfig)
  115. },
  116. )
  117. }
  118. //Add other type of handler here
  119. }
  120. }
  121. //Initialize group storage pool
  122. func GroupStoragePoolInit() {
  123. //Mount permission groups
  124. for _, pg := range permissionHandler.PermissionGroups {
  125. //For each group, check does this group has a config file
  126. err := LoadStoragePoolForGroup(pg)
  127. if err != nil {
  128. continue
  129. }
  130. //Do something else, WIP
  131. }
  132. //Start editing interface for Storage Pool Editor
  133. StoragePoolEditorInit()
  134. }
  135. func LoadStoragePoolForGroup(pg *permission.PermissionGroup) error {
  136. expectedConfigPath := "./system/storage/" + pg.Name + ".json"
  137. if fileExists(expectedConfigPath) {
  138. //Read the config file
  139. pgStorageConfig, err := ioutil.ReadFile(expectedConfigPath)
  140. if err != nil {
  141. log.Println("Failed to read config for " + pg.Name + ": " + err.Error())
  142. return errors.New("Failed to read config for " + pg.Name + ": " + err.Error())
  143. }
  144. //Generate fsHandler form json
  145. thisGroupFsHandlers, err := fs.NewFileSystemHandlersFromJSON(pgStorageConfig)
  146. if err != nil {
  147. log.Println("Failed to load storage configuration: " + err.Error())
  148. return errors.New("Failed to load storage configuration: " + err.Error())
  149. }
  150. //Add these to mounted handlers
  151. for _, thisHandler := range thisGroupFsHandlers {
  152. fsHandlers = append(fsHandlers, thisHandler)
  153. log.Println(thisHandler.Name + " Mounted as " + thisHandler.UUID + ":/ for group " + pg.Name)
  154. }
  155. //Create a storage pool from these handlers
  156. sp, err := storage.NewStoragePool(thisGroupFsHandlers, pg.Name)
  157. if err != nil {
  158. log.Println("Failed to create storage pool for " + pg.Name)
  159. return errors.New("Failed to create storage pool for " + pg.Name)
  160. }
  161. //Set other permission to denied by default
  162. sp.OtherPermission = "denied"
  163. //Assign storage pool to group
  164. pg.StoragePool = sp
  165. } else {
  166. //Storage configuration not exists. Fill in the basic information and move to next storage pool
  167. pg.StoragePool.Owner = pg.Name
  168. pg.StoragePool.OtherPermission = "denied"
  169. }
  170. return nil
  171. }
  172. func GetFsHandlerByUUID(uuid string) (*fs.FileSystemHandler, error) {
  173. for _, fsh := range fsHandlers {
  174. if fsh.UUID == uuid {
  175. return fsh, nil
  176. }
  177. }
  178. return nil, errors.New("Filesystem handler with given UUID not found")
  179. }
  180. func RegisterStorageSettings() {
  181. //Storage Pool Configuration
  182. registerSetting(settingModule{
  183. Name: "Storage Pools",
  184. Desc: "Storage Pool Mounting Configuration",
  185. IconPath: "SystemAO/disk/smart/img/small_icon.png",
  186. Group: "Disk",
  187. StartDir: "SystemAO/storage/poolList.html",
  188. RequireAdmin: true,
  189. })
  190. }
  191. //CloseAllStorages Close all storage database
  192. func CloseAllStorages() {
  193. for _, fsh := range fsHandlers {
  194. fsh.FilesystemDatabase.Close()
  195. }
  196. }