[lxc-devel] [lxd/master] Improve out of box experience on non-Linux

stgraber on Github lxc-bot at linuxcontainers.org
Mon Aug 12 20:48:58 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190812/3405a35e/attachment-0001.bin>
-------------- next part --------------
From bc484539ae8b0c63969cc4a6ff912519b6d426a2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 12 Aug 2019 16:33:23 -0400
Subject: [PATCH 1/2] lxc/config: Detect non-Linux systems
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: St├ęphane Graber <stgraber at ubuntu.com>
---
 lxc/config/errors.go |  8 ++++++++
 lxc/config/remote.go | 11 +++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 lxc/config/errors.go

diff --git a/lxc/config/errors.go b/lxc/config/errors.go
new file mode 100644
index 0000000000..78cf5c65bc
--- /dev/null
+++ b/lxc/config/errors.go
@@ -0,0 +1,8 @@
+package config
+
+import (
+	"fmt"
+)
+
+// ErrNotLinux is returned when attemping to access the "local" remote on non-Linux systems
+var ErrNotLinux = fmt.Errorf("Can't connect to a local LXD server on a non-Linux system")
diff --git a/lxc/config/remote.go b/lxc/config/remote.go
index 02cdaafd87..cd7a7d8eca 100644
--- a/lxc/config/remote.go
+++ b/lxc/config/remote.go
@@ -7,6 +7,7 @@ import (
 	"io/ioutil"
 	"net/url"
 	"os"
+	"runtime"
 	"strings"
 
 	"github.com/juju/persistent-cookiejar"
@@ -51,6 +52,11 @@ func (c *Config) ParseRemote(raw string) (string, string, error) {
 
 // GetContainerServer returns a ContainerServer struct for the remote
 func (c *Config) GetContainerServer(name string) (lxd.ContainerServer, error) {
+	// Handle "local" on non-Linux
+	if name == "local" && runtime.GOOS != "linux" {
+		return nil, ErrNotLinux
+	}
+
 	// Get the remote
 	remote, ok := c.Remotes[name]
 	if !ok {
@@ -109,6 +115,11 @@ func (c *Config) GetContainerServer(name string) (lxd.ContainerServer, error) {
 
 // GetImageServer returns a ImageServer struct for the remote
 func (c *Config) GetImageServer(name string) (lxd.ImageServer, error) {
+	// Handle "local" on non-Linux
+	if name == "local" && runtime.GOOS != "linux" {
+		return nil, ErrNotLinux
+	}
+
 	// Get the remote
 	remote, ok := c.Remotes[name]
 	if !ok {

From 3c656460e4c22f19e703c00bc99cd5bf2d5064ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 12 Aug 2019 16:48:39 -0400
Subject: [PATCH 2/2] lxc: Better error handling on non-Linux
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: St├ęphane Graber <stgraber at ubuntu.com>
---
 lxc/main.go | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/lxc/main.go b/lxc/main.go
index e3f4e6e5c5..b42387f8d3 100644
--- a/lxc/main.go
+++ b/lxc/main.go
@@ -53,6 +53,7 @@ func main() {
 All of LXD's features can be driven through the various commands below.
 For help with any of those, simply call them with --help.`))
 	app.SilenceUsage = true
+	app.SilenceErrors = true
 
 	// Global flags
 	globalCmd := cmdGlobal{cmd: app}
@@ -236,6 +237,18 @@ For help with any of those, simply call them with --help.`))
 	// Run the main command and handle errors
 	err = app.Execute()
 	if err != nil {
+		// Handle non-Linux systems
+		if err == config.ErrNotLinux {
+			fmt.Fprintf(os.Stderr, i18n.G(`This client hasn't been configured to use a remote LXD server yet.
+As your platform can't run native Linux containers, you must connect to a remote LXD server.
+
+If you already added a remote server, make it the default with "lxc remote switch NAME".
+To easily setup a local LXD server in a virtual machine, consider using: https://multipass.run`)+"\n")
+			os.Exit(1)
+		}
+
+		// Default error handling
+		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
 		os.Exit(1)
 	}
 


More information about the lxc-devel mailing list