1 commit 7ef0141356454503ab81460290b5dffa32c1f441
2 Author: David Ward <david.ward@ll.mit.edu>
3 Date: Fri May 4 00:50:15 2012 +0200
7 Add an '--active' option that lists active containers by searching
8 cgroups. (Otherwise, the directories in /var/lib/lxc are listed.)
9 Modify the cgroup search to only use hierarchies that contain one
10 or more subsystems. When searching, if a hierarchy contains the
11 'ns' subsystem, do not append '/lxc' to the parent cgroup.
13 Add a '--help' option that prints the command syntax.
15 Print error messages and help information to stderr.
17 Update the documentation.
19 Signed-off-by: David Ward <david.ward@ll.mit.edu>
20 Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
22 diff --git a/doc/lxc-ls.sgml.in b/doc/lxc-ls.sgml.in
23 index 3ffd4f8..d33e9b3 100644
24 --- a/doc/lxc-ls.sgml.in
25 +++ b/doc/lxc-ls.sgml.in
26 @@ -48,7 +48,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 - <command>lxc-ls <optional>ls option</optional>
31 + <command>lxc-ls <optional>--active</optional> <optional>ls option</optional>
35 @@ -67,6 +67,17 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 + <option><optional>--active</optional></option>
43 + List active containers.
50 <option><optional>ls options</optional></option>
53 @@ -94,10 +105,10 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
57 - <term>lxc-ls -1</term>
58 + <term>lxc-ls --active -1</term>
61 - list all the containers and display the list in one column.
62 + list active containers and display the list in one column.
66 diff --git a/src/lxc/lxc-ls.in b/src/lxc/lxc-ls.in
67 index a1ad642..11a3b45 100644
68 --- a/src/lxc/lxc-ls.in
69 +++ b/src/lxc/lxc-ls.in
73 -localstatedir=@LOCALSTATEDIR@
76 +# lxc: linux Container library
78 -if [ ! -r $lxcpath ]; then
81 +# This library is free software; you can redistribute it and/or
82 +# modify it under the terms of the GNU Lesser General Public
83 +# License as published by the Free Software Foundation; either
84 +# version 2.1 of the License, or (at your option) any later version.
86 +# This library is distributed in the hope that it will be useful,
87 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
88 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
89 +# Lesser General Public License for more details.
91 +# You should have received a copy of the GNU Lesser General Public
92 +# License along with this library; if not, write to the Free Software
93 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
95 +localstatedir=@LOCALSTATEDIR@
98 -function get_cgroup()
102 - mount_string=$(mount -t cgroup |grep -E -e '^lxc ')
103 - if test -n "$mount_string"; then
104 - mount_point=$(echo $mount_string |cut -d' ' -f3)
107 - mount_string=`grep -m1 -E '^[^ \t]+[ \t]+[^ \t]+[ \t]+cgroup' /proc/self/mounts`;
108 - if test -z "$mount_string"; then
109 - echo "failed to find mounted cgroup"
112 - mount_point=`echo "$mount_string" |cut -d' ' -f2`;
113 + echo "usage: $(basename $0) [--active] [--] [LS_OPTIONS...]" >&2
120 + echo "List containers existing on the system." >&2
122 + echo " --active list active containers" >&2
123 + echo " LS_OPTIONS ls command options (see \`ls --help')" >&2
128 + local hierarchies hierarchy fields subsystems init_cgroup mountpoint
132 + # Obtain a list of hierarchies that contain one or more subsystems
133 + hierarchies=$(tail -n +2 /proc/cgroups | cut -f 2)
135 -active=$(netstat -xl 2>/dev/null | grep $lxcpath | \
136 - sed -e 's#.*'"$lxcpath/"'\(.*\)/command#\1#');
137 + # Iterate through the list until a suitable hierarchy is found
138 + for hierarchy in $hierarchies; do
139 + # Obtain information about the init process in the hierarchy
140 + fields=$(grep -E "^$hierarchy:" /proc/1/cgroup | head -n 1)
141 + if [ -z "$fields" ]; then continue; fi
142 + fields=${fields#*:}
144 -if test -n "$active"; then
146 - if test -n "$mount_point"; then
147 - # get cgroup for init
148 - init_cgroup=`cat /proc/1/cgroup | awk -F: '{ print $3 }' | head -1`
149 - if [ ! -d $mount_point/$init_cgroup/lxc ]; then
150 - cd $mount_point/$init_cgroup
151 + # Get a comma-separated list of the hierarchy's subsystems
152 + subsystems=${fields%:*}
154 + # Get the cgroup of the init process in the hierarchy
155 + init_cgroup=${fields#*:}
157 + # Get the filesystem mountpoint of the hierarchy
158 + mountpoint=$(grep -E "^cgroup [^ ]+ [^ ]+ ([^ ]+,)?$subsystems(,[^ ]+)? " /proc/self/mounts | cut -d ' ' -f 2)
159 + if [ -z "$mountpoint" ]; then continue; fi
161 + # Return the absolute path to the containers' parent cgroup
162 + # (do not append '/lxc' if the hierarchy contains the 'ns' subsystem)
163 + if [[ ",$subsystems," == *,ns,* ]]; then
164 + parent_cgroup="${mountpoint}${init_cgroup%/}"
166 - cd $mount_point/$init_cgroup/lxc
167 + parent_cgroup="${mountpoint}${init_cgroup%/}/lxc"
175 +directory="$lxc_path"
182 + get_parent_cgroup; directory="$parent_cgroup"; shift;;
191 +if [ ! -z "$directory" ]; then
192 + containers=$(find $directory -mindepth 1 -maxdepth 1 -type d -printf "%f\n" 2>/dev/null)
195 +if [ -z "$containers" ]; then
196 + echo "$(basename $0): no containers found" >&2
201 +ls -d $@ $containers