2

If I have a script that relies on one of the following being present: overlayfs, aufs, unionfs - what is the best way to determine which is available from a bash script? I would like to use overlayfs, but fall back to aufs or unionfs if it is not available. I can look at the kernel version as a guess - but just because it's a >= 3.18 kernel doesn't mean that overlayfs was built in - is there a reasonably foolproof way to check?

  • easiest way, lsmod and analyzing the output of modprobe, no? I am interested in knowing better ways too, will be modding up this question. – Rui F Ribeiro Nov 19 '15 at 22:29
  • Yeah, I was going to suggest e. g. if modprobe | grep -q 'fs_aufs'; then echo "aufs is present"; fi. – DopeGhoti Nov 19 '15 at 22:32
  • 1
    I don't know much about aufs, but it could be compiled into the kernel as opposed to being a module. – Jeff Schaller Nov 19 '15 at 23:46
  • This is true - it's more typical to be a module, but you can't count on it. – Ryan Nov 20 '15 at 16:38
8

Under Linux, you can see which filesystem types are available in the running kernel in the file /proc/filesystems. The content of this file is built in real time by the kernel when it's read, so it reflects the current status. The format of this file is a bit annoying: each line contains either 8 spaces followed by a filesystem type, or nodev followed by 3 spaces followed by a filesystem type. Strip off the first 8 characters of each line to get just the available filesystem types, or use grep -w (as long as you aren't looking for a filesystem type called nodev).

if grep -qw aufs /proc/filesystems; then
  echo aufs is available
fi

This isn't the complete story, because the filesystem driver could be available in the form of a module that isn't currently loaded. Assuming you can load modules, if the filesystem isn't available, try loading it. Filesystem modules have an alias of the form fs-FSTYPE (the actual module name is often the name of the filesystem type, but not always).

if grep -qw aufs /proc/filesystems; then
  echo aufs is available
elif modprobe fs-aufs; then
  echo now aufs is available
fi

This is for kernel filesystems. For FUSE filessytems, check whether the fuse filesystem is available, and look for the executable that implements the filesystem. While you can look for fuse in /proc/filesystems, that only tells you whether it's available, not whether your program has enough privileges to use it. A more reliable test in practice is to check whether you can write to /dev/fuse.

if [ -w /dev/fuse ]; then
  echo FUSE is available
  if type unionfs-fuse >/dev/null 2>/dev/null; then 
    echo unionfs-fuse is available
  fi
fi
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.