[lxc-devel] [lxc/master] hooks: Adds LXC_NET_ID enviroment variable

tomponline on Github lxc-bot at linuxcontainers.org
Tue Apr 30 11:27:31 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 672 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190430/9a63a7f8/attachment-0001.bin>
-------------- next part --------------
From 72a34205b77e7094b693d7715e76d68b0015a5a2 Mon Sep 17 00:00:00 2001
From: tomponline <thomas.parrott at canonical.com>
Date: Tue, 30 Apr 2019 12:22:52 +0100
Subject: [PATCH] hooks: Adds LXC_NET_ID enviroment variable

Adds LXC_NET_ID environment variable to network up and down hooks.

This represents the netdev index ID of the network device in the config file.

E.g. lxc.net.6.type=ipvlan would result in LXC_NET_ID = 6

This allows hooks to parse the relevant config file section related to the network being processed.

Signed-off-by: tomponline <thomas.parrott at canonical.com>
---
 doc/lxc.container.conf.sgml.in |  8 ++++++++
 src/lxc/conf.c                 | 19 +++++++++++++++----
 src/lxc/conf.h                 |  2 +-
 src/lxc/network.c              | 25 ++++++++++++-------------
 4 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/doc/lxc.container.conf.sgml.in b/doc/lxc.container.conf.sgml.in
index 3b3dd6ddeb..39e224be57 100644
--- a/doc/lxc.container.conf.sgml.in
+++ b/doc/lxc.container.conf.sgml.in
@@ -699,6 +699,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
                  to 1.
                   </para>
                 </listitem>
+
+                <listitem>
+                 <para>
+                 LXC_NET_ID: the network device index ID in the config file.
+                 Note that this information is only available when
+                 <option>lxc.hook.version</option> is set to 1.
+                  </para>
+                </listitem>
               </itemizedlist>
 
               Whether this information is provided in the form of environment
diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index ec9543f743..b9b9700ae8 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -360,7 +360,7 @@ static int run_buffer(char *buffer)
 
 int run_script_argv(const char *name, unsigned int hook_version,
 		    const char *section, const char *script,
-		    const char *hookname, char **argv)
+		    const char *hookname, char **argv, int netdev_idx)
 {
 	__do_free char *buffer = NULL;
 	int buf_pos, i, ret;
@@ -428,8 +428,6 @@ int run_script_argv(const char *name, unsigned int hook_version,
 		TRACE("Set environment variable: LXC_HOOK_SECTION=%s", section);
 
 		if (strcmp(section, "net") == 0) {
-			char *parent;
-
 			if (!argv || !argv[0])
 				return -1;
 
@@ -441,6 +439,19 @@ int run_script_argv(const char *name, unsigned int hook_version,
 			}
 			TRACE("Set environment variable: LXC_NET_TYPE=%s", argv[0]);
 
+			if (netdev_idx > -1) {
+				char netidx_buf[15];
+				sprintf(netidx_buf, "%d", netdev_idx);
+				ret = setenv("LXC_NET_ID", netidx_buf, 1);
+				if (ret < 0) {
+					SYSERROR("Failed to set environment variable: "
+						"LXC_NET_ID=%d", netdev_idx);
+					return -1;
+				}
+				TRACE("Set environment variable: LXC_NET_ID=%d", netdev_idx);
+			}
+
+			char *parent;
 			parent = argv[1] ? argv[1] : "";
 
 			if (strcmp(argv[0], "macvlan") == 0) {
@@ -3729,7 +3740,7 @@ int run_lxc_hooks(const char *name, char *hookname, struct lxc_conf *conf,
 		char *hook = it->elem;
 
 		ret = run_script_argv(name, conf->hooks_version, "lxc", hook,
-				      hookname, argv);
+				      hookname, argv, -1);
 		if (ret < 0)
 			return -1;
 	}
diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index 2664a1527c..a4f7663fcb 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -452,7 +452,7 @@ extern int run_script(const char *name, const char *section, const char *script,
 		      ...);
 extern int run_script_argv(const char *name, unsigned int hook_version,
 			   const char *section, const char *script,
-			   const char *hookname, char **argsin);
+			   const char *hookname, char **argsin, int netdev_idx);
 extern int in_caplist(int cap, struct lxc_list *caps);
 extern int setup_sysctl_parameters(struct lxc_list *sysctls);
 extern int lxc_clear_sysctls(struct lxc_conf *c, const char *key);
diff --git a/src/lxc/network.c b/src/lxc/network.c
index ec7dbccccf..a28980353e 100644
--- a/src/lxc/network.c
+++ b/src/lxc/network.c
@@ -303,7 +303,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
 
 		err = run_script_argv(handler->name,
 				handler->conf->hooks_version, "net",
-				netdev->upscript, "up", argv);
+				netdev->upscript, "up", argv, netdev->idx);
 		if (err < 0)
 			goto out_delete;
 	}
