Skip to contentSkip to conversation

Add netnsid and nsfs columns to lsns

PATCH In review Masatake YAMATO 2 participants 13 messages Nov 24, 2017 – Nov 27, 2017 Util-Linux package

lore
Share & tools
lore
#0 lore raw

v2 cover Add netnsid and nsfs columns to lsns

This patch-set adds netnsid and nsfs columns to lsns command.

The new columns are useful to understand namespaces information printed in the output of "ip link" and "ip netns". The new columns are visible only when --type net is specified.

Masatake YAMATO (7):
lsns: add netnsid column
lsns: disable netnsid column by default
lsns: add a case for testing netnsid column
lsns: add nsfs column
lsns: add --nowrap(-W) option
lsns: add a case for testing nsfs column
man: write about using multi-line in NSFS cell of lsns

 configure.ac                |   1 +
 sys-utils/Makemodule.am     |   4 +-
 sys-utils/lsns.8            |   8 ++
 sys-utils/lsns.c            | 319 +++++++++++++++++++++++++++++++++++++++++++-
 tests/commands.sh           |   1 +
 tests/expected/lsns/netnsid |   1 +
 tests/expected/lsns/nsfs    |   1 +
 tests/ts/lsns/netnsid       |  65 +++++++++
 tests/ts/lsns/nsfs          |  68 ++++++++++
 9 files changed, 459 insertions(+), 9 deletions(-)
 create mode 100644 tests/expected/lsns/netnsid
 create mode 100644 tests/expected/lsns/nsfs
 create mode 100644 tests/ts/lsns/netnsid
 create mode 100644 tests/ts/lsns/nsfs
Signature · 1 line
2.13.6
#1 lore raw

PATCH v2 1/7 lsns: add netnsid column

Linux network subsystem assigns an unique integer to a network namespace.

term0# ip netns add UTIL-LINUX-LSNS-TEST-NS
term0# ip netns list
UTIL-LINUX-LSNS-TEST-NS
term0# ip link add name lsns-vetha type veth peer name lsns-vethb term0 # ip link set lsns-vethb netns UTIL-LINUX-LSNS-TEST-NS term0# ip netns list
UTIL-LINUX-LSNS-TEST-NS (id: 0)
term0# ip link show dev lsns-vetha
230: lsns-vetha@if229: <BROADCAST,MULTICAST> mtu 1500 qdisc noop ...

         link/ether 3e:27:68:ba:b3:95 brd ff:ff:ff:ff:ff:ff link-netnsid 0

In this example 0 is assigned to UTIL-LINUX-LSNS-TEST-NS net namespace. The name, UTIL-LINUX-LSNS-TEST-NS, and it semantics is given and defined by iproute2 in userland; and nothing to do with util-linux.

However, the id, 0, is managed in linux kernel. If lsns can show the ids, it helps users understand the state of network namespaces.

This commit adds NETNSID column to the output.

Here is an example of session:

term0# ip netns exec UTIL-LINUX-LSNS-TEST-NS cat

(Open another terminal)

term1# ./lsns --type net
NS TYPE NPROCS PID USER NETNSID COMMAND
4026531993 net 383 1 root unassigned /usr/lib/systemd/...
4026532433 net 1 1219 rtkit unassigned /usr/libexec/rtkit-daemon
4026532562 net 1 18201 root 0 cat

0 is shown as NETNSID for the cat process.

For the initial name space, "unassigned" is printed. For the namespaces other type than net, "n/a" is printed. If an error occurred during getting the id, "n/a" is printed.

Changes in V2:

* Remove wrongly used & operators.
* Make netnsid field empty if value for the column is not available. Suggested by Karel Zak.
* Remove redundant condtion for checking the avaiablebility of netlink socket. Suggested by Karel Zak.

---
 configure.ac     |   1 +
 sys-utils/lsns.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 185 insertions(+), 2 deletions(-)
