How to check command line options before starting the container's init?


(Mrinal Dhillon) #1

How to check the method and command line options before before starting the container’s init i.e. lxc-execute/lxc-start and the arguments provided. i.e. lxc-execute cntr -d [-s key=val] – command or lxc-start cntr [-s key=val] .

I have had success with the following first approach but not sure if this most is optimal method and will work in future releases. Also can you think of a scenario where this will fail?

  1. In lxc.hook.start-host copy /proc/$LXC_PID/cmdline at a host path that is bind mounted inside container and then read the cmdline file in lxc.hook.start.

This one failed to my surprise:
2. /proc/$PPID/{cmdline,exe} in lxc.hook.start bash script could not provide enough info:

  • /proc/$PPID/cmdline returns [lxc monitor] /var/extensions/lxc app for a daemon container.
    – /proc/$PPID/exe -> /usr/bin/lxc-execute or /proc/$PPID/exe -> /usr/bin/lxc-start atleast identifies the method used to start the container.

Thank You
Mrinal Dhillon


(Mrinal Dhillon) #2

None of the approaches that i discussed work in case of lxc-execute --daemon and lxc-start, both points lxc-monitor. i am not able find any method with which we can identify whether the container is started with lxc-execute or lxc-start in any of the hooks. suggestions?

Thank You
Mrinal


(Stéphane Graber) #3

@brauner suggestions?


(Christian Brauner) #4

The cmdline is rewritten due to setproctitle() you might be able to do something like:

> basename $(readlink /proc/1/exe)
systemd

(Mrinal Dhillon) #5

@brauner This assumption won’t work if pid namespace is shared with lxc-execute for super privileged containers.

lxc-execute ubuntu -- readlink /proc/1/exe
/usr/sbin/init.lxc.static
lxc-execute ubuntu -s "lxc.namespace.keep=pid user" -- readlink /proc/1/exe
/lib/systemd/systemd

(Christian Brauner) #6

You were mentioning running this in a hook. At this point you have access to the correct pid.


(Mrinal Dhillon) #7

Yes we need to ascertain in hooks. Example above was just a bad example. With pid namespace shared how do we figure if container will be started with lxc.init.static or systemd?


(Christian Brauner) #8

In the hook you can do:

readlink /proc/${LXC_PID}/exe

On container start liblxc will set LXC_PID as an env variable containing the container’s pid.