cephfs.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 volume_mount
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  21. container_storage "yunion.io/x/onecloud/pkg/hostman/container/storage"
  22. "yunion.io/x/onecloud/pkg/util/mountutils"
  23. "yunion.io/x/onecloud/pkg/util/procutils"
  24. )
  25. func init() {
  26. RegisterDriver(newCephFS())
  27. }
  28. type cephFS struct{}
  29. func (h cephFS) Mount(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error {
  30. dir, err := h.GetRuntimeMountHostPath(pod, ctrId, vm)
  31. if err != nil {
  32. return err
  33. }
  34. options := fmt.Sprintf("name=%s,secret=%s", vm.CephFS.Name, vm.CephFS.Secret)
  35. if vm.ReadOnly {
  36. options += ",ro"
  37. }
  38. return mountutils.MountWithParams(fmt.Sprintf("%s:%s", vm.CephFS.MonHost, vm.CephFS.Path), dir, "ceph", []string{"-o", options})
  39. }
  40. func (h cephFS) Unmount(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error {
  41. dir, err := h.GetRuntimeMountHostPath(pod, ctrId, vm)
  42. if err != nil {
  43. return errors.Wrap(err, "GetRuntimeMountHostPath")
  44. }
  45. if err := container_storage.Unmount(dir); err != nil {
  46. return errors.Wrapf(err, "unmount %s", dir)
  47. }
  48. if out, err := procutils.NewRemoteCommandAsFarAsPossible("rm", "-fd", dir).Output(); err != nil {
  49. return errors.Wrapf(err, "rm -fd %s: %s", dir, string(out))
  50. }
  51. return nil
  52. }
  53. func newCephFS() IVolumeMount {
  54. return &cephFS{}
  55. }
  56. func (h cephFS) GetType() apis.ContainerVolumeMountType {
  57. return apis.CONTAINER_VOLUME_MOUNT_TYPE_CEPHF_FS
  58. }
  59. func (d cephFS) InjectUsageTags(usage *ContainerVolumeMountUsage, vol *hostapi.ContainerVolumeMount) {
  60. if vol.CephFS != nil {
  61. usage.Tags["cephfs_id"] = vol.CephFS.Id
  62. }
  63. }
  64. func (h cephFS) GetRuntimeMountHostPath(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) (string, error) {
  65. if vm.CephFS == nil {
  66. return "", fmt.Errorf("cephfs is nil")
  67. }
  68. return filepath.Join(pod.GetVolumesDir(), vm.CephFS.Id), nil
  69. }