[virt-tools-list] [PATCH 1/3] Add detection for LXC + tests

Marc Fournier marc.fournier at camptocamp.com
Thu Apr 11 12:51:53 UTC 2013


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.
---
 Makefile.am                                        |  24 ++++++++++
 tests/lxc-libvirt/proc/1/environ                   | Bin 0 -> 181 bytes
 tests/lxc-libvirt/proc/cpuinfo                     |  26 +++++++++++
 tests/lxc-libvirt/proc/self/cgroup                 |   8 ++++
 tests/lxc-libvirt/proc/self/status                 |  39 ++++++++++++++++
 tests/lxc-libvirt/sbin/dmidecode                   |   6 +++
 tests/lxc-libvirt/sbin/uname                       |   2 +
 tests/lxc-libvirt/sbin/virt-what-cpuid-helper      |   2 +
 tests/lxc-native-pre074/proc/1/environ             | Bin 0 -> 140 bytes
 tests/lxc-native-pre074/proc/cpuinfo               |  26 +++++++++++
 tests/lxc-native-pre074/proc/self/cgroup           |   1 +
 tests/lxc-native-pre074/proc/self/status           |  39 ++++++++++++++++
 tests/lxc-native-pre074/sbin/dmidecode             |   6 +++
 tests/lxc-native-pre074/sbin/uname                 |   2 +
 .../lxc-native-pre074/sbin/virt-what-cpuid-helper  |   2 +
 tests/lxc-native/proc/1/environ                    | Bin 0 -> 152 bytes
 tests/lxc-native/proc/cpuinfo                      |  26 +++++++++++
 tests/lxc-native/proc/self/cgroup                  |   1 +
 tests/lxc-native/proc/self/status                  |  39 ++++++++++++++++
 tests/lxc-native/sbin/dmidecode                    |   6 +++
 tests/lxc-native/sbin/uname                        |   2 +
 tests/lxc-native/sbin/virt-what-cpuid-helper       |   2 +
 tests/test-lxc-libvirt.sh                          |  34 ++++++++++++++
 tests/test-lxc-native-pre074.sh                    |  34 ++++++++++++++
 tests/test-lxc-native.sh                           |  34 ++++++++++++++
 virt-what.in                                       |  52 +++++++++++++++++++++
 26 files changed, 413 insertions(+)
 create mode 100644 tests/lxc-libvirt/proc/1/environ
 create mode 100644 tests/lxc-libvirt/proc/cpuinfo
 create mode 100644 tests/lxc-libvirt/proc/self/cgroup
 create mode 100644 tests/lxc-libvirt/proc/self/status
 create mode 100755 tests/lxc-libvirt/sbin/dmidecode
 create mode 100755 tests/lxc-libvirt/sbin/uname
 create mode 100755 tests/lxc-libvirt/sbin/virt-what-cpuid-helper
 create mode 100644 tests/lxc-native-pre074/proc/1/environ
 create mode 100644 tests/lxc-native-pre074/proc/cpuinfo
 create mode 100644 tests/lxc-native-pre074/proc/self/cgroup
 create mode 100644 tests/lxc-native-pre074/proc/self/status
 create mode 100755 tests/lxc-native-pre074/sbin/dmidecode
 create mode 100755 tests/lxc-native-pre074/sbin/uname
 create mode 100755 tests/lxc-native-pre074/sbin/virt-what-cpuid-helper
 create mode 100644 tests/lxc-native/proc/1/environ
 create mode 100644 tests/lxc-native/proc/cpuinfo
 create mode 100644 tests/lxc-native/proc/self/cgroup
 create mode 100644 tests/lxc-native/proc/self/status
 create mode 100755 tests/lxc-native/sbin/dmidecode
 create mode 100755 tests/lxc-native/sbin/uname
 create mode 100755 tests/lxc-native/sbin/virt-what-cpuid-helper
 create mode 100755 tests/test-lxc-libvirt.sh
 create mode 100755 tests/test-lxc-native-pre074.sh
 create mode 100755 tests/test-lxc-native.sh

