[lxc-devel] [PATCH] find veth name for unprivileged containers
S.Çağlar Onur
caglar at 10ur.org
Fri Feb 14 06:42:59 UTC 2014
unprivileged containers uses lxc-user-nic to create the underlying
network. And because of that reason netdev struct misses some
information (like the name of the interface seen from the host side)
Find that information by parsing /proc/self/net/dev and return to
caller.
With this patch lxc-info starts to show network stats for unpriv.
containers.
lxc-info -n rubik
Name: rubik
State: RUNNING
PID: 6054
IP: 10.0.3.119
CPU use: 0.97 seconds
BlkIO use: 0 bytes
Memory use: 6.52 MiB
KMem use: 0 bytes
Link: veth5YTBDK
TX bytes: 3.41 KiB
RX bytes: 8.54 KiB
Total bytes: 11.95 KiB
Signed-off-by: S.Çağlar Onur <caglar at 10ur.org>
---
src/lxc/confile.c | 16 +++++++++++-----
src/lxc/utils.c | 25 +++++++++++++++++++++++++
src/lxc/utils.h | 2 ++
3 files changed, 38 insertions(+), 5 deletions(-)
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index fa263ef..5794220 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -931,7 +931,7 @@ static int config_hook(const char *key, const char *value,
struct lxc_conf *lxc_conf)
{
char *copy;
-
+
if (!value || strlen(value) == 0)
return lxc_clear_hooks(lxc_conf, key);
@@ -2002,10 +2002,16 @@ static int lxc_get_item_nic(struct lxc_conf *c, char *retv, int inlen,
}
} else if (strcmp(p1, "veth.pair") == 0) {
if (netdev->type == LXC_NET_VETH) {
- strprint(retv, inlen, "%s",
- netdev->priv.veth_attr.pair ?
- netdev->priv.veth_attr.pair :
- netdev->priv.veth_attr.veth1);
+ if (am_unpriv()) {
+ char *veth = find_veth_name();
+ strprint(retv, inlen, "%s", veth);
+ if (veth)
+ free(veth);
+ } else {
+ strprint(retv, inlen, "%s", netdev->priv.veth_attr.pair ?
+ netdev->priv.veth_attr.pair :
+ netdev->priv.veth_attr.veth1);
+ }
}
} else if (strcmp(p1, "vlan") == 0) {
if (netdev->type == LXC_NET_VLAN) {
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index 3dff104..d82c704 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -1175,3 +1175,28 @@ uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval)
return hval;
}
+
+char* find_veth_name(void)
+{
+ int len;
+ FILE *fp;
+ char buf[255], *substr = NULL, *semicolon, *str;
+
+ fp = fopen("/proc/self/net/dev", "r");
+ if (!fp)
+ return NULL;
+
+ while (fgets(buf, 255, fp)) {
+ if ((str = strstr(buf, "veth")) != NULL) {
+ semicolon = strchr(str, ':');
+ len = semicolon ? (semicolon - str) : strlen(str);
+
+ substr = malloc(len+1);
+ memcpy(substr, str, len);
+ substr[len] = '\0';
+ }
+ }
+ fclose(fp);
+
+ return substr;
+}
diff --git a/src/lxc/utils.h b/src/lxc/utils.h
index f541253..08cc41c 100644
--- a/src/lxc/utils.h
+++ b/src/lxc/utils.h
@@ -275,4 +275,6 @@ extern bool dir_exists(const char *path);
#define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
+
+extern char* find_veth_name(void);
#endif
--
1.8.3.2
More information about the lxc-devel
mailing list