]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/legacy/lxc-ls.in
licensing: Add missing headers and FSF address
[mirror_lxc.git] / src / lxc / legacy / lxc-ls.in
CommitLineData
2f0e6908 1#!/bin/sh
6a85cf91
SG
2
3#
4# lxc: linux Container library
5
6# This library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10
11# This library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# Lesser General Public License for more details.
15
16# You should have received a copy of the GNU Lesser General Public
17# License along with this library; if not, write to the Free Software
250b1eec 18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6a85cf91 19
2a59a681 20. @DATADIR@/lxc/lxc.functions
6a85cf91
SG
21
22usage()
23{
24 echo "usage: $(basename $0) [--active] [--] [LS_OPTIONS...]" >&2
25}
26
27help() {
28 usage
29 echo >&2
30 echo "List containers existing on the system." >&2
31 echo >&2
32 echo " --active list active containers" >&2
33 echo " LS_OPTIONS ls command options (see \`ls --help')" >&2
34}
35
36get_parent_cgroup()
37{
6a85cf91
SG
38 parent_cgroup=""
39
40 # Obtain a list of hierarchies that contain one or more subsystems
41 hierarchies=$(tail -n +2 /proc/cgroups | cut -f 2)
42
43 # Iterate through the list until a suitable hierarchy is found
44 for hierarchy in $hierarchies; do
45 # Obtain information about the init process in the hierarchy
46 fields=$(grep -E "^$hierarchy:" /proc/1/cgroup | head -n 1)
47 if [ -z "$fields" ]; then continue; fi
48 fields=${fields#*:}
49
50 # Get a comma-separated list of the hierarchy's subsystems
51 subsystems=${fields%:*}
52
53 # Get the cgroup of the init process in the hierarchy
54 init_cgroup=${fields#*:}
55
56 # Get the filesystem mountpoint of the hierarchy
32e86901
NC
57 mountpoint=$(awk -v subsysregex="(^|,)$subsystems(,|\$)" \
58 '$3 == "cgroup" && $4 ~ subsysregex {print $2}' /proc/self/mounts)
6a85cf91
SG
59 if [ -z "$mountpoint" ]; then continue; fi
60
61 # Return the absolute path to the containers' parent cgroup
62 # (do not append '/lxc' if the hierarchy contains the 'ns' subsystem)
2f0e6908
NC
63 case ",$subsystems," in
64 *,ns,*) parent_cgroup="${mountpoint}${init_cgroup%/}";;
65 *) parent_cgroup="${mountpoint}${init_cgroup%/}/lxc";;
66 esac
6a85cf91
SG
67 break
68 done
69}
70
71directory=$(readlink -f "$lxc_path")
72
73for i in "$@"; do
74 case $i in
75 --help)
76 help; exit;;
77 --active)
78 get_parent_cgroup; directory="$parent_cgroup"; shift;;
79 --)
80 shift; break;;
81 *)
82 break;;
2f0e6908 83 esac
6a85cf91
SG
84done
85
86containers=""
87if [ ! -z "$directory" ]; then
f4031540
DE
88 if [ x"$parent_cgroup" = x ]; then
89 containers=$(find $directory -mindepth 2 -maxdepth 2 -name config -type f |awk -F "/" '{print $(NF-1)}')
90 else
91 containers=$(find $directory -mindepth 1 -maxdepth 1 -type d 2>/dev/null | sed 's:.*/::')
92 fi
93 if [ x"$containers" = x ]; then
94 exit 0
95 fi
6a85cf91
SG
96fi
97
98cd "$directory"
99ls -d $@ -- $containers