diff --git a/Makefile.am b/Makefile.am
index 4ddbb64..2b90397 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -45,6 +45,9 @@ TESTS = \
 	tests/test-kvm.sh \
 	tests/test-kvm-explicit-cpu.sh \
 	tests/test-linux-vserver.sh \
+	tests/test-lxc-libvirt.sh \
+	tests/test-lxc-native.sh \
+	tests/test-lxc-native-pre074.sh \
 	tests/test-lx86.sh \
 	tests/test-parallels-desktop.sh \
 	tests/test-qemu.sh \
@@ -93,6 +96,27 @@ EXTRA_DIST = \
 	tests/linux-vserver/sbin/id \
 	tests/linux-vserver/sbin/uname \
 	tests/linux-vserver/sbin/virt-what-cpuid-helper \
+	tests/lxc-libvirt/proc/1/environ \
+	tests/lxc-libvirt/proc/cpuinfo \
+	tests/lxc-libvirt/proc/self/status \
+	tests/lxc-libvirt/sbin/dmidecode \
+	tests/lxc-libvirt/sbin/id \
+	tests/lxc-libvirt/sbin/uname \
+	tests/lxc-libvirt/sbin/virt-what-cpuid-helper \
+	tests/lxc-native/proc/1/environ \
+	tests/lxc-native/proc/cpuinfo \
+	tests/lxc-native/proc/self/status \
+	tests/lxc-native/sbin/dmidecode \
+	tests/lxc-native/sbin/id \
+	tests/lxc-native/sbin/uname \
+	tests/lxc-native/sbin/virt-what-cpuid-helper \
+	tests/lxc-native-pre074/proc/1/environ \
+	tests/lxc-native-pre074/proc/cpuinfo \
+	tests/lxc-native-pre074/proc/self/status \
+	tests/lxc-native-pre074/sbin/dmidecode \
+	tests/lxc-native-pre074/sbin/id \
+	tests/lxc-native-pre074/sbin/uname \
+	tests/lxc-native-pre074/sbin/virt-what-cpuid-helper \
 	tests/lx86/proc/cpuinfo \
 	tests/lx86/proc/self/status \
 	tests/lx86/sbin/dmidecode \
