Skip to content

Commit

Permalink
devmapper: use RAMInBytes() rather than FromHumanSize()
Browse files Browse the repository at this point in the history
Device Mapper needs device sizes in binary (1024) multiples.  Otherwise
kernel checks can find that the specified thin-pool device sizes aren't
a multiple of the specified thin-pool blocksize.

The name for "RAMInBytes" is likely too narrow given the new consumers
but... Also add "tebibyte" support to RAMInBytes.

Docker-DCO-1.1-Signed-off-by: Mike Snitzer <snitzer@redhat.com> (github: snitm)
  • Loading branch information
snitm committed Jun 24, 2014
1 parent d420134 commit 2470a5e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
6 changes: 3 additions & 3 deletions daemon/graphdriver/devmapper/deviceset.go
Original file line number Diff line number Diff line change
Expand Up @@ -1170,19 +1170,19 @@ func NewDeviceSet(root string, doInit bool, options []string) (*DeviceSet, error
key = strings.ToLower(key)
switch key {
case "dm.basesize":
size, err := units.FromHumanSize(val)
size, err := units.RAMInBytes(val)
if err != nil {
return nil, err
}
devices.baseFsSize = uint64(size)
case "dm.loopdatasize":
size, err := units.FromHumanSize(val)
size, err := units.RAMInBytes(val)
if err != nil {
return nil, err
}
devices.dataLoopbackSize = size
case "dm.loopmetadatasize":
size, err := units.FromHumanSize(val)
size, err := units.RAMInBytes(val)
if err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/units/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func FromHumanSize(size string) (int64, error) {
}

// Parses a human-readable string representing an amount of RAM
// in bytes, kibibytes, mebibytes or gibibytes, and returns the
// number of bytes, or -1 if the string is unparseable.
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
// returns the number of bytes, or -1 if the string is unparseable.
// Units are case-insensitive, and the 'b' suffix is optional.
func RAMInBytes(size string) (bytes int64, err error) {
re, error := regexp.Compile("^(\\d+)([kKmMgG])?[bB]?$")
re, error := regexp.Compile("^(\\d+)([kKmMgGtT])?[bB]?$")
if error != nil {
return -1, error
}
Expand All @@ -86,6 +86,8 @@ func RAMInBytes(size string) (bytes int64, err error) {
memLimit *= 1024 * 1024
} else if unit == "g" {
memLimit *= 1024 * 1024 * 1024
} else if unit == "t" {
memLimit *= 1024 * 1024 * 1024 * 1024
}

return memLimit, nil
Expand Down
3 changes: 3 additions & 0 deletions pkg/units/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func TestRAMInBytes(t *testing.T) {
assertRAMInBytes(t, "32kb", false, 32*1024)
assertRAMInBytes(t, "32Kb", false, 32*1024)
assertRAMInBytes(t, "32Mb", false, 32*1024*1024)
assertRAMInBytes(t, "32MB", false, 32*1024*1024)
assertRAMInBytes(t, "32Gb", false, 32*1024*1024*1024)
assertRAMInBytes(t, "32G", false, 32*1024*1024*1024)
assertRAMInBytes(t, "32Tb", false, 32*1024*1024*1024*1024)

assertRAMInBytes(t, "", true, -1)
assertRAMInBytes(t, "hello", true, -1)
Expand Down

0 comments on commit 2470a5e

Please sign in to comment.