@@ -361,7 +361,7 @@ static int instantiate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n
 
 		err = run_script_argv(handler->name,
 				handler->conf->hooks_version, "net",
-				netdev->upscript, "up", argv);
+				netdev->upscript, "up", argv, netdev->idx);
 		if (err < 0)
 			goto on_error;
 	}
@@ -416,7 +416,7 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd
 
 		err = run_script_argv(handler->name,
 				handler->conf->hooks_version, "net",
-				netdev->upscript, "up", argv);
+				netdev->upscript, "up", argv, netdev->idx);
 		if (err < 0) {
 			lxc_netdev_delete_by_name(peer);
 			return -1;
@@ -482,7 +482,7 @@ static int instantiate_phys(struct lxc_handler *handler, struct lxc_netdev *netd
 		return 0;
 
 	ret = run_script_argv(handler->name, handler->conf->hooks_version,
-			      "net", netdev->upscript, "up", argv);
+			      "net", netdev->upscript, "up", argv, netdev->idx);
 	if (ret < 0)
 		return -1;
 
@@ -502,7 +502,7 @@ static int instantiate_empty(struct lxc_handler *handler, struct lxc_netdev *net
 		return 0;
 
 	ret = run_script_argv(handler->name, handler->conf->hooks_version,
-			      "net", netdev->upscript, "up", argv);
+			      "net", netdev->upscript, "up", argv, netdev->idx);
 	if (ret < 0)
 		return -1;
 
@@ -544,7 +544,7 @@ static int shutdown_veth(struct lxc_handler *handler, struct lxc_netdev *netdev)
 
 	ret = run_script_argv(handler->name,
 			handler->conf->hooks_version, "net",
-			netdev->downscript, "down", argv);
+			netdev->downscript, "down", argv, netdev->idx);
 	if (ret < 0)
 		return -1;
 
@@ -564,7 +564,7 @@ static int shutdown_macvlan(struct lxc_handler *handler, struct lxc_netdev *netd
 		return 0;
 
 	ret = run_script_argv(handler->name, handler->conf->hooks_version,
-			      "net", netdev->downscript, "down", argv);
+			      "net", netdev->downscript, "down", argv, netdev->idx);
 	if (ret < 0)
 		return -1;
 
@@ -584,7 +584,7 @@ static int shutdown_vlan(struct lxc_handler *handler, struct lxc_netdev *netdev)
 		return 0;
 
 	ret = run_script_argv(handler->name, handler->conf->hooks_version,
-			      "net", netdev->downscript, "down", argv);
+			      "net", netdev->downscript, "down", argv, netdev->idx);
 	if (ret < 0)
 		return -1;
 
@@ -604,7 +604,7 @@ static int shutdown_phys(struct lxc_handler *handler, struct lxc_netdev *netdev)
 		return 0;
 
 	ret = run_script_argv(handler->name, handler->conf->hooks_version,
-			      "net", netdev->downscript, "down", argv);
+			      "net", netdev->downscript, "down", argv, netdev->idx);
 	if (ret < 0)
 		return -1;
 
@@ -623,7 +623,7 @@ static int shutdown_empty(struct lxc_handler *handler, struct lxc_netdev *netdev
 		return 0;
 
 	ret = run_script_argv(handler->name, handler->conf->hooks_version,
-			      "net", netdev->downscript, "down", argv);
+			      "net", netdev->downscript, "down", argv, netdev->idx);
 	if (ret < 0)
 		return -1;
 
@@ -2319,9 +2319,8 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcna
 			NULL,
 		};
 
-		ret = run_script_argv(lxcname,
-				hooks_version, "net",
-				netdev->upscript, "up", argv);
+		ret = run_script_argv(lxcname, hooks_version, "net",
+				netdev->upscript, "up", argv, netdev->idx);
 		if (ret < 0)
 			return -1;
     }


More information about the lxc-devel mailing list