local_raw.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 local_raw
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/hostman/container/storage"
  21. "yunion.io/x/onecloud/pkg/util/fileutils2"
  22. losetupman "yunion.io/x/onecloud/pkg/util/losetup/manager"
  23. )
  24. func init() {
  25. storage.RegisterDriver(newLocalRaw())
  26. }
  27. type localRaw struct{}
  28. func newLocalRaw() *localRaw {
  29. return &localRaw{}
  30. }
  31. func (l localRaw) GetType() storage.StorageType {
  32. return storage.STORAGE_TYPE_LOCAL_RAW
  33. }
  34. func (l localRaw) CheckConnect(diskPath string) (string, bool, error) {
  35. devs, err := losetupman.ListDevices()
  36. if err != nil {
  37. return "", false, errors.Wrap(err, "list loop devices")
  38. }
  39. for _, dev := range devs.LoopDevs {
  40. if dev.BackFile == diskPath {
  41. return l.checkPartition(dev.Name), true, nil
  42. }
  43. }
  44. return "", false, nil
  45. }
  46. func (l localRaw) ConnectDisk(diskPath string) (string, error) {
  47. loDev, err := losetupman.AttachDevice(diskPath, true)
  48. if err != nil {
  49. return "", errors.Wrapf(err, "failed to attach %s as loop device", diskPath)
  50. }
  51. return l.checkPartition(loDev.Name), nil
  52. }
  53. func (l localRaw) checkPartition(devName string) string {
  54. partPath := fmt.Sprintf("%sp1", devName)
  55. if fileutils2.Exists(partPath) {
  56. return partPath
  57. }
  58. return devName
  59. }
  60. func (l localRaw) DisconnectDisk(diskPath string, mountPoint string) error {
  61. devs, err := losetupman.ListDevices()
  62. if err != nil {
  63. return errors.Wrap(err, "list loop devices")
  64. }
  65. for _, dev := range devs.LoopDevs {
  66. if dev.BackFile == diskPath {
  67. log.Infof("Start detach loop device %s", dev.Name)
  68. //if err := losetupioctl.DetachAndRemoveDevice(dev.Name); err != nil {
  69. if err := losetupman.DetachDevice(dev.Name); err != nil {
  70. if strings.Contains(err.Error(), "No such device or address") {
  71. return nil
  72. }
  73. return errors.Wrapf(err, "detach device %s", dev.Name)
  74. } else {
  75. log.Infof("detach loop device %s of disk %s", dev.Name, diskPath)
  76. return nil
  77. }
  78. }
  79. }
  80. return nil
  81. }