dmsetup_client.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  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 devicemapper
  15. import (
  16. "os/exec"
  17. "strconv"
  18. "strings"
  19. "k8s.io/klog"
  20. )
  21. // DmsetupClient is a low-level client for interacting with device mapper via
  22. // the `dmsetup` utility, which is provided by the `device-mapper` package.
  23. type DmsetupClient interface {
  24. // Table runs `dmsetup table` on the given device name and returns the
  25. // output or an error.
  26. Table(deviceName string) ([]byte, error)
  27. // Message runs `dmsetup message` on the given device, passing the given
  28. // message to the given sector, and returns the output or an error.
  29. Message(deviceName string, sector int, message string) ([]byte, error)
  30. // Status runs `dmsetup status` on the given device and returns the output
  31. // or an error.
  32. Status(deviceName string) ([]byte, error)
  33. }
  34. // NewDmSetupClient returns a new DmsetupClient.
  35. func NewDmsetupClient() DmsetupClient {
  36. return &defaultDmsetupClient{}
  37. }
  38. // defaultDmsetupClient is a functional DmsetupClient
  39. type defaultDmsetupClient struct{}
  40. var _ DmsetupClient = &defaultDmsetupClient{}
  41. func (c *defaultDmsetupClient) Table(deviceName string) ([]byte, error) {
  42. return c.dmsetup("table", deviceName)
  43. }
  44. func (c *defaultDmsetupClient) Message(deviceName string, sector int, message string) ([]byte, error) {
  45. return c.dmsetup("message", deviceName, strconv.Itoa(sector), message)
  46. }
  47. func (c *defaultDmsetupClient) Status(deviceName string) ([]byte, error) {
  48. return c.dmsetup("status", deviceName)
  49. }
  50. func (*defaultDmsetupClient) dmsetup(args ...string) ([]byte, error) {
  51. klog.V(5).Infof("running dmsetup %v", strings.Join(args, " "))
  52. return exec.Command("dmsetup", args...).Output()
  53. }