docker.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 docker
  15. import (
  16. "fmt"
  17. "os"
  18. "strings"
  19. dockertypes "github.com/docker/docker/api/types"
  20. )
  21. const (
  22. DockerInfoDriver = "Driver"
  23. DockerInfoDriverStatus = "DriverStatus"
  24. DriverStatusPoolName = "Pool Name"
  25. DriverStatusDataLoopFile = "Data loop file"
  26. DriverStatusMetadataFile = "Metadata file"
  27. DriverStatusParentDataset = "Parent Dataset"
  28. )
  29. func DriverStatusValue(status [][2]string, target string) string {
  30. for _, v := range status {
  31. if strings.EqualFold(v[0], target) {
  32. return v[1]
  33. }
  34. }
  35. return ""
  36. }
  37. func DockerThinPoolName(info dockertypes.Info) (string, error) {
  38. poolName := DriverStatusValue(info.DriverStatus, DriverStatusPoolName)
  39. if len(poolName) == 0 {
  40. return "", fmt.Errorf("Could not get devicemapper pool name")
  41. }
  42. return poolName, nil
  43. }
  44. func DockerMetadataDevice(info dockertypes.Info) (string, error) {
  45. metadataDevice := DriverStatusValue(info.DriverStatus, DriverStatusMetadataFile)
  46. if len(metadataDevice) != 0 {
  47. return metadataDevice, nil
  48. }
  49. poolName, err := DockerThinPoolName(info)
  50. if err != nil {
  51. return "", err
  52. }
  53. metadataDevice = fmt.Sprintf("/dev/mapper/%s_tmeta", poolName)
  54. if _, err := os.Stat(metadataDevice); err != nil {
  55. return "", err
  56. }
  57. return metadataDevice, nil
  58. }
  59. func DockerZfsFilesystem(info dockertypes.Info) (string, error) {
  60. filesystem := DriverStatusValue(info.DriverStatus, DriverStatusParentDataset)
  61. if len(filesystem) == 0 {
  62. return "", fmt.Errorf("Could not get zfs filesystem")
  63. }
  64. return filesystem, nil
  65. }