On Thu, Apr 11, 2013 at 02:51:53PM +0200, Marc Fournier wrote: > LXC detection is not as trivial as one would expect. As it's based on > cgroups, and cgroups can be used for other things than LXC containers, > we must be careful not to match non-LXC cgroup usage. > Furthermore, there are several LXC container implementations, which > leave different marks, so we have to be sure to test for each of them. Just look for 'container' in /proc/1/environ. Anything else is legacy / deprecated. > diff --git a/virt-what.in b/virt-what.in > index 297956d..c2dad16 100644 > --- a/virt-what.in > +++ b/virt-what.in > @@ -135,6 +135,58 @@ if [ -d $root/proc/vz -a ! -d $root/proc/bc ]; then > echo openvz > fi > > +# Check for LXC containers > +# Added by Marc Fournier > +# Based on a similar script written in ruby by Krzysztof Wilczynski > + > +# LXC containers are based on cgroups. So first check if cgroups are present > +# in the running kernel. > +if [ -e $root/proc/self/cgroup ] && > + # But if the last field of the cgroup colon-delimited file is "/", we are > + # outside of a container. So only proceed further if this field has more > + # characters. > + grep -Eq '^[0-9]+:.+:/.+$' $root/proc/self/cgroup; then > + > + # This is still not enough, as cgroups can be used for other things than LXC > + # containers. We base our final checks on the value/presence of various > + # environment variables passed to the "init" process. > + for envvar in $(cat $root/proc/1/environ | tr '\000' '\n'); do > + > + case $(echo $envvar | cut -f1 -d'=') in > + # native lxc tools and virsh will set the "container" environment > + # variable to either "lxc", or "libvirt-lxc", or "libvirt", depending on > + # the version. > + container) > + container_type=$(echo $envvar | while IFS='=' read k v; do echo $v; done) > + if echo $container_type | grep -Eq '^(lxc|libvirt-lxc|libvirt)$'; then > + echo lxc > + break > + fi > + ;; > + > + # older native lxc tools don't set the "container" environment variable, > + # so the only way to detect them is via the "_" environment variable > + # (which is the command used to start the container). > + _) > + external_command=$(echo $envvar | while IFS='=' read k v; do echo $v; done) > + if echo $external_command | grep -iq 'lxc'; then > + echo lxc > + break > + fi > + ;; > + > + # recent libvirt-based LXC containers will have 2 unambiguous environment > + # variables set. Checking for their presence should be sufficient. > + LIBVIRT_LXC_UUID|LIBVIRT_LXC_NAME) > + echo lxc > + break > + ;; > + > + esac > + done > + > +fi IMHO this is uneccesarily complex. Just check for existence of a 'container' environment varibles in /proc/1/environ, which is the agreed marker for a container[1], and ignore legacy code which didn't set that. The cgroups check doesn't add any value here either. Daniel [1] http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :| _______________________________________________ virt-tools-list mailing list virt-tools-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/virt-tools-list