guestfishpart.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 guestfishpart
  15. import (
  16. "fmt"
  17. "os/exec"
  18. "strings"
  19. "time"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/utils"
  23. "yunion.io/x/onecloud/pkg/hostman/diskutils/libguestfs/guestfish"
  24. "yunion.io/x/onecloud/pkg/hostman/guestfs/fsdriver"
  25. "yunion.io/x/onecloud/pkg/hostman/guestfs/kvmpart"
  26. "yunion.io/x/onecloud/pkg/util/procutils"
  27. )
  28. type SGuestfishDiskPartition struct {
  29. *kvmpart.SLocalGuestFS
  30. fish *guestfish.Guestfish
  31. // device name in guest fish
  32. dev string
  33. // one of part in guest fish filesystems
  34. partDev string
  35. // guest fish detected filesystem type
  36. fs string
  37. // is partition mounted on host filesystem
  38. mounted bool
  39. // mount as readonly
  40. readonly bool
  41. mountErrorChan chan error
  42. }
  43. var _ fsdriver.IDiskPartition = &SGuestfishDiskPartition{}
  44. /* dev like /dev/sda, partDev like /dev/sda1 */
  45. func NewGuestfishDiskPartition(
  46. dev, partDev, fs string, fish *guestfish.Guestfish,
  47. ) *SGuestfishDiskPartition {
  48. mountPath := fmt.Sprintf("/tmp/%s", strings.Replace(partDev, "/", "_", -1))
  49. return &SGuestfishDiskPartition{
  50. SLocalGuestFS: kvmpart.NewLocalGuestFS(mountPath),
  51. dev: dev,
  52. partDev: partDev,
  53. fs: fs,
  54. fish: fish,
  55. mountErrorChan: make(chan error, 1),
  56. }
  57. }
  58. func (d *SGuestfishDiskPartition) GetPartDev() string {
  59. return d.partDev
  60. }
  61. func (d *SGuestfishDiskPartition) IsMounted() bool {
  62. return d.mounted
  63. }
  64. func (d *SGuestfishDiskPartition) fsck() error {
  65. switch d.fs {
  66. case "hfsplus", "ext2", "ext3", "ext4":
  67. return d.fish.Fsck(d.partDev, d.fs)
  68. case "ntfs":
  69. return d.fish.Ntfsfix(d.partDev)
  70. }
  71. return nil
  72. }
  73. func (d *SGuestfishDiskPartition) Mount() bool {
  74. err := d.fsck()
  75. if err != nil {
  76. log.Errorf("fsck error: %s", err)
  77. return false
  78. }
  79. err = d.mount(false)
  80. if err != nil {
  81. log.Errorf("mount error:%s", err)
  82. return false
  83. }
  84. return true
  85. }
  86. func (d *SGuestfishDiskPartition) mount(readonly bool) error {
  87. if output, err := procutils.NewCommand("mkdir", "-p", d.GetMountPath()).Output(); err != nil {
  88. return errors.Wrapf(err, "mkdir %s failed: %s", d.GetMountPath(), output)
  89. }
  90. err := d.fish.Mount(d.partDev)
  91. if err != nil {
  92. return err
  93. }
  94. err = d.fish.MountLocal(d.GetMountPath(), readonly)
  95. if err != nil {
  96. return err
  97. }
  98. go func() {
  99. d.mountErrorChan <- d.fish.MountLocalRun()
  100. }()
  101. select {
  102. case err = <-d.mountErrorChan:
  103. return err
  104. case <-time.After(time.Second * 1):
  105. cmd := exec.Command("ls", d.GetMountPath())
  106. accessibleChan := make(chan error)
  107. go func() {
  108. accessibleChan <- cmd.Run()
  109. }()
  110. select {
  111. case err = <-accessibleChan:
  112. log.Infof("mount filesystem %s accessable: %v", d.GetMountPath(), err)
  113. case <-time.After(3 * time.Second):
  114. log.Errorf("mount filesystem %s not accessable", d.GetMountPath())
  115. err = cmd.Process.Kill()
  116. if err != nil {
  117. log.Errorf("failed kill ls process %s", err)
  118. }
  119. return errors.Errorf("mount filesystem %s not accessable", d.GetMountPath())
  120. }
  121. log.Infof("may be mount success")
  122. }
  123. d.mounted = true
  124. return nil
  125. }
  126. func (d *SGuestfishDiskPartition) MountPartReadOnly() bool {
  127. if len(d.fs) == 0 || utils.IsInStringArray(d.fs, []string{"swap", "btrfs"}) {
  128. return false
  129. }
  130. err := d.mount(true)
  131. if err != nil {
  132. log.Errorf("SGuestfishDiskPartition mount as readonly error: %s", err)
  133. return false
  134. }
  135. d.readonly = true
  136. return true
  137. }
  138. func (d *SGuestfishDiskPartition) Umount() error {
  139. if !d.IsMounted() {
  140. return nil
  141. }
  142. var tries = 0
  143. var err error
  144. for tries < 10 {
  145. tries += 1
  146. output, err := procutils.NewCommand("umount", d.GetMountPath()).Output()
  147. if err != nil {
  148. err = errors.Wrapf(err, "failed umount %s: %s %s", d.GetMountPath(), output, err)
  149. time.Sleep(time.Second * 1)
  150. } else {
  151. d.mounted = false
  152. return nil
  153. }
  154. }
  155. return err
  156. }
  157. func (d *SGuestfishDiskPartition) IsReadonly() bool {
  158. return d.readonly
  159. }
  160. func (d *SGuestfishDiskPartition) GetPhysicalPartitionType() string {
  161. ret, err := d.fish.SfdiskL(d.dev)
  162. if err != nil {
  163. log.Errorf("failed sfdisk-l %s: %s", d.dev, err)
  164. return ""
  165. }
  166. var partType string
  167. for i := 0; i < len(ret); i++ {
  168. if idx := strings.Index(ret[i], "Disk label type:"); idx > 0 {
  169. partType = strings.TrimSpace(string(ret[i])[idx+len("Disk label type:"):])
  170. }
  171. }
  172. if partType == "dos" {
  173. return "mbr"
  174. } else {
  175. return partType
  176. }
  177. }
  178. func (d *SGuestfishDiskPartition) Zerofree() {
  179. if !d.IsMounted() {
  180. switch d.fs {
  181. case "swap":
  182. d.zerofreeSwap()
  183. case "ext2", "ext3", "ext4":
  184. d.zerofreeExt()
  185. case "xfs", "ntfs":
  186. d.zerofreeSpace()
  187. }
  188. }
  189. }
  190. func (d *SGuestfishDiskPartition) zerofreeSwap() {
  191. res, err := d.fish.Blkid(d.partDev)
  192. if err != nil {
  193. log.Errorf("failed get blkid %s", err)
  194. return
  195. }
  196. var label, uuid string
  197. for i := 0; i < len(res); i++ {
  198. if strings.HasPrefix(res[i], "UUID:") {
  199. uuid = strings.TrimSpace(strings.Split(res[i], "")[1])
  200. } else if strings.HasPrefix(res[i], "LABEL:") {
  201. label = strings.TrimSpace(strings.Split(res[i], "")[1])
  202. }
  203. }
  204. if len(uuid) == 0 {
  205. log.Warningf("zerofree swap missing uuid")
  206. return
  207. }
  208. err = d.fish.Mkswap(d.partDev, uuid, label)
  209. if err != nil {
  210. log.Errorf("mkswap failed %s", err)
  211. }
  212. }
  213. func (d *SGuestfishDiskPartition) zerofreeExt() {
  214. err := d.fish.Zerofree(d.partDev)
  215. if err != nil {
  216. log.Errorf("zerofree %s failed %s", d.partDev, err)
  217. }
  218. }
  219. // mount and zero-free-space
  220. func (d *SGuestfishDiskPartition) zerofreeSpace() {
  221. if err := d.fish.Mount(d.partDev); err != nil {
  222. log.Errorf("failed mount partDev %s", err)
  223. return
  224. }
  225. if err := d.fish.ZeroFreeSpace("/"); err != nil {
  226. log.Errorf("guestfish zero free space failed %s", err)
  227. }
  228. }