]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/legacy/lxc-ls.in
licensing: Add missing headers and FSF address
[mirror_lxc.git] / src / lxc / legacy / lxc-ls.in
1 #!/bin/sh
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
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 . @DATADIR@/lxc/lxc.functions
21
22 usage()
23 {
24 echo "usage: $(basename $0) [--active] [--] [LS_OPTIONS...]" >&2
25 }
26
27 help() {
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
36 get_parent_cgroup()
37 {
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
57 mountpoint=$(awk -v subsysregex="(^|,)$subsystems(,|\$)" \
58 '$3 == "cgroup" && $4 ~ subsysregex {print $2}' /proc/self/mounts)
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)
63 case ",$subsystems," in
64 *,ns,*) parent_cgroup="${mountpoint}${init_cgroup%/}";;
65 *) parent_cgroup="${mountpoint}${init_cgroup%/}/lxc";;
66 esac
67 break
68 done
69 }
70
71 directory=$(readlink -f "$lxc_path")
72
73 for 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;;
83 esac
84 done
85
86 containers=""
87 if [ ! -z "$directory" ]; then
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
96 fi
97
98 cd "$directory"
99 ls -d $@ -- $containers