+iscontainer() {
+ # man systemd-detect-virt
+ local cmd=$(which systemd-detect-virt 2>/dev/null || command -v systemd-detect-virt 2>/dev/null)
+ if [ ! -z "${cmd}" -a -x "${cmd}" ]
+ then
+ "${cmd}" --container >/dev/null 2>&1 && return 0
+ fi
+
+ # /proc/1/sched exposes the host's pid of our init !
+ # http://stackoverflow.com/a/37016302
+ local pid=$( cat /proc/1/sched | head -n 1 | { IFS='(),#:' read name pid th threads; echo $pid; } )
+ local p=$(( pid + 0 ))
+ [ ${pid} -ne 1 ] && return 0
+
+ # lxc sets environment variable 'container'
+ [ ! -z "${container}" ] && return 0
+
+ # docker creates /.dockerenv
+ # http://stackoverflow.com/a/25518345
+ [ -f "/.dockerenv" ] && return 0
+
+ # ubuntu and debian supply /bin/running-in-container
+ # https://www.apt-browse.org/browse/ubuntu/trusty/main/i386/upstart/1.12.1-0ubuntu4/file/bin/running-in-container
+ if [ -x "/bin/running-in-container" ]
+ then
+ "/bin/running-in-container" >/dev/null 2>&1 && return 0
+ fi
+
+ return 1
+}
+