overlay.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 disk
  15. import (
  16. "context"
  17. "fmt"
  18. "path/filepath"
  19. "strings"
  20. "yunion.io/x/pkg/errors"
  21. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  22. container_storage "yunion.io/x/onecloud/pkg/hostman/container/storage"
  23. "yunion.io/x/onecloud/pkg/hostman/container/volume_mount"
  24. "yunion.io/x/onecloud/pkg/util/mountutils"
  25. "yunion.io/x/onecloud/pkg/util/procutils"
  26. )
  27. func (d disk) getOverlayDir(pod volume_mount.IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount, oType string, upperDir string) string {
  28. baseDir := strings.TrimPrefix(upperDir, pod.GetVolumesDir())
  29. return filepath.Join(pod.GetVolumesOverlayDir(), vm.Disk.Id, ctrId, oType, baseDir)
  30. }
  31. func (d disk) getOverlayWorkDir(upperDir string) string {
  32. return fmt.Sprintf("%s-work", upperDir)
  33. }
  34. func (d disk) getOverlayMergedDir(pod volume_mount.IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount, upperDir string) string {
  35. return d.getOverlayDir(pod, ctrId, vm, "_merged_", upperDir)
  36. }
  37. type iDiskOverlay interface {
  38. mount(d disk, pod volume_mount.IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error
  39. unmount(d disk, pod volume_mount.IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error
  40. }
  41. type diskOverlayDir struct{}
  42. func newDiskOverlayDir() iDiskOverlay {
  43. return &diskOverlayDir{}
  44. }
  45. func (dod diskOverlayDir) mount(d disk, pod volume_mount.IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error {
  46. vmDisk := vm.Disk
  47. lowerDir := vmDisk.Overlay.LowerDir
  48. upperDir, err := d.getRuntimeMountHostPath(pod, vm)
  49. if err != nil {
  50. return errors.Wrap(err, "getRuntimeMountHostPath")
  51. }
  52. workDir := d.getOverlayWorkDir(upperDir)
  53. mergedDir := d.getOverlayMergedDir(pod, ctrId, vm, upperDir)
  54. for _, dir := range []string{workDir, mergedDir} {
  55. out, err := procutils.NewRemoteCommandAsFarAsPossible("mkdir", "-p", dir).Output()
  56. if err != nil {
  57. return errors.Wrapf(err, "make directory %s: %s", dir, out)
  58. }
  59. }
  60. return mountutils.MountOverlay(lowerDir, upperDir, workDir, mergedDir)
  61. }
  62. func (dod diskOverlayDir) unmount(d disk, pod volume_mount.IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error {
  63. upperDir, err := d.getRuntimeMountHostPath(pod, vm)
  64. if err != nil {
  65. return errors.Wrap(err, "getRuntimeMountHostPath")
  66. }
  67. overlayDir := d.getOverlayMergedDir(pod, ctrId, vm, upperDir)
  68. return container_storage.Unmount(overlayDir)
  69. }
  70. type diskOverlayImage struct{}
  71. func newDiskOverlayImage() iDiskOverlay {
  72. return &diskOverlayImage{}
  73. }
  74. func (di diskOverlayImage) mount(d disk, pod volume_mount.IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error {
  75. if err := d.doTemplateOverlayAction(context.Background(), pod, ctrId, vm, newDiskOverlayDir().mount, true); err != nil {
  76. return errors.Wrapf(err, "mount template overlay")
  77. }
  78. return nil
  79. }
  80. func (di diskOverlayImage) unmount(d disk, pod volume_mount.IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error {
  81. if err := d.doTemplateOverlayAction(context.Background(), pod, ctrId, vm, newDiskOverlayDir().unmount, false); err != nil {
  82. return errors.Wrapf(err, "unmount template overlay")
  83. }
  84. return nil
  85. }