volume_linux.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // +build linux
  2. package volume
  3. import (
  4. "fmt"
  5. "strings"
  6. mounttypes "github.com/docker/docker/api/types/mount"
  7. )
  8. // ConvertTmpfsOptions converts *mounttypes.TmpfsOptions to the raw option string
  9. // for mount(2).
  10. func ConvertTmpfsOptions(opt *mounttypes.TmpfsOptions, readOnly bool) (string, error) {
  11. var rawOpts []string
  12. if readOnly {
  13. rawOpts = append(rawOpts, "ro")
  14. }
  15. if opt != nil && opt.Mode != 0 {
  16. rawOpts = append(rawOpts, fmt.Sprintf("mode=%o", opt.Mode))
  17. }
  18. if opt != nil && opt.SizeBytes != 0 {
  19. // calculate suffix here, making this linux specific, but that is
  20. // okay, since API is that way anyways.
  21. // we do this by finding the suffix that divides evenly into the
  22. // value, returing the value itself, with no suffix, if it fails.
  23. //
  24. // For the most part, we don't enforce any semantic to this values.
  25. // The operating system will usually align this and enforce minimum
  26. // and maximums.
  27. var (
  28. size = opt.SizeBytes
  29. suffix string
  30. )
  31. for _, r := range []struct {
  32. suffix string
  33. divisor int64
  34. }{
  35. {"g", 1 << 30},
  36. {"m", 1 << 20},
  37. {"k", 1 << 10},
  38. } {
  39. if size%r.divisor == 0 {
  40. size = size / r.divisor
  41. suffix = r.suffix
  42. break
  43. }
  44. }
  45. rawOpts = append(rawOpts, fmt.Sprintf("size=%d%s", size, suffix))
  46. }
  47. return strings.Join(rawOpts, ","), nil
  48. }