local_raw.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 container_storage
  15. import (
  16. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
  17. "yunion.io/x/pkg/errors"
  18. api "yunion.io/x/onecloud/pkg/apis/compute"
  19. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  20. "yunion.io/x/onecloud/pkg/hostman/isolated_device"
  21. "yunion.io/x/onecloud/pkg/hostman/storageman"
  22. losetupman "yunion.io/x/onecloud/pkg/util/losetup/manager"
  23. )
  24. func init() {
  25. isolated_device.RegisterContainerDeviceManager(newLocalLoopDiskManager())
  26. }
  27. type localLoopDiskManager struct {
  28. }
  29. func (l localLoopDiskManager) GetType() isolated_device.ContainerDeviceType {
  30. return api.CONTAINER_STORAGE_LOCAL_RAW
  31. }
  32. func (l localLoopDiskManager) NewDevices(dev *isolated_device.ContainerDevice) ([]isolated_device.IDevice, error) {
  33. return nil, errors.Errorf("%s storage doesn't support NewDevices", l.GetType())
  34. }
  35. func (l localLoopDiskManager) NewContainerDevices(_ *hostapi.ContainerCreateInput, input *hostapi.ContainerDevice) ([]*runtimeapi.Device, []*runtimeapi.Device, error) {
  36. dev := input.Disk
  37. disk, err := storageman.GetManager().GetDiskById(dev.Id)
  38. if err != nil {
  39. return nil, nil, errors.Wrapf(err, "GetDiskById %s", dev.Id)
  40. }
  41. format, err := disk.GetFormat()
  42. if err != nil {
  43. return nil, nil, errors.Wrapf(err, "get disk %s format", dev.Id)
  44. }
  45. if format != "raw" {
  46. return nil, nil, errors.Errorf("disk %s format isn't raw", dev.Id)
  47. }
  48. dPath := disk.GetPath()
  49. loDev, err := losetupman.AttachDevice(dPath, false)
  50. if err != nil {
  51. return nil, nil, errors.Wrapf(err, "failed to attach %s as loop device", dPath)
  52. }
  53. retDev := &runtimeapi.Device{
  54. ContainerPath: input.ContainerPath,
  55. HostPath: loDev.Name,
  56. Permissions: "rwm",
  57. }
  58. return []*runtimeapi.Device{retDev}, nil, nil
  59. }
  60. func (m *localLoopDiskManager) ProbeDevices() ([]isolated_device.IDevice, error) {
  61. return nil, nil
  62. }
  63. func (m *localLoopDiskManager) GetContainerExtraConfigures(devs []*hostapi.ContainerDevice) ([]*runtimeapi.KeyValue, []*runtimeapi.Mount) {
  64. return nil, nil
  65. }
  66. func newLocalLoopDiskManager() *localLoopDiskManager {
  67. return &localLoopDiskManager{}
  68. }