[lxc-devel] [lxd/master] shared: export limit parsing function

tych0 on Github lxc-bot at linuxcontainers.org
Wed Feb 24 18:46:25 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 491 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160224/4cb91df2/attachment.bin>
-------------- next part --------------
From 83af6cf8a98cff8750085a69951d125648684d36 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Wed, 24 Feb 2016 11:45:21 -0700
Subject: [PATCH] shared: export limit parsing function

For other golang clients who want to figure out the memory from a LXD limit
in string format from the config, this is useful.

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 lxd/container.go     |  2 +-
 lxd/container_lxc.go |  6 +++---
 lxd/db_update.go     |  4 ++--
 lxd/devices.go       | 51 +++------------------------------------------------
 shared/util.go       | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 56 insertions(+), 54 deletions(-)

diff --git a/lxd/container.go b/lxd/container.go
index 9dc714e..df99790 100644
--- a/lxd/container.go
+++ b/lxd/container.go
@@ -618,7 +618,7 @@ func containerConfigureInternal(c container) error {
 			continue
 		}
 
-		size, err := deviceParseBytes(m["size"])
+		size, err := shared.ParseSizeString(m["size"])
 		if err != nil {
 			return err
 		}
diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 9983067..241f5fc 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -470,7 +470,7 @@ func (c *containerLXC) initLXC() error {
 
 				valueInt = int64((memoryTotal / 100) * percent)
 			} else {
-				valueInt, err = deviceParseBytes(memory)
+				valueInt, err = shared.ParseSizeString(memory)
 				if err != nil {
 					return err
 				}
@@ -1883,7 +1883,7 @@ func (c *containerLXC) Update(args containerArgs, userRequested bool) error {
 		}
 
 		if m["size"] != oldRootfsSize {
-			size, err := deviceParseBytes(m["size"])
+			size, err := shared.ParseSizeString(m["size"])
 			if err != nil {
 				undoChanges()
 				return err
@@ -1986,7 +1986,7 @@ func (c *containerLXC) Update(args containerArgs, userRequested bool) error {
 
 					memory = fmt.Sprintf("%d", int64((memoryTotal/100)*percent))
 				} else {
-					valueInt, err := deviceParseBytes(memory)
+					valueInt, err := shared.ParseSizeString(memory)
 					if err != nil {
 						undoChanges()
 						return err
diff --git a/lxd/db_update.go b/lxd/db_update.go
index 579825c..c43ade8 100644
--- a/lxd/db_update.go
+++ b/lxd/db_update.go
@@ -84,7 +84,7 @@ func dbUpdateFromV18(db *sql.DB) error {
 		value += "B"
 
 		// Deal with completely broken values
-		_, err = deviceParseBytes(value)
+		_, err = shared.ParseSizeString(value)
 		if err != nil {
 			shared.Debugf("Invalid container memory limit, id=%d value=%s, removing.", id, value)
 			_, err = db.Exec("DELETE FROM containers_config WHERE id=?;", id)
@@ -121,7 +121,7 @@ func dbUpdateFromV18(db *sql.DB) error {
 		value += "B"
 
 		// Deal with completely broken values
-		_, err = deviceParseBytes(value)
+		_, err = shared.ParseSizeString(value)
 		if err != nil {
 			shared.Debugf("Invalid profile memory limit, id=%d value=%s, removing.", id, value)
 			_, err = db.Exec("DELETE FROM profiles_config WHERE id=?;", id)
diff --git a/lxd/devices.go b/lxd/devices.go
index 9f54271..770a5dc 100644
--- a/lxd/devices.go
+++ b/lxd/devices.go
@@ -656,51 +656,6 @@ func deviceParseBits(input string) (int64, error) {
 	return valueInt * multiplicator, nil
 }
 
-func deviceParseBytes(input string) (int64, error) {
-	if input == "" {
-		return 0, nil
-	}
-
-	if len(input) < 3 {
-		return -1, fmt.Errorf("Invalid value: %s", input)
-	}
-
-	// Extract the suffix
-	suffix := input[len(input)-2:]
-
-	// Extract the value
-	value := input[0 : len(input)-2]
-	valueInt, err := strconv.ParseInt(value, 10, 64)
-	if err != nil {
-		return -1, fmt.Errorf("Invalid integer: %s", input)
-	}
-
-	if valueInt < 0 {
-		return -1, fmt.Errorf("Invalid value: %d", valueInt)
-	}
-
-	// Figure out the multiplicator
-	multiplicator := int64(0)
-	switch suffix {
-	case "kB":
-		multiplicator = 1024
-	case "MB":
-		multiplicator = 1024 * 1024
-	case "GB":
-		multiplicator = 1024 * 1024 * 1024
-	case "TB":
-		multiplicator = 1024 * 1024 * 1024 * 1024
-	case "PB":
-		multiplicator = 1024 * 1024 * 1024 * 1024 * 1024
-	case "EB":
-		multiplicator = 1024 * 1024 * 1024 * 1024 * 1024 * 1024
-	default:
-		return -1, fmt.Errorf("Unsupported suffix: %s", suffix)
-	}
-
-	return valueInt * multiplicator, nil
-}
-
 func deviceTotalMemory() (int64, error) {
 	// Open /proc/meminfo
 	f, err := os.Open("/proc/meminfo")
@@ -723,8 +678,8 @@ func deviceTotalMemory() (int64, error) {
 		fields := strings.Split(line, " ")
 		value := fields[len(fields)-2] + fields[len(fields)-1]
 
-		// Feed the result to deviceParseBytes to get an int value
-		valueBytes, err := deviceParseBytes(value)
+		// Feed the result to shared.ParseSizeString to get an int value
+		valueBytes, err := shared.ParseSizeString(value)
 		if err != nil {
 			return -1, err
 		}
@@ -892,7 +847,7 @@ func deviceParseDiskLimit(readSpeed string, writeSpeed string) (int64, int64, in
 				return -1, -1, err
 			}
 		} else {
-			bps, err = deviceParseBytes(value)
+			bps, err = shared.ParseSizeString(value)
 			if err != nil {
 				return -1, -1, err
 			}
diff --git a/shared/util.go b/shared/util.go
index a583af4..8940a6d 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -562,3 +562,50 @@ func ParseMetadata(metadata interface{}) (map[string]interface{}, error) {
 
 	return newMetadata, nil
 }
+
+// Parse a size (e.g. 200kB or 5GB) into the number of bytes it represents.
+// Supports suffixes up to EB. "" == 0.
+func ParseSizeString(input string) (int64, error) {
+	if input == "" {
+		return 0, nil
+	}
+
+	if len(input) < 3 {
+		return -1, fmt.Errorf("Invalid value: %s", input)
+	}
+
+	// Extract the suffix
+	suffix := input[len(input)-2:]
+
+	// Extract the value
+	value := input[0 : len(input)-2]
+	valueInt, err := strconv.ParseInt(value, 10, 64)
+	if err != nil {
+		return -1, fmt.Errorf("Invalid integer: %s", input)
+	}
+
+	if valueInt < 0 {
+		return -1, fmt.Errorf("Invalid value: %d", valueInt)
+	}
+
+	// Figure out the multiplicator
+	multiplicator := int64(0)
+	switch suffix {
+	case "kB":
+		multiplicator = 1024
+	case "MB":
+		multiplicator = 1024 * 1024
+	case "GB":
+		multiplicator = 1024 * 1024 * 1024
+	case "TB":
+		multiplicator = 1024 * 1024 * 1024 * 1024
+	case "PB":
+		multiplicator = 1024 * 1024 * 1024 * 1024 * 1024
+	case "EB":
+		multiplicator = 1024 * 1024 * 1024 * 1024 * 1024 * 1024
+	default:
+		return -1, fmt.Errorf("Unsupported suffix: %s", suffix)
+	}
+
+	return valueInt * multiplicator, nil
+}


More information about the lxc-devel mailing list