diff --git a/tests/lxc-libvirt/proc/1/environ b/tests/lxc-libvirt/proc/1/environ
new file mode 100644
index 0000000000000000000000000000000000000000..528fc2f1a183fff9003bd649d377c687ccb10c62
GIT binary patch
literal 181
zcmWG=4DqnlPs+@*(k}**3?Z&TzP34;d8HK$$@zIDiJ5t+MYcH=$+|h2NoAQuB?!^@
z($dTnTT|0yGn3 at BG+l#KlSJJ_qeNrfWFuoUT@#aJ<HS at 0i_|nDa|Rzzr!dc;ka(X6
Y=lIZ2PZ!d4`8oQ!f}K&4T3o^a0LCadEdT%j

literal 0
HcmV?d00001

diff --git a/tests/lxc-libvirt/proc/cpuinfo b/tests/lxc-libvirt/proc/cpuinfo
new file mode 100644
index 0000000..1b12948
--- /dev/null
+++ b/tests/lxc-libvirt/proc/cpuinfo
@@ -0,0 +1,26 @@
+processor	: 0
+vendor_id	: GenuineIntel
+cpu family	: 15
+model		: 4
+model name	: Intel(R) Xeon(TM) CPU 3.80GHz
+stepping	: 3
+microcode	: 0x5
+cpu MHz		: 3790.823
+cache size	: 2048 KB
+physical id	: 0
+siblings	: 2
+core id		: 0
+cpu cores	: 1
+apicid		: 0
+initial apicid	: 0
+fpu		: yes
+fpu_exception	: yes
+cpuid level	: 5
+wp		: yes
+flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc pebs bts nopl pni dtes64 monitor ds_cpl est tm2 cid cx16 xtpr
+bogomips	: 7581.64
+clflush size	: 64
+cache_alignment	: 128
+address sizes	: 36 bits physical, 48 bits virtual
+power management:
+
diff --git a/tests/lxc-libvirt/proc/self/cgroup b/tests/lxc-libvirt/proc/self/cgroup
new file mode 100644
index 0000000..01b1566
--- /dev/null
+++ b/tests/lxc-libvirt/proc/self/cgroup
@@ -0,0 +1,8 @@
+8:blkio:/libvirt/lxc/lxc-test
+7:net_cls:/
+6:freezer:/libvirt/lxc/lxc-test
+5:devices:/libvirt/lxc/lxc-test
+4:memory:/libvirt/lxc/lxc-test
+3:cpuacct:/libvirt/lxc/lxc-test
+2:cpu:/libvirt/lxc/lxc-test
+1:cpuset:/libvirt/lxc/lxc-test
diff --git a/tests/lxc-libvirt/proc/self/status b/tests/lxc-libvirt/proc/self/status
new file mode 100644
index 0000000..23aabb6
--- /dev/null
+++ b/tests/lxc-libvirt/proc/self/status
@@ -0,0 +1,39 @@
+Name:	cat
+State:	R (running)
+Tgid:	8
+Pid:	8
+PPid:	1
+TracerPid:	0
+Uid:	0	0	0	0
+Gid:	0	0	0	0
+Utrace:	0
+FDSize:	256
+Groups:	
+VmPeak:	    5128 kB
+VmSize:	    5128 kB
+VmLck:	       0 kB
+VmHWM:	     476 kB
+VmRSS:	     476 kB
+VmData:	    1212 kB
+VmStk:	      88 kB
+VmExe:	      44 kB
+VmLib:	    1704 kB
+VmPTE:	      40 kB
+VmSwap:	       0 kB
+Threads:	1
+SigQ:	0/3771
+SigPnd:	0000000000000000
+ShdPnd:	0000000000000000
+SigBlk:	0000000000000000
+SigIgn:	0000000000000000
+SigCgt:	0000000000000000
+CapInh:	0000000000000000
+CapPrm:	fffffffdbdbeffff
+CapEff:	fffffffdbdbeffff
+CapBnd:	fffffffdbdbeffff
+Cpus_allowed:	1
+Cpus_allowed_list:	0
+Mems_allowed:	00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
+Mems_allowed_list:	0
+voluntary_ctxt_switches:	2
+nonvoluntary_ctxt_switches:	1
diff --git a/tests/lxc-libvirt/sbin/dmidecode b/tests/lxc-libvirt/sbin/dmidecode
new file mode 100755
index 0000000..d9992ad
--- /dev/null
+++ b/tests/lxc-libvirt/sbin/dmidecode
@@ -0,0 +1,6 @@
+#!/bin/sh -
+cat <<'EOF'
+# dmidecode 2.11
+/dev/mem: Operation not permitted
+EOF
+exit 1
diff --git a/tests/lxc-libvirt/sbin/uname b/tests/lxc-libvirt/sbin/uname
new file mode 100755
index 0000000..ab0ec89
--- /dev/null
+++ b/tests/lxc-libvirt/sbin/uname
@@ -0,0 +1,2 @@
+#!/bin/sh -
+echo x86_64
diff --git a/tests/lxc-libvirt/sbin/virt-what-cpuid-helper b/tests/lxc-libvirt/sbin/virt-what-cpuid-helper
new file mode 100755
index 0000000..ad82504
--- /dev/null
+++ b/tests/lxc-libvirt/sbin/virt-what-cpuid-helper
@@ -0,0 +1,2 @@
+#!/bin/sh -
+echo @
diff --git a/tests/lxc-native-pre074/proc/1/environ b/tests/lxc-native-pre074/proc/1/environ
new file mode 100644
index 0000000000000000000000000000000000000000..e7aed2db6950721adf6260165bc0188836818a58
GIT binary patch
literal 140
zcmWG>4f3 at uPA*DK&0`4maP{%A)lbUI(@#n)&R_@)b`7#E%Fi!h at OAVAN|YrQ>E|Y9
z=IDdP0>WKvAuJz9KX+Sa1`mH<S0s^8M|W4-)V%o6U at M^6KK|~0jzD>^R)%<6{nFwh
RuuVA?$-2cQiA5z0TmTm`E2jVe

literal 0
HcmV?d00001

diff --git a/tests/lxc-native-pre074/proc/cpuinfo b/tests/lxc-native-pre074/proc/cpuinfo
new file mode 100644
index 0000000..1b12948
--- /dev/null
+++ b/tests/lxc-native-pre074/proc/cpuinfo
@@ -0,0 +1,26 @@
+processor	: 0
+vendor_id	: GenuineIntel
+cpu family	: 15
+model		: 4
+model name	: Intel(R) Xeon(TM) CPU 3.80GHz
+stepping	: 3
+microcode	: 0x5
+cpu MHz		: 3790.823
+cache size	: 2048 KB
+physical id	: 0
+siblings	: 2
+core id		: 0
+cpu cores	: 1
+apicid		: 0
+initial apicid	: 0
+fpu		: yes
+fpu_exception	: yes
+cpuid level	: 5
+wp		: yes
+flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc pebs bts nopl pni dtes64 monitor ds_cpl est tm2 cid cx16 xtpr
+bogomips	: 7581.64
+clflush size	: 64
+cache_alignment	: 128
+address sizes	: 36 bits physical, 48 bits virtual
+power management:
+
diff --git a/tests/lxc-native-pre074/proc/self/cgroup b/tests/lxc-native-pre074/proc/self/cgroup
new file mode 100644
index 0000000..cf308a1
--- /dev/null
+++ b/tests/lxc-native-pre074/proc/self/cgroup
@@ -0,0 +1 @@
+1:perf_event,blkio,net_cls,freezer,devices,cpuacct,cpu,cpuset:/lxc/memcache0
diff --git a/tests/lxc-native-pre074/proc/self/status b/tests/lxc-native-pre074/proc/self/status
new file mode 100644
index 0000000..6a4f213
--- /dev/null
+++ b/tests/lxc-native-pre074/proc/self/status
@@ -0,0 +1,39 @@
+Name:	cat
+State:	R (running)
+Tgid:	7424
+Pid:	7424
+PPid:	7216
+TracerPid:	0
+Uid:	0	0	0	0
+Gid:	0	0	0	0
+FDSize:	256
+Groups:	0 
+VmPeak:	    4112 kB
+VmSize:	    4112 kB
+VmLck:	       0 kB
+VmPin:	       0 kB
+VmHWM:	     320 kB
+VmRSS:	     320 kB
+VmData:	     180 kB
+VmStk:	     136 kB
+VmExe:	      48 kB
+VmLib:	    1664 kB
+VmPTE:	      32 kB
+VmSwap:	       0 kB
+Threads:	1
+SigQ:	4/63942
+SigPnd:	0000000000000000
+ShdPnd:	0000000000000000
+SigBlk:	0000000000000000
+SigIgn:	0000000000000000
+SigCgt:	0000000000000000
+CapInh:	0000000000000000
+CapPrm:	fffffffcff9effff
+CapEff:	fffffffcff9effff
+CapBnd:	fffffffcff9effff
+Cpus_allowed:	0f
+Cpus_allowed_list:	0-3
+Mems_allowed:	00000000,00000001
+Mems_allowed_list:	0
+voluntary_ctxt_switches:	1
+nonvoluntary_ctxt_switches:	1
diff --git a/tests/lxc-native-pre074/sbin/dmidecode b/tests/lxc-native-pre074/sbin/dmidecode
new file mode 100755
index 0000000..d9992ad
--- /dev/null
+++ b/tests/lxc-native-pre074/sbin/dmidecode
@@ -0,0 +1,6 @@
+#!/bin/sh -
+cat <<'EOF'
+# dmidecode 2.11
+/dev/mem: Operation not permitted
+EOF
+exit 1
diff --git a/tests/lxc-native-pre074/sbin/uname b/tests/lxc-native-pre074/sbin/uname
new file mode 100755
index 0000000..ab0ec89
--- /dev/null
+++ b/tests/lxc-native-pre074/sbin/uname
@@ -0,0 +1,2 @@
+#!/bin/sh -
+echo x86_64
diff --git a/tests/lxc-native-pre074/sbin/virt-what-cpuid-helper b/tests/lxc-native-pre074/sbin/virt-what-cpuid-helper
new file mode 100755
index 0000000..ad82504
--- /dev/null
+++ b/tests/lxc-native-pre074/sbin/virt-what-cpuid-helper
@@ -0,0 +1,2 @@
+#!/bin/sh -
+echo @
diff --git a/tests/lxc-native/proc/1/environ b/tests/lxc-native/proc/1/environ
new file mode 100644
index 0000000000000000000000000000000000000000..cba6db0533e35a2a24aef54e0fd223d5ce7e1e83
GIT binary patch
literal 152
zcmYL>K?=h#3<P!VXZ!*kj1f*jP9SQ|#R-8L5*u7o^8704p*QW$YJhF;(Lu0xM)Qf)
z>@jVomeODn8u7ZiArSt`b3a*6`g8K$D!Dcf8Ur3}tvm4G|Gjj?s-9Ceb9}#m-7}Dg
VDnuQctYQaC$t|Y5R@{ySZeO3LE$;vT

literal 0
HcmV?d00001

diff --git a/tests/lxc-native/proc/cpuinfo b/tests/lxc-native/proc/cpuinfo
new file mode 100644
index 0000000..1b12948
--- /dev/null
+++ b/tests/lxc-native/proc/cpuinfo
@@ -0,0 +1,26 @@
+processor	: 0
+vendor_id	: GenuineIntel
+cpu family	: 15
+model		: 4
+model name	: Intel(R) Xeon(TM) CPU 3.80GHz
+stepping	: 3
+microcode	: 0x5
+cpu MHz		: 3790.823
+cache size	: 2048 KB
+physical id	: 0
+siblings	: 2
+core id		: 0
+cpu cores	: 1
+apicid		: 0
+initial apicid	: 0
+fpu		: yes
+fpu_exception	: yes
+cpuid level	: 5
+wp		: yes
+flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc pebs bts nopl pni dtes64 monitor ds_cpl est tm2 cid cx16 xtpr
+bogomips	: 7581.64
+clflush size	: 64
+cache_alignment	: 128
+address sizes	: 36 bits physical, 48 bits virtual
+power management:
+
diff --git a/tests/lxc-native/proc/self/cgroup b/tests/lxc-native/proc/self/cgroup
new file mode 100644
index 0000000..cf308a1
--- /dev/null
+++ b/tests/lxc-native/proc/self/cgroup
@@ -0,0 +1 @@
+1:perf_event,blkio,net_cls,freezer,devices,cpuacct,cpu,cpuset:/lxc/memcache0
diff --git a/tests/lxc-native/proc/self/status b/tests/lxc-native/proc/self/status
new file mode 100644
index 0000000..6a4f213
--- /dev/null
+++ b/tests/lxc-native/proc/self/status
@@ -0,0 +1,39 @@
+Name:	cat
+State:	R (running)
+Tgid:	7424
+Pid:	7424
+PPid:	7216
+TracerPid:	0
+Uid:	0	0	0	0
+Gid:	0	0	0	0
+FDSize:	256
+Groups:	0 
+VmPeak:	    4112 kB
+VmSize:	    4112 kB
+VmLck:	       0 kB
+VmPin:	       0 kB
+VmHWM:	     320 kB
+VmRSS:	     320 kB
+VmData:	     180 kB
+VmStk:	     136 kB
+VmExe:	      48 kB
+VmLib:	    1664 kB
+VmPTE:	      32 kB
+VmSwap:	       0 kB
+Threads:	1
+SigQ:	4/63942
+SigPnd:	0000000000000000
+ShdPnd:	0000000000000000
+SigBlk:	0000000000000000
+SigIgn:	0000000000000000
+SigCgt:	0000000000000000
+CapInh:	0000000000000000
+CapPrm:	fffffffcff9effff
+CapEff:	fffffffcff9effff
+CapBnd:	fffffffcff9effff
+Cpus_allowed:	0f
+Cpus_allowed_list:	0-3
+Mems_allowed:	00000000,00000001
+Mems_allowed_list:	0
+voluntary_ctxt_switches:	1
+nonvoluntary_ctxt_switches:	1
diff --git a/tests/lxc-native/sbin/dmidecode b/tests/lxc-native/sbin/dmidecode
new file mode 100755
index 0000000..d9992ad
--- /dev/null
+++ b/tests/lxc-native/sbin/dmidecode
@@ -0,0 +1,6 @@
+#!/bin/sh -
+cat <<'EOF'
+# dmidecode 2.11
+/dev/mem: Operation not permitted
+EOF
+exit 1
diff --git a/tests/lxc-native/sbin/uname b/tests/lxc-native/sbin/uname
new file mode 100755
index 0000000..ab0ec89
--- /dev/null
+++ b/tests/lxc-native/sbin/uname
@@ -0,0 +1,2 @@
+#!/bin/sh -
+echo x86_64
diff --git a/tests/lxc-native/sbin/virt-what-cpuid-helper b/tests/lxc-native/sbin/virt-what-cpuid-helper
new file mode 100755
index 0000000..ad82504
--- /dev/null
+++ b/tests/lxc-native/sbin/virt-what-cpuid-helper
@@ -0,0 +1,2 @@
+#!/bin/sh -
+echo @
diff --git a/tests/test-lxc-libvirt.sh b/tests/test-lxc-libvirt.sh
new file mode 100755
index 0000000..92fe8eb
--- /dev/null
+++ b/tests/test-lxc-libvirt.sh
@@ -0,0 +1,34 @@
+# Test for lxc
+# Copyright (C) 2008-2011 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+root=tests/lxc-libvirt
+
+output="$(./virt-what --test-root=$root 2>&1)"
+expected="lxc"
+
+if [ "$output" != "$expected" ]; then
+    echo "$0: test failed because output did not match expected"
+    echo "Expected output was:"
+    echo "----------------------------------------"
+    echo "$expected"
+    echo "----------------------------------------"
+    echo "But the actual output of the program was:"
+    echo "----------------------------------------"
+    echo "$output"
+    echo "----------------------------------------"
+    exit 1
+fi
diff --git a/tests/test-lxc-native-pre074.sh b/tests/test-lxc-native-pre074.sh
new file mode 100755
index 0000000..b8fe0c1
--- /dev/null
+++ b/tests/test-lxc-native-pre074.sh
@@ -0,0 +1,34 @@
+# Test for lxc
+# Copyright (C) 2008-2011 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+root=tests/lxc-native-pre074
+
+output="$(./virt-what --test-root=$root 2>&1)"
+expected="lxc"
+
+if [ "$output" != "$expected" ]; then
+    echo "$0: test failed because output did not match expected"
+    echo "Expected output was:"
+    echo "----------------------------------------"
+    echo "$expected"
+    echo "----------------------------------------"
+    echo "But the actual output of the program was:"
+    echo "----------------------------------------"
+    echo "$output"
+    echo "----------------------------------------"
+    exit 1
+fi
diff --git a/tests/test-lxc-native.sh b/tests/test-lxc-native.sh
new file mode 100755
index 0000000..beb424e
--- /dev/null
+++ b/tests/test-lxc-native.sh
@@ -0,0 +1,34 @@
+# Test for lxc
+# Copyright (C) 2008-2011 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+root=tests/lxc-native
+
+output="$(./virt-what --test-root=$root 2>&1)"
+expected="lxc"
+
+if [ "$output" != "$expected" ]; then
+    echo "$0: test failed because output did not match expected"
+    echo "Expected output was:"
+    echo "----------------------------------------"
+    echo "$expected"
+    echo "----------------------------------------"
+    echo "But the actual output of the program was:"
+    echo "----------------------------------------"
+    echo "$output"
+    echo "----------------------------------------"
+    exit 1
+fi
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
+
 # Check for Linux-VServer
 if cat $root/proc/self/status | grep -q "VxID: [0-9]*"; then
     echo linux_vserver
-- 
1.8.2.1




More information about the virt-tools-list mailing list