fsdriver.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 qga
  15. import (
  16. "yunion.io/x/pkg/errors"
  17. "yunion.io/x/onecloud/pkg/hostman/diskutils/fsutils/driver"
  18. )
  19. type SQgaDriver struct {
  20. agent *QemuGuestAgent
  21. }
  22. func NewQgaFsutilDriver(agent *QemuGuestAgent) driver.IFsutilExecDriver {
  23. ret := new(SQgaDriver)
  24. ret.agent = agent
  25. return ret
  26. }
  27. func (q *SQgaDriver) ExecInputWait(name string, args []string, input []string) (int, string, string, error) {
  28. return q.agent.CommandWithTimeout(name, args, nil, "", true, -1)
  29. }
  30. func (q *SQgaDriver) Exec(name string, args ...string) ([]byte, error) {
  31. retCode, stdout, stderr, err := q.agent.CommandWithTimeout(name, args, nil, "", true, -1)
  32. if err != nil {
  33. return nil, err
  34. }
  35. if retCode != 0 {
  36. return []byte(stdout + "\n" + stderr), errors.Errorf("Exit code %d", retCode)
  37. }
  38. var retStr = []byte(stdout)
  39. if len(stderr) > 0 {
  40. retStr = []byte(stdout + "\n" + stderr)
  41. }
  42. return retStr, nil
  43. }
  44. func (q *SQgaDriver) Run(name string, args ...string) error {
  45. retCode, stdout, stderr, err := q.agent.CommandWithTimeout(name, args, nil, "", true, -1)
  46. if err != nil {
  47. return err
  48. }
  49. if retCode != 0 {
  50. return errors.Errorf("Exit code %d\n%s\n%s", retCode, stdout, stderr)
  51. }
  52. return nil
  53. }