configure.ac +1 -0
index eebe92259..b2ad5ea98 100644
@@ -223,6 +223,7 @@ AC_CHECK_HEADERS([ \
 	linux/tiocl.h \
 	linux/version.h \
 	linux/securebits.h \
+	linux/net_namespace.h \
 	locale.h \
 	mntent.h \
 	net/if.h \
sys-utils/lsns.c +184 -2

282 lines not on this page · raw message

Signature · 1 line
2.13.6
#2 lore raw

PATCH v2 2/7 lsns: disable netnsid column by default

In reply to Masatake YAMATO

Enable only when

* --type=net is given, or
* -o NETNSID is given.

Suggested by Karel Zak.

---
 sys-utils/lsns.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
sys-utils/lsns.c +20 -5
index 7ccc733a4..bb7d3956d 100644
@@ -839,6 +839,7 @@ int main(int argc, char *argv[])
 		{ 0 }
 	};
 	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
+	bool enabling_netnsid = false;
 
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
@@ -891,6 +892,8 @@ int main(int argc, char *argv[])
 				errx(EXIT_FAILURE, _("unknown namespace type: %s"), optarg);
 			ls.fltr_types[type] = 1;
 			ls.fltr_ntypes++;
+			if (type == LSNS_ID_NET)
+				enabling_netnsid = true;
 			break;
 		}
 		default:
@@ -900,6 +903,7 @@ int main(int argc, char *argv[])
 
 	if (!ls.fltr_ntypes) {
 		size_t i;
+
 		for (i = 0; i < ARRAY_SIZE(ns_names); i++)
 			ls.fltr_types[i] = 1;
 	}
@@ -924,14 +928,24 @@ int main(int argc, char *argv[])
 		columns[ncolumns++] = COL_NPROCS;
 		columns[ncolumns++] = COL_PID;
 		columns[ncolumns++] = COL_USER;
-		columns[ncolumns++] = COL_NETNSID;
+		if (enabling_netnsid)
+			columns[ncolumns++] = COL_NETNSID;
 		columns[ncolumns++] = COL_COMMAND;
 	}
 
-	if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
-					 &ncolumns, column_name_to_id) < 0)
-		return EXIT_FAILURE;
+	if (outarg) {
+		size_t i;
 
+		if (string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
+					  &ncolumns, column_name_to_id) < 0)
+			return EXIT_FAILURE;
+		for (i = 0; i < ncolumns; i++) {
+			if (columns[i] == COL_NETNSID) {
+				enabling_netnsid = true;
+				break;
+			}
+		}
+	}
 	scols_init_debug(0);
 
 	uid_cache = new_idcache();
@@ -939,7 +953,8 @@ int main(int argc, char *argv[])
 		err(EXIT_FAILURE, _("failed to allocate UID cache"));
 
 #ifdef HAVE_LINUX_NET_NAMESPACE_H
-	netlink_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+	if (enabling_netnsid)
+		netlink_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
 #endif
 	r = read_processes(&ls);
 	if (!r)
Signature · 1 line
2.13.6
#3 lore raw

PATCH v2 3/7 lsns: add a case for testing netnsid column

In reply to Masatake YAMATO
---
 tests/commands.sh           |  1 +
 tests/expected/lsns/netnsid |  1 +
 tests/ts/lsns/netnsid       | 65 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+)
 create mode 100644 tests/expected/lsns/netnsid
 create mode 100644 tests/ts/lsns/netnsid
tests/commands.sh +1 -0
index 655344102..502935ede 100644
@@ -74,6 +74,7 @@ TS_CMD_LOSETUP=${TS_CMD_LOSETUP:-"$top_builddir/losetup"}
 TS_CMD_LSBLK=${TS_CMD_LSBLK-"$top_builddir/lsblk"}
 TS_CMD_LSCPU=${TS_CMD_LSCPU-"$top_builddir/lscpu"}
 TS_CMD_LSMEM=${TS_CMD_LSMEM-"$top_builddir/lsmem"}
+TS_CMD_LSNS=${TS_CMD_LSNS-"$top_builddir/lsns"}
 TS_CMD_MCOOKIE=${TS_CMD_MCOOKIE-"$top_builddir/mcookie"}
 TS_CMD_MKCRAMFS=${TS_CMD_MKCRAMFS:-"$top_builddir/mkfs.cramfs"}
 TS_CMD_MKMINIX=${TS_CMD_MKMINIX:-"$top_builddir/mkfs.minix"}
tests/expected/lsns/netnsid new file +1 -0
new file mode 100644
index 000000000..573541ac9
@@ -0,0 +1 @@
+0
tests/ts/lsns/netnsid new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.13.6
#4 lore raw

PATCH v2 4/7 lsns: add nsfs column

In reply to Masatake YAMATO

nsfs provides kernel level interface for assigning logical name to a namespace. Following message is quoted from git log of linux kernel:

commit 0226f4923f6c9b40cfa1c1c1b19a6ac6b3924ead
Author: Al Viro <[email protected]>
Date: Tue Dec 6 12:21:54 2011 -0500

vfs: take /proc/*/mounts and friends to fs/proc_namespace.c

rationale: that stuff is far tighter bound to fs/namespace.c than to the guts of procfs proper.

/proc/self/mountinfo lists the logical names for namespaces:
...
652 81 0:3 net:[4026532579] /tmp/XYZ rw shared:192 - nsfs nsfs rw,seclabel
...

In the lines /tmp/XYZ is a logical name for 4026532579 of net namespace.

This patch adds nsfs column. It seems that the logical name is used only in "ip netns" now. So the column is disabled by default. Use '--type=net' or '-o NSFS' options to enable it.

This feature and the way to implementation using multi lines in a column is Suggested by Karel Zak.

---
 sys-utils/Makemodule.am |   4 +-
 sys-utils/lsns.c        | 108 ++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 106 insertions(+), 6 deletions(-)
sys-utils/Makemodule.am +2 -2
index a85987c8e..b5b1d4f15 100644
@@ -260,8 +260,8 @@ if BUILD_LSNS
 usrbin_exec_PROGRAMS += lsns
 dist_man_MANS += sys-utils/lsns.8
 lsns_SOURCES =	sys-utils/lsns.c
-lsns_LDADD = $(LDADD) libcommon.la libsmartcols.la
-lsns_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir)
+lsns_LDADD = $(LDADD) libcommon.la libsmartcols.la libmount.la
+lsns_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir) -I$(ul_libmount_incdir)
 endif
 
 
sys-utils/lsns.c +104 -4

178 lines not on this page · raw message

Signature · 1 line
2.13.6
#5 lore raw

PATCH 5/5 lsns: add a case for testing nsfs column

In reply to Masatake YAMATO
---
 tests/expected/lsns/nsfs |  1 +
 tests/ts/lsns/nsfs       | 66 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)
 create mode 100644 tests/expected/lsns/nsfs
 create mode 100644 tests/ts/lsns/nsfs
tests/expected/lsns/nsfs new file +1 -0
new file mode 100644
index 000000000..573541ac9
@@ -0,0 +1 @@
+0
tests/ts/lsns/nsfs new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
2.13.6
#6 lore raw

Re: [PATCH 5/5] lsns: add a case for testing nsfs column

I'm sorry.
I took a mistake for sending a patch set.
Please, ignore this

[PATCH 5/5] lsns: add a case for testing nsfs column

one.

Masatake YAMATO

Quoted 90 lines · Masatake YAMATO
---
 tests/expected/lsns/nsfs |  1 +
 tests/ts/lsns/nsfs       | 66 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)
 create mode 100644 tests/expected/lsns/nsfs
 create mode 100644 tests/ts/lsns/nsfs
tests/expected/lsns/nsfs new file +1 -0
new file mode 100644
index 000000000..573541ac9
@@ -0,0 +1 @@
+0
tests/ts/lsns/nsfs new file +66 -0
new file mode 100644
index 000000000..cfc88e06c
@@ -0,0 +1,66 @@
+#!/bin/bash
+#
+# Copyright (C) 2017 Masatake YAMATO <[email protected]>
+#
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="list NSFS for namespaces created by ip-netns-add"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_LSNS"
+
+ts_skip_nonroot
+
+ts_cd "$TS_OUTDIR"
+
+NAME1=ABC
+NAME2=XYZ
+PATH1=/run/netns/$NAME1
+PATH2=$TS_OUTDIR/$NAME2
+FIFO=$TS_OUTDIR/UTIL-LINUX-LSNS-TEST-FIFO
+NULL=/dev/null
+
+ip netns delete $NAME1 2> /dev/null || :
+umount $PATH2 2>/dev/null || :
+rm -f $PATH2
+
+rm -f $FIFO
+mkfifo $FIFO
+
+if ip netns add $NAME1 &&
+	touch ${PATH2} &&
+	mount -o bind ${PATH1} ${PATH2}; then
+    ip netns exec $NAME1 dd if=$FIFO bs=1 count=2 of=$NULL 2> $NULL &
+    PID=$!
+fi
+{
+    dd if=/dev/zero bs=1 count=1 2> $NULL
+    NSFS_NAMES=$($TS_CMD_LSNS -n -o NSFS --type net --task $PID | { while read VAL; do echo $VAL; done; } )
+    dd if=/dev/zero bs=1 count=1 2> $NULL
+} > $FIFO
+
+rm $FIFO
+
+umount $PATH2
+rm -f $PATH2
+ip netns delete $NAME1
+
+test "$NSFS_NAMES" = "$PATH1
+$PATH2"
+echo $? >> $TS_OUTPUT
+
+ts_finalize
Signature · 1 line
2.13.6
#7 lore raw

PATCH v2 5/7 lsns: add --nowrap(-W) option

In reply to Masatake YAMATO

This option forces lsns command not use
multi-line presentation when printing a cell.

Currently, it affects only NSFS column.

Implementing this option is suggested by Karl Zak.

---
 sys-utils/lsns.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
sys-utils/lsns.c +10 -2
index c4d806e24..8af363317 100644
@@ -173,7 +173,8 @@ struct lsns {
 		     tree	: 1,
 		     list	: 1,
 		     notrunc	: 1,
-		     no_headings: 1;
+		     no_headings: 1,
+		     no_wrap    : 1;
 
 	struct libmnt_table *tab;
 };
@@ -746,7 +747,7 @@ static void add_scols_line(struct lsns *ls, struct libscols_table *table,
 			break;
 		case COL_NSFS:
 			nsfs_xasputs(&str, ns, ls->tab,
-				     ls->raw ? ',' : '\n');
+				     (ls->raw || ls->no_wrap) ? ',' : '\n');
 			break;
 		default:
 			break;
@@ -786,6 +787,8 @@ static struct libscols_table *init_scols_table(struct lsns *ls)
 		       flags &= ~SCOLS_FL_TRUNC;
 		if (ls->tree && get_column_id(i) == COL_COMMAND)
 			flags |= SCOLS_FL_TREE;
+		if (ls->no_wrap)
+			flags &= ~SCOLS_FL_WRAP;
 
 		cl = scols_table_new_column(tab, col->name, col->whint, flags);
 		if (cl == NULL) {
@@ -891,6 +894,7 @@ static void __attribute__((__noreturn__)) usage(void)
 	fputs(_(" -p, --task <pid>       print process namespaces\n"), out);
 	fputs(_(" -r, --raw              use the raw output format\n"), out);
 	fputs(_(" -u, --notruncate       don't truncate text in columns\n"), out);
+	fputs(_(" -W, --nowrap           don't use multi-line representation\n"), out);
 	fputs(_(" -t, --type <name>      namespace type (mnt, net, ipc, user, pid, uts, cgroup)\n"), out);
 
 	fputs(USAGE_SEPARATOR, out);
@@ -920,6 +924,7 @@ int main(int argc, char *argv[])
 		{ "notruncate", no_argument,       NULL, 'u' },
 		{ "version",    no_argument,       NULL, 'V' },
 		{ "noheadings", no_argument,       NULL, 'n' },
+		{ "nowrap",     no_argument,       NULL, 'W' },
 		{ "list",       no_argument,       NULL, 'l' },
 		{ "raw",        no_argument,       NULL, 'r' },
 		{ "type",       required_argument, NULL, 't' },
@@ -988,6 +993,9 @@ int main(int argc, char *argv[])
 				enabling_netnsid = true;
 			break;
 		}
+		case 'W':
+			ls.no_wrap = 1;
+			break;
 		default:
 			errtryhelp(EXIT_FAILURE);
 		}
Signature · 1 line
2.13.6
#8 lore raw

PATCH v2 6/7 lsns: add a case for testing nsfs column

In reply to Masatake YAMATO
---
 tests/expected/lsns/nsfs |  1 +
 tests/ts/lsns/nsfs       | 68 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)
 create mode 100644 tests/expected/lsns/nsfs
 create mode 100644 tests/ts/lsns/nsfs
tests/expected/lsns/nsfs new file +1 -0
new file mode 100644
index 000000000..573541ac9
@@ -0,0 +1 @@
+0
tests/ts/lsns/nsfs new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.13.6
#9 lore raw

PATCH v2 7/7 man: write about using multi-line in NSFS cell of lsns

In reply to Masatake YAMATO
---
 sys-utils/lsns.8 | 8 ++++++++
 1 file changed, 8 insertions(+)
sys-utils/lsns.8 +8 -0
index ebdc1df04..4d108a48c 100644
@@ -20,6 +20,11 @@ avoid using default outputs in your scripts.  Always explicitly define expected
 columns by using the \fB\-\-output\fR option together with a columns list in
 environments where a stable output is required.
 
+\fBNSFS\fP column, printed when \fBnet\fP is specified for
+\fB\-\-type\fR option, is specifal; it uses multi-line cells.
+\fB\-\-nowrap\fR is for switching to "," separated single-line
+representation.
+
 Note that \fBlsns\fR reads information directly from the /proc filesystem and
 for non-root users it may return incomplete information.  The current /proc
 filesystem may be unshared and affected by a PID namespace
@@ -60,6 +65,9 @@ Display the specified \fItype\fP of namespaces only.  The supported types are
 .BR \-u , " \-\-notruncate"
 Do not truncate text in columns.
 .TP
+.BR \-W , " \-\-nowrap"
+Do not use multi-line text in columns.
+.TP
 .BR \-V , " \-\-version"
 Display version information and exit.
 .TP
Signature · 1 line
2.13.6
maintainer
#10 lore raw
In reply to Masatake YAMATO
Quoted 6 lines · Masatake YAMATO

This patch-set adds netnsid and nsfs columns to lsns command.

The new columns are useful to understand namespaces information printed in the output of "ip link" and "ip netns". The new columns are visible only when --type net is specified.

Quoted 8 lines

Masatake YAMATO (7):
lsns: add netnsid column
lsns: disable netnsid column by default
lsns: add a case for testing netnsid column
lsns: add nsfs column
lsns: add --nowrap(-W) option
lsns: add a case for testing nsfs column
man: write about using multi-line in NSFS cell of lsns

Cool, I'll merge it next week. Thanks!

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#11 lore raw
In reply to Masatake YAMATO
Quoted 7 lines · Masatake YAMATO

lsns: add netnsid column
lsns: disable netnsid column by default
lsns: add a case for testing netnsid column
lsns: add nsfs column
lsns: add --nowrap(-W) option
lsns: add a case for testing nsfs column
man: write about using multi-line in NSFS cell of lsns

Merged; I have merged also some additional changes to cleanup a little bit the code. Thanks!

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#12 lore raw

Thank you!

Masatake YAMATO

Quoted 17 lines · Masatake YAMATO
Quoted 7 lines

lsns: add netnsid column
lsns: disable netnsid column by default
lsns: add a case for testing netnsid column
lsns: add nsfs column
lsns: add --nowrap(-W) option
lsns: add a case for testing nsfs column
man: write about using multi-line in NSFS cell of lsns

Merged; I have merged also some additional changes to cleanup a little bit the code. Thanks!

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com