[procps] Re: Container Uptime Discussion

  • From: "Craig Small" <dmarc-noreply@xxxxxxxxxxxxx> ("csmall")
  • To: procps@xxxxxxxxxxxxx
  • Date: Thu, 8 Feb 2024 16:51:29 +1100

On Tue, 30 Jan 2024 at 21:36, Dr. Werner Fink <werner@xxxxxxx> wrote:

  ps o etimes= 1

to get the uptime of the container in seconds.

 I have effectively written procps_container_uptime() to do that.

Interesting would be if

the uptime command would accept an further option to specify the namespace
pid of the container to do this from outside aka the host system.  This

Strangely enough its not the pid namespace that matters. You (and I
originally) would think ok this
is a program about PIDS, pids namespace is what I need. But actually what
you want is the mount
namespace to get to "their" /proc.

"nsenter -t <docker_bash_pid>  -p ps ax" shows all the host processes,
because that's what is in /proc

Anyway, it seems to work.
Starting in the top directory of procps and making sure the container glibc
is the same, or close enough to the host's glibc version.
$ docker run -it --mount type=bind,source="$(pwd)",target=/mnt debian:latest
root@c00eb009173d:/# LD_PRELOAD=/mnt/library/.libs/libproc2.so
/mnt/src/.libs/uptime
 22:19:37 up 5 days, 17:04,  0 users,  load average: 0.30, 0.28, 0.21
root@c00eb009173d:/# LD_PRELOAD=/mnt/library/.libs/libproc2.so
/mnt/src/.libs/uptime --container
 22:19:41 up 0 min,  0 users,  load average: 0.28, 0.27, 0.21
(wait 2 minutes)
root@c00eb009173d:/# LD_PRELOAD=/mnt/library/.libs/libproc2.so
/mnt/src/.libs/uptime --container
 22:21:29 up 2 min,  0 users,  load average: 0.54, 0.38, 0.26

Both w and uptime have this change. I haven't attempted to do any
comparison between variables and uptime
(e.g. vmstat's initial set of numbers) because there doesn't seem to be a
clear way of doing it.

But at least now you can work out from inside the container how long its
been running.
 - Craig

Other related posts: