lcow_svm.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // +build windows
  2. package lcow
  3. import (
  4. "errors"
  5. "fmt"
  6. "io"
  7. "strings"
  8. "sync"
  9. "time"
  10. "github.com/Microsoft/hcsshim"
  11. "github.com/Microsoft/opengcs/client"
  12. "github.com/sirupsen/logrus"
  13. )
  14. // Code for all the service VM management for the LCOW graphdriver
  15. var errVMisTerminating = errors.New("service VM is shutting down")
  16. var errVMUnknown = errors.New("service vm id is unknown")
  17. var errVMStillHasReference = errors.New("Attemping to delete a VM that is still being used")
  18. // serviceVMMap is the struct representing the id -> service VM mapping.
  19. type serviceVMMap struct {
  20. sync.Mutex
  21. svms map[string]*serviceVMMapItem
  22. }
  23. // serviceVMMapItem is our internal structure representing an item in our
  24. // map of service VMs we are maintaining.
  25. type serviceVMMapItem struct {
  26. svm *serviceVM // actual service vm object
  27. refCount int // refcount for VM
  28. }
  29. type serviceVM struct {
  30. sync.Mutex // Serialises operations being performed in this service VM.
  31. scratchAttached bool // Has a scratch been attached?
  32. config *client.Config // Represents the service VM item.
  33. // Indicates that the vm is started
  34. startStatus chan interface{}
  35. startError error
  36. // Indicates that the vm is stopped
  37. stopStatus chan interface{}
  38. stopError error
  39. attachedVHDs map[string]int // Map ref counting all the VHDS we've hot-added/hot-removed.
  40. unionMounts map[string]int // Map ref counting all the union filesystems we mounted.
  41. }
  42. // add will add an id to the service vm map. There are three cases:
  43. // - entry doesn't exist:
  44. // - add id to map and return a new vm that the caller can manually configure+start
  45. // - entry does exist
  46. // - return vm in map and increment ref count
  47. // - entry does exist but the ref count is 0
  48. // - return the svm and errVMisTerminating. Caller can call svm.getStopError() to wait for stop
  49. func (svmMap *serviceVMMap) add(id string) (svm *serviceVM, alreadyExists bool, err error) {
  50. svmMap.Lock()
  51. defer svmMap.Unlock()
  52. if svm, ok := svmMap.svms[id]; ok {
  53. if svm.refCount == 0 {
  54. return svm.svm, true, errVMisTerminating
  55. }
  56. svm.refCount++
  57. return svm.svm, true, nil
  58. }
  59. // Doesn't exist, so create an empty svm to put into map and return
  60. newSVM := &serviceVM{
  61. startStatus: make(chan interface{}),
  62. stopStatus: make(chan interface{}),
  63. attachedVHDs: make(map[string]int),
  64. unionMounts: make(map[string]int),
  65. config: &client.Config{},
  66. }
  67. svmMap.svms[id] = &serviceVMMapItem{
  68. svm: newSVM,
  69. refCount: 1,
  70. }
  71. return newSVM, false, nil
  72. }
  73. // get will get the service vm from the map. There are three cases:
  74. // - entry doesn't exist:
  75. // - return errVMUnknown
  76. // - entry does exist
  77. // - return vm with no error
  78. // - entry does exist but the ref count is 0
  79. // - return the svm and errVMisTerminating. Caller can call svm.getStopError() to wait for stop
  80. func (svmMap *serviceVMMap) get(id string) (*serviceVM, error) {
  81. svmMap.Lock()
  82. defer svmMap.Unlock()
  83. svm, ok := svmMap.svms[id]
  84. if !ok {
  85. return nil, errVMUnknown
  86. }
  87. if svm.refCount == 0 {
  88. return svm.svm, errVMisTerminating
  89. }
  90. return svm.svm, nil
  91. }
  92. // decrementRefCount decrements the ref count of the given ID from the map. There are four cases:
  93. // - entry doesn't exist:
  94. // - return errVMUnknown
  95. // - entry does exist but the ref count is 0
  96. // - return the svm and errVMisTerminating. Caller can call svm.getStopError() to wait for stop
  97. // - entry does exist but ref count is 1
  98. // - return vm and set lastRef to true. The caller can then stop the vm, delete the id from this map
  99. // - and execute svm.signalStopFinished to signal the threads that the svm has been terminated.
  100. // - entry does exist and ref count > 1
  101. // - just reduce ref count and return svm
  102. func (svmMap *serviceVMMap) decrementRefCount(id string) (_ *serviceVM, lastRef bool, _ error) {
  103. svmMap.Lock()
  104. defer svmMap.Unlock()
  105. svm, ok := svmMap.svms[id]
  106. if !ok {
  107. return nil, false, errVMUnknown
  108. }
  109. if svm.refCount == 0 {
  110. return svm.svm, false, errVMisTerminating
  111. }
  112. svm.refCount--
  113. return svm.svm, svm.refCount == 0, nil
  114. }
  115. // setRefCountZero works the same way as decrementRefCount, but sets ref count to 0 instead of decrementing it.
  116. func (svmMap *serviceVMMap) setRefCountZero(id string) (*serviceVM, error) {
  117. svmMap.Lock()
  118. defer svmMap.Unlock()
  119. svm, ok := svmMap.svms[id]
  120. if !ok {
  121. return nil, errVMUnknown
  122. }
  123. if svm.refCount == 0 {
  124. return svm.svm, errVMisTerminating
  125. }
  126. svm.refCount = 0
  127. return svm.svm, nil
  128. }
  129. // deleteID deletes the given ID from the map. If the refcount is not 0 or the
  130. // VM does not exist, then this function returns an error.
  131. func (svmMap *serviceVMMap) deleteID(id string) error {
  132. svmMap.Lock()
  133. defer svmMap.Unlock()
  134. svm, ok := svmMap.svms[id]
  135. if !ok {
  136. return errVMUnknown
  137. }
  138. if svm.refCount != 0 {
  139. return errVMStillHasReference
  140. }
  141. delete(svmMap.svms, id)
  142. return nil
  143. }
  144. func (svm *serviceVM) signalStartFinished(err error) {
  145. svm.Lock()
  146. svm.startError = err
  147. svm.Unlock()
  148. close(svm.startStatus)
  149. }
  150. func (svm *serviceVM) getStartError() error {
  151. <-svm.startStatus
  152. svm.Lock()
  153. defer svm.Unlock()
  154. return svm.startError
  155. }
  156. func (svm *serviceVM) signalStopFinished(err error) {
  157. svm.Lock()
  158. svm.stopError = err
  159. svm.Unlock()
  160. close(svm.stopStatus)
  161. }
  162. func (svm *serviceVM) getStopError() error {
  163. <-svm.stopStatus
  164. svm.Lock()
  165. defer svm.Unlock()
  166. return svm.stopError
  167. }
  168. // hotAddVHDs waits for the service vm to start and then attaches the vhds.
  169. func (svm *serviceVM) hotAddVHDs(mvds ...hcsshim.MappedVirtualDisk) error {
  170. if err := svm.getStartError(); err != nil {
  171. return err
  172. }
  173. return svm.hotAddVHDsAtStart(mvds...)
  174. }
  175. // hotAddVHDsAtStart works the same way as hotAddVHDs but does not wait for the VM to start.
  176. func (svm *serviceVM) hotAddVHDsAtStart(mvds ...hcsshim.MappedVirtualDisk) error {
  177. svm.Lock()
  178. defer svm.Unlock()
  179. for i, mvd := range mvds {
  180. if _, ok := svm.attachedVHDs[mvd.HostPath]; ok {
  181. svm.attachedVHDs[mvd.HostPath]++
  182. continue
  183. }
  184. if err := svm.config.HotAddVhd(mvd.HostPath, mvd.ContainerPath, mvd.ReadOnly, !mvd.AttachOnly); err != nil {
  185. svm.hotRemoveVHDsAtStart(mvds[:i]...)
  186. return err
  187. }
  188. svm.attachedVHDs[mvd.HostPath] = 1
  189. }
  190. return nil
  191. }
  192. // hotRemoveVHDs waits for the service vm to start and then removes the vhds.
  193. func (svm *serviceVM) hotRemoveVHDs(mvds ...hcsshim.MappedVirtualDisk) error {
  194. if err := svm.getStartError(); err != nil {
  195. return err
  196. }
  197. return svm.hotRemoveVHDsAtStart(mvds...)
  198. }
  199. // hotRemoveVHDsAtStart works the same way as hotRemoveVHDs but does not wait for the VM to start.
  200. func (svm *serviceVM) hotRemoveVHDsAtStart(mvds ...hcsshim.MappedVirtualDisk) error {
  201. svm.Lock()
  202. defer svm.Unlock()
  203. var retErr error
  204. for _, mvd := range mvds {
  205. if _, ok := svm.attachedVHDs[mvd.HostPath]; !ok {
  206. // We continue instead of returning an error if we try to hot remove a non-existent VHD.
  207. // This is because one of the callers of the function is graphdriver.Put(). Since graphdriver.Get()
  208. // defers the VM start to the first operation, it's possible that nothing have been hot-added
  209. // when Put() is called. To avoid Put returning an error in that case, we simply continue if we
  210. // don't find the vhd attached.
  211. continue
  212. }
  213. if svm.attachedVHDs[mvd.HostPath] > 1 {
  214. svm.attachedVHDs[mvd.HostPath]--
  215. continue
  216. }
  217. // last VHD, so remove from VM and map
  218. if err := svm.config.HotRemoveVhd(mvd.HostPath); err == nil {
  219. delete(svm.attachedVHDs, mvd.HostPath)
  220. } else {
  221. // Take note of the error, but still continue to remove the other VHDs
  222. logrus.Warnf("Failed to hot remove %s: %s", mvd.HostPath, err)
  223. if retErr == nil {
  224. retErr = err
  225. }
  226. }
  227. }
  228. return retErr
  229. }
  230. func (svm *serviceVM) createExt4VHDX(destFile string, sizeGB uint32, cacheFile string) error {
  231. if err := svm.getStartError(); err != nil {
  232. return err
  233. }
  234. svm.Lock()
  235. defer svm.Unlock()
  236. return svm.config.CreateExt4Vhdx(destFile, sizeGB, cacheFile)
  237. }
  238. func (svm *serviceVM) createUnionMount(mountName string, mvds ...hcsshim.MappedVirtualDisk) (err error) {
  239. if len(mvds) == 0 {
  240. return fmt.Errorf("createUnionMount: error must have at least 1 layer")
  241. }
  242. if err = svm.getStartError(); err != nil {
  243. return err
  244. }
  245. svm.Lock()
  246. defer svm.Unlock()
  247. if _, ok := svm.unionMounts[mountName]; ok {
  248. svm.unionMounts[mountName]++
  249. return nil
  250. }
  251. var lowerLayers []string
  252. if mvds[0].ReadOnly {
  253. lowerLayers = append(lowerLayers, mvds[0].ContainerPath)
  254. }
  255. for i := 1; i < len(mvds); i++ {
  256. lowerLayers = append(lowerLayers, mvds[i].ContainerPath)
  257. }
  258. logrus.Debugf("Doing the overlay mount with union directory=%s", mountName)
  259. if err = svm.runProcess(fmt.Sprintf("mkdir -p %s", mountName), nil, nil, nil); err != nil {
  260. return err
  261. }
  262. var cmd string
  263. if mvds[0].ReadOnly {
  264. // Readonly overlay
  265. cmd = fmt.Sprintf("mount -t overlay overlay -olowerdir=%s %s",
  266. strings.Join(lowerLayers, ","),
  267. mountName)
  268. } else {
  269. upper := fmt.Sprintf("%s/upper", mvds[0].ContainerPath)
  270. work := fmt.Sprintf("%s/work", mvds[0].ContainerPath)
  271. if err = svm.runProcess(fmt.Sprintf("mkdir -p %s %s", upper, work), nil, nil, nil); err != nil {
  272. return err
  273. }
  274. cmd = fmt.Sprintf("mount -t overlay overlay -olowerdir=%s,upperdir=%s,workdir=%s %s",
  275. strings.Join(lowerLayers, ":"),
  276. upper,
  277. work,
  278. mountName)
  279. }
  280. logrus.Debugf("createUnionMount: Executing mount=%s", cmd)
  281. if err = svm.runProcess(cmd, nil, nil, nil); err != nil {
  282. return err
  283. }
  284. svm.unionMounts[mountName] = 1
  285. return nil
  286. }
  287. func (svm *serviceVM) deleteUnionMount(mountName string, disks ...hcsshim.MappedVirtualDisk) error {
  288. if err := svm.getStartError(); err != nil {
  289. return err
  290. }
  291. svm.Lock()
  292. defer svm.Unlock()
  293. if _, ok := svm.unionMounts[mountName]; !ok {
  294. return nil
  295. }
  296. if svm.unionMounts[mountName] > 1 {
  297. svm.unionMounts[mountName]--
  298. return nil
  299. }
  300. logrus.Debugf("Removing union mount %s", mountName)
  301. if err := svm.runProcess(fmt.Sprintf("umount %s", mountName), nil, nil, nil); err != nil {
  302. return err
  303. }
  304. delete(svm.unionMounts, mountName)
  305. return nil
  306. }
  307. func (svm *serviceVM) runProcess(command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
  308. process, err := svm.config.RunProcess(command, stdin, stdout, stderr)
  309. if err != nil {
  310. return err
  311. }
  312. defer process.Close()
  313. process.WaitTimeout(time.Duration(int(time.Second) * svm.config.UvmTimeoutSeconds))
  314. exitCode, err := process.ExitCode()
  315. if err != nil {
  316. return err
  317. }
  318. if exitCode != 0 {
  319. return fmt.Errorf("svm.runProcess: command %s failed with exit code %d", command, exitCode)
  320. }
  321. return nil
  322. }