[lxc-devel] [PATCH 2/2] lxc-info: Allow printing IP addresses

Stéphane Graber stgraber at ubuntu.com
Fri Sep 27 04:30:44 UTC 2013


This adds a new -i flag to lxc-info to print the container's IP
addresses using get_ips().

Example:
$ lxc-info -n lxc-dev -s -p -i
state:  RUNNING
pid:    21331
ip:     10.0.3.165
ip:     2607:f2c0:f00f:2751:e9ca:842f:efa9:97d1
ip:     2607:f2c0:f00f:2751:216:3eff:fe3a:f1c1

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 doc/lxc-info.sgml.in | 12 ++++++++++++
 src/lxc/lxc_info.c   | 24 ++++++++++++++++++++----
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/doc/lxc-info.sgml.in b/doc/lxc-info.sgml.in
index c6fc624..819d5ca 100644
--- a/doc/lxc-info.sgml.in
+++ b/doc/lxc-info.sgml.in
@@ -51,6 +51,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
       <arg choice="opt">-c <replaceable>KEY</replaceable></arg>
       <arg choice="opt">-s</arg>
       <arg choice="opt">-p</arg>
+      <arg choice="opt">-i</arg>
       <arg choice="opt">-t <replaceable>state</replaceable></arg>
     </cmdsynopsis>
   </refsynopsisdiv>
@@ -114,6 +115,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
       <varlistentry>
         <term>
+          <option><optional>-i</optional></option>
+        </term>
+        <listitem>
+          <para>
+            Just print the container's IP addresses.
+          </para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term>
           <option><optional>-t <replaceable>state</replaceable></optional></option>
         </term>
         <listitem>
diff --git a/src/lxc/lxc_info.c b/src/lxc/lxc_info.c
index ab3aacf..23cf789 100644
--- a/src/lxc/lxc_info.c
+++ b/src/lxc/lxc_info.c
@@ -20,6 +20,7 @@
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
+
 #include <stdio.h>
 #include <stdbool.h>
 #include <stdlib.h>
@@ -34,6 +35,7 @@
 #include "commands.h"
 #include "arguments.h"
 
+static bool ips;
 static bool state;
 static bool pid;
 static char *test_state = NULL;
@@ -48,6 +50,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
 		key[keys] = arg;
 		keys++;
 		break;
+	case 'i': ips = true; break;
 	case 's': state = true; break;
 	case 'p': pid = true; break;
 	case 't': test_state = arg; break;
@@ -57,6 +60,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
 
 static const struct option my_longopts[] = {
 	{"config", required_argument, 0, 'c'},
+	{"ips", no_argument, 0, 'i'},
 	{"state", no_argument, 0, 's'},
 	{"pid", no_argument, 0, 'p'},
 	{"state-is", required_argument, 0, 't'},
@@ -73,6 +77,7 @@ lxc-info display some information about a container with the identifier NAME\n\
 Options :\n\
   -n, --name=NAME       NAME for name of the container\n\
   -c, --config=KEY      show configuration variable KEY from running container\n\
+  -i, --ips             shows the IP addresses\n\
   -p, --pid             shows the process id of the init container\n\
   -s, --state           shows the state of the container\n\
   -t, --state-is=STATE  test if current state is STATE\n\
@@ -99,14 +104,14 @@ int main(int argc, char *argv[])
 	if (!c)
 		return -1;
 
-	if (!state && !pid && keys <= 0)
-		state = pid = true;
+	if (!state && !pid && !ips && keys <= 0)
+		state = pid = ips = true;
 
 	if (state || test_state) {
 		if (test_state)
 			return strcmp(c->state(c), test_state) != 0;
 
-		printf("state:%10s\n", c->state(c));
+		printf("state: \t%s\n", c->state(c));
 	}
 
 	if (pid) {
@@ -114,7 +119,18 @@ int main(int argc, char *argv[])
 
 		initpid = c->init_pid(c);
 		if (initpid >= 0)
-			printf("pid:%10d\n", initpid);
+			printf("pid: \t%d\n", initpid);
+	}
+
+	if (ips) {
+		char **addresses = c->get_ips(c, NULL, NULL, 0);
+		char *address;
+		i = 0;
+		while (addresses[i]) {
+			address = addresses[i];
+			printf("ip: \t%s\n", address);
+			i++;
+		}
 	}
 
 	for(i = 0; i < keys; i++) {
-- 
1.8.3.2





More information about the lxc-devel mailing list