manager.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package manager
  15. import (
  16. "sync"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/util/losetup"
  21. )
  22. var (
  23. managerObj iManager
  24. managerInitLock sync.Mutex
  25. )
  26. func Init() {
  27. managerInitLock.Lock()
  28. defer managerInitLock.Unlock()
  29. if managerObj == nil {
  30. managerObj = newManager()
  31. }
  32. }
  33. func ListDevices() (*losetup.Devices, error) {
  34. return managerObj.listDevices()
  35. }
  36. func AttachDevice(filePath string, partScan bool) (*losetup.Device, error) {
  37. return managerObj.attachDevice(filePath, partScan)
  38. }
  39. func DetachDevice(filePath string) error {
  40. return managerObj.detachDevice(filePath)
  41. }
  42. type iManager interface {
  43. listDevices() (*losetup.Devices, error)
  44. attachDevice(filePath string, partScan bool) (*losetup.Device, error)
  45. detachDevice(devPath string) error
  46. }
  47. type execType string
  48. const (
  49. EXEC_ATTACH execType = "attach"
  50. EXEC_DETACH execType = "detach"
  51. )
  52. type execAttach struct {
  53. FilePath string
  54. PartScan bool
  55. }
  56. type execDetach struct {
  57. DevicePath string
  58. }
  59. type execOption struct {
  60. Type execType
  61. Attach *execAttach
  62. Detach *execDetach
  63. result chan execResult
  64. }
  65. type execResult struct {
  66. error error
  67. attachedDevice *losetup.Device
  68. }
  69. func newAttachOption(filePath string, partScan bool) *execOption {
  70. return &execOption{
  71. Type: EXEC_ATTACH,
  72. result: make(chan execResult),
  73. Attach: &execAttach{
  74. FilePath: filePath,
  75. PartScan: partScan,
  76. },
  77. }
  78. }
  79. func newDetachOption(devPath string) *execOption {
  80. return &execOption{
  81. Type: EXEC_DETACH,
  82. result: make(chan execResult),
  83. Detach: &execDetach{
  84. DevicePath: devPath,
  85. },
  86. }
  87. }
  88. type manager struct {
  89. execOptCh chan *execOption
  90. loopMan ILoopManager
  91. }
  92. func newManager() iManager {
  93. loopMan, err := newLoopManager()
  94. if err != nil {
  95. log.Fatalf("new loop manager error: %v", err)
  96. }
  97. m := &manager{
  98. execOptCh: make(chan *execOption),
  99. loopMan: loopMan,
  100. }
  101. go func() {
  102. m.startExec()
  103. }()
  104. return m
  105. }
  106. func (m *manager) listDevices() (*losetup.Devices, error) {
  107. return losetup.ListDevices()
  108. }
  109. func (m *manager) attachDevice(filePath string, partScan bool) (*losetup.Device, error) {
  110. opt := newAttachOption(filePath, partScan)
  111. m.execOptCh <- opt
  112. result := <-opt.result
  113. return result.attachedDevice, result.error
  114. }
  115. func (m *manager) detachDevice(devPath string) error {
  116. opt := newDetachOption(devPath)
  117. m.execOptCh <- opt
  118. result := <-opt.result
  119. return result.error
  120. }
  121. func (m *manager) startExec() {
  122. for execOpt := range m.execOptCh {
  123. switch execOpt.Type {
  124. case EXEC_ATTACH:
  125. log.Infof("attach %s", jsonutils.Marshal(execOpt.Attach))
  126. dev, err := m.loopMan.AttachDevice(execOpt.Attach.FilePath, execOpt.Attach.PartScan)
  127. execOpt.result <- execResult{
  128. attachedDevice: dev,
  129. error: err,
  130. }
  131. case EXEC_DETACH:
  132. log.Infof("detach %s", jsonutils.Marshal(execOpt.Detach))
  133. err := m.loopMan.DetachDevice(execOpt.Detach.DevicePath)
  134. execOpt.result <- execResult{
  135. error: err,
  136. }
  137. default:
  138. err := errors.Errorf("unknown exec type: %s", execOpt.Type)
  139. execOpt.result <- execResult{
  140. error: err,
  141. }
  142. }
  143. }
  144. }