btrfsutils.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 btrfsutils
  15. import (
  16. "path/filepath"
  17. "strings"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/util/procutils"
  21. )
  22. /*
  23. ID 256 gen 32 top level 5 path @
  24. ID 257 gen 2426 top level 256 path @/var
  25. ID 258 gen 870 top level 256 path @/usr/local
  26. ID 259 gen 2426 top level 256 path @/tmp
  27. ID 260 gen 35 top level 256 path @/srv
  28. ID 261 gen 2426 top level 256 path @/root
  29. ID 262 gen 874 top level 256 path @/opt
  30. ID 263 gen 35 top level 256 path @/home
  31. ID 264 gen 26 top level 256 path @/boot/grub2/x86_64-efi
  32. ID 265 gen 64 top level 256 path @/boot/grub2/i386-pc
  33. ID 266 gen 881 top level 256 path @/.snapshots
  34. ID 267 gen 2426 top level 266 path @/.snapshots/1/snapshot
  35. ID 272 gen 65 top level 266 path @/.snapshots/2/snapshot
  36. */
  37. func parseBtrfsSubvols(lines string) []string {
  38. log.Debugf("parseBtrfsSubvols %s", lines)
  39. var ret []string
  40. for _, l := range strings.Split(lines, "\n") {
  41. parts := strings.Split(l, " ")
  42. if len(parts) >= 9 && strings.HasPrefix(parts[8], "@/") {
  43. ret = append(ret, strings.TrimSpace(parts[8]))
  44. }
  45. }
  46. return ret
  47. }
  48. func MountSubvols(dev string, mnt string) error {
  49. output, err := procutils.NewCommand("btrfs", "subvolume", "list", mnt).Output()
  50. if err != nil {
  51. return errors.Wrap(err, "btrfs subvolume list")
  52. }
  53. subvols := parseBtrfsSubvols(string(output))
  54. log.Debugf("mount btrfs subvols %s", strings.Join(subvols, ","))
  55. for _, subvol := range subvols {
  56. err := procutils.NewCommand("mount", dev, filepath.Join(mnt, subvol[2:]), "-o", "subvol=/"+subvol).Run()
  57. if err != nil {
  58. return errors.Wrapf(err, "btrfs mount subvolume %s", subvol)
  59. }
  60. }
  61. return nil
  62. }
  63. func UnmountSubvols(mnt string) error {
  64. output, err := procutils.NewCommand("btrfs", "subvolume", "list", mnt).Output()
  65. if err != nil {
  66. return errors.Wrap(err, "btrfs subvolume list")
  67. }
  68. subvols := parseBtrfsSubvols(string(output))
  69. log.Debugf("unmount btrfs subvols %s", strings.Join(subvols, ","))
  70. // scan in reverse order
  71. var errs []error
  72. for i := len(subvols) - 1; i >= 0; i-- {
  73. subvol := subvols[i]
  74. err := procutils.NewCommand("umount", filepath.Join(mnt, subvol[2:])).Run()
  75. if err != nil {
  76. errs = append(errs, errors.Wrapf(err, "btrfs unmount subvolume %s", subvol))
  77. }
  78. }
  79. if len(errs) > 0 {
  80. return errors.NewAggregate(errs)
  81. }
  82. return nil
  83. }