[lxc-devel] [go-lxc/v2] bindings: add runtime version check

brauner on Github lxc-bot at linuxcontainers.org
Thu Nov 1 15:59:12 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 475 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20181101/a346f1e6/attachment.bin>
-------------- next part --------------
From 701170ecba125df92f3934f3795e1aa0289d9479 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Thu, 1 Nov 2018 16:56:27 +0100
Subject: [PATCH] bindings: add runtime version check

Do not expose it as a public function just yet though.

Needed-by: https://github.com/lxc/lxd/pull/5228

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 lxc-binding.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 75 insertions(+), 1 deletion(-)

diff --git a/lxc-binding.go b/lxc-binding.go
index 7be39fd..b28f35f 100644
--- a/lxc-binding.go
+++ b/lxc-binding.go
@@ -19,6 +19,7 @@ import "C"
 import (
 	"fmt"
 	"runtime"
+	"strconv"
 	"strings"
 	"unsafe"
 )
@@ -244,9 +245,82 @@ func IsSupportedConfigItem(key string) bool {
 	return bool(C.go_lxc_config_item_is_supported(configItem))
 }
 
+// runtimeLiblxcVersionAtLeast checks if the system's liblxc matches the
+// provided version requirement
+func runtimeLiblxcVersionAtLeast(major int, minor int, micro int) bool {
+	version := Version()
+	version = strings.Replace(version, " (devel)", "-devel", 1)
+	parts := strings.Split(version, ".")
+	partsLen := len(parts)
+	if partsLen == 0 {
+		return false
+	}
+
+	develParts := strings.Split(parts[partsLen-1], "-")
+	if len(develParts) == 2 && develParts[1] == "devel" {
+		return true
+	}
+
+	maj := -1
+	min := -1
+	mic := -1
+
+	for i, v := range parts {
+		if i > 2 {
+			break
+		}
+
+		num, err := strconv.Atoi(v)
+		if err != nil {
+			return false
+		}
+
+		switch i {
+		case 0:
+			maj = num
+		case 1:
+			min = num
+		case 2:
+			mic = num
+		}
+	}
+
+	/* Major version is greater. */
+	if maj > major {
+		return true
+	}
+
+	if maj < major {
+		return false
+	}
+
+	/* Minor number is greater.*/
+	if min > minor {
+		return true
+	}
+
+	if min < minor {
+		return false
+	}
+
+	/* Patch number is greater. */
+	if mic > micro {
+		return true
+	}
+
+	if mic < micro {
+		return false
+	}
+
+	return true
+}
+
 // HasApiExtension returns true if the extension is supported.
 func HasApiExtension(extension string) bool {
 	apiExtension := C.CString(extension)
 	defer C.free(unsafe.Pointer(apiExtension))
-	return bool(C.go_lxc_has_api_extension(apiExtension))
+	if runtimeLiblxcVersionAtLeast(3, 1, 0) {
+		return bool(C.go_lxc_has_api_extension(apiExtension))
+	}
+	return false
 }


More information about the lxc-devel mailing list