devmapper: use RAMInBytes() rather than FromHumanSize()

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)
This commit is contained in:
Mike Snitzer 2014-06-23 15:36:08 -04:00
parent 1b4ca55744
commit b5dee24bb6
2 changed files with 8 additions and 3 deletions

View file

@ -58,11 +58,11 @@ func FromHumanSize(size string) (int64, error) {
} }
// Parses a human-readable string representing an amount of RAM // Parses a human-readable string representing an amount of RAM
// in bytes, kibibytes, mebibytes or gibibytes, and returns the // in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
// number of bytes, or -1 if the string is unparseable. // returns the number of bytes, or -1 if the string is unparseable.
// Units are case-insensitive, and the 'b' suffix is optional. // Units are case-insensitive, and the 'b' suffix is optional.
func RAMInBytes(size string) (bytes int64, err error) { 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 { if error != nil {
return -1, error return -1, error
} }
@ -86,6 +86,8 @@ func RAMInBytes(size string) (bytes int64, err error) {
memLimit *= 1024 * 1024 memLimit *= 1024 * 1024
} else if unit == "g" { } else if unit == "g" {
memLimit *= 1024 * 1024 * 1024 memLimit *= 1024 * 1024 * 1024
} else if unit == "t" {
memLimit *= 1024 * 1024 * 1024 * 1024
} }
return memLimit, nil return memLimit, nil

View file

@ -64,7 +64,10 @@ func TestRAMInBytes(t *testing.T) {
assertRAMInBytes(t, "32kb", false, 32*1024) assertRAMInBytes(t, "32kb", false, 32*1024)
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, "32MB", false, 32*1024*1024)
assertRAMInBytes(t, "32Gb", false, 32*1024*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, "", true, -1)
assertRAMInBytes(t, "hello", true, -1) assertRAMInBytes(t, "hello", true, -1)