[lxc-devel] [lxd/master] NIC Bridged: Use of network argument when detecting parent

tomponline on Github lxc-bot at linuxcontainers.org
Wed Apr 1 09:26:42 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 378 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200401/de48661c/attachment.bin>
-------------- next part --------------
From 4e1ecc846a238d6ebae63c38010abb5ff2dd1cb1 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 1 Apr 2020 09:20:12 +0100
Subject: [PATCH 1/3] lxc/network: Updates network detach checks to use bridged
 network property

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxc/network.go | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lxc/network.go b/lxc/network.go
index 76e5f06b78..2c24635e5a 100644
--- a/lxc/network.go
+++ b/lxc/network.go
@@ -415,7 +415,7 @@ func (c *cmdNetworkDetach) Run(cmd *cobra.Command, args []string) error {
 	// Find the device
 	if devName == "" {
 		for n, d := range inst.Devices {
-			if d["type"] == "nic" && d["parent"] == resource.name {
+			if d["type"] == "nic" && (d["parent"] == resource.name || d["network"] == resource.name) {
 				if devName != "" {
 					return fmt.Errorf(i18n.G("More than one device matches, specify the device name"))
 				}
@@ -434,7 +434,7 @@ func (c *cmdNetworkDetach) Run(cmd *cobra.Command, args []string) error {
 		return fmt.Errorf(i18n.G("The specified device doesn't exist"))
 	}
 
-	if device["type"] != "nic" || device["parent"] != resource.name {
+	if device["type"] != "nic" || (device["parent"] != resource.name && device["network"] != resource.name) {
 		return fmt.Errorf(i18n.G("The specified device doesn't match the network"))
 	}
 
@@ -500,7 +500,7 @@ func (c *cmdNetworkDetachProfile) Run(cmd *cobra.Command, args []string) error {
 	// Find the device
 	if devName == "" {
 		for n, d := range profile.Devices {
-			if d["type"] == "nic" && d["parent"] == resource.name {
+			if d["type"] == "nic" && (d["parent"] == resource.name || d["network"] == resource.name) {
 				if devName != "" {
 					return fmt.Errorf(i18n.G("More than one device matches, specify the device name"))
 				}
@@ -519,7 +519,7 @@ func (c *cmdNetworkDetachProfile) Run(cmd *cobra.Command, args []string) error {
 		return fmt.Errorf(i18n.G("The specified device doesn't exist"))
 	}
 
-	if device["type"] != "nic" || device["parent"] != resource.name {
+	if device["type"] != "nic" || (device["parent"] != resource.name && device["network"] != resource.name) {
 		return fmt.Errorf(i18n.G("The specified device doesn't match the network"))
 	}
 

From 7b26c661a4b4bec3c71d835ddc0435a88969eb33 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 1 Apr 2020 10:24:42 +0100
Subject: [PATCH 2/3] lxd/network/network/utils: Updates network setting
 detection in IsInUse

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/network/network_utils.go | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lxd/network/network_utils.go b/lxd/network/network_utils.go
index e61b90a00d..91c6c632a0 100644
--- a/lxd/network/network_utils.go
+++ b/lxd/network/network_utils.go
@@ -38,8 +38,9 @@ func IsInUse(c instance.Instance, networkName string) bool {
 			continue
 		}
 
-		if d["network"] == networkName {
-			return true
+		// Temporarily populate parent from network setting if used.
+		if d["network"] != "" {
+			d["parent"] = d["network"]
 		}
 
 		if d["parent"] == "" {

From 6b1a72d5deae8bd03408943d91d4cfd36e5820a7 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 1 Apr 2020 10:25:07 +0100
Subject: [PATCH 3/3] lxd/instance/drivers/driver/qemu: Adds host_name info to
 RenderState when lxd-agent is running

Previously only agentless state info had host_name.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instance/drivers/driver_qemu.go | 40 ++++++++++++++++++++++++-----
 1 file changed, 34 insertions(+), 6 deletions(-)

diff --git a/lxd/instance/drivers/driver_qemu.go b/lxd/instance/drivers/driver_qemu.go
index c603f7f58b..9fc600074c 100644
--- a/lxd/instance/drivers/driver_qemu.go
+++ b/lxd/instance/drivers/driver_qemu.go
@@ -3604,18 +3604,20 @@ func (vm *qemu) RenderState() (*api.InstanceState, error) {
 	pid, _ := vm.pid()
 
 	if statusCode == api.Running {
+		// Try and get state info from agent.
 		status, err := vm.agentGetState()
 		if err != nil {
 			if err != errQemuAgentOffline {
 				logger.Warn("Could not get VM state from agent", log.Ctx{"project": vm.Project(), "instance": vm.Name(), "err": err})
 			}
 
-			// Fallback data.
+			// Fallback data if agent is not reachable.
 			status = &api.InstanceState{}
 			status.Processes = -1
 			networks := map[string]api.InstanceStateNetwork{}
 			for k, m := range vm.ExpandedDevices() {
-				// We only care about nics.
+				// We only care about bridged nics as these can use a local DHCP server that allows
+				// us to parse the leases file below.
 				if m["type"] != "nic" || m.NICType() != "bridged" {
 					continue
 				}
@@ -3626,6 +3628,11 @@ func (vm *qemu) RenderState() (*api.InstanceState, error) {
 					return nil, err
 				}
 
+				// Temporarily populate parent from network setting if used.
+				if m["network"] != "" {
+					m["parent"] = m["network"]
+				}
+
 				// Parse the lease file.
 				addresses, err := network.GetLeaseAddresses(vm.state, m["parent"], m["hwaddr"])
 				if err != nil {
@@ -3642,10 +3649,6 @@ func (vm *qemu) RenderState() (*api.InstanceState, error) {
 					return nil, err
 				}
 
-				if m["host_name"] == "" {
-					m["host_name"] = vm.localConfig[fmt.Sprintf("volatile.%s.host_name", k)]
-				}
-
 				// Retrieve the host counters, as we report the values
 				// from the instance's point of view, those counters need to be reversed below.
 				hostCounters := shared.NetworkGetCounters(m["host_name"])
@@ -3669,6 +3672,31 @@ func (vm *qemu) RenderState() (*api.InstanceState, error) {
 			status.Network = networks
 		}
 
+		// Populate host_name for network devices.
+		for k, m := range vm.ExpandedDevices() {
+			// We only care about nics.
+			if m["type"] != "nic" {
+				continue
+			}
+
+			// Get hwaddr from static or volatile config.
+			hwaddr := m["hwaddr"]
+			if hwaddr == "" {
+				hwaddr = vm.localConfig[fmt.Sprintf("volatile.%s.hwaddr", k)]
+			}
+
+			// We have to match on hwaddr as device name can be different from the configured device
+			// name when reported from the lxd-agent inside the VM (due to the guest OS choosing name).
+			for netName, netStatus := range status.Network {
+				if netStatus.Hwaddr == hwaddr {
+					if netStatus.HostName == "" {
+						netStatus.HostName = vm.localConfig[fmt.Sprintf("volatile.%s.host_name", k)]
+						status.Network[netName] = netStatus
+					}
+				}
+			}
+		}
+
 		status.Pid = int64(pid)
 		status.Status = statusCode.String()
 		status.StatusCode = statusCode


More information about the lxc-devel mailing list