fstabutils.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 fstabutils
  15. import (
  16. "fmt"
  17. "regexp"
  18. "strings"
  19. "yunion.io/x/log"
  20. )
  21. type SFSRecord struct {
  22. Dev string
  23. Mount string
  24. Fs string
  25. Opt string
  26. Dump string
  27. Pas string
  28. }
  29. func FSRecord(line string) *SFSRecord {
  30. data := regexp.MustCompile(`\s+`).Split(line, -1)
  31. if len(data) > 5 {
  32. return &SFSRecord{data[0], data[1], data[2], data[3], data[4], data[5]}
  33. }
  34. log.Errorf("Invalid fstab record %s", line)
  35. return nil
  36. }
  37. func (fsr *SFSRecord) String() string {
  38. return fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%s",
  39. fsr.Dev, fsr.Mount, fsr.Fs, fsr.Opt, fsr.Dump, fsr.Pas)
  40. }
  41. var VDISK_PREFIX = "/dev/vd"
  42. type FsTab []*SFSRecord
  43. func FSTabFile(content string) *FsTab {
  44. if len(content) > 0 {
  45. var res = make(FsTab, 0)
  46. lines := strings.Split(content, "\n")
  47. for _, line := range lines {
  48. line = strings.TrimSpace(line)
  49. if len(line) > 0 && line[0] != '#' {
  50. if fsr := FSRecord(line); fsr != nil {
  51. res = append(res, fsr)
  52. }
  53. }
  54. }
  55. return &res
  56. }
  57. return nil
  58. }
  59. func (ft *FsTab) IsExists(dev string) bool {
  60. for _, f := range *ft {
  61. if f.Dev == dev {
  62. return true
  63. }
  64. }
  65. return false
  66. }
  67. func (ft *FsTab) AddFsrec(line string) {
  68. rec := FSRecord(line)
  69. if rec != nil {
  70. *ft = append(*ft, rec)
  71. }
  72. }
  73. func (ft *FsTab) RemoveDevices(devCnt int) *FsTab {
  74. var newList = make(FsTab, 0)
  75. for _, f := range *ft {
  76. if strings.HasPrefix(f.Dev, VDISK_PREFIX) {
  77. devChar := f.Dev[len(VDISK_PREFIX)]
  78. devIdx := devChar - 'a'
  79. if devIdx >= 0 && int(devIdx) < devCnt {
  80. newList = append(newList, f)
  81. }
  82. } else {
  83. newList = append(newList, f)
  84. }
  85. }
  86. return &newList
  87. }
  88. func (ft *FsTab) ToConf() string {
  89. var res string
  90. for _, f := range *ft {
  91. res += fmt.Sprintf("%s\n", f)
  92. }
  93. return res
  94. }
  95. /* TODO: test
  96. if __name__ == '__main__':
  97. with open('/etc/fstab') as f:
  98. cont = f.read(4096)
  99. print cont
  100. fstab = FSTabFile(cont)
  101. print fstab.is_exists('/dev/sdc2')
  102. fstab.add_fsrec('/dev/sdd1 /data ext4 defaults 0 0')
  103. fstab.add_fsrec('/dev/sdd2')
  104. print fstab.to_conf()
  105. */