#!/bin/sh
# Readiness of lxcfs, two steps:
# 1. the FUSE filesystem is mounted on /var/lib/incus-lxcfs (fuse.lxcfs) --
#    only then can containers bind /proc files from it;
# 2. lxcfs ANSWERS: proc/uptime under the mount is readable. A mount whose
#    daemon hangs or died leaves step 1 true ("Transport endpoint is not
#    connected" or a blocking read); containers then read errors from
#    /proc/uptime. timeout, because a read from a hung FUSE daemon can block.
# incus depends on incus-lxcfs and thereby waits for exactly this point.
M=/var/lib/incus-lxcfs
if ! awk -v m="$M" '$2==m && $3=="fuse.lxcfs" {f=1} END{exit !f}' /proc/mounts; then
  echo "fuse.lxcfs not mounted on $M"
  exit 1
fi
if ! timeout 3 head -c 1 "$M/proc/uptime" >/dev/null 2>&1; then
  echo "fuse.lxcfs mounted on $M, but $M/proc/uptime is not readable"
  exit 1
fi
exit 0
