[Lxc-users] make lxc-ps work with separate mtab

Serge E. Hallyn serge.hallyn at canonical.com
Fri Aug 5 13:48:35 UTC 2011


Quoting Daniel Lezcano (daniel.lezcano at free.fr):
> On 08/04/2011 04:30 PM, Serge E. Hallyn wrote:
> > Quoting Daniel Lezcano (daniel.lezcano at free.fr):
> >> On 07/28/2011 08:19 PM, Serge E. Hallyn wrote:
> >>> It breaks with multiple cgroup mounts unless /etc/mtab is linked
> >>> to /proc/self/mounts (which it is not in Ubuntu, for instance).
> >>>
> >>> Signed-off-by: Serge Hallyn <serge.hallyn at canonical.com>
> >> Hi Serge,
> >>
> >> not sure, you got the email
> > No, I did not.  Where did you send it to?
> 
> The same recipient as this mail. Anyway, the last two days were
> thunderstorm days.  It is probable something went wrong on my home network.
> 
> >> but I was not able to apply this one.
> > Ah I see, I didn't have commit c011782c8d83ece9ee883fcd9406433c09f220be.
> > The jist of this hunk is: ns cgroup might not need to be mounted, so
> > accept any cgroup entry.
> >
> > (Yes, this still makes assumptions - namely that the container is
> > in the same group for all cgroups;  but I think that makes more
> > sense than assuming that ns cgroup is mounted somewhere)
> >
> > Do you want me to send a new patch, or do you want to tweak it
> > in-flight?
> 
> Please, send me a new one if possible.

Sorry for not sending yesterday.  I've got one more lxc-ps patch I'd
like to send in a few minutes.

Date: Fri, 5 Aug 2011 08:44:56 -0500
Subject: [PATCH 1/1] make lxc-ps work with cgroup-bin in Ubuntu (v2)

It breaks with multiple cgroup mounts unless /etc/mtab is linked
to /proc/self/mounts, which it is not in ubuntu.

Also, the current git HEAD fails if ns cgroup is not mounted.
With this patch, it will return the cgroup path in the ns cgroup
if that is mounted, otherwise it will return the cgroup path
in the first cgroup listed in /proc/$$/cgroups.  Rationale: the
ns cgroup stops the container from escaping its cgroup path,
which may still be useful (though I'm doubtful).  Otherwise, there
is no good way for deciding which cgroup's path to use.  (We could
standardize on freezer or devices, since we always use those)

Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/lxc/+bug/817606

Signed-off-by: Serge Hallyn <serge.hallyn at canonical.com>
---
 src/lxc/lxc-ps.in |   40 ++++++++++++++++++++++++++++------------
 1 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/src/lxc/lxc-ps.in b/src/lxc/lxc-ps.in
index 7b3c8ab..6377938 100755
--- a/src/lxc/lxc-ps.in
+++ b/src/lxc/lxc-ps.in
@@ -53,15 +53,22 @@ sub get_cgroup {
 	my $mount_string;
 
 	$mount_string=`mount -t cgroup |grep -E -e '^lxc '`;
-	unless ($mount_string) {
-		$mount_string=`mount |grep -m1 'type cgroup'`;
-	}
-	chomp($mount_string);
 	if ($mount_string) {
+		# use the one 'lxc' cgroup mount if it exists
+		chomp($mount_string);
 		$$ref_cgroup=`echo "$mount_string" |cut -d' ' -f3`;
 		chomp($$ref_cgroup);
+		return
+	}
+	# Otherwise (i.e. cgroup-bin) use the first cgroup mount
+	$mount_string=`grep -m1 -E '^[^ \t]+[ \t]+[^ \t]+[ \t]+cgroup' /proc/self/mounts`;
+	unless ($mount_string) {
+		die "unable to find mounted cgroup" unless $$ref_cgroup;
 	}
-	die "unable to find mounted cgroup" unless $$ref_cgroup;
+	chomp($mount_string);
+	$$ref_cgroup=`echo "$mount_string" |cut -d' ' -f2`;
+	chomp($$ref_cgroup);
+	return;
 }
 
 sub get_pids_in_containers {
@@ -111,21 +118,30 @@ sub execute_ps {
 sub get_container {
     my $pid = shift;
     my $filename = "/proc/$pid/cgroup";
+    my $container = '';
+    my $firstcgroup = '';
     open(LXC, "$filename");
     # read all lines at once
     my @cgroup = <LXC>;
     close LXC;
-    my $container = '';
+    # If ns cgroup is mounted, return the path in there
+    # otherwise return the path of first cgroup in /proc/$$/cgroups.
     foreach ( @cgroup ) {
-        chomp;
-        # find the container name
-        if (m/[:,]ns[:,]/o) {
+	$container = $_;
+        chomp($container);
+	# is this ns cgroup?
+	if ($container =~ m/[:,]ns[:,]/o) {
 	    # container name after :/
-	    s/.*:\///o;
-            $container = $_;
+	    $container =~ s/.*:\///o;
+	    return $container;
         }
+	if ($firstcgroup == '') {
+	    # container name after :/
+	    $container =~ s/.*:\///o;
+	    $firstcgroup = $container;
+	}
     }
-    return $container;
+    return $firstcgroup;
 }
 
 sub display_headers {
-- 
1.7.5.4





More information about the lxc-users mailing list