Facter of the Day: $is_chroot

  |   Source

If you run "Puppet" inside a chroot (like I do during an FAI Deployment run), it would be handy to not (re)start or stop services.

So, a couple of days before I asked the question "How to determine if you are running puppet inside a chroot".

The first answer was, thanks again to Daniel Pittman:

export FACTER_chroot=whatever 
chroot puppetd -vdt --waitforcert var/log/puppetrun.log

You can then question the facter "$chroot" inside your class or node recipe.

But there is another way, IMHO more elegant way:

initramfs-tools and udev are providing a dynamic way of how to determine if you are running in a chroot or not.

Just check /var/lib/dpkg/info and read e.g. initramfs-tools.preinst.
You find this shell function:

chrooted() {
# borrowed from udev's postinst
if [ "$(stat -c %d/%i /)" = "$(stat -Lc %d/%i /proc/1/root 2>/dev/null)" ]; then
# the devicenumber/inode pair of / is the same as that of
# /sbin/init's root, so we're not in a chroot and hence
# return false.
return 1
fi
return 0
}

There you go.

Now...let's convert it into a puppet facter.
To do this, create a module e.g. chroot
$ mkdir -p /etc/puppet/modules/chroot/{manifests,lib,lib/facter}

Create a file named "ischroot.rb" with the following content:

require 'facter'

isChroot = %x{if [ "$(stat -c %d/%i /)" = "$(stat -Lc %d/%i /proc/1/root 2>/dev/null)" ]; then echo "false" ; else echo "true" ; fi}.chomp.downcase

Facter.add("is_chroot") do
        setcode do
                isChroot
        end
end

Save it. There you go. (Eventually restart your puppetmaster, when you are not running the puppetmaster under apache2 with mod_passenger)

Now, your puppet client will sync your plugin dir, and installs the ischroot.rb in /var/lib/puppet/lib/facter/ischroot.rb

When you need to know if you are inside a chroot or not, just ask in your class or node recipe:

case $is_chroot {
'true': {
# do whatever you need to do when you are in a chroot
}
'false': {
# do whatever you need to do when you are not in a chroot
}
}

Hope it helps you :)