Browse code

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)

Mike Snitzer authored on 2014/06/24 04:36:08
Showing 3 changed files
... ...
@@ -1170,19 +1170,19 @@ func NewDeviceSet(root string, doInit bool, options []string) (*DeviceSet, error
1170 1170
 		key = strings.ToLower(key)
1171 1171
 		switch key {
1172 1172
 		case "dm.basesize":
1173
-			size, err := units.FromHumanSize(val)
1173
+			size, err := units.RAMInBytes(val)
1174 1174
 			if err != nil {
1175 1175
 				return nil, err
1176 1176
 			}
1177 1177
 			devices.baseFsSize = uint64(size)
1178 1178
 		case "dm.loopdatasize":
1179
-			size, err := units.FromHumanSize(val)
1179
+			size, err := units.RAMInBytes(val)
1180 1180
 			if err != nil {
1181 1181
 				return nil, err
1182 1182
 			}
1183 1183
 			devices.dataLoopbackSize = size
1184 1184
 		case "dm.loopmetadatasize":
1185
-			size, err := units.FromHumanSize(val)
1185
+			size, err := units.RAMInBytes(val)
1186 1186
 			if err != nil {
1187 1187
 				return nil, err
1188 1188
 			}
... ...
@@ -58,11 +58,11 @@ func FromHumanSize(size string) (int64, error) {
58 58
 }
59 59
 
60 60
 // Parses a human-readable string representing an amount of RAM
61
-// in bytes, kibibytes, mebibytes or gibibytes, and returns the
62
-// number of bytes, or -1 if the string is unparseable.
61
+// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
62
+// returns the number of bytes, or -1 if the string is unparseable.
63 63
 // Units are case-insensitive, and the 'b' suffix is optional.
64 64
 func RAMInBytes(size string) (bytes int64, err error) {
65
-	re, error := regexp.Compile("^(\\d+)([kKmMgG])?[bB]?$")
65
+	re, error := regexp.Compile("^(\\d+)([kKmMgGtT])?[bB]?$")
66 66
 	if error != nil {
67 67
 		return -1, error
68 68
 	}
... ...
@@ -86,6 +86,8 @@ func RAMInBytes(size string) (bytes int64, err error) {
86 86
 		memLimit *= 1024 * 1024
87 87
 	} else if unit == "g" {
88 88
 		memLimit *= 1024 * 1024 * 1024
89
+	} else if unit == "t" {
90
+		memLimit *= 1024 * 1024 * 1024 * 1024
89 91
 	}
90 92
 
91 93
 	return memLimit, nil
... ...
@@ -64,7 +64,10 @@ func TestRAMInBytes(t *testing.T) {
64 64
 	assertRAMInBytes(t, "32kb", false, 32*1024)
65 65
 	assertRAMInBytes(t, "32Kb", false, 32*1024)
66 66
 	assertRAMInBytes(t, "32Mb", false, 32*1024*1024)
67
+	assertRAMInBytes(t, "32MB", false, 32*1024*1024)
67 68
 	assertRAMInBytes(t, "32Gb", false, 32*1024*1024*1024)
69
+	assertRAMInBytes(t, "32G", false, 32*1024*1024*1024)
70
+	assertRAMInBytes(t, "32Tb", false, 32*1024*1024*1024*1024)
68 71
 
69 72
 	assertRAMInBytes(t, "", true, -1)
70 73
 	assertRAMInBytes(t, "hello", true, -1)