lxc-info: Allow printing IP addresses

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: 's avatarStéphane Graber <stgraber@ubuntu.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent a9ac16e2
...@@ -51,6 +51,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ...@@ -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">-c <replaceable>KEY</replaceable></arg>
<arg choice="opt">-s</arg> <arg choice="opt">-s</arg>
<arg choice="opt">-p</arg> <arg choice="opt">-p</arg>
<arg choice="opt">-i</arg>
<arg choice="opt">-t <replaceable>state</replaceable></arg> <arg choice="opt">-t <replaceable>state</replaceable></arg>
</cmdsynopsis> </cmdsynopsis>
</refsynopsisdiv> </refsynopsisdiv>
...@@ -114,6 +115,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ...@@ -114,6 +115,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
<varlistentry> <varlistentry>
<term> <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> <option><optional>-t <replaceable>state</replaceable></optional></option>
</term> </term>
<listitem> <listitem>
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -34,6 +35,7 @@ ...@@ -34,6 +35,7 @@
#include "commands.h" #include "commands.h"
#include "arguments.h" #include "arguments.h"
static bool ips;
static bool state; static bool state;
static bool pid; static bool pid;
static char *test_state = NULL; static char *test_state = NULL;
...@@ -48,6 +50,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg) ...@@ -48,6 +50,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
key[keys] = arg; key[keys] = arg;
keys++; keys++;
break; break;
case 'i': ips = true; break;
case 's': state = true; break; case 's': state = true; break;
case 'p': pid = true; break; case 'p': pid = true; break;
case 't': test_state = arg; break; case 't': test_state = arg; break;
...@@ -57,6 +60,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg) ...@@ -57,6 +60,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
static const struct option my_longopts[] = { static const struct option my_longopts[] = {
{"config", required_argument, 0, 'c'}, {"config", required_argument, 0, 'c'},
{"ips", no_argument, 0, 'i'},
{"state", no_argument, 0, 's'}, {"state", no_argument, 0, 's'},
{"pid", no_argument, 0, 'p'}, {"pid", no_argument, 0, 'p'},
{"state-is", required_argument, 0, 't'}, {"state-is", required_argument, 0, 't'},
...@@ -73,6 +77,7 @@ lxc-info display some information about a container with the identifier NAME\n\ ...@@ -73,6 +77,7 @@ lxc-info display some information about a container with the identifier NAME\n\
Options :\n\ Options :\n\
-n, --name=NAME NAME for name of the container\n\ -n, --name=NAME NAME for name of the container\n\
-c, --config=KEY show configuration variable KEY from running 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\ -p, --pid shows the process id of the init container\n\
-s, --state shows the state of the container\n\ -s, --state shows the state of the container\n\
-t, --state-is=STATE test if current state is STATE\n\ -t, --state-is=STATE test if current state is STATE\n\
...@@ -99,14 +104,14 @@ int main(int argc, char *argv[]) ...@@ -99,14 +104,14 @@ int main(int argc, char *argv[])
if (!c) if (!c)
return -1; return -1;
if (!state && !pid && keys <= 0) if (!state && !pid && !ips && keys <= 0)
state = pid = true; state = pid = ips = true;
if (state || test_state) { if (state || test_state) {
if (test_state) if (test_state)
return strcmp(c->state(c), test_state) != 0; 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) { if (pid) {
...@@ -114,7 +119,18 @@ int main(int argc, char *argv[]) ...@@ -114,7 +119,18 @@ int main(int argc, char *argv[])
initpid = c->init_pid(c); initpid = c->init_pid(c);
if (initpid >= 0) 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++) { for(i = 0; i < keys; i++) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment