[lxc-devel] [lxd/master] lxd/init: Have --auto setup networkng if missing

stgraber on Github lxc-bot at linuxcontainers.org
Thu May 10 02:00:09 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180510/3e907967/attachment.bin>
-------------- next part --------------
From 2733eb8d2ded89a9db3be4d3f0c935528f716b3c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Sat, 5 May 2018 14:14:38 +0200
Subject: [PATCH] lxd/init: Have --auto setup networkng if missing
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>
---
 lxd/main_init_auto.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)

diff --git a/lxd/main_init_auto.go b/lxd/main_init_auto.go
index 352c511b0..656b8d02f 100644
--- a/lxd/main_init_auto.go
+++ b/lxd/main_init_auto.go
@@ -111,5 +111,74 @@ func (c *cmdInit) RunAuto(cmd *cobra.Command, args []string, d lxd.ContainerServ
 		}}
 	}
 
+	// Network configuration
+	networks, err := d.GetNetworks()
+	if err != nil {
+		return nil, errors.Wrap(err, "Failed to retrieve list of networks")
+	}
+
+	// Extract managed networks
+	managedNetworks := []api.Network{}
+	for _, network := range networks {
+		if network.Managed {
+			managedNetworks = append(managedNetworks, network)
+		}
+	}
+
+	// Look for an existing network device in the profile
+	defaultProfileNetwork := false
+	defaultProfile, _, err := d.GetProfile("default")
+	if err == nil {
+		for _, dev := range defaultProfile.Devices {
+			if dev["type"] == "nic" {
+				defaultProfileNetwork = true
+				break
+			}
+		}
+	}
+
+	// Define a new network
+	if len(managedNetworks) == 0 && !defaultProfileNetwork {
+		// Find a new name
+		idx := 0
+		for {
+			if shared.PathExists(fmt.Sprintf("/sys/class/net/lxdbr%d", idx)) {
+				idx += 1
+				continue
+			}
+
+			break
+		}
+
+		// Define the new network
+		network := api.NetworksPost{}
+		network.Name = fmt.Sprintf("lxdbr%d", idx)
+		config.Networks = append(config.Networks, network)
+
+		// Add it to the profile
+		if config.Profiles == nil {
+			config.Profiles = []api.ProfilesPost{{
+				Name: "default",
+				ProfilePut: api.ProfilePut{
+					Devices: map[string]map[string]string{
+						"eth0": {
+							"type":    "nic",
+							"nictype": "bridged",
+							"parent":  network.Name,
+							"name":    "eth0",
+						},
+					},
+				},
+			}}
+		} else {
+			config.Profiles[0].Devices["eth0"] = map[string]string{
+				"type":    "nic",
+				"nictype": "bridged",
+				"parent":  network.Name,
+				"name":    "eth0",
+			}
+		}
+	}
+
 	return &config, nil
 }


More information about the lxc-devel mailing list