lvm.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 fsutils
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/util/procutils"
  20. )
  21. func VgActive(vgname string) error {
  22. out, err := procutils.NewCommand("vgchange", "-ay", vgname).Output()
  23. if err != nil {
  24. return errors.Wrapf(err, "vgchange -ay %s %s", vgname, out)
  25. }
  26. return nil
  27. }
  28. func (d *SFsutilDriver) ExtendLv(lvPath string) error {
  29. out, err := d.Exec("lvextend", "-l", "+100%FREE", lvPath)
  30. if err != nil {
  31. return errors.Wrapf(err, "extend lv %s failed %s", lvPath, out)
  32. }
  33. return nil
  34. }
  35. type LvProps struct {
  36. LvName string
  37. LvPath string
  38. }
  39. type LvNames struct {
  40. Report []struct {
  41. LV []struct {
  42. LVName string `json:"lv_name"`
  43. LVPath string `json:"lv_path"`
  44. } `json:"lv"`
  45. } `json:"report"`
  46. }
  47. func (d *SFsutilDriver) GetVgLvs(vg string) ([]LvProps, error) {
  48. cmd := fmt.Sprintf("lvs --reportformat json -o lv_name,lv_path %s 2>/dev/null", vg)
  49. out, err := d.Exec("sh", "-c", cmd)
  50. if err != nil {
  51. return nil, errors.Wrap(err, "find vg lvs")
  52. }
  53. var res LvNames
  54. err = json.Unmarshal(out, &res)
  55. if err != nil {
  56. return nil, errors.Wrap(err, "unmarshal lvs")
  57. }
  58. if len(res.Report) != 1 {
  59. return nil, errors.Errorf("unexpect res %v", res)
  60. }
  61. lvs := make([]LvProps, 0, len(res.Report[0].LV))
  62. for i := 0; i < len(res.Report[0].LV); i++ {
  63. if res.Report[0].LV[i].LVName == "" {
  64. continue
  65. }
  66. lvs = append(lvs, LvProps{res.Report[0].LV[i].LVName, res.Report[0].LV[i].LVPath})
  67. }
  68. return lvs, nil
  69. }
  70. type VgProps struct {
  71. VgName string
  72. VgUuid string
  73. }
  74. type VgReports struct {
  75. Report []struct {
  76. VG []struct {
  77. VgName string `json:"vg_name"`
  78. VgUuid string `json:"vg_uuid"`
  79. } `json:"vg"`
  80. } `json:"report"`
  81. }
  82. func FindVg(partDev string) (*VgProps, error) {
  83. cmd := fmt.Sprintf("vgs --reportformat json -o vg_name,vg_uuid --devices %s 2>/dev/null", partDev)
  84. out, err := procutils.NewCommand("sh", "-c", cmd).Output()
  85. if err != nil {
  86. return nil, errors.Wrap(err, "find vg lvs")
  87. }
  88. var vgReports VgReports
  89. err = json.Unmarshal(out, &vgReports)
  90. if err != nil {
  91. return nil, errors.Wrapf(err, "unmarshal vgprops %s", out)
  92. }
  93. if len(vgReports.Report) == 1 && len(vgReports.Report[0].VG) == 1 {
  94. var vgProps VgProps
  95. vgProps.VgName = vgReports.Report[0].VG[0].VgName
  96. vgProps.VgUuid = vgReports.Report[0].VG[0].VgUuid
  97. return &vgProps, nil
  98. }
  99. return nil, nil
  100. }