device.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 losetup
  15. import (
  16. "fmt"
  17. "strings"
  18. )
  19. type Device struct {
  20. Name string `json:"name"`
  21. BackFile string `json:"back-file"`
  22. SizeLimit bool `json:"sizelimit"`
  23. //Offset string `json:"offset"`
  24. //AutoClear string `json:"autoclear"`
  25. ReadOnly bool `json:"ro"`
  26. }
  27. type Devices struct {
  28. LoopDevs []Device `json:"loopdevices"`
  29. }
  30. /* $ losetup -l -O NAME,BACK-FILE,SIZELIMIT,RO
  31. * NAME BACK-FILE SIZELIMIT RO
  32. * /dev/loop0 /disks/2b917686-2ace-4a57-a4af-44ece2303dd2 0 0
  33. * /dev/loop1 /disks/033d6bc0-4ce4-48c4-89d3-125077bcc28e 0 0
  34. * /dev/loop2 /disks/48bcff6e-4062-439c-bc9e-e601391b059f 0 0
  35. */
  36. func parseDevices(output string) (*Devices, error) {
  37. devs := &Devices{}
  38. if len(output) == 0 {
  39. return devs, nil
  40. }
  41. lines := strings.Split(output, "\n")
  42. if len(lines) == 0 {
  43. return devs, nil
  44. }
  45. loopDevs := make([]Device, 0)
  46. for _, line := range lines {
  47. if len(line) == 0 || strings.HasPrefix(line, "NAME") {
  48. continue
  49. }
  50. dev, err := parseDevice(line)
  51. if err != nil {
  52. return nil, err
  53. }
  54. loopDevs = append(loopDevs, dev)
  55. }
  56. devs.LoopDevs = loopDevs
  57. return devs, nil
  58. }
  59. func parseDevice(line string) (Device, error) {
  60. fields := strings.Fields(line)
  61. if len(fields) == 0 {
  62. return Device{}, fmt.Errorf("Invalid line: %q", line)
  63. }
  64. if len(fields) < 2 {
  65. return Device{}, fmt.Errorf("Invalid line: %q", line)
  66. }
  67. return Device{
  68. Name: fields[0],
  69. BackFile: strings.Join(fields[1:], " "),
  70. }, nil
  71. }
  72. func (devs Devices) GetDeviceByName(name string) *Device {
  73. for i := range devs.LoopDevs {
  74. dev := &devs.LoopDevs[i]
  75. if dev.Name == name {
  76. return dev
  77. }
  78. }
  79. return nil
  80. }
  81. func (devs Devices) GetDeviceByFile(filePath string) *Device {
  82. for i := range devs.LoopDevs {
  83. dev := &devs.LoopDevs[i]
  84. if dev.BackFile == filePath {
  85. return dev
  86. }
  87. }
  88. return nil
  89. }