The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/4550

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
Signed-off-by: Stéphane Graber <[email protected]>
From 2733eb8d2ded89a9db3be4d3f0c935528f716b3c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <[email protected]>
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 <[email protected]>
---
 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
 }
_______________________________________________
lxc-devel mailing list
[email protected]
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to